-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreadmem.py
More file actions
87 lines (62 loc) · 1.99 KB
/
readmem.py
File metadata and controls
87 lines (62 loc) · 1.99 KB
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
import struct
ASCSTR_C = None
bv = None
def initialize_idc(view):
global bv
bv = view
def get_num_by_size(start, size, is_be, view=None):
view = view or bv
if view is None:
raise ValueError('view')
fmt = '>' if is_be else '<'
if size == 4:
fmt += 'I'
elif size == 8:
fmt += 'Q'
elif size == 2:
fmt += 'H'
elif size == 1:
fmt += 'B'
else:
raise ValueError('Valid values B, H, I Q')
raw = struct.unpack_from(fmt, view.read(start, size))
return raw[0] if raw else -1
def Pointer(start, is_be=False, view=None):
view = view or bv
if view is None:
raise ValueError('view')
size = view.arch.address_size
return get_num_by_size(start, size, is_be, view)
def Qword(start, is_be=False, view=None):
""" Return a qword at the specified location """
return get_num_by_size(start, 8, is_be, view)
def Dword(start, is_be=False, view=None):
""" Return a dword at the specified location """
return get_num_by_size(start, 4, is_be, view)
def Word(start, is_be=False, view=None):
""" Return a word at the specified location """
return get_num_by_size(start, 2, is_be, view)
def Byte(start, view=None):
""" Return a byte at the specified location """
view = view or bv
if view is None:
raise ValueError('view')
return ord(view.read(start, 1))
def GetString(start, length=-1, strtype=ASCSTR_C, view=None, max_read=200):
""" Return a string at the specified location """
view = view or bv
if view is None:
raise ValueError('view')
if length == -1:
str_refs = view.get_strings(start, 1)
if str_refs:
str_ref = str_refs[0]
length = str_ref.length
else:
print('Here with %#x' % start)
data = view.read(start, max_read)
end = data.find('\x00')
if end == -1:
return
return data[:end]
return view.read(start, length)