-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathopencti_practical_demo.py
More file actions
413 lines (350 loc) · 17.1 KB
/
opencti_practical_demo.py
File metadata and controls
413 lines (350 loc) · 17.1 KB
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#!/usr/bin/env python3
"""
OpenCTI Practical Use Cases Demo
Demonstrates real-world threat intelligence scenarios
"""
import requests
import json
import hashlib
import ipaddress
from datetime import datetime
from typing import Dict, List, Any
class OpenCTIPracticalDemo:
def __init__(self, base_url: str, token: str):
self.base_url = base_url.rstrip('/')
self.token = token
self.session = requests.Session()
self.session.headers.update({
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json'
})
def execute_query(self, query: str, variables: Dict = None) -> Dict[str, Any]:
"""Execute GraphQL query"""
payload = {'query': query}
if variables:
payload['variables'] = variables
try:
response = self.session.post(f"{self.base_url}/graphql", json=payload, timeout=30)
response.raise_for_status()
return response.json()
except Exception as e:
return {'errors': [{'message': str(e)}]}
def check_ioc_reputation(self, ioc: str, ioc_type: str) -> Dict[str, Any]:
"""Check if an IOC exists in the threat intelligence database"""
print(f"🔍 Checking reputation for {ioc_type}: {ioc}")
# Create STIX pattern based on IOC type
if ioc_type == "ip":
pattern = f"[ipv4-addr:value = '{ioc}']"
elif ioc_type == "domain":
pattern = f"[domain-name:value = '{ioc}']"
elif ioc_type == "hash":
pattern = f"[file:hashes.MD5 = '{ioc}']"
else:
pattern = ioc
query = """
query SearchIndicators($search: String) {
indicators(search: $search, first: 10) {
edges {
node {
id
pattern
indicator_types
description
confidence
created
labels {
edges {
node {
value
}
}
}
stixCoreRelationships {
edges {
node {
relationship_type
to {
... on StixCoreObject {
entity_type
... on ThreatActor { name }
... on Malware { name }
}
}
}
}
}
}
}
}
}
"""
result = self.execute_query(query, {"search": ioc})
reputation = {
'ioc': ioc,
'type': ioc_type,
'is_malicious': False,
'confidence': 0,
'threat_types': [],
'associated_threats': [],
'description': '',
'first_seen': None
}
if 'data' in result and result['data']['indicators']['edges']:
indicators = result['data']['indicators']['edges']
for edge in indicators:
indicator = edge['node']
if ioc.lower() in indicator['pattern'].lower():
reputation['is_malicious'] = True
reputation['confidence'] = max(reputation['confidence'], indicator['confidence'])
reputation['threat_types'].extend(indicator['indicator_types'])
reputation['description'] = indicator.get('description', '')
reputation['first_seen'] = indicator['created']
# Get associated threats
for rel_edge in indicator['stixCoreRelationships']['edges']:
rel = rel_edge['node']
threat_entity = rel['to']
reputation['associated_threats'].append({
'type': threat_entity['entity_type'],
'name': threat_entity.get('name', 'Unknown'),
'relationship': rel['relationship_type']
})
# Print results
if reputation['is_malicious']:
print(f"🚨 MALICIOUS IOC DETECTED!")
print(f" Confidence: {reputation['confidence']}%")
print(f" Threat Types: {', '.join(set(reputation['threat_types']))}")
print(f" Description: {reputation['description']}")
if reputation['associated_threats']:
print(f" Associated Threats:")
for threat in reputation['associated_threats']:
print(f" - {threat['type']}: {threat['name']} ({threat['relationship']})")
else:
print(f"✅ IOC appears clean - no threats found")
return reputation
def bulk_ioc_check(self, iocs: List[Dict[str, str]]) -> List[Dict[str, Any]]:
"""Check multiple IOCs for threats"""
print(f"\n🔍 BULK IOC REPUTATION CHECK")
print(f"Checking {len(iocs)} indicators...")
print("=" * 50)
results = []
malicious_count = 0
for ioc_data in iocs:
result = self.check_ioc_reputation(ioc_data['value'], ioc_data['type'])
results.append(result)
if result['is_malicious']:
malicious_count += 1
print() # Add spacing between checks
print(f"📊 BULK CHECK SUMMARY:")
print(f" Total IOCs Checked: {len(iocs)}")
print(f" Malicious IOCs Found: {malicious_count}")
print(f" Clean IOCs: {len(iocs) - malicious_count}")
print(f" Threat Detection Rate: {(malicious_count/len(iocs)*100):.1f}%")
return results
def simulate_incident_enrichment(self, incident_iocs: List[str]) -> Dict[str, Any]:
"""Simulate enriching an incident with threat intelligence"""
print(f"\n🚨 INCIDENT ENRICHMENT SIMULATION")
print("=" * 50)
print("Scenario: Security team detected suspicious network activity")
print("IOCs extracted from logs and forensic analysis")
print()
enrichment_data = {
'incident_id': f"INC-{datetime.now().strftime('%Y%m%d-%H%M%S')}",
'iocs_analyzed': len(incident_iocs),
'threat_actors': set(),
'malware_families': set(),
'attack_patterns': set(),
'confidence_scores': [],
'recommendations': []
}
for ioc in incident_iocs:
# Determine IOC type
ioc_type = self.determine_ioc_type(ioc)
result = self.check_ioc_reputation(ioc, ioc_type)
if result['is_malicious']:
enrichment_data['confidence_scores'].append(result['confidence'])
for threat in result['associated_threats']:
if threat['type'] == 'ThreatActor':
enrichment_data['threat_actors'].add(threat['name'])
elif threat['type'] == 'Malware':
enrichment_data['malware_families'].add(threat['name'])
elif threat['type'] == 'AttackPattern':
enrichment_data['attack_patterns'].add(threat['name'])
# Generate recommendations
if enrichment_data['threat_actors']:
enrichment_data['recommendations'].append("Review threat actor TTPs and update defenses")
if enrichment_data['malware_families']:
enrichment_data['recommendations'].append("Deploy malware-specific detection rules")
if enrichment_data['confidence_scores']:
avg_confidence = sum(enrichment_data['confidence_scores']) / len(enrichment_data['confidence_scores'])
if avg_confidence > 80:
enrichment_data['recommendations'].append("HIGH CONFIDENCE - Immediate containment recommended")
elif avg_confidence > 60:
enrichment_data['recommendations'].append("MEDIUM CONFIDENCE - Enhanced monitoring recommended")
# Print enrichment results
print(f"📋 INCIDENT ENRICHMENT RESULTS:")
print(f" Incident ID: {enrichment_data['incident_id']}")
print(f" IOCs Analyzed: {enrichment_data['iocs_analyzed']}")
if enrichment_data['threat_actors']:
print(f" Threat Actors: {', '.join(enrichment_data['threat_actors'])}")
if enrichment_data['malware_families']:
print(f" Malware Families: {', '.join(enrichment_data['malware_families'])}")
if enrichment_data['confidence_scores']:
avg_conf = sum(enrichment_data['confidence_scores']) / len(enrichment_data['confidence_scores'])
print(f" Average Confidence: {avg_conf:.1f}%")
print(f"\n💡 RECOMMENDATIONS:")
for i, rec in enumerate(enrichment_data['recommendations'], 1):
print(f" {i}. {rec}")
return enrichment_data
def determine_ioc_type(self, ioc: str) -> str:
"""Determine the type of IOC"""
try:
ipaddress.ip_address(ioc)
return "ip"
except:
pass
if len(ioc) == 32 and all(c in '0123456789abcdef' for c in ioc.lower()):
return "hash"
elif len(ioc) == 40 and all(c in '0123456789abcdef' for c in ioc.lower()):
return "hash"
elif len(ioc) == 64 and all(c in '0123456789abcdef' for c in ioc.lower()):
return "hash"
elif '.' in ioc and not ioc.replace('.', '').isdigit():
return "domain"
else:
return "unknown"
def demonstrate_threat_hunting_workflow(self) -> Dict[str, Any]:
"""Demonstrate a complete threat hunting workflow"""
print(f"\n🎯 THREAT HUNTING WORKFLOW DEMONSTRATION")
print("=" * 60)
print("Scenario: Proactive threat hunting based on new intelligence")
print()
# Sample IOCs that might be found during hunting
hunting_iocs = [
{"value": "192.168.1.100", "type": "ip", "source": "Network logs"},
{"value": "evil-domain.com", "type": "domain", "source": "DNS logs"},
{"value": "d41d8cd98f00b204e9800998ecf8427e", "type": "hash", "source": "File analysis"},
{"value": "suspicious-site.net", "type": "domain", "source": "Web proxy logs"},
{"value": "10.0.0.50", "type": "ip", "source": "Firewall logs"}
]
workflow_results = {
'hunting_session_id': f"HUNT-{datetime.now().strftime('%Y%m%d-%H%M%S')}",
'iocs_investigated': len(hunting_iocs),
'threats_identified': 0,
'false_positives': 0,
'hunting_effectiveness': 0,
'next_actions': []
}
print(f"🔍 HUNTING SESSION: {workflow_results['hunting_session_id']}")
print(f"Investigating {len(hunting_iocs)} IOCs from various sources...")
print()
for ioc_data in hunting_iocs:
print(f"📍 Investigating {ioc_data['type'].upper()}: {ioc_data['value']}")
print(f" Source: {ioc_data['source']}")
result = self.check_ioc_reputation(ioc_data['value'], ioc_data['type'])
if result['is_malicious']:
workflow_results['threats_identified'] += 1
workflow_results['next_actions'].append(f"Block {ioc_data['value']} in security controls")
else:
workflow_results['false_positives'] += 1
print()
# Calculate hunting effectiveness
if workflow_results['iocs_investigated'] > 0:
workflow_results['hunting_effectiveness'] = (
workflow_results['threats_identified'] / workflow_results['iocs_investigated'] * 100
)
# Add general next actions
if workflow_results['threats_identified'] > 0:
workflow_results['next_actions'].extend([
"Update threat hunting rules with new patterns",
"Share intelligence with security team",
"Review historical logs for IOC presence",
"Update incident response playbooks"
])
print(f"📊 HUNTING SESSION RESULTS:")
print(f" Session ID: {workflow_results['hunting_session_id']}")
print(f" IOCs Investigated: {workflow_results['iocs_investigated']}")
print(f" Threats Identified: {workflow_results['threats_identified']}")
print(f" False Positives: {workflow_results['false_positives']}")
print(f" Hunting Effectiveness: {workflow_results['hunting_effectiveness']:.1f}%")
print(f"\n🎯 NEXT ACTIONS:")
for i, action in enumerate(workflow_results['next_actions'], 1):
print(f" {i}. {action}")
return workflow_results
def run_practical_scenarios(self) -> Dict[str, Any]:
"""Run all practical demonstration scenarios"""
print("🚀 OPENCTI PRACTICAL USE CASES DEMONSTRATION")
print("=" * 80)
print("Demonstrating real-world threat intelligence scenarios")
print("that security teams encounter daily.")
print("=" * 80)
demo_results = {
'start_time': datetime.utcnow().isoformat(),
'scenarios': {}
}
# Scenario 1: Single IOC reputation check
print(f"\n📋 SCENARIO 1: IOC REPUTATION CHECK")
print("-" * 40)
suspicious_ip = "192.168.1.100"
reputation_result = self.check_ioc_reputation(suspicious_ip, "ip")
demo_results['scenarios']['ioc_reputation'] = reputation_result
# Scenario 2: Bulk IOC analysis
print(f"\n📋 SCENARIO 2: BULK IOC ANALYSIS")
print("-" * 40)
bulk_iocs = [
{"value": "192.168.1.100", "type": "ip"},
{"value": "evil-domain.com", "type": "domain"},
{"value": "d41d8cd98f00b204e9800998ecf8427e", "type": "hash"},
{"value": "google.com", "type": "domain"}, # Should be clean
{"value": "8.8.8.8", "type": "ip"} # Should be clean
]
bulk_results = self.bulk_ioc_check(bulk_iocs)
demo_results['scenarios']['bulk_analysis'] = bulk_results
# Scenario 3: Incident enrichment
print(f"\n📋 SCENARIO 3: INCIDENT ENRICHMENT")
print("-" * 40)
incident_iocs = ["192.168.1.100", "evil-domain.com", "suspicious-file.exe"]
enrichment_result = self.simulate_incident_enrichment(incident_iocs)
demo_results['scenarios']['incident_enrichment'] = enrichment_result
# Scenario 4: Threat hunting workflow
print(f"\n📋 SCENARIO 4: THREAT HUNTING WORKFLOW")
print("-" * 40)
hunting_result = self.demonstrate_threat_hunting_workflow()
demo_results['scenarios']['threat_hunting'] = hunting_result
demo_results['end_time'] = datetime.utcnow().isoformat()
# Final summary
print(f"\n🎉 PRACTICAL DEMONSTRATION COMPLETE!")
print("=" * 60)
print("✅ OpenCTI Use Cases Demonstrated:")
print(" • IOC Reputation Checking")
print(" • Bulk Threat Analysis")
print(" • Incident Response Enrichment")
print(" • Proactive Threat Hunting")
print(" • Intelligence-Driven Security Operations")
print(f"\n💡 Business Value Delivered:")
print(" • Faster threat detection and response")
print(" • Reduced false positives in security alerts")
print(" • Enhanced incident investigation capabilities")
print(" • Proactive threat hunting effectiveness")
print(" • Improved security team decision making")
return demo_results
def main():
import argparse
parser = argparse.ArgumentParser(description='OpenCTI Practical Use Cases Demo')
parser.add_argument('--url', default='http://localhost:8080',
help='OpenCTI base URL')
parser.add_argument('--token', required=True,
help='OpenCTI API token')
parser.add_argument('--output',
help='Save results to JSON file')
args = parser.parse_args()
# Create demo instance
demo = OpenCTIPracticalDemo(base_url=args.url, token=args.token)
# Run practical scenarios
results = demo.run_practical_scenarios()
# Save results if requested
if args.output:
with open(args.output, 'w') as f:
json.dump(results, f, indent=2, default=str)
print(f"\n💾 Results saved to: {args.output}")
if __name__ == "__main__":
main()