File size: 12,444 Bytes
115612d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python
"""
Debug Script for OpenEnv-CloudSOC
==================================
Interactive debugging and exploration tool

Usage:
    python debug_cloudsoc.py [--task easy|medium|hard] [--seed 42]
"""

import json
import sys
from cloud_soc_env import CloudSOCEnv, InstanceState


def print_section(title):
    """Print a formatted section header"""
    print(f"\n{'='*60}")
    print(f"  {title}")
    print(f"{'='*60}\n")


def explore_environment(task="easy", seed=42):
    """Interactively explore the environment"""
    
    print_section(f"CloudSOC Environment: {task.upper()}")
    
    env = CloudSOCEnv(task=task, seed=seed)
    obs, info = env.reset()
    
    print(f"Scenario: {env.scenario['name']}")
    print(f"Description: {env.scenario['description']}")
    print(f"Max Steps: {env.max_steps}")
    print(f"Difficulty Multiplier: {env.scenario['difficulty_multiplier']}")
    print()
    
    # =========================================================================
    # SECTION: INITIAL STATE
    # =========================================================================
    print_section("Initial Cloud State")
    
    # Instances
    print(f"EC2 Instances ({len(env.state.instances)}):")
    for iid, inst in env.state.instances.items():
        print(f"  {iid}")
        print(f"    State: {inst.state.value}")
        print(f"    Compromised: {inst.is_compromised}")
        print(f"    Roles: {inst.attached_roles}")
        print(f"    Has Snapshot: {inst.has_forensic_snapshot}")
    print()
    
    # IAM Roles
    print(f"IAM Roles ({len(env.state.roles)}):")
    for name, role in env.state.roles.items():
        print(f"  {name}")
        print(f"    Policies: {', '.join(role.policies[:2])}...")
        print(f"    Compromised: {role.is_compromised}")
        print(f"    Detached: {role.is_detached}")
        print(f"    Has Backdoor: {role.has_backdoor}")
    print()
    
    # S3 Buckets
    print(f"S3 Buckets ({len(env.state.buckets)}):")
    for name, bucket in env.state.buckets.items():
        print(f"  {name}")
        print(f"    Public: {bucket.is_public}")
        print(f"    Contains Credentials: {bucket.contains_credentials}")
        print(f"    Public Access Blocked: {bucket.public_access_blocked}")
    print()
    
    # =========================================================================
    # SECTION: ALERTS AND LOGS
    # =========================================================================
    print_section("Security Alerts and Indicators")
    
    print(f"SOC Alerts ({len(env.state.alerts)}):")
    for i, alert in enumerate(env.state.alerts[:5], 1):
        print(f"  [{i}] {alert.alert_id}: {alert.title}")
        print(f"      Severity: {alert.severity} | Source: {alert.source}")
        print(f"      True Positive: {alert.is_true_positive}")
    print()
    
    print(f"CloudWatch Logs ({len(env.state.logs)} total):")
    
    # Categorize logs
    attack_logs = [log for log in env.state.logs if log.is_attack_indicator]
    red_herrings = [log for log in env.state.logs if log.is_red_herring]
    noise = [log for log in env.state.logs if log.is_noise]
    
    print(f"  Attack Indicators: {len(attack_logs)}")
    for log in attack_logs[:3]:
        print(f"    [{log.timestamp}] {log.message}")
    print()
    
    print(f"  Red Herrings: {len(red_herrings)}")
    for log in red_herrings[:2]:
        print(f"    [{log.timestamp}] {log.message}")
    print()
    
    print(f"  Noise: {len(noise)}")
    print(f"    (Normal operational logs and benign activity)")
    print()
    
    # =========================================================================
    # SECTION: INCIDENT RESPONSE OBJECTIVES
    # =========================================================================
    print_section("Incident Response Objectives")
    
    print(f"Phase Weights:")
    for phase, weight in env.scenario["phase_weights"].items():
        print(f"  {phase}: {weight:.0%}")
    print()
    
    print(f"Required Flags ({len(env.scenario['required_flags'])} to discover):")
    for flag in env.scenario["required_flags"]:
        print(f"  • {flag}")
    print()
    
    print(f"Ground Truth Timeline:")
    for i, event in enumerate(env.scenario["ground_truth_timeline"], 1):
        print(f"  {i}. {event}")
    print()
    
    if "mitre_techniques" in env.scenario:
        print(f"MITRE ATT&CK Techniques:")
        for tech in env.scenario["mitre_techniques"]:
            print(f"  • {tech}")
        print()
    
    # =========================================================================
    # SECTION: INTERACTIVE TOOL TESTING
    # =========================================================================
    print_section("Interactive Tool Testing")
    
    test_sequence = [
        ("Get SOC Alerts", "aws.soc.get_alerts", {}),
        ("Query CloudWatch Basic", "aws.cloudwatch.query_basic", {"log_group": "/aws/ec2"}),
        ("Describe EC2 Instances", "aws.ec2.describe", {}),
        ("Check S3 Bucket Policy", "aws.s3.get_bucket_policy", {"bucket_name": "company-backup-2024"}),
    ]
    
    print("Executing test sequence...\n")
    
    total_reward = 0
    for i, (description, tool, args) in enumerate(test_sequence, 1):
        action = json.dumps({
            "thought": f"Test: {description}",
            "tool": tool,
            "args": args
        })
        
        obs, reward, term, trunc, info = env.step(action)
        total_reward += reward
        
        print(f"Step {i}: {description}")
        print(f"  Tool: {tool}")
        print(f"  Reward: {reward:+.2f}")
        print(f"  Cumulative: {total_reward:+.2f}")
        if info.get("last_action_error"):
            print(f"  Error: {info['last_action_error']}")
        print()
    
    # =========================================================================
    # SECTION: PROGRESS TRACKING
    # =========================================================================
    print_section("Progress Tracking")
    
    print(f"Discovered Flags: {len(env.state.discovered_flags)}/{len(env.scenario['required_flags'])}")
    if env.state.discovered_flags:
        for flag in env.state.discovered_flags:
            print(f"  ✓ {flag}")
    print()
    
    print(f"Tool Usage Analytics:")
    for tool, count in sorted(env.tool_usage.items(), key=lambda x: x[1], reverse=True):
        print(f"  {tool}: {count} calls")
    print()
    
    print(f"Resource Costs:")
    print(f"  Query Costs: {env.query_costs:.2f}")
    print(f"  Total Reward: {total_reward:.2f}")
    print()
    
    print(f"Phase Scores:")
    for phase, score in env.phase_scores.items():
        print(f"  {phase}: {score:.2f}")
    print()
    
    # =========================================================================
    # SECTION: FINAL SCORING
    # =========================================================================
    print_section("Final Scoring Breakdown")
    
    final_scores = env.calculate_final_score()
    for key, value in final_scores.items():
        if key == "weighted_total":
            print(f"\n{'FINAL SCORE': ^40} {value:.3f}")
        else:
            print(f"  {key:20s}: {value:.3f}")
    print()
    
    # =========================================================================
    # SECTION: SYSTEM PROMPT PREVIEW
    # =========================================================================
    print_section("LLM System Prompt (Preview)")
    
    prompt = env.get_system_prompt()
    lines = prompt.split('\n')
    print('\n'.join(lines[:30]))
    print(f"\n... ({len(lines)} lines total)\n")
    
    env.close()


