abdull4h commited on
Commit
6755db5
·
verified ·
1 Parent(s): aabcc85

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +360 -275
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
- .status-warning {
30
- background: #fff3cd;
31
- border: 1px solid #ffeaa7;
32
- color: #856404;
33
- padding: 10px;
34
- border-radius: 5px;
35
  }
36
  """
37
 
38
- # Initialize the LLM pipeline with zeroGPU support
 
 
 
 
 
39
  @spaces.GPU
40
- def initialize_llm():
 
 
 
41
  try:
 
 
 
42
  # Check GPU availability
43
  device = "cuda" if torch.cuda.is_available() else "cpu"
44
- print(f"Using device: {device}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
- # Try to use a larger model with GPU acceleration
47
- model_id = "microsoft/DialoGPT-medium"
48
- pipe = pipeline(
49
- "text-generation",
50
- model=model_id,
51
- torch_dtype=torch.float16 if device == "cuda" else torch.float32,
52
- device_map="auto" if device == "cuda" else "cpu",
53
- max_length=512,
54
- pad_token_id=50256
 
 
 
 
55
  )
56
- return pipe, f"✅ LLM Model loaded on {device}: {model_id}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  except Exception as e:
58
- return None, f"⚠️ LLM not available: {str(e)[:100]}... Using fallback analysis."
59
-
60
- pipe, model_status = initialize_llm()
 
 
61
 
62
- # Enhanced attack scenarios with more realistic data
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-01-15 14:30:45",
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-01-15 14:35:12",
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-01-15 14:42:18",
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-01-15 09:15:30",
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-01-15 09:45:22",
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-01-15 16:20:10",
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-01-15 16:25:33",
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 generate_advanced_llm_analysis(alert_data, analyst_level):
184
- """Generate comprehensive LLM-based analysis with enhanced prompting and GPU acceleration"""
185
 
186
- # Enhanced context with more structured prompting
187
- system_context = f"""You are an expert cybersecurity analyst assistant specializing in SOC operations.
188
- Analyze the following security alert for a Level {analyst_level} analyst.
189
-
190
- ALERT CONTEXT:
191
- ID: {alert_data['id']}
192
- Type: {alert_data['alert_type']}
193
- Severity: {alert_data['severity']}
194
- Timestamp: {alert_data['timestamp']}
195
- Network: {alert_data['source_ip']} → {alert_data['destination_ip']}
196
- User: {alert_data['user']}
197
- Description: {alert_data['description']}
198
- Technical Details: {alert_data['raw_log']}
199
- Threat Intelligence: {alert_data['threat_intel']}
200
- MITRE ATT&CK: {alert_data['mitre_tactic']}
201
- Confidence: {alert_data['confidence']}%
202
-
203
- Provide analysis appropriate for {analyst_level} level:"""
204
-
205
- if pipe:
206
- try:
207
- # Use GPU acceleration for faster inference
208
- device = next(pipe.model.parameters()).device
209
- print(f"LLM running on device: {device}")
210
-
211
- prompt = f"{system_context}\n\nAnalysis:"
212
- response = pipe(
213
- prompt,
214
- max_new_tokens=300,
215
- do_sample=True,
216
- temperature=0.7,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217
  top_p=0.9,
218
- pad_token_id=pipe.tokenizer.eos_token_id
 
 
 
219
  )
220
- generated_text = response[0]['generated_text']
221
- analysis = generated_text[len(prompt):].strip()
222
- return analysis if analysis else get_fallback_analysis(alert_data, analyst_level)
223
- except Exception as e:
224
- print(f"LLM Error: {e}")
225
- return f"LLM Processing Error: {str(e)}\n\n{get_fallback_analysis(alert_data, analyst_level)}"
226
-
227
- return get_fallback_analysis(alert_data, analyst_level)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
228
 
229
  def get_fallback_analysis(alert_data, analyst_level):
230
- """Enhanced fallback analysis with detailed recommendations"""
231
 
232
- base_analysis = {
233
- "L1": {
234
- "icon": "🚨",
235
- "title": "L1 TRIAGE ANALYSIS",
236
- "focus": "Initial Assessment & Escalation",
237
- "template": """
238
- {icon} {title}
239
  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
