Skip to content

Commit 60e3fdf

Browse files
committed
feat: Make teacher IP address optional in command line arguments
1 parent 1db9b02 commit 60e3fdf

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

Jiyu_udp_attack/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"--teacher-ip",
3030
type=str,
3131
metavar="ip",
32-
required=True,
32+
default=None,
3333
help="Teacher's IP address",
3434
)
3535
network_config_group.add_argument(

Jiyu_udp_attack/sender.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def create_raw_udp_packet(
119119

120120

121121
def send_packet(
122-
src_ip: str,
122+
src_ip: Optional[str],
123123
src_port: Optional[int],
124124
dst_ip: str,
125125
dst_port: int,
@@ -128,21 +128,26 @@ def send_packet(
128128
ip_id: Optional[int] = None,
129129
) -> None:
130130
"""
131-
Sends a UDP packet with the specified source IP, destination IP, destination port, and data payload.
132-
133-
Ensure that the source IP address is valid and that you have permission to send packets with spoofed addresses.
131+
Sends a UDP packet with a spoofed source IP address.
134132
135133
Args:
136-
src_ip (str): The source IP address.
134+
src_ip (Optional[str]): The source IP address to spoof. If None, spoofing is not performed.
137135
src_port (Optional[int]): The source port number. If None, a random port will be used.
138136
dst_ip (str): The destination IP address.
139137
dst_port (int): The destination port number.
140138
payload (bytes): The data payload to include in the packet.
139+
ip_id (Optional[int]): The IP identification number. If None, a random ID will be used.
141140
"""
142-
client = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
143-
client.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
144-
packet = create_raw_udp_packet(src_ip, src_port, dst_ip, dst_port, payload, ip_id=ip_id)
145-
client.sendto(packet, (dst_ip, dst_port))
141+
if src_ip is None:
142+
if src_port is not None or ip_id is not None:
143+
raise ValueError("If src_ip is None, src_port and ip_id must also be None.")
144+
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
145+
client.sendto(payload, (dst_ip, dst_port))
146+
else:
147+
client = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
148+
client.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
149+
packet = create_raw_udp_packet(src_ip, src_port, dst_ip, dst_port, payload, ip_id=ip_id)
150+
client.sendto(packet, (dst_ip, dst_port))
146151

147152

148153
def broadcast_packet(

0 commit comments

Comments
 (0)