Skip to content

Commit d596192

Browse files
committed
feat: Enhance IP analysis and broadcasting functionality with port handling
1 parent 7b1b72c commit d596192

File tree

5 files changed

+41
-18
lines changed

5 files changed

+41
-18
lines changed

Jiyu_udp_attack/__main__.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import binascii
1212
import shlex
1313

14+
import sys
1415
from typing import Any, List, Sequence, cast
1516

1617
try:
@@ -412,20 +413,20 @@ def main():
412413
if len(targets) == 0:
413414
parser.error("Target IP address cannot be empty. Use -h for help.")
414415

416+
print(*logger, sep="\n\n", end="\n\n")
417+
415418
for target in targets:
416-
broadcast_packet(
419+
for dest in broadcast_packet(
417420
teacher_ip, teacher_port, target, port, payload, ip_id=args.ip_id # pylint: disable=E0601, E0606
418-
)
419-
logger.append(f"Sent packet with a length of {len(payload)} to {target}:{port}")
420-
421-
print(*logger, sep="\n\n")
421+
):
422+
print(f"Sent packet with a length of {len(payload)} to {dest[0]}:{dest[1]}")
422423
except Exception as e: # pylint: disable=broad-except
423424
parser.error(f"({e.__class__.__name__}) {e}")
424425

425426

426427
if __name__ == "__main__":
427428
main()
428429
else:
429-
print("This script is intended to be run as a standalone program, not imported as a module.")
430-
print("Please run it directly using 'python Jiyu_udp_attack/__main__.py' or similar command.")
430+
print("This script is intended to be run as a standalone program, not imported as a module.", file=sys.stderr)
431+
print("Please run it directly using 'python Jiyu_udp_attack/__main__.py' or similar command.", file=sys.stderr)
431432
raise ImportError("This script is not designed to be imported as a module.")

Jiyu_udp_attack/ip_analyze.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def ip_to_tuple(ip: str) -> Tuple[int, int, int, int]:
2626
return ip_tuple
2727

2828

29-
def ip_analyze(ip: str) -> List[str]: # pylint: disable=too-many-branches
29+
def ip_analyze(address: str) -> List[Tuple[str, int]]:
3030
"""
3131
Analyzes an IP address or range and returns a list of valid IP addresses.
3232
@@ -36,9 +36,15 @@ def ip_analyze(ip: str) -> List[str]: # pylint: disable=too-many-branches
3636
Returns:
3737
list[str]: A list of valid IP addresses.
3838
"""
39-
if not isinstance(ip, str):
40-
raise TypeError(f"Expected string, got {type(ip).__name__}")
41-
ip = ip.replace(" ", "")
39+
if not isinstance(address, str):
40+
raise TypeError(f"Expected string, got {type(address).__name__}")
41+
if address.count(":") > 1:
42+
raise ValueError(f"Invalid address format: {address}")
43+
address = address.replace(" ", "")
44+
ip, port = address.split(":") if ":" in address else (address, -1)
45+
if port != -1 and not (port.isdigit() and (0 <= int(port) <= 65535)):
46+
raise ValueError(f"Invalid port number: {port}")
47+
port = int(port)
4248
if "/" in ip:
4349
try:
4450
ip_addr, mask = ip.split("/")
@@ -52,7 +58,7 @@ def ip_analyze(ip: str) -> List[str]: # pylint: disable=too-many-branches
5258
ip_tuple = ip_to_tuple(ip_addr)
5359
ip32 = ip_tuple[0] << 24 | ip_tuple[1] << 16 | ip_tuple[2] << 8 | ip_tuple[3]
5460
ip32 |= (1 << (32 - mask)) - 1
55-
return [f"{(ip32 >> 24) & 0xFF}.{(ip32 >> 16) & 0xFF}.{(ip32 >> 8) & 0xFF}.{ip32 & 0xFF}"]
61+
return [(f"{(ip32 >> 24) & 0xFF}.{(ip32 >> 16) & 0xFF}.{(ip32 >> 8) & 0xFF}.{ip32 & 0xFF}", port)]
5662
if "-" in ip:
5763
ip_range_tuple = ip.split(".")
5864
if len(ip_range_tuple) != 4:
@@ -75,11 +81,11 @@ def ip_analyze(ip: str) -> List[str]: # pylint: disable=too-many-branches
7581
if ip_count > 65536:
7682
raise ValueError(f"IP address range too large: {ip_count} addresses")
7783
return [
78-
f"{a}.{b}.{c}.{d}"
84+
(f"{a}.{b}.{c}.{d}", port)
7985
for a in range(ip_range[0][0], ip_range[0][1] + 1)
8086
for b in range(ip_range[1][0], ip_range[1][1] + 1)
8187
for c in range(ip_range[2][0], ip_range[2][1] + 1)
8288
for d in range(ip_range[3][0], ip_range[3][1] + 1)
8389
]
8490
ip_tuple = ip_to_tuple(ip)
85-
return [f"{ip_tuple[0]}.{ip_tuple[1]}.{ip_tuple[2]}.{ip_tuple[3]}"]
91+
return [(f"{ip_tuple[0]}.{ip_tuple[1]}.{ip_tuple[2]}.{ip_tuple[3]}", port)]

