Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated findsecbugs parser #501

Merged
merged 1 commit into from Sep 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
375 changes: 212 additions & 163 deletions scanners/scanner_parser/staticscanner_parser/findbugs_report_parser.py
Expand Up @@ -37,170 +37,219 @@
total_high = ""
total_medium = ""
total_low = ""


def xml_parser(root, project_id, scan_id):
"""

:param root:
:param project_id:
:param scan_id:
:return:
"""
date_time = datetime.now()
global name, classname, risk, ShortMessage, LongMessage, sourcepath, vul_col, ShortDescription, Details, lenth_match, duplicate_hash, vul_id, total_vul, total_high, total_medium, total_low
# print root
for bug in root:
if bug.tag == "BugInstance":
name = bug.attrib["type"]
priority = bug.attrib["priority"]
for BugInstance in bug:
if BugInstance.tag == "ShortMessage":
global ShortMessage
ShortMessage = BugInstance.text
if BugInstance.tag == "LongMessage":
global LongMessage
LongMessage = BugInstance.text
if BugInstance.tag == "Class":
global classname
classname = BugInstance.attrib["classname"]
if BugInstance.tag == "SourceLine":
global sourcepath, sourcefile
sourcepath = BugInstance.attrib["sourcepath"]
sourcefile = BugInstance.attrib["sourcefile"]

if priority == "1":
risk = "High"
vul_col = "danger"

elif priority == "2":
risk = "Medium"
vul_col = "warning"

elif priority == "3":
risk = "Low"
vul_col = "info"

vul_id = uuid.uuid4()

dup_data = name + classname + risk

duplicate_hash = hashlib.sha256(dup_data.encode("utf-8")).hexdigest()

match_dup = StaticScanResultsDb.objects.filter(
dup_hash=duplicate_hash
).values("dup_hash")
lenth_match = len(match_dup)

if lenth_match == 0:
duplicate_vuln = "No"

false_p = StaticScanResultsDb.objects.filter(
false_positive_hash=duplicate_hash
)
fp_lenth_match = len(false_p)

if fp_lenth_match == 1:
false_positive = "Yes"
else:
false_positive = "No"

save_all = StaticScanResultsDb(
vuln_id=vul_id,
date_time=date_time,
scan_id=scan_id,
project_id=project_id,
title=name,
severity=risk,
description=str(ShortMessage)
+ "\n\n"
+ str(LongMessage)
+ "\n\n"
+ str(classname),
fileName=sourcepath,
severity_color=vul_col,
vuln_status="Open",
dup_hash=duplicate_hash,
vuln_duplicate=duplicate_vuln,
false_positive=false_positive,
scanner="Findbugs",
)
save_all.save()

else:
duplicate_vuln = "Yes"

save_all = StaticScanResultsDb(
vuln_id=vul_id,
date_time=date_time,
scan_id=scan_id,
project_id=project_id,
title=name,
severity=risk,
description=str(ShortMessage)
+ "\n\n"
+ str(LongMessage)
+ "\n\n"
+ str(classname),
fileName=sourcepath,
severity_color=vul_col,
vuln_status="Duplicate",
dup_hash=duplicate_hash,
vuln_duplicate=duplicate_vuln,
false_positive="Duplicate",
scanner="Findbugs",
)
save_all.save()

if bug.tag == "BugPattern":
for BugPattern in bug:
details = 'na'
message = 'na'


class FindsecbugsParser(object):

def __init__(self, root, project_id, scan_id):
self.root = root
self.project_id = project_id
self.scan_id = scan_id

def find_bug_pattern(self, type):
Details = 'NA'
for bug in self.root:
if bug.tag == "BugPattern":
if bug.attrib['type'] is not None:
if bug.attrib['type'] == type:
for BugPattern in bug:
if BugPattern.tag == "Details":
Details = BugPattern.text
return Details

def xml_parser(self):
"""

:param root:
:param project_id:
:param scan_id:
:return:
"""
date_time = datetime.now()
global name, classname, risk, ShortMessage, LongMessage, sourcepath, vul_col, ShortDescription, Details, lenth_match, duplicate_hash, vul_id, total_vul, total_high, total_medium, total_low, details, message
for bug in self.root:
if bug.tag == "BugInstance":
name = bug.attrib["type"]
if BugPattern.tag == "ShortDescription":
ShortDescription = BugPattern.text
if BugPattern.tag == "Details":
global Details
Details = BugPattern.text

StaticScanResultsDb.objects.filter(scan_id=scan_id, title=name).update(
description=str(Details)
+ "\n\n"
+ str(ShortMessage)
+ "\n\n"
+ str(LongMessage)
+ "\n\n"
+ str(classname),
)

all_findbugs_data = StaticScanResultsDb.objects.filter(
scan_id=scan_id, false_positive="No"
)
priority = bug.attrib["priority"]
for BugInstance in bug:
if BugInstance.tag == "ShortMessage":
global ShortMessage
ShortMessage = BugInstance.text
if BugInstance.tag == "LongMessage":
global LongMessage
LongMessage = BugInstance.text
if BugInstance.tag == "Class":
global classname
try:
classname = BugInstance.attrib["classname"]
except:
classname = 'na'
if BugInstance.tag == "SourceLine":
global sourcepath, sourcefile
try:
sourcepath = BugInstance.attrib["sourcepath"]
except:
sourcepath = 'NA'
try:
sourcefile = BugInstance.attrib["sourcefile"]
except:
sourcefile = 'NA'

for data in bug:
for message_data in data:
if message_data.tag == 'Message':
message = message_data.text

