-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.py
1235 lines (975 loc) · 32.3 KB
/
utils.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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
lldbinit core functions
Author : peternguyen
'''
from typing import List, Dict, Union, Optional, Type, Set, Any, Generic, TypeVar, Tuple, Iterator
import typing
from typing_extensions import Self
from lldb import SBDebugger, SBFrame, SBProcess, SBThread, SBTarget, SBAddress, \
SBValue, SBSymbol, SBError, SBType, SBValueList, SBInstructionList, \
SBInstruction, SBModule, SBModuleSpecList, SBCommandInterpreter, \
SBCommandReturnObject, SBSection, SBBreakpoint
import ctypes
import lldb
import re
from subprocess import Popen, PIPE, check_call, CalledProcessError
from pathlib import Path
from struct import pack, unpack
from dataclasses import dataclass
import struct
import platform
import time
# default colors - modify as you wish
COLOR_REGVAL = "WHITE"
COLOR_REGNAME = "GREEN"
COLOR_CPUFLAGS = "RED"
COLOR_SEPARATOR = "BLUE"
COLOR_HIGHLIGHT_LINE = "RED"
COLOR_REGVAL_MODIFIED = "YELLOW"
COLOR_SYMBOL_NAME = "BLUE"
COLOR_CURRENT_PC = "RED"
#
# Don't mess after here unless you know what you are doing!
#
COLORS = {
"BLACK": "\033[30m",
"RED": "\033[31m",
"GREEN": "\033[32m",
"YELLOW": "\033[33m",
"BLUE": "\033[34m",
"MAGENTA": "\033[35m",
"CYAN": "\033[36m",
"WHITE": "\033[37m",
"RESET": "\033[0m",
"BOLD": "\033[1m",
"UNDERLINE": "\033[4m"
}
# ----------------------------------------------------------
# Packing and Unpacking functions
# ----------------------------------------------------------
def p32(value: int) -> bytes:
return pack('<I', value)
def p64(value: int) -> bytes:
return pack('<Q', value)
# ----------------------------------------------------------
# Color Related Functions
# ----------------------------------------------------------
def get_color_status(addr: int) -> str:
target = get_target()
if target == None:
return ''
process = get_process()
if process == None:
return ''
module_map = resolve_mem_map(target, addr)
if module_map.section_name.startswith('__TEXT'):
# address is excutable page
return "RED"
elif module_map.section_name.startswith('__DATA'):
return "MAGENTA"
return "WHITE" if not readable(addr) else "CYAN"
# ----------------------------------------------------------
# Functions to extract internal and process lldb information
# ----------------------------------------------------------
class LLDBTargetNotFound(Exception):
# This exception will raise error when frame is None
def __init__(self, *args: object) -> None:
super().__init__(*args)
class LLDBFrameNotFound(Exception):
def __init__(self, *args: object) -> None:
super().__init__(*args)
def get_debugger() -> SBDebugger:
debugger: Optional[SBDebugger] = lldb.debugger
assert debugger != None, 'lldb.debugger == None'
return debugger
def get_target() -> SBTarget:
# try to get current target
debugger = get_debugger()
target = debugger.GetSelectedTarget()
if not target:
raise LLDBTargetNotFound("[-] error: no target available. please add a target to lldb.")
return target
def get_arch() -> str:
arch: str = get_target().triple
return arch.split('-')[0]
def get_process() -> SBProcess:
'''
A read only property that returns an lldb object
that represents the process (lldb.SBProcess)that this target owns.
'''
return get_target().process
def get_frame() -> SBFrame:
frame = None
# SBProcess supports thread iteration -> SBThread
for thread_i in get_process():
thread: SBThread = thread_i
if thread.GetStopReason() != lldb.eStopReasonInvalid:
frame = thread.GetFrameAtIndex(0)
break
# this will generate a false positive when we start the target the first time because there's no context yet.
if not frame:
raise LLDBFrameNotFound("[-] warning: get_frame() failed. Is the target binary started?")
return frame
def get_thread() -> Optional[SBThread]:
thread = None
# SBProcess supports thread iteration -> SBThread
for thread_i in get_process():
thread_i: SBThread = thread_i
if thread_i.GetStopReason() != lldb.eStopReasonInvalid:
thread = thread_i
if not thread:
print("[-] warning: get_thread() failed. Is the target binary started?")
return thread
class ParseValueError(Exception):
def __init__(self, *args: object) -> None:
super().__init__(*args)
def parse_number(str_num: str) -> int:
num = -1
if not str_num:
raise ParseValueError('str_num is empty')
try:
if str_num.startswith('0x') or str_num.startswith('0X'):
# parse hex number
num = int(str_num, 16)
else:
# parse number
num = int(str_num)
except ValueError:
try:
# parse hex number without prefix
num = int(str_num, 16)
except ValueError:
raise ParseValueError('str_num is not hex number of decimal number')
return num
# evaluate an expression and return the value it represents
def get_c_array_addr(value: SBValue) -> int:
var_type_name = value.GetTypeName()
if not var_type_name:
return 0
if 'char [' in var_type_name or 'unsigned char [' in var_type_name:
return value.GetLoadAddress()
if 'int [' in var_type_name or 'unsigned int [' in var_type_name:
return value.GetLoadAddress()
return 0
def evaluate(command: str) -> int:
'''
Trying to parse command in str format into address
Assume type of command are:
- LLDB command -> execute this command and try to get int value
- Variable name -> this variable hold address of not ??
-> if not get address of this variable (&var)
- hex string -> convert to int
- int string -> convert to int
'''
# assume command is number
try:
return parse_number(command)
except ParseValueError:
# assume command is variable
try:
some_var = ESBValue(command)
if not some_var.is_valid:
# this command is not variable name, assum command is lldb command
raise ESBValueException
if some_var.value == None:
# this variable name doesn't hold address, get address of this some_var instead
return some_var.addr_of()
return some_var.int_value
except ESBValueException:
# assume command is lldb command
some_express = ESBValue.init_with_expression(command)
if some_express.is_valid:
return some_express.int_value
return 0
def is_i386() -> bool:
arch = get_arch()
return arch[0:1] == "i"
def is_x64() -> bool:
arch = get_arch()
return arch.startswith("x86_64")
def is_arm() -> bool:
arch = get_arch()
return arch == "armv7"
def is_aarch64() -> bool:
arch = get_arch()
return arch == 'aarch64' or arch.startswith('arm64')
def is_supported_arch() -> bool:
return is_i386() or is_x64() or is_arm() or is_aarch64()
def get_pointer_size() -> int:
poisz = evaluate("sizeof(long)")
return poisz
# from https://github.com/facebook/chisel/blob/master/fblldbobjcruntimehelpers.py
def get_instance_object() -> str:
instanceObject = ''
if is_i386():
instanceObject = '*(id*)($esp+4)'
elif is_x64():
instanceObject = '(id)$rdi'
elif is_aarch64():
instanceObject = '(id)$x0'
# not supported yet
elif is_arm():
instanceObject = '(id)$r0'
return instanceObject
# -------------------------
# Register related commands
# -------------------------
# return the int value of a general purpose register
def get_gp_register(reg_name: str) -> int:
if reg_name.lower() == 'x30':
reg_name = 'lr'
regs = get_registers("general")
for reg in regs:
reg: SBValue = reg
if reg_name == reg.GetName():
return reg.unsigned
return 0
def get_gp_registers() -> Dict[str, int]:
regs = get_registers("general")
registers = {}
for reg in regs:
reg_name = reg.GetName()
registers[reg_name] = reg.unsigned
return registers
def get_registers_by_frame(frame: SBFrame, kind: str) -> SBValue:
registerSets: SBValueList = frame.GetRegisters()
for registerSet in registerSets:
registerSet: SBValue = registerSet
registerName: str = registerSet.GetName()
if kind.lower() in registerName.lower():
return registerSet
raise OSError(f'Unable to find register {kind}')
def get_registers(kind) -> SBValue:
"""Returns the registers given the frame and the kind of registers desired.
Returns None if there's no such kind.
"""
return get_registers_by_frame(get_frame(), kind)
# retrieve current instruction pointer via platform independent $pc register
def get_current_pc() -> int:
try:
frame = get_frame()
except LLDBFrameNotFound:
return 0
return frame.pc
# retrieve current stack pointer via registers information
# XXX: add ARM
def get_current_sp() -> int:
if is_i386():
sp_addr = get_gp_register("esp")
elif is_x64():
sp_addr = get_gp_register("rsp")
elif is_aarch64():
sp_addr = get_gp_register("sp")
else:
print("[-] get_current_sp() error: wrong architecture.")
return 0
return sp_addr
def get_module_name_from(address: int) -> str:
target = get_target()
sb_addr = SBAddress(address, target)
module: SBModule = sb_addr.module
return typing.cast(str, module.file.fullpath)
def read_instructions(start: int, count: int) -> SBInstructionList:
target = get_target()
sb_start = SBAddress(start, target)
return target.ReadInstructions(sb_start, count, 'intel')
def get_instruction_count(start: int, end: int, max_inst: int) -> int:
'''
Return how many instructions from start address to end address
'''
target = get_target()
sb_start = SBAddress(start, target)
sb_end = SBAddress(end, target)
instructions = read_instructions(start, max_inst)
return instructions.GetInstructionsCount(sb_start, sb_end, False)
# ----------------------------------------------------------
# LLDB Module functions
# ----------------------------------------------------------
def objc_get_classname(objc: str) -> str:
classname_command = '(const char *)object_getClassName((id){})'.format(objc)
class_name = ESBValue.init_with_expression(classname_command)
if not class_name.is_valid:
return ''
return class_name.str_value
def find_module_by_name(target: SBTarget, module_name: str):
for module in target.modules:
module: SBModule = module
if module.file.basename == module_name:
return module
return None
def get_text_section(module: SBModule) -> SBSection:
return module.FindSection('__TEXT')
def resolve_symbol_name(address: int) -> str:
'''
Return a symbold corresponding with an address
'''
target = get_target()
# because address could less than zero -> force it into unsigned int
pz = get_pointer_size()
if pz == 4:
address = ctypes.c_uint32(address).value
elif pz == 8:
address = ctypes.c_uint64(address).value
try:
sb_addr = SBAddress(address, target)
addr_sym: SBSymbol = sb_addr.GetSymbol()
if addr_sym.IsValid():
return addr_sym.GetName()
except TypeError:
pass
return ''
@dataclass
class ModuleInfo:
module_name: str = ''
section_name: str = ''
perms: int = 0
offset: int = -1
abs_offset: int = -1
def resolve_mem_map(target: SBTarget, addr: int) -> ModuleInfo:
module_info = ModuleInfo()
# found in load image
for module in target.modules:
module: SBModule
absolute_offset = 0
for section in module.sections:
section: SBSection = section
if section.GetLoadAddress(target) == 0xffffffffffffffff:
continue
start_addr = section.GetLoadAddress(target)
end_addr = start_addr + section.GetFileByteSize()
if start_addr <= addr <= end_addr:
module_info = ModuleInfo(
module.file.basename,
section.GetName(),
section.GetPermissions(),
addr - start_addr,
absolute_offset + (addr - start_addr)
)
return module_info
absolute_offset += section.GetFileByteSize()
return module_info
@dataclass
class MapInfo(object):
map_type: str
start: int
end: int
perm: str
shm: str
region: str
def __hash__(self) -> int:
pack_fields = f'{self.map_type}_{self.start}_{self.end}'
pack_fields+= f'_{self.perm}_{self.shm}_{self.region}'
return hash(pack_fields)
class MacOSVMMapCache(object):
caches: Set[MapInfo]
is_loaded: bool
def __init__(self: Self) -> None:
self.caches = set()
self.is_loaded = False
if platform.system() != 'Darwin':
print(f'[!] Command vmmap was not supported on {platform.system()}')
def get_vmmap_info(self: Self) -> str:
process = get_process()
if not process:
return ''
process_info = process.GetProcessInfo()
if not process_info.IsValid():
return ''
cmd = ['vmmap', str(process_info.GetProcessID()), "-interleaved"]
proc = Popen(cmd, stdout = PIPE)
out, _ = proc.communicate()
return out.decode('utf-8')
def parse_vmmap_info(self: Self) -> Optional[Set[MapInfo]]:
vmmap_info = self.get_vmmap_info()
if self.is_loaded:
# no need to reload vmmap again
return self.caches
if not len(self.caches):
self.is_loaded = True
if not vmmap_info:
return None
match_map = re.findall(
r'([\x20-\x7F]+)\s+([0-9a-f]+)\-([0-9a-f]+)\s+\[[0-9KMG\.\s]+\]\s+([rwx\-\/]+)\s+([A-Za-z=]+)([\x20-\x7F]+)?',
vmmap_info
)
max_name_len = max([len(line[0].strip()) for line in match_map])
if not match_map:
return None
for m in match_map:
# add map_info to caches
o_map_info = MapInfo(m[0].strip().ljust(max_name_len, " "),
int(m[1], 16),
int(m[2], 16),
m[3],
m[4],
m[5].strip())
self.caches.add(o_map_info)
return self.caches
def query_vmmap(self: Self, address: int) -> Optional[MapInfo]:
# search it in caches
for map_info in self.caches:
if map_info.start <= address < map_info.end:
return map_info
# if a new vmmap record hasn't found in caches, try to parse it from vmmap
process = get_process()
process_info = process.GetProcessInfo()
if not process_info.IsValid():
return None
cmd = ['vmmap', str(process_info.GetProcessID()), hex(address)]
proc = Popen(cmd, stdout = PIPE)
out, err = proc.communicate()
out = out.decode('utf-8')
m = re.search(
r'\-\-\->\s+([\x20-\x7F]+)\s+([0-9a-f]+)\-([0-9a-f]+)\s+\[[0-9KMG\.\s]+\]\s+([rwx\-/]+)\s+([A-Za-z=]+)\s+([\x20-\x7F]+)',
out
)
if not m:
return None
map_info = MapInfo(m[1], int(m[2], 16), int(m[3], 16), m[4], m[5], m[6])
self.caches.add(map_info)
return map_info
# ----------------------------------------------------------
# Memory Read/Write Support
# ----------------------------------------------------------
class LLDBMemoryException(Exception):
def __init__(self, *args: object) -> None:
super().__init__(*args)
def read_mem(addr: int, size: int) -> bytes:
err = SBError()
process = get_process()
if process == None:
raise LLDBMemoryException('get_process() return None')
mem_data = process.ReadMemory(addr, size, err)
if mem_data == None:
mem_data = b''
return mem_data
def readable(addr: int) -> bool:
try:
mem = read_mem(addr, 1)
except LLDBMemoryException:
return False
return True if len(mem) == 1 else False
def read_pointer_from(addr: int, pointer_size: int) -> int:
membuf = read_mem(addr, pointer_size)
if not membuf:
raise LLDBMemoryException(f'Unable to read pointer from {hex(addr)}')
return int.from_bytes(membuf, byteorder='little')
def read_u8(addr: int) -> int:
arr = read_mem(addr, 1)
if not len(arr):
raise LLDBMemoryException(f'Unable to read mem at {hex(addr)}')
return unpack('<B', arr)[0]
def read_u16(addr: int) -> int:
arr = read_mem(addr, 2)
if not len(arr):
raise LLDBMemoryException(f'Unable to read mem at {hex(addr)}')
return unpack('<H', arr)[0]
def read_u32(addr: int) -> int:
arr = read_mem(addr, 4)
if not len(arr):
raise LLDBMemoryException(f'Unable to read mem at {hex(addr)}')
return unpack('<I', arr)[0]
def read_u64(addr: int) -> int:
arr = read_mem(addr, 8)
if not len(arr):
raise LLDBMemoryException(f'Unable to read mem at {hex(addr)}')
return unpack('<Q', arr)[0]
def read_cstr(addr: int, max_size: int=1024) -> bytes:
c_str = bytearray()
i = 0
while i < max_size:
try:
ch = read_u8(addr + i)
if ch == 0x00:
break
c_str.append(ch)
i+=1
except LLDBMemoryException:
break
return bytes(c_str)
def write_mem(addr: int, data: bytes) -> int:
err = SBError()
process = get_process()
if process == None:
raise LLDBMemoryException('get_process() return None')
sz_write = process.WriteMemory(addr, data, err)
if not err.Success():
sz_write = 0
return sz_write
def size_of(struct_name: str) -> int:
res = lldb.SBCommandReturnObject()
ci: SBCommandInterpreter = get_debugger().GetCommandInterpreter()
ci.HandleCommand(f"p sizeof({struct_name})", res)
if res.GetError():
# struct is not exists
return -1
m = re.search(r'\(unsigned long\) \$\d+ = (\d+)\n', res.GetOutput())
if m:
return int(m.group(1))
return -1
PAC_BL_INSTS = (
'blraa', 'blraaz', 'blrab', 'blrabz', 'braa', 'braaz', 'brab', 'brabz'
)
def is_bl_pac_inst(mnemonic: str) -> bool:
return mnemonic in PAC_BL_INSTS
SIGN_MASK = 1 << 55
INT64_MAX = 18446744073709551616
def is_kernel_space(addr: int) -> bool:
# assum address is 64 bit address
return (addr & 0xfffffff000000000) != 0
def stripPAC(pointer: int, type_size: int) -> int:
ptr_mask = (1 << (64 - type_size)) - 1
pac_mask = ~ptr_mask
if pointer & SIGN_MASK:
return (pointer | pac_mask) + INT64_MAX
else:
return pointer & ptr_mask
def strip_kernel_or_userPAC(pointer: int) -> int:
if get_arch() != 'arm64e':
return pointer
try:
T1Sz = ESBValue('gT1Sz')
return stripPAC(pointer, T1Sz.int_value)
except ESBValueException:
return stripPAC(pointer, 25)
TYPE_NAME_CACHE = {}
ENUM_NAME_CACHE = {}
def get_type(type_name: str) -> SBType:
'''
Borrow this from XNU debug script
'''
global TYPE_NAME_CACHE
target_type = str(type_name).strip()
if target_type in TYPE_NAME_CACHE:
# use cache to speedup
return TYPE_NAME_CACHE[target_type]
requested_type_is_struct = False
m = re.match(r'\s*struct\s*(.*)$', target_type)
if m:
requested_type_is_struct = True
target_type = m.group(1)
tmp_type = None
requested_type_is_pointer = False
if target_type.endswith('*') :
requested_type_is_pointer = True
search_type = target_type.rstrip('*').strip()
type_arr = [t for t in get_target().FindTypes(search_type)]
if requested_type_is_struct:
type_arr = [t for t in type_arr if t.type == lldb.eTypeClassStruct]
# After the sort, the struct type with more fields will be at index [0].
# This hueristic helps selecting struct type with more fields compared to ones with "opaque" members
type_arr.sort(reverse=True, key=lambda x: x.GetNumberOfFields())
if len(type_arr) > 0:
tmp_type = type_arr[0]
else:
raise NameError(f'Unable to find type {target_type}')
if not tmp_type.IsValid():
raise NameError(f'Unable to Cast to type {target_type}')
if requested_type_is_pointer:
tmp_type = tmp_type.GetPointerType()
TYPE_NAME_CACHE[target_type] = tmp_type
return TYPE_NAME_CACHE[target_type]
def get_enum_name(enum_name, _key, prefix = ''):
'''
Borrow this from XNU debug script
'''
global ENUM_NAME_CACHE
ty = get_type(enum_name)
if enum_name not in ENUM_NAME_CACHE:
ty_dict = {}
for e in ty.get_enum_members_array():
if ty.GetTypeFlags() & lldb.eTypeIsSigned:
ty_dict[e.GetValueAsSigned()] = e.GetName()
else:
ty_dict[e.GetValueAsUnsigned()] = e.GetName()
ENUM_NAME_CACHE[enum_name] = ty_dict
else:
ty_dict = ENUM_NAME_CACHE[enum_name]
if ty.GetTypeFlags() & lldb.eTypeIsSigned:
key = ctypes.c_long(_key).value
else:
key = _key
name = ty_dict.get(key, "UNKNOWN({:d})".format(key))
if name.startswith(prefix):
return name[len(prefix):]
return name
# overwrites SBValue for easier to access struct member
def find_global_variable(name: str) -> Optional[SBValue]:
target = get_target()
sbvar_list: SBValueList = target.FindGlobalVariables(name, 1)
sbvar: SBValue = sbvar_list.GetValueAtIndex(0)
if not sbvar.IsValid():
return None
return sbvar
class ESBValueException(Exception):
# handle exception while using sb_value
def __init__(self, *args: object) -> None:
super().__init__(*args)
class ESBValue(object):
'''
Wrapper of lldb.SBValue make it easier to use to load debug variable from binary
'''
sb_var_name: str
sb_value: SBValue
is_expression: bool
# store metadata for ESBValue
sb_attributes: Dict[str, Any]
def __init__(self: Self, var_name: str, var_type: str = ''):
super().__init__()
self.sb_var_name = ''
# store metadata
self.sb_attributes = {}
self.is_expression = False
if var_name == 'classcall':
# skip initialize for classcall
return
# find this variable in global context
g_sb_value = find_global_variable(var_name)
if not g_sb_value:
# find this variable in local context
sb_value: SBValue = get_frame().FindVariable(var_name)
if not sb_value.IsValid():
raise ESBValueException(f'Unable to find variable {var_name} in this context.')
self.sb_value = sb_value
else:
self.sb_value = g_sb_value
if var_type and self.sb_value:
address = int(self.sb_value.GetValue(), 16)
target = get_target()
self.sb_value = target.CreateValueFromExpression('var_name', f'({var_type}){address}')
self.sb_var_name = 'var_name'
@classmethod
def init_with_SBValue(cls: Type['ESBValue'], sb_value: SBValue):
new_esbvalue = cls('classcall')
new_esbvalue.sb_value = sb_value
new_esbvalue.sb_var_name = sb_value.GetName()
return new_esbvalue
@classmethod
def init_with_address(cls: Type['ESBValue'], address: int, var_type: str):
target = get_target()
new_esbvalue = cls('classcall')
new_esbvalue.sb_value = target.CreateValueFromExpression('var_name', f'({var_type}){address}')
new_esbvalue.sb_var_name = 'var_name'
return new_esbvalue
@classmethod
def init_with_expression(cls: Type['ESBValue'], expression: str):
frame = get_frame()
if frame != None:
exp_sbvalue: SBValue = frame.EvaluateExpression(expression)
else:
target = get_target()
exp_sbvalue: SBValue = target.EvaluateExpression(expression)
new_esbvalue = cls('classcall')
new_esbvalue.sb_value = exp_sbvalue
new_esbvalue.is_expression = True
return new_esbvalue
@classmethod
def init_null(cls: Type['ESBValue'], var_type: str):
return cls.init_with_address(0, var_type=var_type)
# save metadata for this ESBValue
def set_attribute(self: Self, attr_name: str, value: Any):
self.sb_attributes[attr_name] = value
def get_attribute(self, attr_name: str) -> Optional[Any]:
try:
return self.sb_attributes[attr_name]
except KeyError:
return None
def get(self: Self, attr_name: str) -> 'ESBValue':
'''
Get child member of a struct.
Developer can pass 'ips_wqset.wqset_q' directly to attr_name to get wqset_q
rather than esb_value.get('ips_wqset').get('wqset_q')
'''
if '.' in attr_name:
attr_names = attr_name.split('.')
else:
attr_names = [attr_name]
sb_value = self.sb_value
# automatically get the last child in `attr_name`
for attr_name in attr_names:
sb_value: SBValue = sb_value.GetChildMemberWithName(attr_name)
if not sb_value.IsValid():
raise ESBValueException(f'member attribute {attr_name} didn\'t exists.')
return ESBValue.init_with_SBValue(sb_value)
def has_member(self: Self, attr_name: str) -> bool:
'''
check attr_name exists or not
'''
try:
self.get(attr_name)
return True
except ESBValueException:
return False
def addr_of(self: Self) -> int:
'''
return address of variable
allproc = ESBValue('allproc')
allproc.add_of() is equal to &allproc in C-lang
'''
return self.sb_value.GetLoadAddress()
@property
def is_valid(self: Self) -> bool:
return self.sb_value.IsValid()
@property
def is_null(self: Self) -> bool:
return self.int_value == 0
@property
def is_not_null(self: Self) -> bool:
return not self.is_null
@property
def value_type(self: Self) -> str:
'''
return type name of sb_value
'''
return self.sb_value.GetTypeName()
@property
def value(self: Self) -> str:
''' extract content of sb_value'''
return self.sb_value.GetValue()
@property
def int_value(self: Self) -> int:
''' extract content of sb_value in integer '''
content = self.value
type_name = self.value_type
if content == None:
return 0
if type_name.startswith('uint8_t') or type_name.startswith('int8_t') or \
type_name.startswith('char'):
# trying to cast value to int in Python
content = content.strip("'\\x")
return int(content, 16)
return parse_number(content)
@property
def str_value(self: Self, max_length: int = 1024) -> str:
if self.is_expression:
summary:str = self.sb_value.GetSummary()
return summary.strip('"')
return read_cstr(self.addr_of(), max_length).decode('utf-8')
@property
def var_name(self: Self) -> str:
if self.var_name:
return self.var_name
return self.sb_value.GetName()
@property
def var_type_name(self: Self) -> str:
return self.sb_value.GetTypeName()
@property
def summary(self: Self) -> str:
return self.sb_value.GetSummary()
def dereference(self: Self) -> 'ESBValue':
'''
dereference a pointer
'''
return ESBValue.init_with_SBValue(self.sb_value.Dereference())
def get_SBAddress(self: Self) -> SBAddress:
return self.sb_value.GetAddress()
def cast_to(self: Self, var_type: str) -> 'ESBValue':
return ESBValue.init_with_address(self.int_value, var_type)
def cast_ref(self: Self, var_type: str) -> 'ESBValue':
return ESBValue.init_with_address(self.addr_of(), var_type)
def __getitem__(self: Self, idx) -> 'ESBValue':
return ESBValue.init_with_SBValue(self.sb_value.GetChildAtIndex(idx))
# ----------------------------------------------------------
# Cyclic algorithm to find offset on memory
# ----------------------------------------------------------
def de_bruijn(charset: bytes, n: int = 4, maxlen: int = 0x10000) -> bytearray:
# string cyclic function
# this code base on https://github.com/Gallopsled/pwntools/blob/master/pwnlib/util/cyclic.py
# Taken from https://en.wikipedia.org/wiki/De_Bruijn_sequence but changed to a generator
"""de_bruijn(charset = string.ascii_lowercase, n = 4) -> generator
Generator for a sequence of unique substrings of length `n`. This is implemented using a
De Bruijn Sequence over the given `charset`.
The returned generator will yield up to ``len(charset)**n`` elements.
Arguments:
charset: List or string to generate the sequence over.
n(int): The length of subsequences that should be unique.
"""
k = len(charset)
a = [0] * k * n