forked from Scags/IDA-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisgoodsig.py
58 lines (47 loc) · 1.74 KB
/
isgoodsig.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
import idc
import idaapi
import idautils
def main():
bytesig = idaapi.ask_str("", 0, "Insert signature: ")
sig = bytesig.replace(r"\x", " ").replace("2A", "?").replace("2a", "?").strip()
count = checksig(sig)
if not count:
print(r"INVALID: {}".format(bytesig))
print("Could not find any matching signatures for input")
elif count == 1:
print(r"VALID: {}".format(bytesig))
else:
print(r"INVALID: {}".format(bytesig))
print("Found {} instances of input signature".format(count))
def checksig(sig):
# Get the first segment that is executable to use its addresses for parse_binpat_str
endea = idc.BADADDR
for segea in idautils.Segments():
s = idaapi.getseg(segea)
if s.perm & idaapi.SEGPERM_EXEC:
segstart = segea
# Set the end ea to the end of the last executable segment
# Speed isn't as important in this script, so reading any extra X
# segments is fine
if endea == idc.BADADDR or endea < segstart + s.size():
endea = segstart + s.size()
break
count = 0
addr = 0
addr = idaapi.find_binary(addr, endea, sig, 0, idc.SEARCH_DOWN|idc.SEARCH_NEXT)
while addr != idc.BADADDR:
count = count + 1
addr = idaapi.find_binary(addr, endea, sig, 0, idc.SEARCH_DOWN|idc.SEARCH_NEXT)
return count
# bin_search3 breaks after 15 or so bytes or something, idk man
# binpat = idaapi.compiled_binpat_vec_t()
# idaapi.parse_binpat_str(binpat, segstart, sig, 16, idaapi.get_default_encoding_idx(idaapi.get_encoding_bpu_by_name("UTF-8")))
# count = 0
# addr = 0
# addr, _ = idaapi.bin_search3(addr, endea, binpat, idaapi.BIN_SEARCH_FORWARD)
# while addr != idc.BADADDR:
# count += 1
# # +1 because the search finds itself
# addr, _ = idaapi.bin_search3(addr + 1, endea, binpat, idaapi.BIN_SEARCH_FORWARD)
# return count
main()