-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc_str_assoc.py
75 lines (55 loc) · 1.87 KB
/
func_str_assoc.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
# Function String Associate for IDAPython
# By Syn
from idautils import *
from idc import *
from idaapi import *
MAX_STRING_PREVIEW_LENGTH = 35
def long_string_preview(text):
"""
Returns a shortened preview of a long string
Also cleans the string, removing any bad characters
"""
clean_text = text.replace("\n", " ")
if len(clean_text) > MAX_STRING_PREVIEW_LENGTH:
return clean_text[0:MAX_STRING_PREVIEW_LENGTH] + "..."
else:
return clean_text
def append_unique_comment(ea, text):
"""
Appends a new string to the end of the comments at this address
Will only append it if the string is not already commented there
"""
# filter bad strings
if text is None:
return
c = GetCommentEx(ea, 0)
new_comment = "\""+long_string_preview(str(text))+"\""
# The string we are about to append is already commented
if new_comment in str(c):
return
# Seperate each one by commas
if c is not None:
new_comment = str(c) + "," + new_comment
MakeComm(ea, new_comment)
def process_string_xref(s, ea):
"""
Everywhere the function containing the given reference is used,
a comment is placed there indicating the string is referenced inside
"""
# What function is this code inside of?
func = idaapi.get_func(ea)
if func is None:
return
# Process everywhere this function gets called / reference
for fxref in XrefsTo(func.startEA, 0):
append_unique_comment(fxref.frm, s)
def main():
sc = Strings()
# For all strings ...
for s in sc:
# Find all the references to the string
for xref in XrefsTo(s.ea, 0):
process_string_xref(s, xref.frm)
print "function string associate has finished!"
if __name__ == "__main__":
main()