/
PyCheckmarx.py
357 lines (289 loc) · 8.89 KB
/
PyCheckmarx.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
###############################
# > Author: Duarte Monteiro
# > Version: 1.0
# > Vendor: www.checkmarx.com
# > Notes: Python API for Checkmarx WSDL
###############################
# Python Dependencies
from suds.client import Client
from suds.sudsobject import asdict
from suds.cache import NoCache
import base64
import re
import json
import time
class PyCheckmarx(object):
# Internal Variables for the Class
DEBUG = False
configPath = "etc/"
errorLog = []
ttlReport = 6
timeWaitReport = 3
#
# Init Function
#
def __init__(self):
# Get Configuration
self.getConfig()
# Open Connection With Checkmarx
self.Initclient = self.openConnection()
# Get the Service URL
self.serviceUrl = self.getServiceUrl(self.Initclient)
# Get the Session Id and Client Object
(self.sessionId, self.client) = self.getSessionId(self.Initclient,self.serviceUrl)
return None
##########################################
#
# Functions Related to Openning session with Checkmarx
#
##########################################
#
# Get Configuration
#
def getConfig(self):
try:
with open(self.configPath + "config.json", "r") as outfile:
tmpJson = json.load(outfile)["checkmarx"]
self.USERNAME = str(tmpJson["username"])
self.PASSWORD = str(tmpJson["password"])
self.URL = str(tmpJson["url"])
self.APITYPE = tmpJson["APIType"]
except Exception as e:
raise Exception("Unable to get configuration: %s" % e.message)
#
# Open Connection
#
def openConnection(self):
try:
tmpClient = Client(self.URL)
if self.DEBUG:
print dir(tmpClient)
return tmpClient
except Exception as e:
raise Exception("Unable to establish connection with WSDL [%s]: %s " % (self.URL, e.message))
#
# Get Service URL
#
def getServiceUrl(self, client):
try:
CxClient = client.factory.create('CxClientType')
responseDiscovery = client.service.GetWebServiceUrl(CxClient.SDK,self.APITYPE)
if responseDiscovery.IsSuccesfull:
serviceUrl = responseDiscovery.ServiceURL
else:
raise Exception("Error establishing connection > %s" % cxSDK.ErrorMessage)
if self.DEBUG:
print "Response Discovery Object:", dir(responseDiscovery)
print "Service Url:", serviceUrl
return serviceUrl
except Exception as e:
raise Exception("Unable to get Service URL: %s" % e.message)
#
# Login in Checkmarx and retrive the Session ID
#
def getSessionId(self,client, serviceUrl):
try:
clientSDK = Client(serviceUrl + "?wsdl", cache=NoCache())
CxLogin = clientSDK.factory.create("Credentials")
CxLogin.User = self.USERNAME
CxLogin.Pass = self.PASSWORD
cxSDK = clientSDK.service.Login(CxLogin,1033)
if not cxSDK.IsSuccesfull:
raise Exception("Unable to Login > %s" % cxSDK.ErrorMessage)
if self.DEBUG:
print "Service Object:", dir(client)
print "Login Object:", dir(cxSDK)
print "Session ID:", cxSDK.SessionId
return (cxSDK.SessionId, clientSDK)
except Exception as e:
raise Exception("Unable to get SessionId from [%s] : %s" % (serviceUrl,e.message))
##########################################
#
# Functions Related to the functionality of the WSDL
#
##########################################
#
# Get data from the Projects
#
def getProjectScannedDisplayData(self, filterOn=False):
tmp = self.client.service.GetProjectScannedDisplayData(self.sessionId)
if not tmp.IsSuccesfull:
raise Exception("Unable to get data from the server.")
if self.DEBUG:
print dir(tmp)
if not filterOn:
return self.convertToJson(tmp)
else:
return tmp.ProjectScannedList[0]
#
# Get Project Display Data
#
def getProjectsDisplayData(self, filterOn=False):
tmp = self.client.service.GetProjectsDisplayData(self.sessionId)
if not tmp.IsSuccesfull:
raise Exception("Unable to get data from the server.")
if self.DEBUG:
print dir(tmp)
if not filterOn:
return self.convertToJson(tmp)
else:
return tmp.projectList[0]
#
# Get Scan Info For All Projects
#
def getScanInfoForAllProjects(self, filterOn=False):
tmp = self.client.service.GetScansDisplayDataForAllProjects(self.sessionId)
if not tmp.IsSuccesfull:
raise Exception("Unable to get data from the server.")
if self.DEBUG:
print dir(tmp)
if not filterOn:
return self.convertToJson(tmp)
else:
return tmp
#
# Get Preset List
#
def getPresetList(self):
tmp = self.client.service.GetPresetList(self.sessionId)
if not tmp.IsSuccesfull:
raise Exception("Unable to get data from the server.")
if self.DEBUG:
print dir(tmp)
return self.convertToJson(tmp)
#
# Get Configuration List
#
def getConfigurationList(self):
tmp = self.client.service.GetConfigurationSetList(self.sessionId)
if not tmp.IsSuccesfull:
raise Exception("Unable to get data from the server.")
if self.DEBUG:
print dir(tmp)
return self.convertToJson(tmp)
#
# Get Associated Groups List
#
def getAssociatedGroups(self):
tmp = self.client.service.GetAssociatedGroupsList(self.sessionId)
if not tmp.IsSuccesfull:
raise Exception("Unable to get data from the server.")
if self.DEBUG:
print dir(tmp)
return self.convertToJson(tmp)
#
# Filter For [getProjectScannedDisplayData]
#
def filterProjectScannedDisplayData(self, projectID):
tmpProjects = self.getProjectScannedDisplayData(True)
for project in tmpProjects:
if project.ProjectID == projectID:
return self.convertToJson(project)
raise Exception("Could not find ProjectID: %s " % projectID)
#
# Filter for [getProjectsDisplayData]
#
def filterProjectsDisplayData(self,projectID):
tmpProjects = self.getProjectsDisplayData(True)
for project in tmpProjects:
if project.projectID == projectID:
return self.convertToJson(project)
raise Exception("Could not find ProjectID: %s " % projectID)
#
# Filter for [getScanInfoForAllProjects]
#
def filterScanInfoForAllProjects(self,projectID):
tmpProjects = self.getScanInfoForAllProjects(True).ScanList[0]
for project in tmpProjects:
if project.ProjectId == projectID:
return self.convertToJson(project)
raise Exception("Could not find ProjectID: %s " % projectID)
#
# Get Suppressed Issues
#
def getSupressedIssues(self, scanID):
CxWSReportType = self.client.factory.create("CxWSReportType")
CxReportRequest = self.client.factory.create("CxWSReportRequest")
CxReportRequest.ScanID = scanID
CxReportRequest.Type = CxWSReportType.XML
createReportResponse = self.client.service.CreateScanReport(self.sessionId, CxReportRequest)
if createReportResponse.IsSuccesfull:
if self.DEBUG:
print createReportResponse
print "Success. Creating Get Scan Report Status"
inc = 0
while inc < self.ttlReport:
inc += 1
reportStatusResponse = self.client.service.GetScanReportStatus(self.sessionId, createReportResponse.ID)
if reportStatusResponse.IsSuccesfull and reportStatusResponse.IsReady:
break
if self.DEBUG:
print "fail"
time.sleep(self.timeWaitReport)
if self.DEBUG:
print "Sucess. Creating Get Scan Report"
responseScanResults = self.client.service.GetScanReport(self.sessionId, createReportResponse.ID )
if responseScanResults.IsSuccesfull and responseScanResults.ScanResults:
XMLData = base64.b64decode(responseScanResults.ScanResults)
issues = re.findall('FalsePositive="([a-zA-Z]+)" Severity="([a-zA-Z]+)"', XMLData)
if self.DEBUG:
print responseScanResults
print issues
mediumSupressIssues = 0
lowSupressIssues = 0
highSupressIssues = 0
otherSupressIssues = 0
for a,b in issues:
if a == "True":
if b == "Medium":
mediumSupressIssues += 1
elif b == "High":
highSupressIssues += 1
elif b == "Low":
lowSupressIssues += 1
else:
otherSupressIssues += 1
if self.DEBUG:
print highSupressIssues
print mediumSupressIssues
print lowSupressIssues
return {"highSupressIssues": highSupressIssues, "mediumSupressIssues": mediumSupressIssues, "lowSupressIssues": lowSupressIssues}
else:
raise Exception("Unable to Get Report")
else:
raise Exception("Unable to get Supressed")
#
# Convert Suds object into serializable format.
#
def recursive_asdict(self,d):
out = {}
for k, v in asdict(d).iteritems():
if hasattr(v, '__keylist__'):
out[k] = self.recursive_asdict(v)
elif isinstance(v, list):
out[k] = []
for item in v:
if hasattr(item, '__keylist__'):
out[k].append(self.recursive_asdict(item))
else:
out[k].append(item)
else:
out[k] = v
return out
#
# Return Subs Object into Serializable format Handler
#
def convertToJson(self, data):
try:
tmp = self.recursive_asdict(data)
return json.dumps(tmp)
except Exception as e:
raise Exception("Unable to convert to JSON: %s" % e.message)
##########################################
#
# Testing AREA
#
##########################################
#tmp = PyCheckmarx()
#print tmp.filterProjectScannedDisplayData(20004)
#print tmp.getSupressedIssues("1000004")