-
Notifications
You must be signed in to change notification settings - Fork 18
/
disp.py
138 lines (123 loc) · 2.97 KB
/
disp.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
X86_EFLAGS_CF = 1 << 0
X86_EFLAGS_FIXED = 1 << 1
X86_EFLAGS_PF = 1 << 2
X86_EFLAGS_AF = 1 << 4
X86_EFLAGS_ZF = 1 << 6
X86_EFLAGS_SF = 1 << 7
X86_EFLAGS_TF = 1 << 8
X86_EFLAGS_IF = 1 << 9
X86_EFLAGS_DF = 1 << 10
X86_EFLAGS_OF = 1 << 11
X86_EFLAGS_IOPL = 1 << 12
X86_EFLAGS_IOPL_MASK = 3 << 12
X86_EFLAGS_NT = 1 << 14
X86_EFLAGS_RF = 1 << 16
X86_EFLAGS_VM = 1 << 17
X86_EFLAGS_AC = 1 << 18
X86_EFLAGS_VIF = 1 << 19
X86_EFLAGS_VIP = 1 << 20
X86_EFLAGS_ID = 1 << 21
eflag_list = [
( "CF", X86_EFLAGS_CF ),
( "PF", X86_EFLAGS_PF ),
( "AF", X86_EFLAGS_AF ),
( "ZF", X86_EFLAGS_ZF ),
( "SF", X86_EFLAGS_SF ),
( "IF", X86_EFLAGS_IF ),
( "TF", X86_EFLAGS_TF ),
( "OF", X86_EFLAGS_OF ),
( "NT", X86_EFLAGS_NT ),
( "RF", X86_EFLAGS_RF ),
( "VM", X86_EFLAGS_VM ),
( "AC", X86_EFLAGS_AC ),
( "VIF", X86_EFLAGS_VIF ),
( "ID", X86_EFLAGS_ID )
]
def efl_iopl(efl):
return (efl >> 12) & 3
def dump_eflags(efl, iopl=True):
e = ""
if iopl:
"iopl({:x}) ".format(efl_iopl(efl))
for flag in eflag_list:
if efl & flag[1]:
e += flag[0] + " "
return e
def dump_deflags(efl, prev_efl):
eop = ""
dif = efl ^ prev_efl
if dif == 0:
return eop
for flag in eflag_list:
if dif & flag[1]:
if efl & flag[1]:
eop += flag[0] + "=1 "
else:
eop += flag[0] + "=0 "
return eop
def read_str(uc, addr):
tmp = ""
r = addr
while True:
try:
v = uc.mem_read(r, 1)
except:
break
if v == '\0' or v < 0 or v > 128:
break
tmp += v
r += 1
return tmp
def read_bytes(uc, addr, length):
b = bytes()
r = addr
i = 0
while i < length:
try:
v = uc.mem_read(r, 1)
except:
break
b += v
i += 1
r += 1
return b
def disp_hex_ascii_line(buf, length, off):
r = "{:05d} ".format(off)
for i in range(length):
r += "{:02X} ".format(buf[i])
if i == 7:
r += ' '
if length < 8:
r += ' '
if length < 16:
gap = 16 - length
for i in range(gap):
r += " "
r += " "
for i in range(length):
if ord(chr(buf[i])) < 128:
r += chr(buf[i])
else:
r += "."
r += "\n"
return r
def disp_bytes(uc, addr, length):
buf = read_bytes(uc, addr, length)
if len(buf) == 0:
return ""
if len(buf) <= 16:
return disp_hex_ascii_line(buf, len(buf), 0)
r = ""
off = 0
boff = 0
rem = len(buf)
while True:
line_len = 16 % rem
r += disp_hex_ascii_line(buf[boff:boff+line_len], line_len, off)
rem -= line_len
boff += line_len
off += 16
if rem <= 16:
r += disp_hex_ascii_line(buf[boff:boff+line_len], rem, off)
break
return r