-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmain.py
320 lines (292 loc) · 12 KB
/
main.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
import sys
import os
import json
import yaml
import importlib
import time
from rich.console import Console
from rich.table import Table
from rich.panel import Panel
from rich.text import Text
from collections import defaultdict
from hawk_scanner.internals import system
from rich import print
import ssl
# Disable SSL verification globally
ssl._create_default_https_context = ssl._create_unverified_context
def clear_screen():
os.system('cls' if os.name == 'nt' else 'clear')
clear_screen()
console = Console()
def load_command_module(command):
try:
module = importlib.import_module(f"hawk_scanner.commands.{command}")
return module
except Exception as e:
print(f"Command '{command}' is not supported. {e}")
sys.exit(1)
def execute_command(command, args):
module = load_command_module(command)
return module.execute(args)
def group_results(args, results):
grouped_results = defaultdict(list)
for result in results:
connection = system.get_connection(args)
result = system.evaluate_severity(result, connection)
grouped_results[result['data_source']].append(result)
return grouped_results
def format_slack_message(group, result, records_mini):
template_map = {
's3': """
*** PII Or Secret Found ***
Data Source: S3 Bucket - {vulnerable_profile}
Bucket: {bucket}
File Path: {file_path}
Pattern Name: {pattern_name}
Total Exposed: {total_exposed}
Exposed Values: {exposed_values}
""",
'mysql': """
*** PII Or Secret Found ***
Data Source: MySQL - {vulnerable_profile}
Host: {host}
Database: {database}
Table: {table}
Column: {column}
Pattern Name: {pattern_name}
Total Exposed: {total_exposed}
Exposed Values: {exposed_values}
""",
'postgresql': """
*** PII Or Secret Found ***
Data Source: PostgreSQL - {vulnerable_profile}
Host: {host}
Database: {database}
Table: {table}
Column: {column}
Pattern Name: {pattern_name}
Total Exposed: {total_exposed}
Exposed Values: {exposed_values}
""",
'mongodb': """
*** PII Or Secret Found ***
Data Source: MongoDB - {vulnerable_profile}
Host: {host}
Database: {database}
Collection: {collection}
Field: {field}
Pattern Name: {pattern_name}
Total Exposed: {total_exposed}
Exposed Values: {exposed_values}
""",
'redis': """
*** PII Or Secret Found ***
Data Source: Redis - {vulnerable_profile}
Host: {host}
Key: {key}
Pattern Name: {pattern_name}
Total Exposed: {total_exposed}
Exposed Values: {exposed_values}
""",
'firebase': """
*** PII Or Secret Found ***
Data Source: Firebase - {vulnerable_profile}
Bucket: {bucket}
File Path: {file_path}
Pattern Name: {pattern_name}
Total Exposed: {total_exposed}
Exposed Values: {exposed_values}
""",
'gcs': """
*** PII Or Secret Found ***
Data Source: GCS - {vulnerable_profile}
Bucket: {bucket}
File Path: {file_path}
Pattern Name: {pattern_name}
Total Exposed: {total_exposed}
Exposed Values: {exposed_values}
""",
'fs': """
*** PII Or Secret Found ***
Data Source: File System - {vulnerable_profile}
File Path: {file_path}
Pattern Name: {pattern_name}
Total Exposed: {total_exposed}
Exposed Values: {exposed_values}
""",
'slack': """
*** PII Or Secret Found ***
Data Source: Slack - {vulnerable_profile}
Channel Name: {channel_name}
Message Link: {message_link}
Pattern Name: {pattern_name}
Total Exposed: {total_exposed}
Exposed Values: {exposed_values}
""",
'couchdb': """
*** PII Or Secret Found ***
Data Source: CouchDB - {vulnerable_profile}
Host: {host}
Database: {database}
Document ID: {doc_id}
Field: {field}
Pattern Name: {pattern_name}
Total Exposed: {total_exposed}
Exposed Values: {exposed_values}
""",
'gdrive': """
*** PII Or Secret Found ***
Data Source: Google Drive - {vulnerable_profile}
File Name: {file_name}
Pattern Name: {pattern_name}
Total Exposed: {total_exposed}
Exposed Values: {exposed_values}
""",
'gdrive_workspace': """
*** PII Or Secret Found ***
Data Source: Google Drive Workspace - {vulnerable_profile}
File Name: {file_name}
User: {user}
Pattern Name: {pattern_name}
Total Exposed: {total_exposed}
Exposed Values: {exposed_values}
""",
'text': """
*** PII Or Secret Found ***
Data Source: Text - {vulnerable_profile}
Pattern Name: {pattern_name}
Total Exposed: {total_exposed}
Exposed Values: {exposed_values}
"""
}
return template_map.get(group, "").format(
vulnerable_profile=result['profile'],
bucket=result.get('bucket', ''),
file_path=result.get('file_path', ''),
host=result.get('host', ''),
database=result.get('database', ''),
table=result.get('table', ''),
column=result.get('column', ''),
doc_id=result.get('doc_id', ''),
channel_name=result.get('channel_name', ''),
message_link=result.get('message_link', ''),
file_name=result.get('file_name', ''),
user=result.get('user', ''),
pattern_name=result['pattern_name'],
total_exposed=str(len(result['matches'])),
exposed_values=records_mini
)
def add_columns_to_table(group, table):
if group in ['s3', 'firebase', 'gcs']:
table.add_column("Bucket > File Path")
elif group in ['mysql', 'postgresql']:
table.add_column("Host > Database > Table.Column")
elif group == 'redis':
table.add_column("Host > Key")
elif group == 'mongodb':
table.add_column("Host > Database > Collection > Field")
elif group == 'slack':
table.add_column("Channel Name > Message Link")
elif group == 'gdrive':
table.add_column("File Name")
elif group == 'gdrive_workspace':
table.add_column("File Name")
table.add_column("User")
elif group == 'couchdb':
table.add_column("Host > Database > Document ID > Field")
elif group == 'fs':
table.add_column("File Path")
table.add_column("Pattern Name")
table.add_column("Total Exposed")
table.add_column("Exposed Values")
table.add_column("Sample Text")
def main():
start_time = time.time()
args = system.parse_args()
system.print_banner(args)
results = []
if args.command:
connections = system.get_connection(args)
data_sources = connections.get('sources', {}).keys()
commands = [args.command] if args.command != 'all' else data_sources
for command in commands:
results.extend(execute_command(command, args))
else:
system.print_error(args, "Please provide a command to execute")
sys.exit(1)
grouped_results = group_results(args, results)
if args.json:
if args.json:
with open(args.json, 'w') as file:
file.write(json.dumps(grouped_results, indent=4))
system.print_success(args, f"Results saved to {args.json}")
else:
print(json.dumps(grouped_results, indent=4))
sys.exit(0)
if args.stdout:
print(json.dumps(grouped_results, indent=4))
sys.exit(0)
# Display results in the table format
console.print(Panel(Text("Now, let's look at findings!", justify="center")))
for group, group_data in grouped_results.items():
table = Table(show_header=True, header_style="bold magenta", show_lines=True,
title=f"[bold blue]Total {len(group_data)} findings in {group}[/bold blue]")
table.add_column("Sl. No.")
table.add_column("Vulnerable Profile")
print(group_data)
add_columns_to_table(group, table)
for i, result in enumerate(group_data, 1):
records_mini = ', '.join(result['matches']) if len(result['matches']) < 25 else ', '.join(result['matches'][:25]) + f" + {len(result['matches']) - 25} more"
slack_message = format_slack_message(group, result, records_mini)
if slack_message:
system.create_jira_ticket(args, result, slack_message)
system.SlackNotify(slack_message, args)
if group == 's3':
table.add_row(str(i), result['profile'], f"{result['bucket']} > {result['file_path']}",
result['pattern_name'], str(len(result['matches'])), records_mini, result['sample_text'])
elif group in ['mysql', 'postgresql']:
table.add_row(str(i), result['profile'],
f"{result['host']} > {result['database']} > {result['table']}.{result['column']}",
result['pattern_name'], str(len(result['matches'])), records_mini, result['sample_text'])
elif group == 'mongodb':
table.add_row(str(i), result['profile'],
f"{result['host']} > {result['database']} > {result['collection']} > {result['field']}",
result['pattern_name'], str(len(result['matches'])), records_mini, result['sample_text'])
elif group == 'slack':
table.add_row(str(i), result['profile'],
f"{result['channel_name']} > {result['message_link']}",
result['pattern_name'], str(len(result['matches'])), records_mini, result['sample_text'])
elif group == 'redis':
table.add_row(str(i), result['profile'], f"{result['host']} > {result['key']}",
result['pattern_name'], str(len(result['matches'])), records_mini, result['sample_text'])
elif group in ['firebase', 'gcs']:
table.add_row(str(i), result['profile'], f"{result['bucket']} > {result['file_path']}",
result['pattern_name'], str(len(result['matches'])), records_mini, result['sample_text'])
elif group == 'fs':
table.add_row(str(i), result['profile'], result['file_path'], result['pattern_name'],
str(len(result['matches'])), records_mini, result['sample_text'])
elif group == 'couchdb':
table.add_row(str(i), result['profile'],
f"{result['host']} > {result['database']} > {result['doc_id']} > {result['field']}",
result['pattern_name'], str(len(result['matches'])), records_mini, result['sample_text'])
elif group == 'gdrive':
table.add_row(str(i), result['profile'], result['file_name'], result['pattern_name'],
str(len(result['matches'])), records_mini, result['sample_text'])
elif group == 'gdrive_workspace':
table.add_row(str(i), result['profile'], result['file_name'], result['user'],
result['pattern_name'], str(len(result['matches'])), records_mini, result['sample_text'])
elif group == 'text':
table.add_row(str(i), result['profile'], result['pattern_name'],
str(len(result['matches'])), records_mini, result['sample_text'])
console.print(table)
if args.hawk_thuu:
console.print("Hawk thuuu, Spitting on that thang!....")
os.system("rm -rf data/*")
time.sleep(2)
console.print("Cleaned hawk data! 🧹")
# Measure and print the total execution time
end_time = time.time()
execution_time = end_time - start_time
print(f"[bold green]Execution completed in {execution_time:.2f} seconds.[/bold green]")
if __name__ == '__main__':
main()