forked from mephi42/ctf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemu.py
272 lines (263 loc) · 6.95 KB
/
emu.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
#!/usr/bin/env python3
import sys
import z3
bytecode = open(sys.argv[1], 'rb').read().decode()
answer = [z3.BitVec(f'flag[{i}]', 8) for i in range(24)]
solver = z3.Solver()
for c in answer:
solver.add(c != 0x00)
solver.add(c != 0x0A)
answer[0] = ord(b'p')
answer[1] = ord(b'l')
answer[2] = ord(b'i')
answer[3] = ord(b's')
answer[5] = ord(b'g')
answer[6] = ord(b'1')
answer[7] = ord(b'v')
answer[8] = ord(b'3')
answer[10] = ord(b'm')
answer[11] = ord(b'e')
answer[12] = ord(b'3')
answer[13] = ord(b'3')
answer[14] = ord(b't')
answer[15] = ord(b't')
answer[16] = ord(b'h')
answer[17] = ord(b'3')
answer[18] = ord(b'e')
answer[20] = ord(b'f')
answer[21] = ord(b'1')
answer[22] = ord(b'4')
answer[23] = ord(b'g')
for i in (0x4, 0x9, 0xE, 0x13):
answer[i] = ord(b'-')
print(bytes(answer).decode())
def eq(a, b):
if type(a) is int:
cond = b == a
else:
cond = a == b
if type(cond) is bool:
return 1 if cond else 0
else:
return z3.If(cond, 1, 0)
def check(x):
if type(x) == bool:
return 'sat' if x else 'unsat'
return solver.check(x)
def model(result):
values = {}
if isinstance(result, str):
return values
m = solver.model()
for c in answer:
if isinstance(c, int):
continue
values[c] = chr(m[c].as_long())
return values
predefined_inputs = [
answer,
]
code_table = {
127539: 1,
10133: 2,
10134: 3,
10060: 4,
10067: 5,
10062: 6,
128107: 7,
128128: 8,
128175: 9,
128640: 10,
127542: 11,
127514: 12,
9196: 13,
128285: 14,
128228: 15,
128229: 16,
127381: 17,
127379: 18,
128196: 19,
128221: 20,
128289: 21,
128290: 22,
128721: 23,
}
data_table = {
128512: 0,
128513: 1,
128514: 2,
129315: 3,
128540: 4,
128516: 5,
128517: 6,
128518: 7,
128521: 8,
128522: 9,
128525: 10,
}
pc = 0
stack = [None] * 10 + [0, 0]
gptr = stack
while True:
code = code_table[ord(bytecode[pc])]
if code == 1:
print(f'0x{pc:05X} NOP')
pc += 1
elif code == 2:
arg0 = stack.pop()
arg1 = stack.pop()
print(f'0x{pc:05X} ADD {arg0} {arg1}')
stack.append(arg0 + arg1)
pc += 1
elif code == 3:
arg0 = stack.pop()
arg1 = stack.pop()
print(f'0x{pc:05X} SUB {arg0} {arg1}')
stack.append(arg0 - arg1)
pc += 1
elif code == 4:
arg0 = stack.pop()
arg1 = stack.pop()
print(f'0x{pc:05X} MUL {arg0} {arg1}')
stack.append(arg0 * arg1)
pc += 1
elif code == 5:
arg0 = stack.pop()
arg1 = stack.pop()
print(f'0x{pc:05X} MOD {arg0} {arg1}')
stack.append(arg0 % arg1)
pc += 1
elif code == 6:
arg0 = stack.pop()
arg1 = stack.pop()
print(f'0x{pc:05X} XOR {arg0} {arg1}')
stack.append(arg0 ^ arg1)
pc += 1
elif code == 7:
arg0 = stack.pop()
arg1 = stack.pop()
print(f'0x{pc:05X} AND {arg0} {arg1}')
stack.append(arg0 & arg1)
pc += 1
elif code == 8:
arg0 = stack.pop()
arg1 = stack.pop()
print(f'0x{pc:05X} LT {arg0} {arg1}')
stack.append(1 if arg0 < arg1 else 0)
pc += 1
elif code == 9:
arg0 = stack.pop()
arg1 = stack.pop()
print(f'0x{pc:05X} EQ {arg0} {arg1}')
stack.append(eq(arg0, arg1))
pc += 1
elif code == 0xa:
# btw this and all of the above do not check for underflow - pwn opportunity?
pc = stack.pop()
print(f'0x{pc:05X} JMP {pc}')
elif code == 0xb:
arg0 = stack.pop()
arg1 = stack.pop()
print(f'0x{pc:05X} JNZ {arg0} {arg1}')
can_be_0 = check(arg1 == 0)
if str(can_be_0) == 'unsat':
pc = arg0
else:
m0 = model(can_be_0)
cannot_be_0 = check(arg1 != 0)
if str(cannot_be_0) == 'unsat':
pc += 1
else:
m1 = model(cannot_be_0)
print(m0)
print(m1)
raise Exception('Too dumb to fork')
elif code == 0xc:
arg0 = stack.pop()
arg1 = stack.pop()
print(f'0x{pc:05X} JZ {arg0} {arg1}')
if arg1 == 0:
pc = arg0
else:
pc += 1
elif code == 0xd:
if len(stack) == 1024:
raise Exception('Stack overflow')
value = data_table[ord(bytecode[pc + 1])]
print(f'0x{pc:05X} PUSH {value}')
stack.append(value)
pc += 2
elif code == 0xe:
if len(stack) == 0:
raise Exception('Stack underflow')
arg0 = stack.pop()
print(f'0x{pc:05X} POP {arg0}')
pc += 1
elif code == 0xf:
index = stack.pop()
offset = stack.pop()
print(f'0x{pc:05X} LD {index}[{offset}]')
stack.append(gptr[index][offset])
pc += 1
elif code == 0x10:
index = stack.pop()
offset = stack.pop()
value = stack.pop() & 0xff
print(f'0x{pc:05X} ST {index}[{offset}] <- {value}')
gptr[index][offset] = value
pc += 1
elif code == 0x11:
size = stack.pop()
if size > 1500:
raise Exception('Size is too large')
print(f'0x{pc:05X} NEW 0x{size:X}')
for i in range(len(gptr)):
if gptr[i] is None:
gptr[i] = [0] * size
print(f' INDEX = 0x{i:x}')
break
pc += 1
elif code == 0x12:
index = stack.pop()
print(f'0x{pc:05X} DEL {index}')
if gptr[index] is None:
raise Exception('Not allocated')
gptr[index] = None
pc += 1
elif code == 0x13:
index = stack.pop()
print(f'0x{pc:05X} INPUT {index}')
if gptr[index] is None:
raise Exception('Not allocated')
# btw we can omit trailing \0 - read opportunity?
gptr_element = gptr[index]
if len(predefined_inputs) == 0:
raw = input().encode()
else:
raw = predefined_inputs.pop(0)
print(f' PREDEFINED {raw}')
gptr_element[:len(raw)] = raw
pc += 1
elif code == 0x14:
index = stack.pop()
print(f'0x{pc:05X} PRINT GPTR ASCIIZ {index}')
if gptr[index] is None:
raise Exception('Not allocated')
gptr_element = gptr[index]
gptr_element_len = gptr_element.index(0)
if gptr_element_len == -1:
raise Exception('No trailing zero') # this is NOT a part of VM
print(bytes(gptr_element[:gptr_element_len]).decode(), end='')
pc += 1
elif code == 0x15:
print(f'0x{pc:05X} PRINT STACK ASCIIZ')
raise Exception('TODO')
elif code == 0x16:
value = stack.pop()
print(f'0x{pc:05X} PRINT {value}')
pc += 1
elif code == 0x17:
print(f'0x{pc:05X} RETURN')
break
else:
raise Exception(f'Not implemented: 0x{code:X}')