-
Notifications
You must be signed in to change notification settings - Fork 115
/
linux_x64_syscall_bpcond.py
297 lines (296 loc) · 10.3 KB
/
linux_x64_syscall_bpcond.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
import idaapi
nn = idaapi.netnode("$ kernel")
sc = cpu.rax
cpu.rax = 0
#rather than building a big dict of handlers, this is
#a binary search based on system call #
if sc < 177:
if sc < 89:
if sc < 45:
if sc < 23:
if sc < 12:
if sc < 6:
if sc < 3:
if sc == 0: #read
pass
elif sc == 1: #write
pass
else: #2, open
msg("open(\"%s\", 0x%x, 0x%x)\n" % (idaapi.get_strlit_contents(cpu.rdi, -1, STRTYPE_C), cpu.rsi, cpu.rdx))
cpu.rax = 3
elif sc > 3:
if sc == 4: #stat
pass
else: #5, fstat
idaapi.patch_dword(cpu.rsi, 0x4000)
idaapi.patch_qword(cpu.rsi + 0x20, 0x1000)
else: #3, close
pass
elif sc > 6:
if sc < 9:
pass
elif sc > 9:
if sc == 10:
pass # 10, mprotect
else: # 11, munmap
msg("munmap(0x%x, 0x%x)\n" % (cpu.rdi, cpu.rsi))
rv = idaapi.idc_value_t()
idaapi.eval_idc_expr(rv, BADADDR, "sk3wl_munmap(0x%x, 0x%x)" % (cpu.rdi, cpu.rsi))
mmt = nn.altval(11) #KERNEL_MMAP_TOP
if mmt == cpu.rdi:
nn.altset(11, mmt + cpu.rsi) #KERNEL_MMAP_TOP
else: #mmap
msg("mmap(0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x)\n" % (cpu.rdi, cpu.rsi, cpu.rdx, cpu.r10, cpu.r8, cpu.r9))
mmt = nn.altval(11) #KERNEL_MMAP_TOP
cpu.rax = mmt - cpu.rsi
mmt = nn.altset(11, cpu.rax) #KERNEL_MMAP_TOP
rv = idaapi.idc_value_t()
idaapi.eval_idc_expr(rv, BADADDR, "sk3wl_mmap(0x%x, 0x%x, 7)" % (cpu.rax, cpu.rsi))
else: #lstat
pass
elif sc > 12:
if sc < 17:
pass
elif sc > 17:
if sc < 20:
if sc == 18: #pwrite64
pass
else: #19 readv
pass
elif sc > 20:
if sc == 21: #access
cpu.eax = -2 #ENOENT
pass
else: #22 pipe
pass
else: #writev
pass
else: #pread64
pass
else: #brk
cb = nn.altval(0) #KERNEL_BRK
if cpu.rdi == 0 or cpu.rdi == cb:
cpu.rax = cb
elif cpu.rdi < cb: #shrinking
rv = idaapi.idc_value_t()
nb = cpu.rdi & ~0xfff
idaapi.eval_idc_expr(rv, BADADDR, "sk3wl_munmap(0x%x, 0x%x)" % (nb, cb - nb))
nn.altset(0, nb) #KERNEL_BRK
cpu.rax = nb
else: #growing
rv = idaapi.idc_value_t()
nb = (cpu.rdi + 0xfff) & ~0xfff
idaapi.eval_idc_expr(rv, BADADDR, "sk3wl_mmap(0x%x, 0x%x, 7)" % (cb, nb - cb))
nn.altset(0, nb) #KERNEL_BRK
cpu.rax = nb
elif sc > 23:
if sc < 34:
pass
elif sc > 34:
if sc < 40:
if sc < 37:
pass
elif sc > 37:
if sc == 38: #setitimer
pass
else: #getpid
cpu.rax = nn.altval(1) #KERNEL_PID
else: # alarm
pass
elif sc > 40:
pass
else: # sendfile
pass
else: # pause
pass
else: #select
pass
elif sc > 45:
if sc < 67:
if sc < 56:
pass
elif sc > 56:
if sc < 61:
if sc < 58:
pass #fork, returning 0
elif sc > 58:
if sc == 59: #execve
msg("execve(\"%s\", ...)\n" % idc.get_strlit_contents(cpu.rdi, -1, STRTYPE_C))
cpu.rax = -2 # ENOENT seems reasonable since we can't do this anyway
return 1 #actually break
else: #60 exit
warning("process exited")
return 1 #actually break
else: #vfork
pass
elif sc > 61:
if sc < 64:
if sc == 62: #kill
pass
else: #uname
for i, ch in enumerate("Linux"):
idaapi.patch_byte(cpu.rdi + i, ord(ch))
for i, ch in enumerate("ubuntu"):
idaapi.patch_byte(cpu.rdi + i + 0x41, ord(ch))
for i, ch in enumerate("4.4.0-119-generic"):
idaapi.patch_byte(cpu.rdi + i + 0x82, ord(ch))
for i, ch in enumerate("#143-Ubuntu SMP Mon Apr 2 16:08:24 UTC 2018"):
idaapi.patch_byte(cpu.rdi + i + 0xc3, ord(ch))
for i, ch in enumerate("x86_64"):
idaapi.patch_byte(cpu.rdi + i + 0x104, ord(ch))
elif sc > 64:
if sc == 65: #semop
pass
else: #semctl
pass
else: #semget
pass
else: #wait4
pass
else: #clone
cpu.rax = -1 #make clone attempts fail
elif sc > 67:
if sc < 78:
pass
elif sc > 78:
if sc < 83:
if sc < 80: #getcwd
default_cwd = "/home/user\x00"
if cpu.rsi < len(default_cwd):
cpu.rax = -34 #ERANGE
else:
cpu.rax = cpu.rdi
for i, ch in enumerate(default_cwd):
idaapi.patch_byte(cpu.rdi + i, ord(ch))
elif sc > 80:
if sc == 81: #fchdir
pass
else: #rename
pass
else: #chdir
pass
elif sc > 83:
pass
else: #mkdir
pass
else: #getdents
pass
else: #shmdt
pass
else: #recvfrom
pass
elif sc > 89:
if sc < 133:
if sc < 111:
if sc < 100:
if sc < 94:
pass
elif sc > 94:
if sc < 97:
pass
elif sc > 97:
pass
else: #getrlimit
if cpu.rdi == 3: #RLIMIT_STACK
idaapi.patch_qword(cpu.rsi, 0x100000)
idaapi.patch_qword(cpu.rsi + 8, 0x100000)
#implement others
else: #lchown
pass
elif sc > 100:
if sc < 105:
if sc < 103:
if sc == 101: #ptrace
pass
else: #getuid
cpu.rax = nn.altval(3) #KERNEL_UID
elif sc > 103: #getgid
cpu.rax = nn.altval(5) #KERNEL_GID
else: #syslog
pass
elif sc > 105:
if sc < 108:
if sc == 106: #setgid
pass
else: #geteuid
cpu.rax = nn.altval(4) #KERNEL_EUID
elif sc > 108:
if sc == 109: #getpgid
pass
else: #110, getppid
cpu.rax = nn.altval(2) #KERNEL_PPID
else: #getegid
cpu.rax = nn.altval(6) #KERNEL_EGID
else: #setuid
#nn.altset(3, cpu.rax) #KERNEL_UID
pass
else: #times
pass
elif sc > 111:
pass
else: #getpgrp
pass
elif sc > 133:
pass
else: #mknod
pass
else: #readlink
msg("readlink: %s\n" % idc.get_strlit_contents(cpu.rdi, -1, STRTYPE_C))
for i, ch in enumerate("/home/user/crack_me"):
cpu.rax = i
if i > cpu.rdx:
break
idaapi.patch_byte(cpu.rsi + i, ord(ch))
elif sc > 177:
if sc < 251:
if sc < 214:
pass
elif sc > 214:
if sc < 232:
if sc < 223:
if sc < 218:
pass
elif sc > 218:
pass
else: #set_tid_address
nn.altset(8, cpu.rdi) #KERNEL_TID_ADDRESS
cpu.rax = nn.altval(7) #KERNEL_TID
elif sc > 223:
pass
else: #timer_settime
pass
elif sc > 232:
pass
else: #epoll_wait
pass
else: #epoll_ctl_old
pass
elif sc > 251:
if sc < 288:
if sc < 269:
pass
elif sc > 269:
if sc < 278:
if sc < 273:
pass
elif sc > 273:
pass
else: #set_robust_list
nn.altset(9, cpu.rsi) # KERNEL_ROBUST_LIST
nn.altset(10, cpu.rdx) # KERNEL_ROBUST_LIST_SIZE
elif sc > 278:
pass
else: #vmsplice
pass
else: #faccessat
pass
elif sc > 288:
pass
else: #accept4
pass
else: #ioprio_set
pass
else: #get_kernel_syms
pass
msg("syscall %d returning 0x%x\n" % (sc, cpu.rax))
return 0