-
Notifications
You must be signed in to change notification settings - Fork 0
/
PyInstruction.py
217 lines (172 loc) · 5.62 KB
/
PyInstruction.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
#!/usr/bin/env python
########################################################################
#
# PyEmu: scriptable x86 emulator
#
# Cody Pierce - cpierce@tippingpoint.com - 2007
#
# License: None
#
########################################################################
import sys, os, time, struct, re
sys.path.append("lib")
import pydasm
'''
PyOperand:
Stores information about a single operand. We duplicate pydasm
information in case one day we want to replace it.
'''
class PyOperand:
def __init__(self, operand):
self.type = 0x0
self.reg = 0x0
self.basereg = 0x0
self.indexreg = 0x0
self.scale = 0x0
self.dispbytes = 0x0
self.dispoffset = 0x0
self.immbytes = 0x0
self.immoffset = 0x0
self.sectionbytes = 0x0
self.section = 0x0
self.displacement = 0x0
self.immediate = 0x0
self.flags = 0x0
# Set up the operand information
self.set_operand(operand)
#
# set_operand: Responsible for initializing the operands values from pydasm
#
def set_operand(self, operand):
self.type = operand.type
self.reg = operand.reg
self.basereg = operand.basereg
self.indexreg = operand.indexreg
self.scale = operand.scale
self.dispbytes = operand.dispbytes
self.dispoffset = operand.dispoffset
self.immbytes = operand.immbytes
self.immoffset = operand.immoffset
self.sectionbytes = operand.sectionbytes
self.section = operand.section
self.displacement = operand.displacement
self.immediate = operand.immediate
self.flags = operand.flags
'''
PyInstruction:
Contains information about each instruction including opcode and
operands. We duplicate pydasm information in case one day we want
to replace it.
'''
class PyInstruction:
def __init__(self, instruction):
self.length = 0x0
self.type = 0x0
self.mode = 0x0
self.opcode = 0x0
self.modrm = 0x0
self.sib = 0x0
self.extindex = 0x0
self.fpuindex = 0x0
self.dispbytes = 0x0
self.immbytes = 0x0
self.sectionbytes = 0x0
self.flags = 0x0
self.disasm = ""
self.mnemonic = ""
self.op1 = ""
self.op2 = ""
self.op3 = ""
# Set up our instruction values
self.set_instruction(instruction)
def group1(self):
return bool(self.flags & 0xff000000)
def group2(self):
return bool(self.flags & 0x00ff0000)
def group3(self):
return bool(self.flags & 0x0000ff00)
def lock(self):
return bool(self.flags & 0x01000000)
def repne(self):
return bool(self.flags & 0x02000000)
def rep(self):
return bool(self.flags & 0x03000000)
def repe(self):
return bool(self.flags & 0x03000000)
def es_override(self):
return bool(self.flags & 0x00010000)
def cs_override(self):
return bool(self.flags & 0x00020000)
def ss_override(self):
return bool(self.flags & 0x00030000)
def ds_override(self):
return bool(self.flags & 0x00040000)
def fs_override(self):
return bool(self.flags & 0x00050000)
def gs_override(self):
return bool(self.flags & 0x00060000)
def operand_so(self):
return bool(self.flags & 0x00000100)
def address_so(self):
return bool(self.flags & 0x00001000)
def get_rm(self):
if self.modrm:
return self.modrm & 0x7
else:
return False
return False
def get_reg_opcode(self):
if self.modrm:
return (self.modrm >> 3) & 0x7
else:
return False
return False
def get_mod(self):
if self.modrm:
return (self.modrm >> 6) & 0x7
else:
return False
return False
def get_base(self):
if self.sib:
return self.sib & 0x7
else:
return False
return False
def get_index(self):
if self.sib:
return (self.sib >> 3) & 0x7
else:
return False
return False
def get_scale(self):
if self.sib:
return (self.sib >> 6) & 0x7
else:
return False
return False
#
# set_instruction: Initializes the class members from pydasm
#
def set_instruction(self, instruction):
self.length = instruction.length
self.type = instruction.type
self.mode = instruction.mode
self.opcode = instruction.opcode
self.modrm = instruction.modrm
self.sib = instruction.sib
self.extindex = instruction.extindex
self.fpuindex = instruction.fpuindex
self.dispbytes = instruction.dispbytes
self.immbytes = instruction.immbytes
self.sectionbytes = instruction.sectionbytes
self.flags = instruction.flags
if instruction.op1.type:
self.op1 = PyOperand(instruction.op1)
if instruction.op2.type:
self.op2 = PyOperand(instruction.op2)
if instruction.op3.type:
self.op3 = PyOperand(instruction.op3)
# Disassembly string of instruction
self.disasm = pydasm.get_instruction_string(instruction, pydasm.FORMAT_INTEL, 0x0).rstrip(" ")
self.mnemonic = pydasm.get_mnemonic_string(instruction, pydasm.FORMAT_INTEL).rstrip(" ")