-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsignsrch.py
152 lines (121 loc) · 4.31 KB
/
signsrch.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from idaapi import *
import codecs
import time
import os
def chunks(l, n):
"""Yield successive n-sized chunks from l."""
for i in range(0, len(l), n):
yield l[i:i + n]
def load_signatures():
import xml.etree.ElementTree as ET
db = idadir("plugins/signsrch.xml")
if not os.path.isfile(db):
db = os.path.join(get_user_idadir(), "plugins/signsrch.xml")
root = ET.parse(db).getroot()
signature = []
for p in root:
# <p t="name [64.le rev.64&]">
name, data = p.attrib['t'].split(" [")
bits, endian, size = data[:-1].split(".")
if "&" in size:
if bits == "float":
bits = 32
elif bits == "double":
bits = 64
else:
bits = int(bits)
signature.append({
"name": name,
"bits": bits,
"endian": endian,
"size": size,
"data": codecs.decode(p.text, ('hex')),
})
return signature
class Chooser(Choose):
def __init__(self, items):
Choose.__init__(self, "Signsrch", [["Address", 20], ["Label", 80]], embedded=False)
self.items = items
self.icon = 160
def GetItems(self):
return self.items
def SetItems(self, items):
self.items = [] if items is None else items
def OnClose(self):
pass
def OnGetLine(self, n):
addr, label = self.items[n]
seg_name = get_segm_name(getseg(addr))
if seg_name:
return ["%s:%X" % (seg_name, addr), label]
else:
return ["unknown:%X" % addr, label]
def OnGetSize(self):
return len(self.items)
def OnSelectLine(self, n):
jumpto(self.items[n][0])
class signsrch_t(plugin_t):
flags = PLUGIN_PROC
comment = "Signsrch"
help = ""
wanted_name = "Signsrch"
wanted_hotkey = ""
def init(self):
print("Signsrch (Python Version) (v1.0) plugin has been loaded.")
return PLUGIN_OK
def run(self, arg):
ignored = ["be", "le"][cvar.inf.is_be()]
signatures = [s for s in load_signatures() if s["endian"] != ignored]
if not signatures:
print("No signature loaded, Aborted")
return
print("%d signatures loaded" % len(signatures))
# Scan every segments
start_time = time.time()
found = []
for i in range(get_segm_qty()):
seg = getnseg(i)
seg_name = get_segm_name(seg)
seg_class = get_segm_class(seg)
if seg.type in (SEG_XTRN, SEG_GRP, SEG_NULL, SEG_UNDF, SEG_ABSSYM, SEG_COMM, SEG_IMEM,):
print("Skipping segment: %s, %s" % (seg_name, seg_class))
continue
print("Processing segment: %s, %s, 0x%08X - 0x%08X, %dbytes " % (seg_name, seg_class, seg.start_ea, seg.end_ea, seg.size()))
bytes = get_bytes(seg.start_ea, seg.size())
for sig in signatures:
ea = None
if "&" in sig["size"]:
bits = sig["bits"]
idx = 0
for s in chunks(sig["data"], bits // 8):
idx = bytes.find(s, idx)
if idx == -1:
ea = None
break
if ea == None:
ea = seg.start_ea + idx
idx += bits // 8
else:
idx = bytes.find(sig["data"])
if idx != -1:
ea = seg.start_ea + idx
if ea != None: # found
name = sig["name"]
found.append([ea, name])
print("Found %s @ 0x%X" % (name, ea))
# Add comment
cmt = get_cmt(ea, True)
if not cmt:
set_cmt(ea, '<Signsrch> "%s"' % name, True)
elif "Signsrch" not in cmt:
set_cmt(ea, cmt + ' <Signsrch> "%s"' % name, True)
print("Found %d matches in %s seconds" % (len(found), time.time() - start_time))
if found:
ch = Chooser(found)
ch.Show()
def term(self):
pass
def PLUGIN_ENTRY():
return signsrch_t()