-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRC4_find.py
More file actions
227 lines (195 loc) · 11.7 KB
/
Copy pathRC4_find.py
File metadata and controls
227 lines (195 loc) · 11.7 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
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
218
219
220
221
222
223
224
225
226
227
import ida_funcs
import ida_idaapi
import ida_kernwin
import ida_hexrays
import ida_netnode
import ida_idp
import idaapi
import idautils
import ida_lines
import ida_hexrays as hx
# Icon
icon = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x000\x00\x00\x00%\x08\x06\x00\x00\x00\x04\x19j\xaf\x00\x00\x00\x01sRGB\x00\xae\xce\x1c\xe9\x00\x00\x00\x04gAMA\x00\x00\xb1\x8f\x0b\xfca\x05\x00\x00\x00\tpHYs\x00\x00\x0e\xc3\x00\x00\x0e\xc3\x01\xc7o\xa8d\x00\x00\x06\x0cIDATXG\xed\x97{PTu\x14\xc7\xcf\xdd]vY\x9e\xbb\x08\x08\xb8\x12h>\x92t@\x13\x07\xb2\x04\x93\xac\x1c\tL\x19\x9f\x199\x16\x16\x1a\x98\xa3i9\x84J\xa33\x068\xcd\xa0\xd9\x03m0Lj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V\x15\x99\xed.x\x8a\xdb\x04(\x85\x8b\xd4M*\x85x?X\xc8\x84q\xbb\x959\xdcx\xbbO\xf4\xcaGh\xb9\xd9G\x8a\xe4\x1c\x83D\xdf\xea:N\xf8\xf8\x01\xec\xd7\xfc\x06\xe3\xc0\x94s<\xf3\x13\xd1\x8bN4\x01J%\xe9\x870\xdf~\xd1Q\xa1\xcf\x04\xaa-G,\xe5(\xc5\xd0ge\xcd\x99\x1d\xa6\x96\x0e\xa0\x86\xa4U&\x10JB\xed\'+=\xfd\xe6\xc1\xefP\xbae\xc8\x04(\x07\xd7\x0e[*\x03\x8b\xe8_\xa2\xa3\x17R\x96\xef\xeeu:a\x84\xa8\xcc\xbf1\xef\xc7"\xa9\x02%\x1f<M\xc2\tY\xb0\x0e\xfc5\x1e\x8d(\x1e%@\xfa\xa1\'i\x14\x9b\xe9\xc3\x88\xf7C\xb3\x81-&7\xb9\xdb~\xe0\xfe\\\x91!a\r[\x93\xe2\x18\xa8/\xf3A\xaf5\x89\xe4w0\tR\xf7\x10<+\x9bI\xd0y2\xa2x\x96\x00\x85\xf6C\x80\xb4\x7f\xc8~\xb8\xa2\xb7\x1c\xfe]\xcf9\xde\xbc<\x9c6%\x14:N\x97\x83\xd9\x1aWR\x9c\x04\x0e\x15>L\xe2\xd3w\xa5$xr\x96\xcaG\xacf\x9e\xfeVt\xb7\x1dy\xa4/\xf5\x94\x8c-\xfa7dJ\x19?j\xd8\xb2\xb7}S\xb0\xe9\xd8\x08\x85n\xc72u\x1dJ\x1e\xee\xec\x98x\x90\xca\x1cv\x87~\x9a\x85\xa5\x05\xcd3*\n5\xbf\xf2\xbf\xc5\'\xd8\x00\x93\x1b\xbe!;.\xfc\xe5\xc3\x8b\x17/^\xbc<^\x00\xfe\x03t\xd3\x12d\xcd*\xb7\xe1\x00\x00\x00\x00IEND\xaeB`\x82'
icon_id = idaapi.load_custom_icon(data=icon, format="png")
class RC4_Finder(ida_kernwin.action_handler_t):
def __init__(self):
ida_kernwin.action_handler_t.__init__(self)
def activate(self, ctx):
print("[RC4] Search started")
rc4run()
print("[RC4] search done")
pass
def update(self, ctx):
return idaapi.AST_ENABLE_ALWAYS
def rc4run():
expr1 = lambda cf, i: ((i.op is idaapi.cot_asg and
i.x.op is idaapi.cot_var and
i.y.op is idaapi.cot_smod and
i.y.x.op is idaapi.cot_add and
i.y.x.x.op is idaapi.cot_var and
i.y.x.y.op is idaapi.cot_num and
i.y.y.op is idaapi.cot_num and
i.y.y.numval() == 256) or
(i.op is idaapi.cot_asg and
i.x.op is idaapi.cot_var and
i.y.op is idaapi.cot_cast and
i.y.x.op is idaapi.cot_add and
i.y.x.x.op is idaapi.cot_var and
i.y.x.y.op is idaapi.cot_num and
("8" in str(i.y.type))))
expr2 = lambda cf, i: ((i.op is idaapi.cot_asg and
i.x.op is idaapi.cot_var and
i.y.op is idaapi.cot_smod and
i.y.y.op is idaapi.cot_num and
i.y.y.numval() == 256) or (i.op is idaapi.cot_asg and
i.x.op is idaapi.cot_var and
i.y.op is idaapi.cot_cast and
i.y.x.op is idaapi.cot_add and
("8" in str(i.y.type))))
expr3 = lambda cf, i: (i.op is idaapi.cot_asgxor and
i.x.op is idaapi.cot_ptr and
i.x.x.op is idaapi.cot_cast and
i.x.x.x.op is idaapi.cot_add)
expr = []
expr.append(expr1)
expr.append(expr2)
expr.append(expr3)
tmp = findrc4(expr)
print(tmp)
res = {1: [], 2: [], 3: []}
match = [0, 0, 0]
for i in tmp:
# i:str
index = str(i.keys()).split('[')[1][0]
value = str(i.values())[14:-4]
res[int(index)].append(value)
if len(res[1]) != 0:
for k in res[1]:
pattern = k[k.find('['):k.find(']') + 1]
cnt = 0
for i in range(2, 4):
for j in res[i]:
if pattern in j:
match[i - 1] = match[i - 1] + 1
if (match[1] >= 2 and match[2] >= 1):
print(f"RC4_function at addr:0x{pattern[1:-1]}")
pass
def findrc4(expr):
try:
r = exec_findrc4(expr)
print("RC4finder done! %d unique hits." % len(r))
for e in r:
print(e)
except Exception as exc:
print("some errors happened")
return
return r
def exec_findrc4(expr):
flags = 0
eas = idautils.Functions()
result = list()
for ea in eas:
f = ida_funcs.get_func(ea)
if f:
cfunc = None
hf = ida_hexrays.hexrays_failure_t()
try:
cfunc = ida_hexrays.decompile(f, hf, flags)
except Exception as e:
pass
if cfunc:
result += findexpr(cfunc, cfunc.body, expr)
return result
def findexpr(cfunc, ctree, expr):
"""
cfunc : cfunc_t
ctree: citem_i
expr: lambda/function:f(cfunc_t,citem_t)
return addr
"""
class Visitor(ida_hexrays.ctree_visitor_t):
def __init__(self, cfunc, expr):
ida_hexrays.ctree_visitor_t.__init__(self, ida_hexrays.CV_FAST)
self.cfunc = cfunc
self.query = expr
self.found = list()
return
def process(self, i):
try:
cnt = 1
for _query in self.query:
if _query(self.cfunc, i):
print(ParseResult(self.cfunc, i))
self.found.append({cnt: str(ParseResult(self.cfunc, i))})
cnt = cnt + 1
except:
pass
return 0
def visit_insn(self, i):
return self.process(i)
def visit_expr(self, e):
return self.process(e)
if cfunc:
itfinder = Visitor(cfunc, expr)
itfinder.apply_to(cfunc.body, None)
return itfinder.found
return list()
class ParseResult():
def __init__(self, cfunc=None, i=None):
if isinstance(cfunc, hx.cfuncptr_t):
self.entry = cfunc.entry_ea
elif isinstance(cfunc, int):
self.entry = cfunc
else:
self.entry = idaapi.BADADDR
if isinstance(i, (hx.cexpr_t, hx.cinsn_t)):
self.ea = i.ea if not isinstance(cfunc, hx.cfuncptr_t) else self.find_closest_address(cfunc, i)
self.v = ida_lines.tag_remove(i.print1(None))
elif isinstance(i, tuple):
self.ea, self.v = i
else:
self.ea = idaapi.BADADDR
self.v = "<undefined>"
def find_closest_address(self, cfunc, i):
parent = i
while parent:
if parent and parent.ea != idaapi.BADADDR:
return parent.ea
parent = cfunc.body.find_parent_of(parent)
return idaapi.BADADDR
def __str__(self):
return "[%x] %x: \"%s\"" % (self.entry, self.ea, self.v)
class MyPlugin(ida_idaapi.plugin_t):
wanted_name = "RC4_finder"
wanted_hotkey = ""
comment = "A plugin for find RC4 cryptor"
help = ""
flags = idaapi.PLUGIN_KEEP
def init(self):
self.banner()
if ida_hexrays.init_hexrays_plugin():
idaapi.register_action(
idaapi.action_desc_t(
"RC4:find", # The action name. This acts like an ID and must be unique
"RC4 finder", # The action text.
RC4_Finder(),
"",
"",
icon_id
)
)
if not idaapi.attach_action_to_menu("Search", "RC4:find", idaapi.SETMENU_APP):
print("[RC4 Finder] Failed attaching to menu.")
return idaapi.PLUGIN_KEEP
def run(self):
pass
def term(self):
pass
def banner(self):
print("---------------------------------------------------------------------------------------------")
print("RC4 finder loaded.")
print("Run via Search -> RC4 Finder")
print("---------------------------------------------------------------------------------------------")
# print("RC4finder start...")
def PLUGIN_ENTRY():
return MyPlugin()