-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmake_yara.py
178 lines (137 loc) · 4.83 KB
/
make_yara.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
#! /usr/bin/env python
from idautils import *
from idaapi import *
from idc import *
def make_all_absolute(instruction):
size_in_bytes = instruction.size
ea = instruction.ea
signature = ""
for byte in range(size_in_bytes):
signature += "{0:0{1}x}".format(Byte(instruction.ea + byte), 2).upper() + " "
return signature
def make_only_first(instruction):
size_in_bytes = instruction.size
ea = instruction.ea
signature = ""
for byte in range(size_in_bytes):
if byte is 0:
signature += "{0:0{1}x}".format(Byte(instruction.ea), 2).upper() + " "
continue
signature += "?? "
return signature
def make_all_wildcard(instruction):
size_in_bytes = instruction.size
ea = instruction.ea
signature = ""
for byte in range(size_in_bytes):
signature += "?? "
return signature
def decode_call(instruction):
signature = ""
ea = instruction.ea
size_in_bytes = instruction.size
byte = Byte(ea)
if byte == 0xFF or byte == 0x9A or byte == 0xE8:
signature = make_only_first(instruction)
return signature
else:
print("Double Check this might be wrong")
signature = make_only_first(instruction)
return signature
def decode_push(instruction):
signature = ""
ea = instruction.ea
size_in_bytes = instruction.size
byte = Byte(ea)
if byte == 0xFF or byte == 0x68:
signature = make_only_first(instruction)
elif byte == 0x6A:
signature = make_all_absolute(instruction)
else:
signature == "?? "
return signature
def decode_move(instruction):
# B9 mov ecx, 0AH, B8 mov eax, 1
signature = ""
size_in_bytes = instruction.size
ea = instruction.ea
byte = Byte(ea)
if byte == 0x8B and size_in_bytes == 2:
signature = make_all_absolute(instruction)
elif byte == 0x8B:
signature = make_only_first(instruction)
elif instruction.size >= 5:
# TODO FIX THIS
signature = make_only_first(instruction)
else:
signature = make_all_wildcard(instruction)
return signature
def decode_movdqu(instruction):
return make_only_first(instruction)
def decode_cmp(instruction):
if instruction.size > 2:
return make_only_first(instruction)
else:
return make_all_absolute(instruction)
def get_basic_info(instruction):
# TODO CLEAN UP OUTPUT
func = idaapi.get_func(instruction.ea)
func_name = GetFunctionName(func.startEA)
md5 = GetInputFileMD5()
basic_info = []
basic_info.append("MD5: %s" % md5)
basic_info.append("Function: {0:0X} {1:s}".format(func.startEA, func_name))
return basic_info
def decode_instructions():
start = SelStart()
stop = SelEnd()
if start == BADADDR:
print "Please select something"
instruction = DecodeInstruction(start)
disasm_text = get_basic_info(instruction)
signature = ""
for instruct_address in Heads(start, stop):
instruction = DecodeInstruction(instruct_address)
mnem_name = instruction.get_canon_mnem()
size_in_bytes = instruction.size
test_opcodes = get_opcodes()
# print(test_opcodes)
if mnem_name == "call":
signature += decode_call(instruction)
elif mnem_name == "push":
signature += decode_push(instruction)
elif mnem_name == "mov":
signature += decode_move(instruction)
elif mnem_name == "retn":
signature += "C3 "
elif mnem_name == "pop":
signature += "5? "
elif mnem_name == "movdqu":
signature += decode_movdqu(instruction)
elif mnem_name == "cmp":
signature += decode_cmp(instruction)
elif mnem_name[0] == "j": # hopefully only getting the jumps
signature += make_only_first(instruction)
else:
for byte in range(size_in_bytes):
if byte != 0:
signature += "?? "
# build the textual rep of yara signature
bytes = ""
size_in_bytes = instruction.size
ea = instruction.ea
for byte in range(size_in_bytes):
bytes += "{0:0{1}x}".format(Byte(instruction.ea + byte), 2).upper() + " "
disasm_text.append("{0:0X} {1:24s} {2:s}".format(instruct_address, bytes, GetDisasm(instruct_address)))
return signature, disasm_text
def print_signature():
signature, disasm_text = decode_instructions()
print "rule opcode{"
print "\tstrings:"
for line in disasm_text:
print "\t\t// %s" % line
print "\t\t$opcodes = {", signature, "}"
print "\tcondition:"
print "\t\tall of them"
print "}"
print_signature()