if priority == "1":
risk = "High"
vul_col = "danger"

elif priority == "2":
risk = "Medium"
vul_col = "warning"

elif priority == "3":
risk = "Low"
vul_col = "info"

vul_id = uuid.uuid4()

dup_data = str(ShortMessage) + str(message) + str(sourcepath) + str(risk)

duplicate_hash = hashlib.sha256(dup_data.encode("utf-8")).hexdigest()

match_dup = StaticScanResultsDb.objects.filter(
dup_hash=duplicate_hash
).values("dup_hash")
lenth_match = len(match_dup)

details = self.find_bug_pattern(name)
if lenth_match == 0:
duplicate_vuln = "No"

false_p = StaticScanResultsDb.objects.filter(
false_positive_hash=duplicate_hash
)
fp_lenth_match = len(false_p)

if fp_lenth_match == 1:
false_positive = "Yes"
else:
false_positive = "No"

save_all = StaticScanResultsDb(
vuln_id=vul_id,
date_time=date_time,
scan_id=self.scan_id,
project_id=self.project_id,
title=str(ShortMessage),
severity=risk,
description="<b>Finding Path & Line:</b> %s" % str(message)
+ "<br><br>"
"<b>Finding Classes:</b> %s" % str(classname)
+ "<br><br>"
"<b>Finding Source Path</b>: %s" % str(sourcepath)
+ "<br><br>"
+ str(ShortMessage)
+ "<br><br>"
+ str(LongMessage)
+ "<br><br>"
+ str(details),
# + "\n\n"
# + str(classname),
fileName=str(message),
severity_color=vul_col,
vuln_status="Open",
dup_hash=duplicate_hash,
vuln_duplicate=duplicate_vuln,
false_positive=false_positive,
scanner="Findbugs",
)
save_all.save()

duplicate_count = StaticScanResultsDb.objects.filter(
scan_id=scan_id, vuln_duplicate="Yes"
else:
duplicate_vuln = "Yes"
save_all = StaticScanResultsDb(
vuln_id=vul_id,
date_time=date_time,
scan_id=self.scan_id,
project_id=self.project_id,
title=str(ShortMessage),
severity=risk,
description="<b>Finding Path & Line:</b> %s" % str(message)
+ "<br><br>"
"<b>Finding Classes:</b> %s" % str(classname)
+ "<br><br>"
"<b>Finding Source Path</b>: %s" % str(sourcepath)
+ "<br><br>"
+ str(ShortMessage)
+ "<br><br>"
+ str(LongMessage)
+ "<br><br>"
+ str(details),
# + "\n\n"
# + str(classname),
fileName=str(message),
severity_color=vul_col,
vuln_status="Duplicate",
dup_hash=duplicate_hash,
vuln_duplicate=duplicate_vuln,
false_positive="Duplicate",
scanner="Findbugs",
)
save_all.save()

# if bug.tag == "BugPattern":
# for BugPattern in bug:
# name = bug.attrib["type"]
# if BugPattern.tag == "ShortDescription":
# ShortDescription = BugPattern.text
# if BugPattern.tag == "Details":
# global Details
# Details = BugPattern.text
# print(Details)
# StaticScanResultsDb.objects.filter(vuln_id=vul_id, title=name).update(
# description=str(Details)
# + "\n\n"
# + str(ShortMessage)
# + "\n\n"
# + str(LongMessage)
# + "\n\n"
# + str(classname),
# )

all_findbugs_data = StaticScanResultsDb.objects.filter(
scan_id=self.scan_id, false_positive="No"
)

duplicate_count = StaticScanResultsDb.objects.filter(
scan_id=self.scan_id, vuln_duplicate="Yes"
)

total_vul = len(all_findbugs_data)
total_high = len(all_findbugs_data.filter(severity="High"))
total_medium = len(all_findbugs_data.filter(severity="Medium"))
total_low = len(all_findbugs_data.filter(severity="Low"))
total_duplicate = len(duplicate_count.filter(vuln_duplicate="Yes"))

StaticScansDb.objects.filter(scan_id=self.scan_id).update(
total_vul=total_vul,
date_time=date_time,
high_vul=total_high,
medium_vul=total_medium,
low_vul=total_low,
total_dup=total_duplicate,
scanner="Findbugs",
)
trend_update()
subject = "Archery Tool Scan Status - Findbugs Report Uploaded"
message = (
"Findbugs Scanner has completed the scan "
" %s <br> Total: %s <br>High: %s <br>"
"Medium: %s <br>Low %s"
% (self.scan_id, total_vul, total_high, total_medium, total_low)
)

total_vul = len(all_findbugs_data)
total_high = len(all_findbugs_data.filter(severity="High"))
total_medium = len(all_findbugs_data.filter(severity="Medium"))
total_low = len(all_findbugs_data.filter(severity="Low"))
total_duplicate = len(duplicate_count.filter(vuln_duplicate="Yes"))

StaticScansDb.objects.filter(scan_id=scan_id).update(
total_vul=total_vul,
date_time=date_time,
high_vul=total_high,
medium_vul=total_medium,
low_vul=total_low,
total_dup=total_duplicate,
scanner="Findbugs",
)
trend_update()
subject = "Archery Tool Scan Status - Findbugs Report Uploaded"
message = (
"Findbugs Scanner has completed the scan "
" %s <br> Total: %s <br>High: %s <br>"
"Medium: %s <br>Low %s"
% (scan_id, total_vul, total_high, total_medium, total_low)
)

email_sch_notify(subject=subject, message=message)
email_sch_notify(subject=subject, message=message)