240
- 🎯 THREAT SUMMARY: {alert_type} - {severity} severity
241
- OCCURRED: {timestamp}
242
- 🌐 AFFECTED SYSTEM: {source_ip} (User: {user})
243
- 🔍 CONFIDENCE LEVEL: {confidence}%
244
-
245
- 🚀 IMMEDIATE ACTIONS:
246
- Isolate affected system: {source_ip}
247
- Verify user account status: {user}
248
- Check for similar alerts in timeframe
249
- Document incident ID: {id}
250
-
251
- ⬆️ ESCALATION CRITERIA:
252
- Severity: {severity} - Meets L2 escalation threshold
253
- • MITRE Tactic: {mitre_tactic}
254
- • Recommend immediate L2 review
255
-
256
- 📋 INITIAL NOTES:
257
- {threat_intel}
258
- """
259
- },
260
- "L2": {
261
- "icon": "🔍",
262
- "title": "L2 INVESTIGATION ANALYSIS",
263
- "focus": "Detailed Investigation & Correlation",
264
- "template": """
265
- {icon} {title}
266
  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
267
- 🎯 ATTACK VECTOR: {description}
268
- ⚙️ TECHNICAL DETAILS: {raw_log}
269
- 🧠 THREAT CONTEXT: {threat_intel}
270
- 🎪 MITRE ATT&CK: {mitre_tactic}
271
-
272
- 🔬 INVESTIGATION STEPS:
273
- 1. Examine parent process tree for {source_ip}
274
- 2. Correlate network connections in ±30min window
275
- 3. Review authentication logs for user: {user}
276
- 4. Check for indicators across environment
277
- 5. Analyze file system changes (if applicable)
278
-
279
- 🎯 CORRELATION POINTS:
280
- Source IP timeline analysis
281
- User behavior baseline comparison
282
- Similar TTPs in recent incidents
283
- Network segmentation verification
284
-
285
- 📊 RISK ASSESSMENT:
286
- Technical Impact: {severity}
287
- Business Risk: Review asset criticality
288
- Containment Priority: High (based on {confidence}% confidence)
289
-
290
- ⬆️ L3 ESCALATION IF:
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
- 🎭 ADVERSARY PROFILE: Advanced threat actor
304
- 🎪 CAMPAIGN ANALYSIS: {threat_intel}
305
- 💼 BUSINESS IMPACT: {severity} - Requires C-level awareness
306
- 🛡️ DEFENSIVE POSTURE: Enhanced monitoring required
307
-
308
- 🕵️ THREAT HUNTING PRIORITIES:
309
- 1. Memory forensics on {source_ip}
310
- 2. Network traffic deep packet inspection
311
- 3. Endpoint artifact preservation
312
- 4. Active Directory security log analysis
313
- 5. Cloud infrastructure review (if applicable)
314
-
315
- 🎯 ATTRIBUTION INDICATORS:
316
- TTPs match: {mitre_tactic}
317
- Technical sophistication: High
318
- Targeting pattern: [Analyze organizational profile]
319
- • Infrastructure overlap: Review IOC databases
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
- Potential for lateral movement and data exfiltration
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
- if analyst_level in base_analysis:
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 analyze_alert_comprehensive(scenario_name, alert_index, analyst_level):
352
- """Enhanced main analysis function with timing and status updates"""
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 comprehensive analysis
368
- analysis = generate_advanced_llm_analysis(selected_alert, analyst_level)
369
 
370
- # Enhanced alert details formatting
371
- alert_details = f"""
372
- 🎫 ALERT ID: {selected_alert['id']} | 🕐 {selected_alert['timestamp']}
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
- ⚠️ ALERT CLASSIFICATION:
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 MAPPING:
396
  {selected_alert['mitre_tactic']}
397
 
398
- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
399
- """
400
 
401
  processing_time = round(time.time() - start_time, 2)
402
- status_message = f" {analyst_level} analysis completed in {processing_time}s | Model: {model_status}"
 
403
 
