-
Notifications
You must be signed in to change notification settings - Fork 539
/
Copy pathsolve.py
485 lines (380 loc) · 14.1 KB
/
solve.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#!/usr/bin/env python3
## -*- coding: utf-8 -*-
##
## Jonathan Salwan - 2020-05-03
##
## Description: Solution of the catalyst system challenge from the ALEXCTF 2017.
## In this solution, we fully emulate the binary and we solve each branch
## to go through the good path and print the serial. The particularity of this
## challenge is that we need to emulate the real behavior of srand and rand of
## the libc. Thus, we use ctypes to load those two functions. However, in order
## to be able to use this example as unittest on Linux, Winwdows and OSX, I've
## hardcoded the return values of rand() with known inputs. If you want to use
## the ctypes verion, just set the UNITTEST flag to False.
##
## Output:
##
## $ time python3 ./solve.py
## Loading..............................
## Username: Password: Logging in..............................
## your flag is: ALEXCTF{1_t41d_y0u_y0u_ar3__gr34t__reverser__s33
## [+] The username is : catalyst_ceo
## [+] The password is : sLSVpQ4vK3cGWyW86AiZhggwLHBjmx9CRspVGggj
## [+] The flag is : 1_t41d_y0u_y0u_ar3__gr34t__reverser__s33
## Success!
## python3 ./solve.py 2.68s user 0.02s system 99% cpu 2.709 total
## $
##
from __future__ import print_function
from triton import *
import ctypes
import random
import string
import sys
import lief
import os
import platform
TARGET = os.path.join(os.path.dirname(__file__), 'catalyst')
DEBUG = False
UNITTEST = True
# The debug function
def debug(s):
if DEBUG: print(s)
# Memory mapping
BASE_PLT = 0x10000000
BASE_ARGV = 0x20000000
BASE_ALLOC = 0x03000000
BASE_STACK = 0x9fffffff
# Allocation information used by malloc()
mallocMaxAllocation = 0x03ffffff
mallocBase = BASE_ALLOC
# Used for srand and rand. Those two functions are
# used for the flag computation and cannot be simulated
# in Python
if not UNITTEST:
assert(platform.system() == 'Linux')
libc = ctypes.CDLL('libc.so.6')
# The flag at the end of the analysis
flag = str()
scanf_inputs = [
b"a" * 12 + b"\x00",
b"b" * 40 + b"\x00"
]
WITH_PREDICATE = True
conditions = [
(0x400CBF, REG.X86_64.ZF, 0, WITH_PREDICATE),
(0x400D2F, REG.X86_64.ZF, 1, WITH_PREDICATE),
(0x400D5C, REG.X86_64.ZF, 1, WITH_PREDICATE),
(0x400D77, REG.X86_64.ZF, 1, WITH_PREDICATE),
(0x400A44, REG.X86_64.ZF, 1, WITH_PREDICATE),
(0x400A80, REG.X86_64.ZF, 1, not WITH_PREDICATE),
(0x400AAE, REG.X86_64.ZF, 1, not WITH_PREDICATE),
(0x400ADC, REG.X86_64.ZF, 1, not WITH_PREDICATE),
(0x400B0A, REG.X86_64.ZF, 1, not WITH_PREDICATE),
(0x400B38, REG.X86_64.ZF, 1, not WITH_PREDICATE),
(0x400B66, REG.X86_64.ZF, 1, not WITH_PREDICATE),
(0x400B94, REG.X86_64.ZF, 1, not WITH_PREDICATE),
(0x400BC2, REG.X86_64.ZF, 1, not WITH_PREDICATE),
(0x400BF0, REG.X86_64.ZF, 1, not WITH_PREDICATE),
(0x400C1E, REG.X86_64.ZF, 1, not WITH_PREDICATE),
]
def getMemoryString(ctx, addr):
s = str()
index = 0
while ctx.getConcreteMemoryValue(addr+index):
c = chr(ctx.getConcreteMemoryValue(addr+index))
if c not in string.printable: c = ""
s += c
index += 1
return s
def printfHandler(ctx):
debug('[+] printf hooked')
# In this challenge, there is no need of format string
arg1 = getMemoryString(ctx, ctx.getConcreteRegisterValue(ctx.registers.rdi))
sys.stdout.write(arg1)
# Return value
return len(arg1)
def putcharHandler(ctx):
debug('[+] putchar hooked')
arg1 = ctx.getConcreteRegisterValue(ctx.registers.rdi)
sys.stdout.write(chr(arg1))
return arg1
def putsHandler(ctx):
debug('[+] puts hooked')
# Don't care about puts in this challenge
arg1 = getMemoryString(ctx, ctx.getConcreteRegisterValue(ctx.registers.rdi))
return len(arg1) + 1
def exitHandler(ctx):
debug('[+] exit hooked')
ret = ctx.getConcreteRegisterValue(ctx.registers.rdi)
sys.exit(ret)
def libcMainHandler(ctx):
debug('[+] __libc_start_main hooked')
# Get arguments
main = ctx.getConcreteRegisterValue(ctx.registers.rdi)
# Push the return value to jump into the main() function
ctx.setConcreteRegisterValue(ctx.registers.rsp, ctx.getConcreteRegisterValue(ctx.registers.rsp)-CPUSIZE.QWORD)
ret2main = MemoryAccess(ctx.getConcreteRegisterValue(ctx.registers.rsp), CPUSIZE.QWORD)
ctx.setConcreteMemoryValue(ret2main, main)
# Setup argc / argv
ctx.concretizeRegister(ctx.registers.rdi)
ctx.concretizeRegister(ctx.registers.rsi)
argvs = [
bytes(TARGET.encode('utf-8')), # argv[0]
]
# Define argc / argv
base = BASE_ARGV
addrs = list()
index = 0
for argv in argvs:
addrs.append(base)
ctx.setConcreteMemoryAreaValue(base, argv+b'\x00')
base += len(argv)+1
debug('[+] argv[%d] = %s' %(index, argv))
index += 1
argc = len(argvs)
argv = base
for addr in addrs:
ctx.setConcreteMemoryValue(MemoryAccess(base, CPUSIZE.QWORD), addr)
base += CPUSIZE.QWORD
ctx.setConcreteRegisterValue(ctx.registers.rdi, argc)
ctx.setConcreteRegisterValue(ctx.registers.rsi, argv)
return 0
def mallocHandler(ctx):
global mallocBase
global mallocMaxAllocation
debug('[+] malloc hooked')
size = ctx.getConcreteRegisterValue(ctx.registers.rdi)
if mallocBase + size > mallocMaxAllocation:
debug('[+] malloc failed: out of memory')
sys.exit(-1)
area = mallocBase
mallocBase += size
return area
def strlenHandler(ctx):
debug('[+] strlen hooked')
arg1 = getMemoryString(ctx, ctx.getConcreteRegisterValue(ctx.registers.rdi))
return len(arg1)
def timeHandler(ctx):
debug('[+] time hooked')
return 0
def sleepHandler(ctx):
debug('[+] sleep hooked')
return 0
def fflushHandler(ctx):
debug('[+] fflush hooked')
arg1 = ctx.getConcreteRegisterValue(ctx.registers.rdi)
f = [sys.stdin, sys.stdout, sys.stderr]
f[arg1].flush()
return 0
def srandHandler(ctx):
debug('[+] srand hooked')
arg1 = ctx.getConcreteRegisterValue(ctx.registers.rdi)
if not UNITTEST:
libc.srand(arg1)
return None
def randHandler(ctx):
debug('[+] rand hooked')
if not UNITTEST:
return libc.rand()
else:
rax = ctx.getConcreteRegisterValue(ctx.registers.rax)
oracles = {
# in rax : out rax
0x030003e8 : 0x00684749,
0x030003ec : 0x673ce537,
0x030003f0 : 0x7b4505e7,
0x030003f4 : 0x70a0b262,
0x030003f8 : 0x33d5253c,
0x030003fc : 0x515a7675,
0x03000400 : 0x596d7d5d,
0x03000404 : 0x7cd29049,
0x03000408 : 0x59e72db6,
0x0300040c : 0x4654600d,
}
if rax in oracles:
return oracles[rax]
return 0
def scanfHandler(ctx):
global scanf_inputs
debug('[+] scanf hooked')
# Get arguments
ast = ctx.getAstContext()
arg1 = ctx.getConcreteRegisterValue(ctx.registers.rdi)
arg2 = ctx.getConcreteRegisterValue(ctx.registers.rsi)
seed = scanf_inputs.pop(0)
# Fill scanf buffer with the input
ctx.setConcreteMemoryAreaValue(arg2, seed)
# Symbolize the username and password
debug('[+] symbolizing scanf buffer')
for index in range(0, len(seed) - 1):
var = ctx.symbolizeMemory(MemoryAccess(arg2 + index, CPUSIZE.BYTE))
vast = ast.variable(var)
chars = ast.lor([
ast.land([vast >= ord(b'A'), vast <= ord(b'Z')]),
ast.land([vast >= ord(b'a'), vast <= ord(b'z')]),
vast == ord(b'_'),
])
ctx.pushPathConstraint(chars)
# Return value
return len(seed) - 1
# Functions to emulate
customRelocation = [
('__isoc99_scanf', scanfHandler, BASE_PLT + 0),
('__libc_start_main', libcMainHandler, BASE_PLT + 1),
('exit', exitHandler, BASE_PLT + 2),
('fflush', fflushHandler, BASE_PLT + 3),
('malloc', mallocHandler, BASE_PLT + 4),
('printf', printfHandler, BASE_PLT + 5),
('putchar', putcharHandler, BASE_PLT + 6),
('puts', putsHandler, BASE_PLT + 7),
('rand', randHandler, BASE_PLT + 8),
('sleep', sleepHandler, BASE_PLT + 9),
('srand', srandHandler, BASE_PLT + 10),
('strlen', strlenHandler, BASE_PLT + 11),
('time', timeHandler, BASE_PLT + 12),
]
def hookingHandler(ctx):
pc = ctx.getConcreteRegisterValue(ctx.registers.rip)
for rel in customRelocation:
if rel[2] == pc:
# Emulate the routine and the return value
ret_value = rel[1](ctx)
if ret_value is not None:
ctx.setConcreteRegisterValue(ctx.registers.rax, ret_value)
# Get the return address
ret_addr = ctx.getConcreteMemoryValue(MemoryAccess(ctx.getConcreteRegisterValue(ctx.registers.rsp), CPUSIZE.QWORD))
# Hijack RIP to skip the call
ctx.setConcreteRegisterValue(ctx.registers.rip, ret_addr)
# Restore RSP (simulate the ret)
ctx.setConcreteRegisterValue(ctx.registers.rsp, ctx.getConcreteRegisterValue(ctx.registers.rsp)+CPUSIZE.QWORD)
return
# Emulate the binary.
def emulate(ctx, pc):
global conditions
global flag
count = 0
while pc:
# Fetch opcodes
opcodes = ctx.getConcreteMemoryAreaValue(pc, 16)
# Create the Triton instruction
instruction = Instruction(pc, opcodes)
# Process
if ctx.processing(instruction) == EXCEPTION.FAULT_UD:
debug('[-] Instruction not supported: %s' %(str(instruction)))
break
if DEBUG:
if instruction.isSymbolized():
print("\033[92m" + str(instruction) + "\033[0m")
else:
print(instruction)
if instruction.getType() == OPCODE.X86.HLT:
break
# Simulate routines
hookingHandler(ctx)
# Here we just restore the password as alphanumeric input
# to avoid the exit from the loop at loc_4009A4.
if instruction.getAddress() == 0x400998:
# The password area from the symbolic variables 12 to 51
for index in range(12, 52):
var = ctx.getSymbolicVariable(index)
ctx.setConcreteVariableValue(var, 0x61)
for condition in conditions:
sym_addr, sym_reg, sym_val, with_pp = condition
if instruction.getAddress() == sym_addr:
reg = ctx.getRegisterAst(ctx.getRegister(sym_reg))
ast = ctx.getAstContext()
mod = (ctx.getModel(ast.land([reg == sym_val, ctx.getPathPredicate()])) if with_pp else ctx.getModel(reg == sym_val))
for k,v in sorted(mod.items()):
ctx.setConcreteVariableValue(ctx.getSymbolicVariable(v.getId()), v.getValue())
# Print the flag. Here we just save it for unittest in
# our solution() function.
#
# .text:00000000004008BB movzx eax, byte ptr [rax]
# .text:00000000004008BE movsx eax, al
# .text:00000000004008C1 xor eax, edx
# .text:00000000004008C3 mov edi, eax
if instruction.getAddress() == 0x4008C3:
edi = ctx.getConcreteRegisterValue(ctx.registers.edi)
flag += chr(edi & 0xff)
# Next
pc = ctx.getConcreteRegisterValue(ctx.registers.rip)
count += 1
debug('[+] Instruction executed: %d' %(count))
return
def loadBinary(ctx, binary):
# Map the binary into the memory
phdrs = binary.segments
for phdr in phdrs:
size = phdr.physical_size
vaddr = phdr.virtual_address
debug('[+] Loading 0x%06x - 0x%06x' %(vaddr, vaddr+size))
ctx.setConcreteMemoryAreaValue(vaddr, list(phdr.content))
return
def makeRelocation(ctx, binary):
# Perform our own relocations
try:
for rel in binary.pltgot_relocations:
symbolName = rel.symbol.name
symbolRelo = rel.address
for crel in customRelocation:
if symbolName == crel[0]:
debug('[+] Hooking %s' %(symbolName))
ctx.setConcreteMemoryValue(MemoryAccess(symbolRelo, CPUSIZE.QWORD), crel[2])
except:
pass
# Perform our own relocations
try:
for rel in binary.dynamic_relocations:
symbolName = rel.symbol.name
symbolRelo = rel.address
for crel in customRelocation:
if symbolName == crel[0]:
debug('[+] Hooking %s' %(symbolName))
ctx.setConcreteMemoryValue(MemoryAccess(symbolRelo, CPUSIZE.QWORD), crel[2])
except:
pass
return
def run(ctx, binary):
# Define a fake stack
ctx.setConcreteRegisterValue(ctx.registers.rbp, BASE_STACK)
ctx.setConcreteRegisterValue(ctx.registers.rsp, BASE_STACK)
# Let's emulate the binary from the entry point
debug('[+] Starting emulation.')
emulate(ctx, binary.entrypoint)
debug('[+] Emulation done.')
return
def solution(ctx):
global flag
inputs = bytearray(52)
for k, v in sorted(ctx.getSymbolicVariables().items()):
inputs[k] = ctx.getConcreteVariableValue(v)
if inputs[:12] != b'catalyst_ceo':
return -1
if inputs[12:52] != b'sLSVpQ4vK3cGWyW86AiZhggwLHBjmx9CRspVGggj':
return -1
if flag != '1_t41d_y0u_y0u_ar3__gr34t__reverser__s33':
return -1
print("\n[+] The username is : %s" %(inputs[:12].decode('utf-8')))
print("[+] The password is : %s" %(inputs[12:52].decode('utf-8')))
print("[+] The flag is : %s" %(flag))
print('Success!')
return 0
def main():
# Get a Triton context
ctx = TritonContext(ARCH.X86_64)
# Set optimization
ctx.setMode(MODE.ALIGNED_MEMORY, True)
ctx.setMode(MODE.ONLY_ON_SYMBOLIZED, True)
# Parse the binary
binary = lief.parse(TARGET)
# Load the binary
loadBinary(ctx, binary)
# Perform our own relocations
makeRelocation(ctx, binary)
# Init and emulate
run(ctx, binary)
return solution(ctx)
if __name__ == '__main__':
retValue = main()
sys.exit(retValue)