1111
1212import secrets
1313
14- from typing import Literal
14+ from typing import Literal , Optional
1515
1616import scapy .all as scapy
1717
@@ -117,7 +117,7 @@ def ip_analyze(ip: str) -> list[str]:
117117 return [f"{ ip_tuple [0 ]} .{ ip_tuple [1 ]} .{ ip_tuple [2 ]} .{ ip_tuple [3 ]} " ]
118118
119119
120- def format_data (data : str , max_length : int ) -> bytes :
120+ def format_data (data : str , max_length : Optional [ int ] = None ) -> bytes :
121121 """
122122 Formats a string into a byte array, ensuring it is within the specified maximum length.
123123
@@ -130,6 +130,8 @@ def format_data(data: str, max_length: int) -> bytes:
130130 """
131131 if not isinstance (data , str ):
132132 raise TypeError (f"Expected string, got { type (data ).__name__ } " )
133+ if max_length is None :
134+ return data .encode ("utf-16le" )
133135 if not isinstance (max_length , int ):
134136 raise TypeError (f"Expected int, got { type (max_length ).__name__ } " )
135137 if max_length <= 0 :
@@ -205,6 +207,39 @@ def pkg_command(
205207 return head + data0 + data1 + b"\x00 " * 66 + data2
206208
207209
210+ def pkg_website (url : str ) -> bytes :
211+ """
212+ Packages a website URL into a specific byte format, including a header and padding.
213+
214+ Args:
215+ url (str): The website URL to package.
216+
217+ Returns:
218+ bytes: The packaged URL as a byte array, including a header and padding.
219+
220+ Raises:
221+ ValueError: If the URL length exceeds 800 bytes.
222+ TypeError: If the URL is not a string.
223+ """
224+ data = format_data (url )
225+
226+ len1 = len (data ) + 36
227+ len2 = len1 - 13
228+ siz1 = len1 .to_bytes (4 , "little" )
229+ siz2 = len2 .to_bytes (4 , "little" )
230+ siz2 = siz2 [1 :] + siz2 [0 :1 ]
231+
232+ head = (
233+ b"DMOC\x00 \x00 \x01 \x00 "
234+ + siz1
235+ + secrets .token_bytes (25 )
236+ + siz2
237+ + b"\x00 \x00 \x00 \x00 \x02 \x00 \x00 \x00 \x00 \x00 \x00 \x18 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
238+ )
239+
240+ return head + data + b"\x00 " * 4
241+
242+
208243def send_packet (src_ip : str , dst_ip : str , dst_port : int , data : bytes ) -> None :
209244 """
210245 Sends a UDP packet with the specified source IP, destination IP, destination port, and data payload.
0 commit comments