404
- return alert_details, analysis, status_message
405
 
406
- def get_enhanced_scenario_info(scenario_name):
407
- """Enhanced scenario information with threat overview"""
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 Level:** {scenario['severity']}
416
- **📊 Total Alerts:** {len(scenario['alerts'])} security events detected
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 += f"""
428
- ### 🎯 **Analysis Capabilities:**
429
- - **L1 Triage:** Initial assessment and escalation decisions
430
- - **L2 Investigation:** Detailed technical analysis and correlation
431
- - **L3 Expert:** Attribution, impact assessment, and strategic response
432
- """
433
 
434
  return info
435
- return "⚠️ No scenario selected. Please choose an attack scenario to begin analysis."
436
 
437
- # Create enhanced Gradio interface
438
- with gr.Blocks(title="SOC LLM Assistant - Advanced PoC", theme=gr.themes.Soft(), css=custom_css) as demo:
439
 
440
- # Header
441
  gr.Markdown("""
442
- # 🛡️ SOC LLM Assistant - Advanced Proof of Concept
443
- **Intelligent Security Alert Analysis for Multi-Level SOC Operations**
444
 
445
- *Demonstrating LLM-powered assistance for L1, L2, and L3 security analysts*
446
  """)
447
 
448
  # Model status display
449
- gr.Markdown(f"🤖 **System Status:** {model_status}")
450
 
451
  with gr.Row():
452
- # Left Panel - Controls
453
  with gr.Column(scale=1, min_width=300):
454
- gr.Markdown("## 🎮 Attack Simulation Control")
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 Analysis"
482
  )
483
 
484
  analyze_btn = gr.Button(
485
- "🔍 Analyze Alert",
486
  variant="primary",
487
  size="lg"
488
  )
489
 
 
 
 
 
 
490
  gr.Markdown("---")
491
- gr.Markdown("## 📊 Quick Stats")
492
  gr.Markdown("""
493
- **🎯 Demo Features:**
494
- - 3 realistic attack scenarios
495
- - Multi-level analysis (L1/L2/L3)
496
- - MITRE ATT&CK mapping
497
- - Threat intelligence integration
498
- - Real-time LLM processing
 
499
  """)
500
 
501
- # Right Panel - Results
502
  with gr.Column(scale=2):
503
  gr.Markdown("## 📋 Security Alert Details")
504
  alert_output = gr.Textbox(
505
- label="🎫 Raw Alert Information",
506
  lines=15,
507
- interactive=False,
508
- placeholder="Alert details will appear here after analysis..."
509
  )
510
 
511
- gr.Markdown("## 🤖 AI-Powered Analysis")
512
  analysis_output = gr.Textbox(
513
- label="🧠 Intelligent Analysis & Recommendations",
514
- lines=20,
515
- interactive=False,
516
- placeholder="LLM analysis will appear here after processing..."
517
  )
518
 
519
  status_output = gr.Textbox(
520
- label="📊 Processing Status",
521
- interactive=False,
522
- lines=1
523
  )
524
 
525
- # Footer information
526
  gr.Markdown("""
527
  ---
528
- ## 📖 **Usage Instructions:**
529
 
530
- 1. **📊 Select Scenario:** Choose from realistic cybersecurity attack scenarios
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
- ## 🎯 **Key Capabilities Demonstrated:**
 
 
 
 
536
 
537
- - **🎭 Realistic Scenarios:** Based on actual cybersecurity incidents and attack patterns
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
- **👨‍🎓 Developed by:** Abdullah Alanazi | **🏛️ Institution:** KAUST | **👨‍🏫 Supervisor:** Prof. Ali Shoker
548
  """)
549
 
550
- # Event handlers with enhanced functionality
551
  scenario_dropdown.change(
552
- fn=get_enhanced_scenario_info,
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=analyze_alert_comprehensive,
572
  inputs=[scenario_dropdown, alert_slider, analyst_level],
573
  outputs=[alert_output, analysis_output, status_output]
574
  )
575
 
576
- # Initialize with default scenario
 
 
 
 
 
577
  demo.load(
578
- fn=get_enhanced_scenario_info,
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,