-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpatch_seccomp.py
322 lines (308 loc) · 5.42 KB
/
patch_seccomp.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
#!/usr/bin/env python
import lief, os
from pwn import *
from subprocess import Popen, PIPE
shell_seccomp_x86_pie = '''
push edi
push esi
push eax
push ebx
push ecx
push edx
push ebp
mov ebp, esp
push 0
pop edi
push 0
pop esi
push 0
pop edx
push 0x1
pop ecx
push 38
pop ebx
xor eax, eax
mov al, 0xac
int 0x80
push 22
pop ebx
call get_ip
and eax, 0xfffff000
lea edx, [eax]
push edx
push %d
mov edx, esp
push 0x2
pop ecx
xor eax, eax
mov al, 0xac
syscall
leave
pop edx
pop ecx
pop ebx
pop eax
pop esi
pop edi
call get_ip
and eax, 0xfffff000
sub eax, %d
add eax, %d
jmp eax
get_ip:
pop eax
jmp eax'''
shell_seccomp_x86 = '''
push edi
push esi
push eax
push ebx
push ecx
push edx
push ebp
mov ebp, esp
push 0
pop edi
push 0
pop esi
push 0
pop edx
push 0x1
pop ecx
push 38
pop ebx
xor eax, eax
mov al, 0xac
int 0x80
push 22
pop ebx
call get_ip
and eax, 0xfffff000
lea edx, [eax]
push edx
push %d
mov edx, esp
push 0x2
pop ecx
xor eax, eax
mov al, 0xac
syscall
leave
pop edx
pop ecx
pop ebx
pop eax
pop esi
pop edi
mov eax, %d
jmp eax
get_ip:
pop eax
jmp eax'''
shell_seccomp = '''
push rdi
push rsi
push rax
push rcx
push rdx
push r8
push r10
push rbp
mov rbp, rsp
push 38
pop rdi
push 0x1
pop rsi
xor rdx, rdx
xor rcx, rcx
xor r10, r10
xor r8, r8
xor rax, rax
mov al, 0x9d
syscall
push 22
pop rdi
call get_ip
and rax, 0xfffffffffffff000
lea rdx, [rax]
push rdx
push %d
mov rdx, rsp
push 0x2
pop rsi
xor rax, rax
mov al, 0x9d
xor rcx, rcx
xor r8, r8
syscall
leave
pop r10
pop r8
pop rdx
pop rcx
pop rax
pop rsi
pop rdi
mov rax, %d
jmp rax
get_ip:
pop rax
jmp rax'''
shell_seccomp_pie = '''
push rdi
push rsi
push rax
push rcx
push rdx
push r8
push r10
push rbp
mov rbp, rsp
push 38
pop rdi
push 0x1
pop rsi
xor rdx, rdx
xor rcx, rcx
xor r10, r10
xor r8, r8
xor rax, rax
mov al, 0x9d
syscall
push 22
pop rdi
call get_ip
and rax, 0xfffffffffffff000
lea rdx, [rax]
push rdx
push %d
mov rdx, rsp
push 0x2
pop rsi
xor rax, rax
mov al, 0x9d
xor rcx, rcx
xor r8, r8
syscall
leave
pop r10
pop r8
pop rdx
pop rcx
pop rax
pop rsi
pop rdi
call get_ip
and rax, 0xfffffffffffff000
sub rax, %d
add rax, %d
jmp rax
get_ip:
pop rax
jmp rax'''
blacklist=['mmap', 'mprotect','rt_sigreturn','connect','bind','open','dup2','symlink','fork','sendfile','vfork','openat','execve','execveat']
whitelist = []
def write_file(filename,content):
fd=open(filename,'w')
fd.write(content)
fd.close()
def convert(code, flag = 1):
if flag == 1:
assembly = asm(code, arch='amd64')
else:
assembly = code
ret = [ord(c) for c in assembly]
return ret
def generate_seccomp(x86):
if x86:
template="A = arch\nA == ARCH_i386 ? next: dead\nA = sys_number\n"
else:
template="A = arch\nA == ARCH_X86_64 ? next: dead\nA = sys_number\n"
for sn in blacklist:
if sn not in whitelist:
template+="A == {} ? dead : next\n".format(sn)
template+="ok:\nreturn ALLOW\ndead:\nreturn KILL"
write_file("scmp.asm",template)
p=Popen(["seccomp-tools", "asm", "scmp.asm", "-f", "raw"],stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()
os.remove("scmp.asm")
return stdout
def patch_binary(binary, x86):
sc=generate_seccomp(x86)
b = lief.parse(binary)
entrypoint = b.header.entrypoint
print("entrypoint: 0x%x"%entrypoint)
code = lief.ELF.Section()
code += lief.ELF.SECTION_FLAGS.EXECINSTR
code += lief.ELF.SECTION_FLAGS.WRITE
if x86:
code.content = convert(sc, 0) + convert(shell_seccomp_x86%( len(sc)/8, entrypoint))
else:
code.content = convert(sc, 0) + convert(shell_seccomp%( len(sc)/8, entrypoint))
new_code = b.add(code)
addr_new_code = new_code.virtual_address
print("new entrypoint: 0x%x" % (addr_new_code + len(sc) ))
# b.header.entrypoint = addr_new_code + len(sc)
os.system("rm -f %s_patched"%binary)
b.write("%s_patched"%binary)
os.system("chmod +x %s_patched"%binary)
def patch_pie(binary, x86):
sc=generate_seccomp(x86)
b = lief.parse(binary)
entrypoint = b.header.entrypoint
print("entrypoint: 0x%x"%entrypoint)
code = lief.ELF.Section()
# code += lief.ELF.SECTION_FLAGS.EXECINSTR
# code += lief.ELF.SECTION_FLAGS.WRITE
code.content=[0x90]*0x10
new_code = b.add(code)
new_entrypoint = b.header.entrypoint
print("entrypoint: 0x%x"%new_entrypoint)
addr_new_code = new_code.virtual_address
if x86:
new_code.content = convert(sc, 0) + convert(shell_seccomp_x86_pie%( len(sc)/8, addr_new_code , new_entrypoint))
else:
new_code.content = convert(sc, 0) + convert(shell_seccomp_pie%( len(sc)/8, addr_new_code , new_entrypoint))
b.header.entrypoint = addr_new_code + len(sc)
print("new entrypoint: 0x%x" % (addr_new_code + len(sc) ))
os.system("rm -f %s_patched"%binary)
b.write("%s_patched"%binary)
os.system("chmod +x %s_patched"%binary)
def menu():
m='0. patch\t'
for i in range(len(blacklist)):
m+="%d. %s\t"%(i+1,blacklist[i])
print(m)
def main(argv):
if len(argv) <=3:
print("no")
return
binary=argv[1]
pie = 0
x86 = 0
c = ''
while c!='0':
try:
menu()
c = raw_input("choice: ").strip("\n")
if c == '0':
break
if not c.isdigit():
return
i = int(c) -1
if i >= len(blacklist):
print("out of range")
return
whitelist.append(blacklist[i])
except Exception as e:
print(e)
if argv[1] == '1':
pie = 1
if argv[2] == '1':
x86 = 1
if pie:
patch_pie(binary, x86)
else:
patch_binary(binary, x86)
if __name__=="__main__":
main(sys.argv)