|
4 | 4 |
|
5 | 5 | import scapy.all as scapy |
6 | 6 |
|
7 | | - |
8 | | -def ip_to_tuple(ip: str) -> tuple[int, int, int, int]: |
9 | | - """ |
10 | | - Converts an IP address string to a tuple of four integers. |
11 | | -
|
12 | | - Args: |
13 | | - ip (str): The IP address in string format (e.g., "192.168.1.1"). |
14 | | -
|
15 | | - Returns: |
16 | | - tuple[int, int, int, int]: A tuple representing the four octets of the IP address. |
17 | | -
|
18 | | - Raises: |
19 | | - TypeError: If the input is not a string. |
20 | | - ValueError: If the input is not a valid IP address format. |
21 | | - """ |
22 | | - if not isinstance(ip, str): |
23 | | - raise TypeError(f"Expected string, got {type(ip).__name__}") |
24 | | - try: |
25 | | - ip_tuple = tuple(int(x) for x in ip.split(".")) |
26 | | - except ValueError: |
27 | | - raise ValueError(f"Invalid IP address format: {ip}") from None |
28 | | - if len(ip_tuple) != 4 or any(x < 0 or x > 255 for x in ip_tuple): |
29 | | - raise ValueError(f"Invalid IP address format: {ip}") |
30 | | - return ip_tuple |
31 | | - |
32 | | - |
33 | | -# pylint: disable=too-many-branches |
34 | | -def ip_analyze(ip: str) -> list[str]: |
35 | | - """ |
36 | | - Analyzes an IP address or range and returns a list of valid IP addresses. |
37 | | -
|
38 | | - Args: |
39 | | - ip (str): The IP address or range in string format (e.g., "192.168.1.1", "192.168.1.1/24", "192.168.1.1-100"). |
40 | | -
|
41 | | - Returns: |
42 | | - list[str]: A list of valid IP addresses. |
43 | | -
|
44 | | - Raises: |
45 | | - TypeError: If the input is not a string. |
46 | | - ValueError: If the input is not a valid IP address or range. |
47 | | - """ |
48 | | - if not isinstance(ip, str): |
49 | | - raise TypeError(f"Expected string, got {type(ip).__name__}") |
50 | | - ip = ip.replace(" ", "") |
51 | | - if "/" in ip: |
52 | | - match ip.split("/"): |
53 | | - case [ip_addr, mask]: |
54 | | - if not mask.isdigit(): |
55 | | - raise ValueError(f"Invalid subnet mask: {mask}") |
56 | | - mask = int(mask) |
57 | | - if mask < 0 or mask > 32: |
58 | | - raise ValueError(f"Subnet mask out of range: {mask}") |
59 | | - if mask < 16: |
60 | | - raise ValueError(f"Subnet mask too small: {mask}") |
61 | | - case _: |
62 | | - raise ValueError(f"Invalid IP address format: {ip}") |
63 | | - ip_tuple = ip_to_tuple(ip_addr) |
64 | | - ip32 = ip_tuple[0] << 24 | ip_tuple[1] << 16 | ip_tuple[2] << 8 | ip_tuple[3] |
65 | | - ip32 &= (0xFFFFFFFF >> (32 - mask)) << (32 - mask) |
66 | | - return [ |
67 | | - f"{(i >> 24) & 0xFF}.{(i >> 16) & 0xFF}.{(i >> 8) & 0xFF}.{i & 0xFF}" |
68 | | - for i in range(ip32 + 1, ip32 | ((1 << (32 - mask)) - 1)) |
69 | | - ] |
70 | | - if "-" in ip: |
71 | | - ip_range_tuple = ip.split(".") |
72 | | - if len(ip_range_tuple) != 4: |
73 | | - raise ValueError(f"Invalid IP address range format: {ip}") |
74 | | - ip_count = 1 |
75 | | - ip_range: list[tuple[int, int]] = [] |
76 | | - for i in ip_range_tuple: |
77 | | - match i.split("-"): |
78 | | - case [single]: |
79 | | - if not single.isdigit(): |
80 | | - raise ValueError(f"Invalid IP address range format: {ip}") |
81 | | - single = int(single) |
82 | | - if single < 0 or single > 255: |
83 | | - raise ValueError(f"IP address out of range: {single}") |
84 | | - ip_range.append((single, single)) |
85 | | - case [start, end]: |
86 | | - if not (start.isdigit() and end.isdigit()): |
87 | | - raise ValueError(f"Invalid IP address range format: {ip}") |
88 | | - start = int(start) |
89 | | - end = int(end) |
90 | | - if start < 0 or start > 255 or end < 0 or end > 255 or start > end: |
91 | | - raise ValueError(f"Invalid IP address range: {start}-{end}") |
92 | | - ip_range.append((start, end)) |
93 | | - case _: |
94 | | - raise ValueError(f"Invalid IP address range format: {ip}") |
95 | | - ip_count *= ip_range[-1][1] - ip_range[-1][0] + 1 |
96 | | - if ip_count > 65536: |
97 | | - raise ValueError(f"IP address range too large: {ip_count} addresses") |
98 | | - return [ |
99 | | - f"{a}.{b}.{c}.{d}" |
100 | | - for a in range(ip_range[0][0], ip_range[0][1] + 1) |
101 | | - for b in range(ip_range[1][0], ip_range[1][1] + 1) |
102 | | - for c in range(ip_range[2][0], ip_range[2][1] + 1) |
103 | | - for d in range(ip_range[3][0], ip_range[3][1] + 1) |
104 | | - ] |
105 | | - ip_tuple = ip_to_tuple(ip) |
106 | | - return [f"{ip_tuple[0]}.{ip_tuple[1]}.{ip_tuple[2]}.{ip_tuple[3]}"] |
| 7 | +from Jiyu_udp_attack.ip_analyze import ip_analyze |
107 | 8 |
|
108 | 9 |
|
109 | 10 | def send_packet(src_ip: str, dst_ip: str, dst_port: int, data: bytes) -> None: |
|
0 commit comments