-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_patch.py
78 lines (63 loc) · 2.78 KB
/
create_patch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import re
import os
patch_next_int = False
patch_next_bigint = False
condition_num = b""
big_int_fails = 0
int_fails = 0
with open("patched.js", "wb") as patched:
with open("check_flag.js", "rb") as src:
for line in src:
if patch_next_int:
if b"Math.random" in line:
patched_line = b'console.log("1");Math.random()'
else:
patched_line = b'console.log("1")'
if len(patched_line) < len(line):
print("patching int")
line = patched_line.ljust(len(line) - 1, b" ") + b"\n"
else:
print(f'Can\'t patch Int: {line.decode("ascii")}')
int_fails += 1
patch_next_int = False
if patch_next_bigint:
# if b'Math.random' in line:
# patched_line = b'console.log("2");Math.floor(Math.random())'
# else:
# patched_line = b'console.log("2")'
if b"Math.random" in line:
patched_line = bytes(
f'console.log("T:{condition_num.decode("ascii")}");Math.floor(Math.random())'.encode(
"ascii"
)
)
else:
patched_line = bytes(
f'console.log("T:{condition_num.decode("ascii")}");'.encode(
"ascii"
)
)
if len(patched_line) < len(line):
print("patching big int")
line = patched_line.ljust(len(line) - 1, b" ") + b"\n"
else:
print(f'Can\'t patch BigInt: {line.decode("ascii")}')
big_int_fails += 1
patch_next_bigint = False
if re.match(rb"\s+if \([0-9]+\)", line):
patch_next_int = True
if re.match(rb"\s+if \([0-9]+n\)", line):
patch_next_bigint = True
condition_num = re.match(rb"\s+if \(([0-9]+n)\)", line)[1]
if b"var target = " in line:
patched_line = b"console.log(b);var target = [];"
line = patched_line.ljust(len(line), b" ")
patched.write(line)
print(f'Original size: {os.path.getsize("check_flag.js")}')
print(f'Patched size: {os.path.getsize("patched.js")}')
print(f"Failed patches: (int) {int_fails}, (BigInt) {big_int_fails}")
assert os.path.getsize("patched.js") == os.path.getsize("check_flag.js")
with open("patched.js", "rb") as patched:
with open("anode-exp.exe", "rb+") as exe:
exe.seek(0x35E3806)
exe.write(patched.read())