-
Notifications
You must be signed in to change notification settings - Fork 0
/
idasonar_200404.py
206 lines (151 loc) · 6.64 KB
/
idasonar_200404.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
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
# Codesonar to IDA Pro (c) 2019 Alexander Pick
# Changelog:
# 2019/11/13 - converted to IDA plugin
# 2019/11/13 - fixed expansion problem
#
import urllib
import urlparse
import xml.etree.ElementTree as ET
import sys
import idautils, idc, idaapi, ida_kernwin
initialized = False
color = 0xc7c7ff
class SonarHandler(idaapi.action_handler_t):
def __init__(self):
idaapi.action_handler_t.__init__(self)
# Mainaction if invoked
def activate(self, ctx):
a = IDASonar()
a.importcsdata()
return 1
# This action is always available.
def update(self, ctx):
return idaapi.AST_ENABLE_ALWAYS
class IDASonar(idaapi.plugin_t):
comment = "Codesonar IDA Integration"
version = "v1.0"
website = ""
help = ""
wanted_name = "IDASonar"
wanted_hotkey = ""
flags = idaapi.PLUGIN_KEEP
def init(self):
global initialized
if initialized == False:
initialized = True
action_desc = idaapi.action_desc_t(
'my:csintegrate', # The action name. This acts like an ID and must be unique
'Load XML Data', # The action text.
SonarHandler(), # The action handler.
'', # Optional: the action shortcut
'Load XML Data from a Codesonar Analysis', # Optional: the action tooltip (available in menus/toolbar)
) # Optional: the action icon (shows when in menus/toolbars) use numbers 1-255
# Register the action
idaapi.register_action(action_desc)
idaapi.attach_action_to_menu("Edit/Codesonar/Fetch Results", 'my:csintegrate', idaapi.SETMENU_APP)
return idaapi.PLUGIN_KEEP
def run(self, arg):
self.about()
pass
def term(self):
return
def about(self):
print(self.wanted_name + " " + self.version)
# add function comments and set color if security issue
def processDetails(self, warningNode, detailsNode, wType):
#return None, None
msgFound = 0
allmsgs = warningNode.find("id").text + ": "
func = None
# sys.stdout.write(".")
try:
c = detailsNode.find("s/c") #[@preprocessor='1']
cParts = c.text.split(":")
#sys.stdout.write(c)
for msgNode in detailsNode.findall('msg'):
detail = msgNode.get("detail")
if ((detail == None) or (detail == 1)):
allmsgs += ET.tostring(msgNode, encoding='UTF-8', method='text')
msgFound = 1
if msgFound == 1:
cea = cParts[1].strip().lstrip("0")
ea = long(cea, 16)
print(cea)
currentcmt = idaapi.get_cmt(ea, 0)
if currentcmt:
allmsgs = currentcmt + "\n" + allmsgs
idaapi.set_cmt(ea, allmsgs, 0)
idc.set_color(ea, idc.CIC_ITEM, color)
if wType == "Security":
idc.set_color(ea, idc.CIC_FUNC, 0xcdffff)
# else:
# set_color(ea, CIC_FUNC, 0xffffeb)
# catch if possibly no function is defined in IDA db at the offset or something
try:
func = idaapi.get_func(ea).start_ea
currentcmt = idc.get_func_cmt(ea, 1)
return func, currentcmt
except Exception as e:
print(e)
return None, None
pass
return None, None
except Exception as e:
print(e)
pass
# add function header comment with details
def addFuncHeader(self, warningNode, func, currentcmt):
idc.add_bpt(func, 0, idc.BPT_SOFT)
idc.enable_bpt(func, False)
headcomment = "Warning! " + warningNode.find("id").text + " Possible " + warningNode.find(
"class").text + " (Score:" + warningNode.find("score").text + ")"
if currentcmt:
headcomment = currentcmt + "\n" + headcomment
idc.set_func_cmt(func, headcomment, 1)
return None
def importcsdata(self):
url = ida_kernwin.ask_str("http://127.0.0.1:7340/analysis/", 250, "Codesonar Analysis XML URL")
try:
if url == False:
print("IDASonar: Loading aborted!")
return
baseurl = parsed_uri = urlparse.urlparse(url)
root = ET.parse(urllib.urlopen(url)).getroot()
ida_kernwin.show_wait_box("Processing Codesonar data please wait!")
for warningNode in root.findall('warning'):
warningUrl = warningNode.get('url')
# print(warningUrl)
# if int(warningNode.find("score").text) < 55:
# continue
procedureNode = warningNode.find("procedure")
print(procedureNode.text + " " + warningNode.find("class").text)
host = '{uri.scheme}://{uri.netloc}'.format(uri=baseurl)
fullWarningUrl = (host + warningUrl)
WarningDetails = ET.parse(urllib.urlopen(fullWarningUrl)).getroot()
func = None
currentcmt = None
wType = WarningDetails.get("significance")
print("Significance: " + wType)
# do for main
for detailsNode in WarningDetails.findall('listing/procedure/line'):
func, currentcmt = self.processDetails(warningNode, detailsNode, wType)
if func:
self.addFuncHeader(warningNode, func, currentcmt)
# do again for expansions
func = None
for detailsNode in WarningDetails.findall('listing/procedure/line/expansion/procedure/line'):
func, currentcmt = self.processDetails(warningNode, detailsNode, wType)
if func:
self.addFuncHeader(warningNode, func, currentcmt)
print("------------------------------------------------------------------")
print("Done!")
ida_kernwin.hide_wait_box()
idaapi.request_refresh(0xFFFFFFFF)
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno))
print(e)
ida_kernwin.hide_wait_box()
print("IDASonar: An error occurred while processing the URL!")
pass
def PLUGIN_ENTRY():
return IDASonar()