-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmake_yara_dotnet.py
141 lines (109 loc) · 3.67 KB
/
make_yara_dotnet.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
#! /usr/bin/env python
from idautils import *
from idaapi import *
from idc import *
try:
import _csv as csv
except ImportError:
import csv
def get_opcodes():
opcode_dict = {}
with open('C:\Tools\opcodes.csv') as csvfile:
opcode_file = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in opcode_file:
opcode_dict[row[0]] = row[1]
return opcode_dict
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_only_first_two(instruction, hex_value):
size_in_bytes = instruction.size
ea = instruction.ea
signature = ""
for byte in range(size_in_bytes):
if byte is 0:
signature += hex_value + " "
continue
if byte is 1:
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 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_function(instruction, hex_value):
signature = ""
if len(hex_value) == 2:
return hex_value + " "
elif len(hex_value) == 5:
return make_only_first_two(instruction, hex_value)
else:
print("Nothing Found")
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)
for row in test_opcodes:
if mnem_name == row:
opcode_match = str(test_opcodes[row])
signature += decode_function(instruction, str(opcode_match))
# print "Didnt find it for %s" % mnem_name
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()