def test_preconditions(task="easy", seed=42):
    """Test action precondition checking"""
    
    print_section("Testing Action Preconditions")
    
    env = CloudSOCEnv(task=task, seed=seed)
    env.reset()
    
    # Find a compromised instance
    instance_id = None
    for iid, inst in env.state.instances.items():
        if inst.is_compromised:
            instance_id = iid
            break
    
    if not instance_id:
        print("No compromised instance found!")
        return
    
    print(f"Using instance: {instance_id}")
    print()
    
    # TEST 1: Isolate without snapshot
    print("TEST 1: Try to isolate without snapshot")
    print("-" * 40)
    action = json.dumps({
        "thought": "Isolate instance",
        "tool": "aws.ec2.isolate",
        "args": {"instance_id": instance_id}
    })
    obs, reward, term, trunc, info = env.step(action)
    print(f"Reward: {reward}")
    print(f"Error: {info['last_action_error']}")
    print(f"Instance still has snapshot: {not env.state.instances[instance_id].has_forensic_snapshot}")
    print()
    
    # Reset for next test
    env = CloudSOCEnv(task=task, seed=seed)
    env.reset()
    
    # TEST 2: Snapshot then isolate
    print("TEST 2: Snapshot followed by isolate (should succeed)")
    print("-" * 40)
    
    # Step 1: Snapshot
    action = json.dumps({
        "thought": "Create forensic snapshot",
        "tool": "aws.ec2.snapshot",
        "args": {"instance_id": instance_id}
    })
    obs, reward1, term1, trunc1, info1 = env.step(action)
    print(f"Snapshot: reward={reward1}, error={info1['last_action_error']}")
    
    # Step 2: Isolate
    action = json.dumps({
        "thought": "Isolate instance",
        "tool": "aws.ec2.isolate",
        "args": {"instance_id": instance_id}
    })
    obs, reward2, term2, trunc2, info2 = env.step(action)
    print(f"Isolate: reward={reward2}, error={info2['last_action_error']}")
    print(f"Instance state: {env.state.instances[instance_id].state.value}")
    print()
    
    env.close()