Jiyu_udp_attack/sender.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import struct
66
import random
77
import socket
8-
from typing import Optional
8+
from typing import List, Optional, Tuple
99

1010
try:
1111
from Jiyu_udp_attack.ip_analyze import ip_analyze
@@ -160,7 +160,7 @@ def broadcast_packet(
160160
payload: bytes,
161161
*,
162162
ip_id: Optional[int] = None,
163-
) -> None:
163+
) -> List[Tuple[str, int]]:
164164
"""
165165
Sends a broadcast UDP packet to the specified destination IP address or range.
166166
@@ -172,6 +172,14 @@ def broadcast_packet(
172172
dst_ip (str): The broadcast IP address (e.g., "192.168.1.255", "192.168.1.0/24", "192.168.1.10-100").
173173
dst_port (int): The destination port number.
174174
payload (bytes): The data payload to include in the packet.
175+
ip_id (Optional[int]): The IP identification number. If None, a random ID will be used.
176+
177+
Returns:
178+
List[Tuple[str, int]]: A list of tuples containing the IP addresses and ports to which the packets were sent.
175179
"""
176-
for ip in ip_analyze(dst_ip):
180+
sent_list: List[Tuple[str, int]] = []
181+
for ip, port in ip_analyze(dst_ip):
182+
port = port if 0 <= port <= 0xffff else dst_port
177183
send_packet(src_ip, src_port, ip, dst_port, payload, ip_id=ip_id)
184+
sent_list.append((ip, port))
185+
return sent_list

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ Example usage:
113113
+ 指定 ip 段,如 `192.168.3.0/24`,这将被转换到广播地址 `192.168.3.255`
114114
+ 使用极域的组播地址 `224.50.50.42`
115115

116+
### Specify the Port
117+
118+
一般情况下,可以使用 `-tp` 指定端口,默认为 `4705`,这是 2016 版极域使用的端口。如果你使用的是其他版本的极域,可能需要指定其他端口。
119+
120+
不过,也可以直接在 `-t` 中指定端口,例如 `-t 192.168.233.100:1234`,这样,在向该 ip 发送数据包时,将使用指定的端口。
121+
122+
对于教师机的端口,暂时不支持该语法,只能使用 `-fp` 指定。
123+
116124
### `--setting`
117125

118126
由于 `--setting` 的配置过于复杂,程序将其的配置项传入另一个命令行解析器,帮助文档如下:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "Jiyu-udp-attack"
3-
version = "1.2.0"
3+
version = "1.2.1"
44
description = "Send packets to student machines disguised as Jiyu teacher machine"
55
authors = ["weilycoder <[email protected]>"]
66
license = "MIT"

0 commit comments

Comments
 (0)