Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@ import spaces
|
|
3 |
import json
|
4 |
import datetime
|
5 |
import random
|
6 |
-
from transformers import pipeline
|
7 |
import torch
|
8 |
import time
|
9 |
|
@@ -26,40 +26,91 @@ custom_css = """
|
|
26 |
padding: 10px;
|
27 |
border-radius: 5px;
|
28 |
}
|
29 |
-
.
|
30 |
-
background: #
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
}
|
36 |
"""
|
37 |
|
38 |
-
#
|
|
|
|
|
|
|
|
|
|
|
39 |
@spaces.GPU
|
40 |
-
def
|
|
|
|
|
|
|
41 |
try:
|
|
|
|
|
|
|
42 |
# Check GPU availability
|
43 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
44 |
-
print(f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
55 |
)
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
except Exception as e:
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
61 |
|
62 |
-
# Enhanced attack scenarios
|
63 |
ATTACK_SCENARIOS = {
|
64 |
"🔄 Lateral Movement": {
|
65 |
"description": "Advanced Persistent Threat (APT) - Attacker moving laterally through network after initial compromise",
|
@@ -67,7 +118,7 @@ ATTACK_SCENARIOS = {
|
|
67 |
"alerts": [
|
68 |
{
|
69 |
"id": "ALR-001",
|
70 |
-
"timestamp": "2025-
|
71 |
"source_ip": "192.168.1.100",
|
72 |
"destination_ip": "192.168.1.25",
|
73 |
"user": "corp\\john.doe",
|
@@ -81,7 +132,7 @@ ATTACK_SCENARIOS = {
|
|
81 |
},
|
82 |
{
|
83 |
"id": "ALR-002",
|
84 |
-
"timestamp": "2025-
|
85 |
"source_ip": "192.168.1.100",
|
86 |
"destination_ip": "192.168.1.50",
|
87 |
"user": "corp\\john.doe",
|
@@ -95,7 +146,7 @@ ATTACK_SCENARIOS = {
|
|
95 |
},
|
96 |
{
|
97 |
"id": "ALR-003",
|
98 |
-
"timestamp": "2025-
|
99 |
"source_ip": "192.168.1.100",
|
100 |
"destination_ip": "10.0.0.15",
|
101 |
"user": "SYSTEM",
|
@@ -115,7 +166,7 @@ ATTACK_SCENARIOS = {
|
|
115 |
"alerts": [
|
116 |
{
|
117 |
"id": "ALR-004",
|
118 |
-
"timestamp": "2025-
|
119 |
"source_ip": "203.0.113.50",
|
120 |
"destination_ip": "192.168.1.75",
|
121 |
"user": "corp\\sarah.wilson",
|
@@ -129,7 +180,7 @@ ATTACK_SCENARIOS = {
|
|
129 |
},
|
130 |
{
|
131 |
"id": "ALR-005",
|
132 |
-
"timestamp": "2025-
|
133 |
"source_ip": "192.168.1.75",
|
134 |
"destination_ip": "203.0.113.50",
|
135 |
"user": "corp\\sarah.wilson",
|
@@ -149,7 +200,7 @@ ATTACK_SCENARIOS = {
|
|
149 |
"alerts": [
|
150 |
{
|
151 |
"id": "ALR-006",
|
152 |
-
"timestamp": "2025-
|
153 |
"source_ip": "192.168.1.85",
|
154 |
"destination_ip": "192.168.1.85",
|
155 |
"user": "corp\\admin.backup",
|
@@ -163,7 +214,7 @@ ATTACK_SCENARIOS = {
|
|
163 |
},
|
164 |
{
|
165 |
"id": "ALR-007",
|
166 |
-
"timestamp": "2025-
|
167 |
"source_ip": "192.168.1.85",
|
168 |
"destination_ip": "45.33.22.11",
|
169 |
"user": "SYSTEM",
|
@@ -180,179 +231,213 @@ ATTACK_SCENARIOS = {
|
|
180 |
}
|
181 |
|
182 |
@spaces.GPU
|
183 |
-
def
|
184 |
-
"""Generate
|
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 |
top_p=0.9,
|
218 |
-
|
|
|
|
|
|
|
219 |
)
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
228 |
|
229 |
def get_fallback_analysis(alert_data, analyst_level):
|
230 |
-
"""
|
231 |
|
232 |
-
|
233 |
-
"L1":
|
234 |
-
"icon": "🚨",
|
235 |
-
"title": "L1 TRIAGE ANALYSIS",
|
236 |
-
"focus": "Initial Assessment & Escalation",
|
237 |
-
"template": """
|
238 |
-
{icon} {title}
|
239 |
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
},
|
260 |
-
"L2": {
|
261 |
-
"icon": "🔍",
|
262 |
-
"title": "L2 INVESTIGATION ANALYSIS",
|
263 |
-
"focus": "Detailed Investigation & Correlation",
|
264 |
-
"template": """
|
265 |
-
{icon} {title}
|
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 |
-
• Attack campaign indicators found
|
292 |
-
• Critical asset involvement confirmed
|
293 |
-
• Advanced persistent threat suspected
|
294 |
-
"""
|
295 |
-
},
|
296 |
-
"L3": {
|
297 |
-
"icon": "🎯",
|
298 |
-
"title": "L3 EXPERT ANALYSIS",
|
299 |
-
"focus": "Attribution & Strategic Response",
|
300 |
-
"template": """
|
301 |
-
{icon} {title}
|
302 |
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
🛠️ MITIGATION STRATEGY:
|
322 |
-
• Immediate: Block C2 communications
|
323 |
-
• Short-term: Deploy hunting queries
|
324 |
-
• Medium-term: Security architecture review
|
325 |
-
• Long-term: Staff training and awareness
|
326 |
-
|
327 |
-
📈 EXECUTIVE BRIEFING POINTS:
|
328 |
• Sophisticated attack requiring coordinated response
|
329 |
-
•
|
330 |
• Recommend incident response team activation
|
331 |
-
• Consider external forensics support
|
332 |
-
|
333 |
-
🔮 PREDICTIVE ANALYSIS:
|
334 |
-
• High probability of follow-up attacks
|
335 |
-
• Recommend 48-72 hour enhanced monitoring
|
336 |
-
• Consider threat landscape implications
|
337 |
-
"""
|
338 |
-
}
|
339 |
}
|
340 |
|
341 |
-
|
342 |
-
template = base_analysis[analyst_level]["template"]
|
343 |
-
return template.format(
|
344 |
-
icon=base_analysis[analyst_level]["icon"],
|
345 |
-
title=base_analysis[analyst_level]["title"],
|
346 |
-
**alert_data
|
347 |
-
)
|
348 |
-
|
349 |
-
return "Analysis not available for specified level."
|
350 |
|
351 |
-
def
|
352 |
-
"""
|
353 |
start_time = time.time()
|
354 |
|
355 |
-
# Validate inputs
|
356 |
if scenario_name not in ATTACK_SCENARIOS:
|
357 |
return "❌ Invalid scenario selected.", "", "Error: Invalid scenario"
|
358 |
|
@@ -364,94 +449,90 @@ def analyze_alert_comprehensive(scenario_name, alert_index, analyst_level):
|
|
364 |
|
365 |
selected_alert = alerts[alert_index]
|
366 |
|
367 |
-
# Generate
|
368 |
-
analysis =
|
369 |
|
370 |
-
#
|
371 |
-
alert_details = f"""
|
372 |
-
|
373 |
-
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
374 |
|
375 |
-
🌐 NETWORK FLOW
|
376 |
Source: {selected_alert['source_ip']} → Destination: {selected_alert['destination_ip']}
|
377 |
|
378 |
-
👤 USER CONTEXT
|
379 |
Account: {selected_alert['user']}
|
380 |
|
381 |
-
⚠️
|
382 |
Type: {selected_alert['alert_type']}
|
383 |
Severity: {selected_alert['severity']}
|
384 |
Confidence: {selected_alert['confidence']}%
|
385 |
|
386 |
-
📝 DESCRIPTION
|
387 |
{selected_alert['description']}
|
388 |
|
389 |
-
🔍 TECHNICAL EVIDENCE
|
390 |
{selected_alert['raw_log']}
|
391 |
|
392 |
-
🧠 THREAT INTELLIGENCE
|
393 |
{selected_alert['threat_intel']}
|
394 |
|
395 |
-
🎪 MITRE ATT&CK
|
396 |
{selected_alert['mitre_tactic']}
|
397 |
|
398 |
-
|
399 |
-
"""
|
400 |
|
401 |
processing_time = round(time.time() - start_time, 2)
|
402 |
-
|
|
|
403 |
|
404 |
-
return alert_details, analysis,
|
405 |
|
406 |
-
def
|
407 |
-
"""
|
408 |
if scenario_name in ATTACK_SCENARIOS:
|
409 |
scenario = ATTACK_SCENARIOS[scenario_name]
|
410 |
|
411 |
-
info = f"""
|
412 |
-
## 🎭 **Attack Scenario: {scenario_name}**
|
413 |
|
414 |
**📋 Description:** {scenario['description']}
|
415 |
-
**⚠️ Severity
|
416 |
-
**📊 Total Alerts:** {len(scenario['alerts'])} security events
|
417 |
|
418 |
### 🔍 **Alert Timeline:**
|
419 |
"""
|
420 |
|
421 |
for i, alert in enumerate(scenario['alerts']):
|
422 |
-
info += f"""
|
423 |
-
**[{i+1}] {alert['timestamp']}** - {alert['alert_type']}
|
424 |
└─ Severity: {alert['severity']} | Confidence: {alert['confidence']}%
|
425 |
"""
|
426 |
|
427 |
-
info +=
|
428 |
-
###
|
429 |
-
- **
|
430 |
-
- **
|
431 |
-
- **
|
432 |
-
|
433 |
|
434 |
return info
|
435 |
-
return "⚠️ No scenario selected.
|
436 |
|
437 |
-
# Create
|
438 |
-
with gr.Blocks(title="SOC
|
439 |
|
440 |
-
# Header
|
441 |
gr.Markdown("""
|
442 |
-
# 🛡️ SOC LLM Assistant -
|
443 |
-
**
|
444 |
|
445 |
-
*
|
446 |
""")
|
447 |
|
448 |
# Model status display
|
449 |
-
gr.Markdown(
|
450 |
|
451 |
with gr.Row():
|
452 |
-
# Left Panel
|
453 |
with gr.Column(scale=1, min_width=300):
|
454 |
-
gr.Markdown("## 🎮 Attack Simulation
|
455 |
|
456 |
scenario_dropdown = gr.Dropdown(
|
457 |
choices=list(ATTACK_SCENARIOS.keys()),
|
@@ -470,91 +551,86 @@ with gr.Blocks(title="SOC LLM Assistant - Advanced PoC", theme=gr.themes.Soft(),
|
|
470 |
maximum=2,
|
471 |
step=1,
|
472 |
value=0,
|
473 |
-
label="📋 Alert Selection"
|
474 |
-
info="Choose which alert from the scenario to analyze"
|
475 |
)
|
476 |
|
477 |
analyst_level = gr.Radio(
|
478 |
choices=["L1", "L2", "L3"],
|
479 |
label="👤 Analyst Level",
|
480 |
value="L2",
|
481 |
-
info="L1: Triage | L2: Investigation | L3: Expert
|
482 |
)
|
483 |
|
484 |
analyze_btn = gr.Button(
|
485 |
-
"
|
486 |
variant="primary",
|
487 |
size="lg"
|
488 |
)
|
489 |
|
|
|
|
|
|
|
|
|
|
|
490 |
gr.Markdown("---")
|
491 |
-
gr.Markdown("##
|
492 |
gr.Markdown("""
|
493 |
-
**🎯
|
494 |
-
-
|
495 |
-
-
|
496 |
-
-
|
497 |
-
-
|
498 |
-
-
|
|
|
499 |
""")
|
500 |
|
501 |
-
# Right Panel
|
502 |
with gr.Column(scale=2):
|
503 |
gr.Markdown("## 📋 Security Alert Details")
|
504 |
alert_output = gr.Textbox(
|
505 |
-
label="🎫
|
506 |
lines=15,
|
507 |
-
interactive=False
|
508 |
-
placeholder="Alert details will appear here after analysis..."
|
509 |
)
|
510 |
|
511 |
-
gr.Markdown("## 🤖
|
512 |
analysis_output = gr.Textbox(
|
513 |
-
label="🧠
|
514 |
-
lines=
|
515 |
-
interactive=False
|
516 |
-
placeholder="LLM analysis will appear here after processing..."
|
517 |
)
|
518 |
|
519 |
status_output = gr.Textbox(
|
520 |
-
label="📊 Processing Status",
|
521 |
-
|
522 |
-
|
523 |
)
|
524 |
|
525 |
-
# Footer information
|
526 |
gr.Markdown("""
|
527 |
---
|
528 |
-
##
|
529 |
|
530 |
-
|
531 |
-
2. **🎯 Pick Alert:** Use the slider to select which alert in the sequence to analyze
|
532 |
-
3. **👤 Choose Level:** Select analyst expertise level (L1/L2/L3) for tailored analysis
|
533 |
-
4. **🔍 Analyze:** Click the analyze button to get AI-powered insights and recommendations
|
534 |
|
535 |
-
|
|
|
|
|
|
|
|
|
536 |
|
537 |
-
|
538 |
-
- **🧠 Contextual Analysis:** LLM considers all available metadata, threat intelligence, and historical patterns
|
539 |
-
- **👥 Role-Based Insights:** Tailored recommendations for different SOC analyst skill levels
|
540 |
-
- **⚡ Real-Time Processing:** Immediate analysis with actionable next steps
|
541 |
-
- **🎪 Industry Standards:** MITRE ATT&CK framework integration for standardized threat classification
|
542 |
-
|
543 |
-
## 🔬 **Research Value:**
|
544 |
-
This PoC demonstrates the feasibility of LLM integration in operational security environments, supporting research in automated threat analysis, human-AI collaboration, and intelligent SOC operations.
|
545 |
|
546 |
---
|
547 |
-
**👨🎓
|
548 |
""")
|
549 |
|
550 |
-
# Event handlers
|
551 |
scenario_dropdown.change(
|
552 |
-
fn=
|
553 |
inputs=[scenario_dropdown],
|
554 |
outputs=[scenario_info]
|
555 |
)
|
556 |
|
557 |
-
# Update slider maximum based on scenario
|
558 |
def update_slider_max(scenario_name):
|
559 |
if scenario_name in ATTACK_SCENARIOS:
|
560 |
max_alerts = len(ATTACK_SCENARIOS[scenario_name]["alerts"]) - 1
|
@@ -568,19 +644,28 @@ with gr.Blocks(title="SOC LLM Assistant - Advanced PoC", theme=gr.themes.Soft(),
|
|
568 |
)
|
569 |
|
570 |
analyze_btn.click(
|
571 |
-
fn=
|
572 |
inputs=[scenario_dropdown, alert_slider, analyst_level],
|
573 |
outputs=[alert_output, analysis_output, status_output]
|
574 |
)
|
575 |
|
576 |
-
|
|
|
|
|
|
|
|
|
|
|
577 |
demo.load(
|
578 |
-
fn=
|
579 |
inputs=[scenario_dropdown],
|
580 |
outputs=[scenario_info]
|
581 |
)
|
|
|
|
|
|
|
|
|
|
|
582 |
|
583 |
-
# Launch configuration
|
584 |
if __name__ == "__main__":
|
585 |
demo.launch(
|
586 |
share=True,
|
|
|
3 |
import json
|
4 |
import datetime
|
5 |
import random
|
6 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
7 |
import torch
|
8 |
import time
|
9 |
|
|
|
26 |
padding: 10px;
|
27 |
border-radius: 5px;
|
28 |
}
|
29 |
+
.gpt-oss-badge {
|
30 |
+
background: linear-gradient(45deg, #00c6ff, #0072ff);
|
31 |
+
color: white;
|
32 |
+
padding: 5px 10px;
|
33 |
+
border-radius: 15px;
|
34 |
+
font-weight: bold;
|
35 |
}
|
36 |
"""
|
37 |
|
38 |
+
# Global variables for model management
|
39 |
+
model = None
|
40 |
+
tokenizer = None
|
41 |
+
model_status = "🔄 Initializing GPT-OSS-20B..."
|
42 |
+
|
43 |
+
# Initialize GPT-OSS-20B with proper harmony format
|
44 |
@spaces.GPU
|
45 |
+
def initialize_gpt_oss():
|
46 |
+
"""Initialize OpenAI GPT-OSS-20B with harmony response format"""
|
47 |
+
global model, tokenizer, model_status
|
48 |
+
|
49 |
try:
|
50 |
+
model_id = "openai/gpt-oss-20b"
|
51 |
+
print(f"🚀 Loading {model_id}...")
|
52 |
+
|
53 |
# Check GPU availability
|
54 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
55 |
+
print(f"Device: {device}")
|
56 |
+
|
57 |
+
if torch.cuda.is_available():
|
58 |
+
print(f"GPU: {torch.cuda.get_device_name()}")
|
59 |
+
print(f"GPU Memory: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f}GB")
|
60 |
+
|
61 |
+
# Load tokenizer
|
62 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
63 |
+
print("✅ Tokenizer loaded")
|
64 |
+
|
65 |
+
# Load model with optimized settings for zeroGPU
|
66 |
+
model = AutoModelForCausalLM.from_pretrained(
|
67 |
+
model_id,
|
68 |
+
torch_dtype="auto", # Let it choose best dtype (MXFP4)
|
69 |
+
device_map="auto", # Automatic GPU placement
|
70 |
+
trust_remote_code=True,
|
71 |
+
low_cpu_mem_usage=True,
|
72 |
+
# MXFP4 quantization is built-in
|
73 |
+
)
|
74 |
|
75 |
+
print("✅ Model loaded with MXFP4 quantization")
|
76 |
+
print(f"Model device: {next(model.parameters()).device}")
|
77 |
+
|
78 |
+
# Test generation to ensure everything works
|
79 |
+
test_messages = [
|
80 |
+
{"role": "user", "content": "Hello, test message."}
|
81 |
+
]
|
82 |
+
|
83 |
+
test_inputs = tokenizer.apply_chat_template(
|
84 |
+
test_messages,
|
85 |
+
add_generation_prompt=True,
|
86 |
+
return_tensors="pt",
|
87 |
+
return_dict=True,
|
88 |
)
|
89 |
+
|
90 |
+
if device == "cuda":
|
91 |
+
test_inputs = {k: v.to(model.device) for k, v in test_inputs.items()}
|
92 |
+
|
93 |
+
with torch.no_grad():
|
94 |
+
test_output = model.generate(
|
95 |
+
**test_inputs,
|
96 |
+
max_new_tokens=10,
|
97 |
+
do_sample=False,
|
98 |
+
pad_token_id=tokenizer.eos_token_id
|
99 |
+
)
|
100 |
+
|
101 |
+
print("✅ Test generation successful")
|
102 |
+
|
103 |
+
model_status = f"✅ OpenAI GPT-OSS-20B loaded successfully on {device} | MXFP4 Quantized | ~16GB Memory"
|
104 |
+
return model_status
|
105 |
+
|
106 |
except Exception as e:
|
107 |
+
error_msg = f"❌ Failed to load GPT-OSS-20B: {str(e)}"
|
108 |
+
print(error_msg)
|
109 |
+
model_status = error_msg
|
110 |
+
model, tokenizer = None, None
|
111 |
+
return model_status
|
112 |
|
113 |
+
# Enhanced attack scenarios
|
114 |
ATTACK_SCENARIOS = {
|
115 |
"🔄 Lateral Movement": {
|
116 |
"description": "Advanced Persistent Threat (APT) - Attacker moving laterally through network after initial compromise",
|
|
|
118 |
"alerts": [
|
119 |
{
|
120 |
"id": "ALR-001",
|
121 |
+
"timestamp": "2025-08-10 14:30:45",
|
122 |
"source_ip": "192.168.1.100",
|
123 |
"destination_ip": "192.168.1.25",
|
124 |
"user": "corp\\john.doe",
|
|
|
132 |
},
|
133 |
{
|
134 |
"id": "ALR-002",
|
135 |
+
"timestamp": "2025-08-10 14:35:12",
|
136 |
"source_ip": "192.168.1.100",
|
137 |
"destination_ip": "192.168.1.50",
|
138 |
"user": "corp\\john.doe",
|
|
|
146 |
},
|
147 |
{
|
148 |
"id": "ALR-003",
|
149 |
+
"timestamp": "2025-08-10 14:42:18",
|
150 |
"source_ip": "192.168.1.100",
|
151 |
"destination_ip": "10.0.0.15",
|
152 |
"user": "SYSTEM",
|
|
|
166 |
"alerts": [
|
167 |
{
|
168 |
"id": "ALR-004",
|
169 |
+
"timestamp": "2025-08-10 09:15:30",
|
170 |
"source_ip": "203.0.113.50",
|
171 |
"destination_ip": "192.168.1.75",
|
172 |
"user": "corp\\sarah.wilson",
|
|
|
180 |
},
|
181 |
{
|
182 |
"id": "ALR-005",
|
183 |
+
"timestamp": "2025-08-10 09:45:22",
|
184 |
"source_ip": "192.168.1.75",
|
185 |
"destination_ip": "203.0.113.50",
|
186 |
"user": "corp\\sarah.wilson",
|
|
|
200 |
"alerts": [
|
201 |
{
|
202 |
"id": "ALR-006",
|
203 |
+
"timestamp": "2025-08-10 16:20:10",
|
204 |
"source_ip": "192.168.1.85",
|
205 |
"destination_ip": "192.168.1.85",
|
206 |
"user": "corp\\admin.backup",
|
|
|
214 |
},
|
215 |
{
|
216 |
"id": "ALR-007",
|
217 |
+
"timestamp": "2025-08-10 16:25:33",
|
218 |
"source_ip": "192.168.1.85",
|
219 |
"destination_ip": "45.33.22.11",
|
220 |
"user": "SYSTEM",
|
|
|
231 |
}
|
232 |
|
233 |
@spaces.GPU
|
234 |
+
def generate_gpt_oss_analysis(alert_data, analyst_level):
|
235 |
+
"""Generate analysis using OpenAI GPT-OSS-20B with harmony format"""
|
236 |
|
237 |
+
if not model or not tokenizer:
|
238 |
+
return get_fallback_analysis(alert_data, analyst_level)
|
239 |
+
|
240 |
+
# Enhanced prompts designed for GPT-OSS reasoning capabilities
|
241 |
+
security_prompts = {
|
242 |
+
"L1": f"""You are a Level 1 SOC analyst conducting initial triage. Analyze this security alert and provide immediate actionable recommendations.
|
243 |
+
|
244 |
+
**SECURITY ALERT:**
|
245 |
+
- ID: {alert_data['id']}
|
246 |
+
- Type: {alert_data['alert_type']}
|
247 |
+
- Severity: {alert_data['severity']}
|
248 |
+
- Source: {alert_data['source_ip']} → {alert_data['destination_ip']}
|
249 |
+
- User: {alert_data['user']}
|
250 |
+
- Evidence: {alert_data['raw_log']}
|
251 |
+
- Intel: {alert_data['threat_intel']}
|
252 |
+
- MITRE ATT&CK: {alert_data['mitre_tactic']}
|
253 |
+
- Confidence: {alert_data['confidence']}%
|
254 |
+
|
255 |
+
**PROVIDE L1 TRIAGE:**
|
256 |
+
1. Immediate containment actions
|
257 |
+
2. Risk assessment
|
258 |
+
3. Escalation decision with reasoning
|
259 |
+
4. Priority timeline
|
260 |
+
|
261 |
+
Think step-by-step about the threat level and required response.""",
|
262 |
+
|
263 |
+
"L2": f"""You are a Level 2 SOC analyst conducting detailed investigation. Perform comprehensive analysis of this cybersecurity incident.
|
264 |
+
|
265 |
+
**INCIDENT DETAILS:**
|
266 |
+
- Alert: {alert_data['alert_type']} | Severity: {alert_data['severity']}
|
267 |
+
- Network Flow: {alert_data['source_ip']} → {alert_data['destination_ip']}
|
268 |
+
- User Context: {alert_data['user']}
|
269 |
+
- Technical Evidence: {alert_data['raw_log']}
|
270 |
+
- Threat Intelligence: {alert_data['threat_intel']}
|
271 |
+
- MITRE ATT&CK Technique: {alert_data['mitre_tactic']}
|
272 |
+
- Detection Confidence: {alert_data['confidence']}%
|
273 |
+
|
274 |
+
**CONDUCT L2 INVESTIGATION:**
|
275 |
+
1. Technical root cause analysis
|
276 |
+
2. Evidence correlation and timeline
|
277 |
+
3. Threat actor behavior analysis
|
278 |
+
4. Impact assessment and containment strategy
|
279 |
+
5. Investigation roadmap
|
280 |
+
|
281 |
+
Use chain-of-thought reasoning to analyze the attack progression and recommend next steps.""",
|
282 |
+
|
283 |
+
"L3": f"""You are a senior cybersecurity expert analyzing a sophisticated threat. Provide strategic assessment and executive-level recommendations.
|
284 |
+
|
285 |
+
**THREAT INTELLIGENCE:**
|
286 |
+
- Attack Vector: {alert_data['description']}
|
287 |
+
- Technical Indicators: {alert_data['raw_log']}
|
288 |
+
- Attribution Context: {alert_data['threat_intel']}
|
289 |
+
- MITRE Technique: {alert_data['mitre_tactic']}
|
290 |
+
- Confidence Level: {alert_data['confidence']}%
|
291 |
+
|
292 |
+
**DELIVER L3 EXPERT ANALYSIS:**
|
293 |
+
1. Adversary attribution and campaign analysis
|
294 |
+
2. Strategic threat landscape assessment
|
295 |
+
3. Business impact and risk quantification
|
296 |
+
4. Comprehensive response strategy
|
297 |
+
5. Executive briefing points
|
298 |
+
|
299 |
+
Apply deep reasoning to assess the broader implications and provide strategic recommendations."""
|
300 |
+
}
|
301 |
+
|
302 |
+
try:
|
303 |
+
prompt = security_prompts.get(analyst_level, security_prompts["L2"])
|
304 |
+
|
305 |
+
# Use proper harmony format for chat
|
306 |
+
messages = [
|
307 |
+
{"role": "user", "content": prompt}
|
308 |
+
]
|
309 |
+
|
310 |
+
# Apply chat template (automatically uses harmony format)
|
311 |
+
inputs = tokenizer.apply_chat_template(
|
312 |
+
messages,
|
313 |
+
add_generation_prompt=True,
|
314 |
+
return_tensors="pt",
|
315 |
+
return_dict=True,
|
316 |
+
)
|
317 |
+
|
318 |
+
# Move to device if using GPU
|
319 |
+
if torch.cuda.is_available():
|
320 |
+
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
321 |
+
|
322 |
+
# Generate with optimized parameters for reasoning
|
323 |
+
with torch.no_grad():
|
324 |
+
outputs = model.generate(
|
325 |
+
**inputs,
|
326 |
+
max_new_tokens=500,
|
327 |
+
do_sample=True,
|
328 |
+
temperature=0.2, # Lower for focused analysis
|
329 |
top_p=0.9,
|
330 |
+
top_k=50,
|
331 |
+
repetition_penalty=1.1,
|
332 |
+
pad_token_id=tokenizer.eos_token_id,
|
333 |
+
eos_token_id=tokenizer.eos_token_id
|
334 |
)
|
335 |
+
|
336 |
+
# Decode the response
|
337 |
+
input_length = inputs["input_ids"].shape[-1]
|
338 |
+
generated_tokens = outputs[0][input_length:]
|
339 |
+
analysis = tokenizer.decode(generated_tokens, skip_special_tokens=True)
|
340 |
+
|
341 |
+
# Ensure quality
|
342 |
+
if len(analysis.strip()) < 100:
|
343 |
+
return get_fallback_analysis(alert_data, analyst_level)
|
344 |
+
|
345 |
+
return f"""🤖 **OpenAI GPT-OSS-20B Analysis**
|
346 |
+
<div class="gpt-oss-badge">Powered by GPT-OSS-20B • MoE Architecture • MXFP4 Quantized</div>
|
347 |
+
|
348 |
+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
349 |
+
|
350 |
+
{analysis.strip()}
|
351 |
+
|
352 |
+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
353 |
+
*Analysis generated using OpenAI's latest open-weight reasoning model*
|
354 |
+
*21B parameters • 3.6B active per token • Apache 2.0 licensed*"""
|
355 |
+
|
356 |
+
except Exception as e:
|
357 |
+
print(f"GPT-OSS Error: {e}")
|
358 |
+
return f"⚠️ GPT-OSS Error: {str(e)[:100]}\n\n{get_fallback_analysis(alert_data, analyst_level)}"
|
359 |
|
360 |
def get_fallback_analysis(alert_data, analyst_level):
|
361 |
+
"""High-quality fallback when model fails"""
|
362 |
|
363 |
+
templates = {
|
364 |
+
"L1": f"""🚨 **L1 SOC TRIAGE ANALYSIS**
|
|
|
|
|
|
|
|
|
|
|
365 |
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
366 |
+
|
367 |
+
**🎯 THREAT ASSESSMENT:**
|
368 |
+
Alert: {alert_data['alert_type']} | Severity: {alert_data['severity']}
|
369 |
+
Confidence: {alert_data['confidence']}% | Source: {alert_data['source_ip']}
|
370 |
+
|
371 |
+
**⚡ IMMEDIATE ACTIONS:**
|
372 |
+
1. Isolate affected system: {alert_data['source_ip']}
|
373 |
+
2. Disable user account: {alert_data['user']}
|
374 |
+
3. Block connections to: {alert_data['destination_ip']}
|
375 |
+
4. Preserve evidence for investigation
|
376 |
+
|
377 |
+
**⬆️ ESCALATION DECISION:**
|
378 |
+
Severity: {alert_data['severity']} → ESCALATE TO L2
|
379 |
+
Technique: {alert_data['mitre_tactic']} requires deeper analysis
|
380 |
+
|
381 |
+
**📝 INITIAL ASSESSMENT:**
|
382 |
+
{alert_data['threat_intel']}""",
|
383 |
+
|
384 |
+
"L2": f"""🔍 **L2 INVESTIGATION ANALYSIS**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
385 |
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
386 |
+
|
387 |
+
**🎯 ATTACK VECTOR ANALYSIS:**
|
388 |
+
Technique: {alert_data['mitre_tactic']}
|
389 |
+
Evidence: {alert_data['raw_log']}
|
390 |
+
Context: {alert_data['description']}
|
391 |
+
|
392 |
+
**🔬 INVESTIGATION ROADMAP:**
|
393 |
+
1. Timeline correlation: ±30min from {alert_data['timestamp']}
|
394 |
+
2. User behavior analysis: {alert_data['user']} baseline comparison
|
395 |
+
3. Network flow analysis: {alert_data['source_ip']} → {alert_data['destination_ip']}
|
396 |
+
4. Process tree examination: Parent/child relationships
|
397 |
+
5. Artifact collection: Memory dumps, logs, files
|
398 |
+
|
399 |
+
**📊 THREAT ASSESSMENT:**
|
400 |
+
Confidence Level: {alert_data['confidence']}%
|
401 |
+
Business Impact: {alert_data['severity']}
|
402 |
+
Attribution: {alert_data['threat_intel']}
|
403 |
+
|
404 |
+
**🎯 RECOMMENDATIONS:**
|
405 |
+
Deploy hunting queries for similar TTPs
|
406 |
+
Review authentication logs for compromise
|
407 |
+
Consider L3 escalation if campaign indicators found""",
|
408 |
+
|
409 |
+
"L3": f"""🎯 **L3 EXPERT STRATEGIC ANALYSIS**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
410 |
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
411 |
+
|
412 |
+
**🎭 ADVERSARY ASSESSMENT:**
|
413 |
+
Sophistication: Advanced (based on {alert_data['mitre_tactic']})
|
414 |
+
Campaign Context: {alert_data['threat_intel']}
|
415 |
+
Success Probability: {alert_data['confidence']}%
|
416 |
+
|
417 |
+
**💼 BUSINESS IMPACT:**
|
418 |
+
Severity Level: {alert_data['severity']}
|
419 |
+
Executive Notification: Required
|
420 |
+
Regulatory Implications: Under review
|
421 |
+
|
422 |
+
**🛡️ STRATEGIC RESPONSE:**
|
423 |
+
Immediate: Threat hunting deployment across environment
|
424 |
+
Short-term: Enhanced monitoring and detection rules
|
425 |
+
Medium-term: Security architecture review
|
426 |
+
Long-term: Threat intelligence integration enhancement
|
427 |
+
|
428 |
+
**📈 EXECUTIVE BRIEFING:**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
429 |
• Sophisticated attack requiring coordinated response
|
430 |
+
• High potential for lateral movement and data exfiltration
|
431 |
• Recommend incident response team activation
|
432 |
+
• Consider external forensics support engagement"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
433 |
}
|
434 |
|
435 |
+
return templates.get(analyst_level, templates["L2"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
436 |
|
437 |
+
def analyze_alert_with_gpt_oss(scenario_name, alert_index, analyst_level):
|
438 |
+
"""Main analysis function using GPT-OSS-20B"""
|
439 |
start_time = time.time()
|
440 |
|
|
|
441 |
if scenario_name not in ATTACK_SCENARIOS:
|
442 |
return "❌ Invalid scenario selected.", "", "Error: Invalid scenario"
|
443 |
|
|
|
449 |
|
450 |
selected_alert = alerts[alert_index]
|
451 |
|
452 |
+
# Generate analysis using GPT-OSS-20B
|
453 |
+
analysis = generate_gpt_oss_analysis(selected_alert, analyst_level)
|
454 |
|
455 |
+
# Format alert details
|
456 |
+
alert_details = f"""🎫 **ALERT {selected_alert['id']}** | 🕐 {selected_alert['timestamp']}
|
457 |
+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
|
458 |
|
459 |
+
🌐 **NETWORK FLOW:**
|
460 |
Source: {selected_alert['source_ip']} → Destination: {selected_alert['destination_ip']}
|
461 |
|
462 |
+
👤 **USER CONTEXT:**
|
463 |
Account: {selected_alert['user']}
|
464 |
|
465 |
+
⚠️ **CLASSIFICATION:**
|
466 |
Type: {selected_alert['alert_type']}
|
467 |
Severity: {selected_alert['severity']}
|
468 |
Confidence: {selected_alert['confidence']}%
|
469 |
|
470 |
+
📝 **DESCRIPTION:**
|
471 |
{selected_alert['description']}
|
472 |
|
473 |
+
🔍 **TECHNICAL EVIDENCE:**
|
474 |
{selected_alert['raw_log']}
|
475 |
|
476 |
+
🧠 **THREAT INTELLIGENCE:**
|
477 |
{selected_alert['threat_intel']}
|
478 |
|
479 |
+
🎪 **MITRE ATT&CK:**
|
480 |
{selected_alert['mitre_tactic']}
|
481 |
|
482 |
+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"""
|
|
|
483 |
|
484 |
processing_time = round(time.time() - start_time, 2)
|
485 |
+
device_info = "GPU" if torch.cuda.is_available() else "CPU"
|
486 |
+
status = f"✅ {analyst_level} analysis completed in {processing_time}s | Device: {device_info} | {model_status}"
|
487 |
|
488 |
+
return alert_details, analysis, status
|
489 |
|
490 |
+
def get_scenario_info(scenario_name):
|
491 |
+
"""Get scenario information"""
|
492 |
if scenario_name in ATTACK_SCENARIOS:
|
493 |
scenario = ATTACK_SCENARIOS[scenario_name]
|
494 |
|
495 |
+
info = f"""## 🎭 **Attack Scenario: {scenario_name}**
|
|
|
496 |
|
497 |
**📋 Description:** {scenario['description']}
|
498 |
+
**⚠️ Severity:** {scenario['severity']}
|
499 |
+
**📊 Total Alerts:** {len(scenario['alerts'])} security events
|
500 |
|
501 |
### 🔍 **Alert Timeline:**
|
502 |
"""
|
503 |
|
504 |
for i, alert in enumerate(scenario['alerts']):
|
505 |
+
info += f"""**[{i+1}] {alert['timestamp']}** - {alert['alert_type']}
|
|
|
506 |
└─ Severity: {alert['severity']} | Confidence: {alert['confidence']}%
|
507 |
"""
|
508 |
|
509 |
+
info += """
|
510 |
+
### 🤖 **AI-Powered Analysis:**
|
511 |
+
- **OpenAI GPT-OSS-20B:** Latest open-weight reasoning model
|
512 |
+
- **MXFP4 Quantization:** Optimized for efficient inference
|
513 |
+
- **Harmony Format:** Advanced response structure
|
514 |
+
- **21B Parameters:** With 3.6B active per token (MoE)"""
|
515 |
|
516 |
return info
|
517 |
+
return "⚠️ No scenario selected."
|
518 |
|
519 |
+
# Create Gradio interface
|
520 |
+
with gr.Blocks(title="SOC Assistant - GPT-OSS-20B", theme=gr.themes.Soft(), css=custom_css) as demo:
|
521 |
|
|
|
522 |
gr.Markdown("""
|
523 |
+
# 🛡️ SOC LLM Assistant - OpenAI GPT-OSS-20B Edition
|
524 |
+
**Powered by OpenAI's Latest Open-Weight Reasoning Model**
|
525 |
|
526 |
+
*First open-weight model from OpenAI since GPT-2 • Released August 8, 2025*
|
527 |
""")
|
528 |
|
529 |
# Model status display
|
530 |
+
status_display = gr.Markdown("🔄 Loading OpenAI GPT-OSS-20B...")
|
531 |
|
532 |
with gr.Row():
|
533 |
+
# Left Panel
|
534 |
with gr.Column(scale=1, min_width=300):
|
535 |
+
gr.Markdown("## 🎮 Attack Simulation")
|
536 |
|
537 |
scenario_dropdown = gr.Dropdown(
|
538 |
choices=list(ATTACK_SCENARIOS.keys()),
|
|
|
551 |
maximum=2,
|
552 |
step=1,
|
553 |
value=0,
|
554 |
+
label="📋 Alert Selection"
|
|
|
555 |
)
|
556 |
|
557 |
analyst_level = gr.Radio(
|
558 |
choices=["L1", "L2", "L3"],
|
559 |
label="👤 Analyst Level",
|
560 |
value="L2",
|
561 |
+
info="L1: Triage | L2: Investigation | L3: Expert"
|
562 |
)
|
563 |
|
564 |
analyze_btn = gr.Button(
|
565 |
+
"🚀 Analyze with GPT-OSS-20B",
|
566 |
variant="primary",
|
567 |
size="lg"
|
568 |
)
|
569 |
|
570 |
+
init_btn = gr.Button(
|
571 |
+
"🔄 Reinitialize Model",
|
572 |
+
variant="secondary"
|
573 |
+
)
|
574 |
+
|
575 |
gr.Markdown("---")
|
576 |
+
gr.Markdown("## 🤖 Model Information")
|
577 |
gr.Markdown("""
|
578 |
+
**🎯 GPT-OSS-20B Features:**
|
579 |
+
- 21B parameters (3.6B active)
|
580 |
+
- MXFP4 quantization
|
581 |
+
- 128K context length
|
582 |
+
- Apache 2.0 licensed
|
583 |
+
- Harmony response format
|
584 |
+
- Reasoning capabilities
|
585 |
""")
|
586 |
|
587 |
+
# Right Panel
|
588 |
with gr.Column(scale=2):
|
589 |
gr.Markdown("## 📋 Security Alert Details")
|
590 |
alert_output = gr.Textbox(
|
591 |
+
label="🎫 Alert Information",
|
592 |
lines=15,
|
593 |
+
interactive=False
|
|
|
594 |
)
|
595 |
|
596 |
+
gr.Markdown("## 🤖 GPT-OSS-20B Analysis")
|
597 |
analysis_output = gr.Textbox(
|
598 |
+
label="🧠 AI-Powered Security Analysis",
|
599 |
+
lines=25,
|
600 |
+
interactive=False
|
|
|
601 |
)
|
602 |
|
603 |
status_output = gr.Textbox(
|
604 |
+
label="📊 Processing Status",
|
605 |
+
lines=1,
|
606 |
+
interactive=False
|
607 |
)
|
608 |
|
|
|
609 |
gr.Markdown("""
|
610 |
---
|
611 |
+
## 🎉 **About OpenAI GPT-OSS-20B**
|
612 |
|
613 |
+
Released August 8, 2025 - OpenAI's first open-weight model since GPT-2! This groundbreaking release features:
|
|
|
|
|
|
|
614 |
|
615 |
+
- **🧠 Advanced Reasoning:** Comparable to o3-mini performance
|
616 |
+
- **⚡ Efficient Architecture:** MoE with only 3.6B active parameters per token
|
617 |
+
- **🔧 Harmony Format:** New structured response system for better tool use
|
618 |
+
- **📱 Consumer Hardware:** Runs on just 16GB memory
|
619 |
+
- **🔓 Open License:** Apache 2.0 - fully permissive for commercial use
|
620 |
|
621 |
+
Perfect for cybersecurity analysis requiring sophisticated reasoning and chain-of-thought capabilities!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
622 |
|
623 |
---
|
624 |
+
**👨🎓 Research:** Abdullah Alanazi | **🏛️ KAUST** | **👨🏫 Prof. Ali Shoker**
|
625 |
""")
|
626 |
|
627 |
+
# Event handlers
|
628 |
scenario_dropdown.change(
|
629 |
+
fn=get_scenario_info,
|
630 |
inputs=[scenario_dropdown],
|
631 |
outputs=[scenario_info]
|
632 |
)
|
633 |
|
|
|
634 |
def update_slider_max(scenario_name):
|
635 |
if scenario_name in ATTACK_SCENARIOS:
|
636 |
max_alerts = len(ATTACK_SCENARIOS[scenario_name]["alerts"]) - 1
|
|
|
644 |
)
|
645 |
|
646 |
analyze_btn.click(
|
647 |
+
fn=analyze_alert_with_gpt_oss,
|
648 |
inputs=[scenario_dropdown, alert_slider, analyst_level],
|
649 |
outputs=[alert_output, analysis_output, status_output]
|
650 |
)
|
651 |
|
652 |
+
init_btn.click(
|
653 |
+
fn=initialize_gpt_oss,
|
654 |
+
outputs=[status_display]
|
655 |
+
)
|
656 |
+
|
657 |
+
# Initialize on startup
|
658 |
demo.load(
|
659 |
+
fn=get_scenario_info,
|
660 |
inputs=[scenario_dropdown],
|
661 |
outputs=[scenario_info]
|
662 |
)
|
663 |
+
|
664 |
+
demo.load(
|
665 |
+
fn=initialize_gpt_oss,
|
666 |
+
outputs=[status_display]
|
667 |
+
)
|
668 |
|
|
|
669 |
if __name__ == "__main__":
|
670 |
demo.launch(
|
671 |
share=True,
|