def test_adversarial_trap(task="easy", seed=42):
    """Test adversarial trap - terminating compromised instance"""
    
    print_section("Testing Adversarial Trap: Instance Termination")
    
    env = CloudSOCEnv(task=task, seed=seed)
    env.reset()
    
    # Find a compromised instance
    instance_id = None
    for iid, inst in env.state.instances.items():
        if inst.is_compromised:
            instance_id = iid
            break
    
    if not instance_id:
        print("No compromised instance found!")
        return
    
    print(f"Compromised instance: {instance_id}")
    print()
    
    print("Attempting to terminate compromised instance...")
    print("-" * 40)
    
    action = json.dumps({
        "thought": "Terminate the compromised instance",
        "tool": "aws.ec2.terminate",
        "args": {"instance_id": instance_id}
    })
    
    obs, reward, term, trunc, info = env.step(action)
    
    print(f"Reward: {reward} (EXPECTED: -1.0)")
    print(f"Terminated: {term} (EXPECTED: true)")
    print(f"Error: {info['last_action_error']}")
    print(f"Instance state: {env.state.instances[instance_id].state.value}")
    print()
    
    if reward == -1.0 and term:
        print("✓ ADVERSARIAL TRAP TRIGGERED!")
        print("  The agent made a critical error by destroying forensic evidence.")
    else:
        print("✗ Trap not triggered as expected")
    
    env.close()


def main():
    """Main debug menu"""
    
    if len(sys.argv) > 1 and sys.argv[1] == "--quick":
        explore_environment(task="easy", seed=42)
        return
    
    while True:
        print("\n" + "="*60)
        print("  CloudSOC Debug Menu")
        print("="*60)
        print("\n1. Explore Environment (Easy)")
        print("2. Explore Environment (Medium)")
        print("3. Explore Environment (Hard)")
        print("4. Test Preconditions")
        print("5. Test Adversarial Trap")
        print("6. Exit")
        
        choice = input("\nSelect option (1-6): ").strip()
        
        if choice == "1":
            explore_environment(task="easy", seed=42)
        elif choice == "2":
            explore_environment(task="medium", seed=42)
        elif choice == "3":
            explore_environment(task="hard", seed=42)
        elif choice == "4":
            test_preconditions(task="easy", seed=42)
        elif choice == "5":
            test_adversarial_trap(task="easy", seed=42)
        elif choice == "6":
            print("\nGoodbye!")
            break
        else:
            print("Invalid option!")


if __name__ == "__main__":
    if len(sys.argv) > 1 and sys.argv[1] == "--quick":
        explore_environment(task="easy", seed=42)
    else:
        main()