-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgitleaks.py
179 lines (158 loc) · 5.52 KB
/
gitleaks.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
import hashlib
import json
from converters.models import Finding
class GitleaksParser(object):
"""
A class that can be used to parse the Gitleaks JSON report files
"""
def get_scan_types(self):
return ["Gitleaks Scan"]
def get_label_for_scan_types(self, scan_type):
return scan_type
def get_description_for_scan_types(self, scan_type):
return "Import Gitleaks Scan findings in JSON format."
def get_findings(self, filename, test):
"""
Converts a Gitleaks report to findings
"""
issues = json.load(filename)
# empty report are just null object
if issues is None:
return list()
dupes = dict()
for issue in issues:
if issue.get("rule"):
self.get_finding_legacy(issue, test, dupes)
elif issue.get("Description"):
self.get_finding_current(issue, test, dupes)
else:
raise ValueError("Format is not recognized for Gitleaks")
return list(dupes.values())
def get_finding_legacy(self, issue, test, dupes):
line = None
file_path = issue["file"]
reason = issue["rule"]
titleText = "Hard Coded " + reason
description = (
"**Commit:** " + issue["commitMessage"].rstrip("\n") + "\n"
)
description += "**Commit Hash:** " + issue["commit"] + "\n"
description += "**Commit Date:** " + issue["date"] + "\n"
description += (
"**Author:** "
+ issue["author"]
+ " <"
+ issue["email"]
+ ">"
+ "\n"
)
description += "**Reason:** " + reason + "\n"
description += "**Path:** " + file_path + "\n"
if "lineNumber" in issue:
description += "**Line:** %i\n" % issue["lineNumber"]
line = issue["lineNumber"]
if "operation" in issue:
description += "**Operation:** " + issue["operation"] + "\n"
if "leakURL" in issue:
description += (
"**Leak URL:** ["
+ issue["leakURL"]
+ "]("
+ issue["leakURL"]
+ ")\n"
)
description += (
"\n**String Found:**\n\n```\n"
+ issue["line"].replace(issue["offender"], "REDACTED")
+ "\n```"
)
severity = "High"
if "Github" in reason or "AWS" in reason or "Heroku" in reason:
severity = "Critical"
finding = Finding(
title=titleText,
test=test,
cwe=798,
description=description,
severity=severity,
file_path=file_path,
line=line,
dynamic_finding=False,
static_finding=True,
)
# manage tags
finding.unsaved_tags = issue.get("tags", "").split(", ")
dupe_key = hashlib.sha256(
(issue["offender"] + file_path + str(line)).encode("utf-8")
).hexdigest()
if dupe_key not in dupes:
dupes[dupe_key] = finding
def get_finding_current(self, issue, test, dupes):
reason = issue.get("Description")
line = issue.get("StartLine")
if line:
line = int(line)
else:
line = 0
match = issue.get("Match")
secret = issue.get("Secret")
file_path = issue.get("File")
commit = issue.get("Commit")
# Author and email will not be used because of GDPR
# author = issue.get('Author')
# email = issue.get('Email')
date = issue.get("Date")
message = issue.get("Message")
tags = issue.get("Tags")
ruleId = issue.get("RuleID")
title = f"Hard coded {reason} found in {file_path}"
description = ""
if secret:
description += f"**Secret:** {secret}\n"
if match:
description += f"**Match:** {match}\n"
if message:
if len(message.split("\n")) > 1:
description += (
"**Commit message:**"
+ "\n```\n"
+ message.replace("```", "\\`\\`\\`")
+ "\n```\n"
)
else:
description += f"**Commit message:** {message}\n"
if commit:
description += f"**Commit hash:** {commit}\n"
if date:
description += f"**Commit date:** {date}\n"
if ruleId:
description += f"**Rule Id:** {ruleId}"
if description[-1] == "\n":
description = description[:-1]
severity = "High"
dupe_key = hashlib.md5(
(title + secret + str(line)).encode("utf-8")
).hexdigest()
if dupe_key in dupes:
finding = dupes[dupe_key]
finding.description = (
finding.description + "\n\n***\n\n" + description
)
finding.nb_occurences += 1
dupes[dupe_key] = finding
else:
finding = Finding(
title=title,
test=test,
cwe=798,
description=description,
severity=severity,
file_path=file_path,
line=line,
dynamic_finding=False,
static_finding=True,
nb_occurences=1
)
if tags:
finding.unsaved_tags = tags
dupes[dupe_key] = finding