-
Notifications
You must be signed in to change notification settings - Fork 263
/
Copy pathsolver.py
333 lines (309 loc) · 10.1 KB
/
solver.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import socket
import telnetlib
from keystone import *
from capstone import *
from unicorn import *
from unicorn.x86_const import *
from unicorn.arm_const import *
from unicorn.arm64_const import *
from unicorn.mips_const import *
import riscv_asm
import riscv_dis
import wasmlib
from crypto_commons.netcat.netcat_commons import receive_until_match, receive_until
from ppclib import ppc_execute
from risclib import risc_execute
last_stage = False
s = socket.socket()
Ks(KS_ARCH_PPC, KS_MODE_PPC32 + KS_MODE_BIG_ENDIAN)
def interactive(s):
t = telnetlib.Telnet()
t.sock = s
t.interact()
def level():
challenge = receive_until_match(s, r'Press the Enter key to start the challenge...')
print(challenge)
s.send("\n")
code = receive_until_match(s, r'Answer:')
print 'code', code
raw_code = '\n'.join(code.split('\n')[1:-1])
print 'raw_code', raw_code
if '| | |___ | | |_| | | |___ ' in challenge:
arch = 'i386'
ks = (KS_ARCH_X86, KS_MODE_32)
cs = (CS_ARCH_X86, CS_MODE_32)
uc = (UC_ARCH_X86, UC_MODE_32)
uc_result = UC_X86_REG_EAX
uc_stack = UC_X86_REG_ESP
elif "`. `' .' / / \ ' / ' _ \\" in challenge:
arch = 'x64'
ks = (KS_ARCH_X86, KS_MODE_64)
cs = (CS_ARCH_X86, CS_MODE_64)
uc = (UC_ARCH_X86, UC_MODE_64)
uc_result = UC_X86_REG_RAX
uc_stack = UC_X86_REG_RSP
elif ' |_ _| | || | |_ __ \ | || | / ___ | |' in challenge:
arch = 'mips'
ks = (KS_ARCH_MIPS, KS_MODE_MIPS32 + KS_MODE_BIG_ENDIAN)
cs = (CS_ARCH_MIPS, CS_MODE_MIPS32 + CS_MODE_BIG_ENDIAN)
uc = (UC_ARCH_MIPS, UC_MODE_MIPS32 + UC_MODE_BIG_ENDIAN)
uc_result = UC_MIPS_REG_V0
uc_stack = UC_MIPS_REG_29
elif '/ /\ \ / /\ \ ) (__) ) / / \ (__) / / / ( (__) (_' in challenge:
arch = 'aarch64'
ks = (KS_ARCH_ARM64, KS_MODE_LITTLE_ENDIAN)
cs = (CS_ARCH_ARM64, CS_MODE_LITTLE_ENDIAN)
uc = (UC_ARCH_ARM64, UC_MODE_ARM)
uc_result = UC_ARM64_REG_X0
uc_stack = UC_ARM64_REG_SP
elif '|____| |____||____| |___||_____||_____' in challenge:
arch = 'arm'
ks = (KS_ARCH_ARM, KS_MODE_ARM)
cs = (CS_ARCH_ARM, CS_MODE_ARM)
uc = (UC_ARCH_ARM, UC_MODE_ARM)
uc_result = UC_ARM_REG_R0
uc_stack = UC_ARM_REG_SP
elif '|_| \_\_____|_____/ \_____| \/ ' in challenge: # risc
arch = 'risc'
response = riscv_asm.asm(raw_code).encode('base64').replace('\n', '')
global last_stage
last_stage = True
elif last_stage:
arch = 'wasm'
response = wasmlib.asm(raw_code).encode('base64').replace('\n', '')
else:
# assume PPC32
arch = 'ppc32'
ks = (KS_ARCH_PPC, KS_MODE_PPC32 + KS_MODE_BIG_ENDIAN)
cs = (CS_ARCH_PPC, CS_MODE_32 + CS_MODE_BIG_ENDIAN)
for i in range(32):
raw_code = raw_code.replace("r" + str(i), str(i))
print 'architecture:', arch
if arch != 'risc' and arch!='wasm':
ks = Ks(ks[0], ks[1])
encoding, count = ks.asm(raw_code)
if arch == 'mips':
encoding = encoding[:-4] # untestedy yet
response = ''.join(map(chr, encoding)).encode('base64').replace('\n', '')
print 'response', response
s.send(response + '\n')
print(receive_until(s, '\n'))
out = receive_until(s, '\n')
print(out)
if arch == 'risc':
code = out.decode('base64')
result = riscv_dis.dis(code)
elif arch == 'wasm': # wasm
code = out.decode('base64')
result = wasmlib.dis(code)
else:
cs = Cs(cs[0], cs[1])
result = []
for i in cs.disasm(out.decode('base64'), 0x1000):
result += [(i.mnemonic + " " + i.op_str).strip()]
result = '\n'.join(result)
print 'asm', result
resultb64 = result.encode('base64').replace('\n', '')
print 'asmb64', resultb64
s.send(resultb64 + '\n')
print 'D', s.recv(99999)
print 'E', s.recv(99999)
challenge = s.recv(99999)
print repr(challenge)
data = challenge[8:-10]
print data.encode('hex')
if arch == 'risc':
result = riscv_dis.dis(data)
print(result)
elif arch == 'wasm':
result = wasmlib.dis(data)
print(result)
else:
result = []
for i in cs.disasm(challenge, 0x1000):
result += [(i.mnemonic + " " + i.op_str).strip()]
result = '\n'.join(result)
print result
# callback for tracing instructions
def hook_code(uc, address, size, user_data):
print(">>> Tracing instruction at 0x%x, instruction size = 0x%x" % (address, size))
for kot in cs.disasm(data[address - ADDRESS:address - ADDRESS + size], 4):
OP = kot.mnemonic + " " + kot.op_str
print 'OK', OP
break
if 'jr $ra' in OP:
uc.emu_stop()
elif 'ret' in OP:
uc.emu_stop()
elif 'bx lr' in OP:
uc.emu_stop()
return 'kot'
if arch == 'ppc32':
out = ppc_execute(result)
elif arch == 'risc':
out = risc_execute(result)
elif arch == 'wasm':
out = wasmlib.eval(result)
else:
uc = Uc(uc[0], uc[1])
ADDRESS = 0x10000000
STACK = 0x20000000
uc.mem_map(ADDRESS, 4 * 1024 * 1024)
uc.mem_map(STACK, 4 * 1024 * 1024)
uc.mem_write(ADDRESS, data)
uc.reg_write(uc_stack, STACK + 0x2000)
# tracing one instruction at ADDRESS with customized callback
uc.hook_add(UC_HOOK_CODE, hook_code, begin=ADDRESS, end=ADDRESS + len(data))
uc.emu_start(ADDRESS, ADDRESS + len(data))
out = uc.reg_read(uc_result)
print "RESULT", hex(out)
if arch!='wasm':
s.sendall(hex(out).replace('L', '') + '\n')
else:
s.sendall(str(out)+"\n")
interactive(s)
print(receive_until_match(s, '------------------------------'))
data = receive_until_match(s, '------------------------------')
print(data)
if "Wrong" in data:
interactive(s)
def main():
global s
s = socket.socket()
s.connect(('13.231.83.89', 30262))
print 'A', s.recv(999999)
print 'B', s.recv(999999)
s.send('\n')
print 'C', s.recv(999999)
print 'D', s.recv(999999)
s.send('\n')
print 'E', s.recv(999999)
print 'F', s.recv(999999)
s.send('\n')
print 'G', s.recv(999999)
print 'H', s.recv(999999)
s.send('A\n')
print 'I', s.recv(999999)
print 'J', s.recv(999999)
s.send('\n')
print 'K', s.recv(999999)
print 'L', s.recv(999999)
s.send('B\n')
print 'M', s.recv(999999)
print 'N', s.recv(999999)
s.send('\n')
print 'O', s.recv(999999)
s.send('duck\n')
for i in range(7):
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('w\n')
level()
for i in range(16):
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('d\n')
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('w\n')
level()
for i in range(9):
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('s\n')
level()
for i in range(16):
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('a\n')
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('s\n')
level()
for i in range(15):
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('a\n')
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('s\n')
level()
for i in range(6):
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('d\n')
for i in range(4):
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('w\n')
for i in range(2):
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('a\n')
level()
for i in range(4):
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('w\n')
for i in range(5):
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('a\n')
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('w\n')
level()
while True:
data = receive_until_match(s, '(Press the Enter key to continue...)')
print(data)
if 'EXIT2' in data:
break
s.sendall('\n')
rakflag()
def rakflag():
for i in range(7):
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('w\n')
for i in range(16):
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('d\n')
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('w\n')
for i in range(10):
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('s\n')
for i in range(17):
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('a\n')
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('s\n')
for i in range(16):
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('a\n')
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('s\n')
for i in range(6):
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('d\n')
for i in range(4):
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('w\n')
for i in range(2):
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('a\n')
for i in range(4):
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('w\n')
for i in range(5):
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('a\n')
for i in range(2):
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('w\n')
for i in range(25):
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('d\n')
for i in range(4):
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('s\n')
for i in range(10):
print(receive_until_match(s, "w/a/s/d:"))
s.sendall('d\n')
print(receive_until_match(s, "(Press the Enter key to continue...)"))
s.sendall("\n")
print(receive_until_match(s, "(Press the Enter key to continue...)"))
s.sendall("\n")
print(receive_until_match(s, "(Press the Enter key to continue...)"))
s.sendall("\n")
level()
s.sendall("\n")
s.sendall("\n")
s.sendall("\n")
interactive(s)
main()