forked from neoz/asm_ops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assembler.py
119 lines (102 loc) · 2.36 KB
/
assembler.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
import idaapi
import idautils
import idc
import sys
from keystone import *
class Error(Exception):
"""Base class for exceptions in this module."""
pass
class InputError(Error):
"""Exception raised for errors in the input.
Attributes:
expr -- input expression in which the error occurred
msg -- explanation of the error
"""
def __init__(self, expr, msg):
self.expr = expr
self.msg = msg
def get_arch():
(arch, mode) = (None, None)
for x in idaapi.ph_get_regnames():
name = x
if name.upper() == 'AX':
arch = KS_ARCH_X86
info = idaapi.get_inf_structure()
if info.is_64bit():
mode = KS_MODE_64
elif info.is_32bit():
mode = KS_MODE_32
else:
mode = KS_MODE_16
break
elif name.upper() == 'R0':
arch = KS_ARCH_ARM
mode = KS_MODE_ARM
break
return (arch, mode)
def get_thumb(ea):
val = idc.GetReg(ea,'T')
return val
class myplugin_t(idaapi.plugin_t):
flags = idaapi.PLUGIN_UNL
comment = "Opcode Assembler helper (using Keystone framework)"
help = "This is help"
wanted_name = "Opcode Assembler"
wanted_hotkey = "Alt-N"
(arch, bits) = (None,None)
def init(self):
(self.arch, self.bits) = get_arch()
return idaapi.PLUGIN_OK
def run(self, arg):
startasm()
def term(self):
pass
def PLUGIN_ENTRY():
return myplugin_t()
def tohex(val, nbits):
return hex((val + (1 << nbits)) % (1 << nbits))
def remove_doublespace(str):
pos = str.find(" ")
while pos != -1:
str = str.replace(" "," ")
pos = str.find(" ")
return str
def clean_part(str):
str = str.strip()
pos = str.find(' ')
while pos != -1:
str = str.replace(' ','')
pos = str.find(' ')
return str
def asm_keystone(startea,instruction_string):
(arch, mode) = get_arch()
if arch==KS_ARCH_ARM:
thumb = get_thumb(startea)
if thumb==1:
mode = KS_MODE_THUMB
ks = Ks(arch, mode)
encoding, count = ks.asm(instruction_string,startea)
beginea=startea
total=0
for i in encoding:
PatchByte(beginea,i)
beginea=beginea+1
return beginea-startea
def startasm():
curEA = idc.ScreenEA()
isCont = 1
while isCont:
t = idaapi.generate_disasm_line(curEA)
if t:
line = idaapi.tag_remove(t)
else:
line = ""
str = AskStr(line,"Address :"+hex(curEA)+"\nInstruction")
if str:
try:
next=asm_keystone(curEA,str)
curEA = curEA + next
except InputError as e:
print e.msg
else:
isCont = 0