CrypticallyRequie commited on
Commit
fa7ee1e
·
verified ·
1 Parent(s): 837038b

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +380 -0
  2. huggingface-space.yaml +16 -0
app.py ADDED
@@ -0,0 +1,380 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import json
4
+ import datetime
5
+ import pandas as pd
6
+ import matplotlib.pyplot as plt
7
+ import seaborn as sns
8
+ import yaml
9
+ import uuid
10
+ import tempfile
11
+ import shutil
12
+
13
+ # Demo configuration
14
+ DEMO_CASE_ID = f"DEMO-{uuid.uuid4().hex[:8]}"
15
+ DEMO_OUTPUT_DIR = "demo_output"
16
+ DEMO_EVIDENCE_DIR = os.path.join(DEMO_OUTPUT_DIR, "evidence")
17
+ DEMO_ANALYSIS_DIR = os.path.join(DEMO_OUTPUT_DIR, "analysis")
18
+ DEMO_REPORT_DIR = os.path.join(DEMO_OUTPUT_DIR, "reports")
19
+
20
+ # Create directories if they don't exist
21
+ os.makedirs(DEMO_EVIDENCE_DIR, exist_ok=True)
22
+ os.makedirs(DEMO_ANALYSIS_DIR, exist_ok=True)
23
+ os.makedirs(DEMO_REPORT_DIR, exist_ok=True)
24
+
25
+ # Cloud provider connection functions
26
+ def test_aws_connection(access_key, secret_key, region):
27
+ """Test connection to AWS"""
28
+ try:
29
+ import boto3
30
+ session = boto3.Session(
31
+ aws_access_key_id=access_key,
32
+ aws_secret_access_key=secret_key,
33
+ region_name=region
34
+ )
35
+ sts = session.client('sts')
36
+ identity = sts.get_caller_identity()
37
+ return True, f"Successfully connected to AWS as {identity['Arn']}"
38
+ except Exception as e:
39
+ return False, f"Failed to connect to AWS: {str(e)}"
40
+
41
+ def test_azure_connection(tenant_id, client_id, client_secret):
42
+ """Test connection to Azure"""
43
+ try:
44
+ from azure.identity import ClientSecretCredential
45
+ from azure.mgmt.resource import ResourceManagementClient
46
+
47
+ credential = ClientSecretCredential(
48
+ tenant_id=tenant_id,
49
+ client_id=client_id,
50
+ client_secret=client_secret
51
+ )
52
+
53
+ # Create a resource management client
54
+ resource_client = ResourceManagementClient(credential, subscription_id)
55
+
56
+ # List resource groups to test the connection
57
+ resource_groups = list(resource_client.resource_groups.list())
58
+ return True, f"Successfully connected to Azure. Found {len(resource_groups)} resource groups."
59
+ except Exception as e:
60
+ return False, f"Failed to connect to Azure: {str(e)}"
61
+
62
+ def test_gcp_connection(service_account_json):
63
+ """Test connection to GCP"""
64
+ try:
65
+ import json
66
+ from google.oauth2 import service_account
67
+ from google.cloud import storage
68
+
69
+ # Create a temporary file to store the service account JSON
70
+ fd, path = tempfile.mkstemp()
71
+ try:
72
+ with os.fdopen(fd, 'w') as tmp:
73
+ tmp.write(service_account_json)
74
+
75
+ # Create credentials from the service account file
76
+ credentials = service_account.Credentials.from_service_account_file(path)
77
+
78
+ # Create a storage client to test the connection
79
+ storage_client = storage.Client(credentials=credentials)
80
+
81
+ # List buckets to test the connection
82
+ buckets = list(storage_client.list_buckets())
83
+ return True, f"Successfully connected to GCP. Found {len(buckets)} storage buckets."
84
+ finally:
85
+ os.remove(path)
86
+ except Exception as e:
87
+ return False, f"Failed to connect to GCP: {str(e)}"
88
+
89
+ # Sample data for demonstration
90
+ def generate_sample_data(case_info, cloud_provider, incident_type, use_real_data=False, credentials=None):
91
+ """Generate sample data for demonstration purposes or collect real data if credentials provided"""
92
+
93
+ if use_real_data and credentials:
94
+ # This would be where we implement real data collection using the provided credentials
95
+ # For now, we'll return a message indicating this would use real data
96
+ return {
97
+ "timeline": [],
98
+ "patterns": [],
99
+ "anomalies": [],
100
+ "files": {},
101
+ "message": "In a production deployment, this would collect real data from your cloud provider."
102
+ }
103
+
104
+ # Create sample timeline data
105
+ timeline_data = []
106
+ base_time = datetime.datetime.now() - datetime.timedelta(days=1)
107
+
108
+ # Different events based on incident type
109
+ if incident_type == "Unauthorized Access":
110
+ events = [
111
+ {"event": "Failed login attempt", "source": "Authentication Logs", "severity": "Low"},
112
+ {"event": "Successful login from unusual IP", "source": "Authentication Logs", "severity": "Medium"},
113
+ {"event": "User privilege escalation", "source": "IAM Logs", "severity": "High"},
114
+ {"event": "Access to sensitive data", "source": "Data Access Logs", "severity": "High"},
115
+ {"event": "Configuration change", "source": "Configuration Logs", "severity": "Medium"},
116
+ {"event": "New API key created", "source": "IAM Logs", "severity": "High"},
117
+ {"event": "Data download initiated", "source": "Data Access Logs", "severity": "Critical"},
118
+ {"event": "Unusual network traffic", "source": "Network Logs", "severity": "Medium"}
119
+ ]
120
+ elif incident_type == "Data Exfiltration":
121
+ events = [
122
+ {"event": "Large query executed", "source": "Database Logs", "severity": "Medium"},
123
+ {"event": "Unusual data access pattern", "source": "Data Access Logs", "severity": "Medium"},
124
+ {"event": "Large data transfer initiated", "source": "Network Logs", "severity": "High"},
125
+ {"event": "Connection to unknown external endpoint", "source": "Network Logs", "severity": "High"},
126
+ {"event": "Storage object permissions modified", "source": "Storage Logs", "severity": "Medium"},
127
+ {"event": "Unusual user behavior", "source": "User Activity Logs", "severity": "Medium"},
128
+ {"event": "Data archive created", "source": "Storage Logs", "severity": "Medium"},
129
+ {"event": "Unusual egress traffic spike", "source": "Network Logs", "severity": "Critical"}
130
+ ]
131
+ else: # Ransomware
132
+ events = [
133
+ {"event": "Unusual process execution", "source": "System Logs", "severity": "Medium"},
134
+ {"event": "Multiple file modifications", "source": "File System Logs", "severity": "High"},
135
+ {"event": "Encryption library loaded", "source": "System Logs", "severity": "High"},
136
+ {"event": "Mass file type changes", "source": "Storage Logs", "severity": "Critical"},
137
+ {"event": "Backup deletion attempt", "source": "Backup Logs", "severity": "Critical"},
138
+ {"event": "Unusual IAM activity", "source": "IAM Logs", "severity": "Medium"},
139
+ {"event": "Recovery service disabled", "source": "System Logs", "severity": "High"},
140
+ {"event": "Ransom note created", "source": "File System Logs", "severity": "Critical"}
141
+ ]
142
+
143
+ # Create timeline with timestamps
144
+ for i, event in enumerate(events):
145
+ event_time = base_time + datetime.timedelta(minutes=i*15)
146
+ timeline_data.append({
147
+ "timestamp": event_time.isoformat(),
148
+ "event": event["event"],
149
+ "source": event["source"],
150
+ "cloud_provider": cloud_provider,
151
+ "severity": event["severity"],
152
+ "case_id": case_info["case_id"]
153
+ })
154
+
155
+ # Create patterns data
156
+ patterns = []
157
+ if incident_type == "Unauthorized Access":
158
+ patterns = [
159
+ {"pattern": "Brute Force Login Attempt", "confidence": 0.85, "matched_events": 3},
160
+ {"pattern": "Privilege Escalation", "confidence": 0.92, "matched_events": 2}
161
+ ]
162
+ elif incident_type == "Data Exfiltration":
163
+ patterns = [
164
+ {"pattern": "Data Staging Activity", "confidence": 0.88, "matched_events": 3},
165
+ {"pattern": "Exfiltration Over Alternative Protocol", "confidence": 0.76, "matched_events": 2}
166
+ ]
167
+ else: # Ransomware
168
+ patterns = [
169
+ {"pattern": "Mass File Encryption", "confidence": 0.94, "matched_events": 4},
170
+ {"pattern": "Defense Evasion", "confidence": 0.81, "matched_events": 3}
171
+ ]
172
+
173
+ # Create anomalies data
174
+ anomalies = []
175
+ if incident_type == "Unauthorized Access":
176
+ anomalies = [
177
+ {"anomaly": "Login from unusual location", "deviation": 3.6, "severity": "High"},
178
+ {"anomaly": "Off-hours access", "deviation": 2.8, "severity": "Medium"}
179
+ ]
180
+ elif incident_type == "Data Exfiltration":
181
+ anomalies = [
182
+ {"anomaly": "Unusual data access volume", "deviation": 4.2, "severity": "High"},
183
+ {"anomaly": "Abnormal query pattern", "deviation": 3.1, "severity": "Medium"}
184
+ ]
185
+ else: # Ransomware
186
+ anomalies = [
187
+ {"anomaly": "Unusual file system activity", "deviation": 4.7, "severity": "Critical"},
188
+ {"anomaly": "Suspicious process behavior", "deviation": 3.9, "severity": "High"}
189
+ ]
190
+
191
+ # Save data to files
192
+ timeline_file = os.path.join(DEMO_EVIDENCE_DIR, f"{DEMO_CASE_ID}_timeline.json")
193
+ patterns_file = os.path.join(DEMO_ANALYSIS_DIR, f"{DEMO_CASE_ID}_patterns.json")
194
+ anomalies_file = os.path.join(DEMO_ANALYSIS_DIR, f"{DEMO_CASE_ID}_anomalies.json")
195
+
196
+ with open(timeline_file, 'w') as f:
197
+ json.dump(timeline_data, f, indent=2)
198
+
199
+ with open(patterns_file, 'w') as f:
200
+ json.dump(patterns, f, indent=2)
201
+
202
+ with open(anomalies_file, 'w') as f:
203
+ json.dump(anomalies, f, indent=2)
204
+
205
+ return {
206
+ "timeline": timeline_data,
207
+ "patterns": patterns,
208
+ "anomalies": anomalies,
209
+ "files": {
210
+ "timeline": timeline_file,
211
+ "patterns": patterns_file,
212
+ "anomalies": anomalies_file
213
+ }
214
+ }
215
+
216
+ def analyze_evidence(data):
217
+ """Perform analysis on the evidence data"""
218
+
219
+ # If there's no timeline data, return empty results
220
+ if not data["timeline"]:
221
+ return {
222
+ "severity_counts": {},
223
+ "source_counts": {},
224
+ "charts": {
225
+ "analysis": None,
226
+ "timeline": None
227
+ }
228
+ }
229
+
230
+ # Convert timeline to DataFrame for analysis
231
+ timeline_df = pd.DataFrame(data["timeline"])
232
+ timeline_df["timestamp"] = pd.to_datetime(timeline_df["timestamp"])
233
+
234
+ # Sort by timestamp
235
+ timeline_df = timeline_df.sort_values("timestamp")
236
+
237
+ # Count events by severity
238
+ severity_counts = timeline_df["severity"].value_counts()
239
+
240
+ # Count events by source
241
+ source_counts = timeline_df["source"].value_counts()
242
+
243
+ # Create visualizations
244
+ fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
245
+
246
+ # Severity pie chart
247
+ ax1.pie(severity_counts, labels=severity_counts.index, autopct='%1.1f%%',
248
+ colors=sns.color_palette("YlOrRd", len(severity_counts)))
249
+ ax1.set_title("Events by Severity")
250
+
251
+ # Source bar chart
252
+ sns.barplot(x=source_counts.values, y=source_counts.index, ax=ax2, palette="viridis")
253
+ ax2.set_title("Events by Source")
254
+ ax2.set_xlabel("Count")
255
+
256
+ # Save the figure
257
+ chart_file = os.path.join(DEMO_ANALYSIS_DIR, f"{DEMO_CASE_ID}_analysis_charts.png")
258
+ plt.tight_layout()
259
+ plt.savefig(chart_file)
260
+ plt.close()
261
+
262
+ # Create a timeline visualization
263
+ plt.figure(figsize=(12, 6))
264
+
265
+ # Create a categorical y-axis based on source
266
+ sources = timeline_df["source"].unique()
267
+ source_map = {source: i for i, source in enumerate(sources)}
268
+ timeline_df["source_num"] = timeline_df["source"].map(source_map)
269
+
270
+ # Map severity to color
271
+ severity_colors = {
272
+ "Low": "green",
273
+ "Medium": "blue",
274
+ "High": "orange",
275
+ "Critical": "red"
276
+ }
277
+ colors = timeline_df["severity"].map(severity_colors)
278
+
279
+ # Plot the timeline
280
+ plt.scatter(timeline_df["timestamp"], timeline_df["source_num"], c=colors, s=100)
281
+
282
+ # Add event labels
283
+ for i, row in timeline_df.iterrows():
284
+ plt.text(row["timestamp"], row["source_num"], row["event"],
285
+ fontsize=8, ha="right", va="bottom", rotation=25)
286
+
287
+ plt.yticks(range(len(sources)), sources)
288
+ plt.xlabel("Time")
289
+ plt.ylabel("Event Source")
290
+ plt.title("Incident Timeline")
291
+
292
+ # Save the timeline
293
+ timeline_chart = os.path.join(DEMO_ANALYSIS_DIR, f"{DEMO_CASE_ID}_timeline_chart.png")
294
+ plt.tight_layout()
295
+ plt.savefig(timeline_chart)
296
+ plt.close()
297
+
298
+ return {
299
+ "severity_counts": severity_counts.to_dict(),
300
+ "source_counts": source_counts.to_dict(),
301
+ "charts": {
302
+ "analysis": chart_file,
303
+ "timeline": timeline_chart
304
+ }
305
+ }
306
+
307
+ def generate_report(case_info, data, analysis, report_format):
308
+ """Generate a report based on the analysis"""
309
+
310
+ # Create report content
311
+ report = {
312
+ "case_information": case_info,
313
+ "executive_summary": f"This report presents the findings of a forensic investigation into a {case_info['incident_type']} incident in {case_info['cloud_provider']} cloud environment.",
314
+ "timeline": data["timeline"],
315
+ "patterns_detected": data["patterns"],
316
+ "anomalies_detected": data["anomalies"],
317
+ "analysis_results": {
318
+ "severity_distribution": analysis.get("severity_counts", {}),
319
+ "source_distribution": analysis.get("source_counts", {})
320
+ },
321
+ "recommendations": [
322
+ "Implement multi-factor authentication for all privileged accounts",
323
+ "Review and restrict IAM permissions following principle of least privilege",
324
+ "Enable comprehensive logging across all cloud services",
325
+ "Implement automated alerting for suspicious activities",
326
+ "Conduct regular security assessments of cloud environments"
327
+ ]
328
+ }
329
+
330
+ # Save report in requested format
331
+ if report_format == "JSON":
332
+ report_file = os.path.join(DEMO_REPORT_DIR, f"{DEMO_CASE_ID}_report.json")
333
+ with open(report_file, 'w') as f:
334
+ json.dump(report, f, indent=2)
335
+ else: # HTML
336
+ # Create a simple HTML report
337
+ html_content = f"""
338
+ <!DOCTYPE html>
339
+ <html>
340
+ <head>
341
+ <title>Forensic Analysis Report - {case_info['case_id']}</title>
342
+ <style>
343
+ body {{ font-family: Arial, sans-serif; margin: 40px; }}
344
+ h1, h2, h3 {{ color: #2c3e50; }}
345
+ .section {{ margin-bottom: 30px; }}
346
+ .severity-high {{ color: #e74c3c; }}
347
+ .severity-medium {{ color: #f39c12; }}
348
+ .severity-low {{ color: #27ae60; }}
349
+ table {{ border-collapse: collapse; width: 100%; }}
350
+ th, td {{ border: 1px solid #ddd; padding: 8px; text-align: left; }}
351
+ th {{ background-color: #f2f2f2; }}
352
+ tr:nth-child(even) {{ background-color: #f9f9f9; }}
353
+ .chart-container {{ display: flex; justify-content: center; margin: 20px 0; }}
354
+ .chart {{ max-width: 100%; height: auto; margin: 10px; }}
355
+ .message {{ background-color: #f8f9fa; padding: 15px; border-left: 5px solid #4e73df; margin-bottom: 20px; }}
356
+ </style>
357
+ </head>
358
+ <body>
359
+ <h1>Cloud Forensics Analysis Report</h1>
360
+
361
+ <div class="section">
362
+ <h2>Case Information</h2>
363
+ <p><strong>Case ID:</strong> {case_info['case_id']}</p>
364
+ <p><strong>Investigator:</strong> {case_info['investigator']}</p>
365
+ <p><strong>Organization:</strong> {case_info['organization']}</p>
366
+ <p><strong>Cloud Provider:</strong> {case_info['cloud_provider']}</p>
367
+ <p><strong>Incident Type:</strong> {case_info['incident_type']}</p>
368
+ <p><strong>Report Date:</strong> {datetime.datetime.now().strftime('%Y-%m-%d')}</p>
369
+ </div>
370
+
371
+ <div class="section">
372
+ <h2>Executive Summary</h2>
373
+ <p>{report['executive_summary']}</p>
374
+ """
375
+
376
+ # Add message if using real data
377
+ if "message" in data:
378
+ html_content += f"""
379
+ <div class="mes
380
+ (Content truncated due to size limit. Use line ranges to read in chunks)
huggingface-space.yaml ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Hugging Face Space Configuration
2
+
3
+ sdk_version: 3.0
4
+ app_file: app.py
5
+ pinned: false
6
+ license: mit
7
+
8
+ # Python dependencies
9
+ python: "3.9"
10
+ packages:
11
+ - gradio==3.50.2
12
+ - pandas==1.5.3
13
+ - numpy==1.24.3
14
+ - matplotlib==3.7.1
15
+ - seaborn==0.12.2
16
+ - pyyaml==6.0