-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai_analysis.py
More file actions
474 lines (386 loc) · 15.4 KB
/
ai_analysis.py
File metadata and controls
474 lines (386 loc) · 15.4 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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# =============================================================================
# ai_analysis.py
# Sentinel-JIT - Autonomous Just-In-Time Deception Security System
# =============================================================================
#
# PURPOSE:
# This module simulates an AI-powered threat intelligence system.
# It reads attacker command events from logs.json, classifies each command
# into a known attack stage, and generates a human-readable behavior report.
#
# IMPORTANT:
# This module does NOT generate events and does NOT calculate risk scores.
# It only interprets what the attacker did once inside the decoy environment.
#
# ARCHITECTURE FLOW:
# attack_simulator.py -> logs.json -> risk_engine.py
# -> ai_analysis.py -> report
#
# RUN WITH:
# python ai_analysis.py
#
# =============================================================================
import json
import os
from typing import Dict, List, Optional
# Load environment variables from .env file automatically.
# This is how the GEMINI_API_KEY set in .env reaches the program.
try:
from dotenv import load_dotenv
load_dotenv()
except ImportError:
pass # dotenv not installed — env vars must be set manually
# google-genai is an optional dependency.
# If installed and GEMINI_API_KEY is set, LLM reports are enabled.
# Otherwise the system falls back to rule-based reports silently.
try:
from google import genai
GENAI_AVAILABLE = True
except ImportError:
GENAI_AVAILABLE = False
# ---------------------------------------------------------------------------
# CONSTANTS
# ---------------------------------------------------------------------------
LOG_FILE = "logs.json"
# ---------------------------------------------------------------------------
# STAGE CLASSIFICATION MAP
# ---------------------------------------------------------------------------
# Maps each attack stage name to a list of keywords.
# If any keyword appears inside a command string, it belongs to that stage.
# Stages are checked in order - the first match wins.
#
# These stages are loosely based on the MITRE ATT&CK framework,
# simplified for beginner readability.
# ---------------------------------------------------------------------------
STAGE_KEYWORDS = {
"Reconnaissance": ["whoami", "id", "uname", "hostname"],
"Discovery": ["ls", "cd", "find", "ps", "netstat", "ifconfig"],
"Credential Access": [
"cat passwords", "cat /etc/passwd", "cat /etc/shadow"
],
"Malware Deployment": ["wget", "curl", "ftp", "scp"],
"Privilege Escalation": ["sudo", "su", "chmod", "useradd", "chown"],
}
# ---------------------------------------------------------------------------
# FUNCTION 1: load_logs
# ---------------------------------------------------------------------------
def load_logs(filepath: str = LOG_FILE) -> List[Dict[str, str]]:
"""
Reads logs.json and returns all security events as a list.
Returns an empty list if the file is missing or empty, so the
rest of the program continues safely without crashing.
Args:
filepath (str): Path to the JSON log file.
Returns:
list[dict]: A list of event dictionaries.
"""
try:
with open(filepath, "r") as f:
content = f.read().strip()
if not content:
return []
return json.loads(content)
except FileNotFoundError:
print(
f"[WARNING] {filepath} not found. "
"Run attack_simulator.py first."
)
return []
# ---------------------------------------------------------------------------
# FUNCTION 2: extract_attacker_commands
# ---------------------------------------------------------------------------
def extract_attacker_commands(
events: List[Dict[str, str]]
) -> List[str]:
"""
Filters the full event list and returns only attacker command strings.
We only care about events where type = "attacker_command".
We extract just the "command" string (not the full dict) because
that is all the classifier needs.
Args:
events (list[dict]): All events loaded from logs.json.
Returns:
list[str]: A list of command strings, in the order they were run.
"""
commands = []
for event in events:
if event.get("type") == "attacker_command":
commands.append(event.get("command", ""))
return commands
# ---------------------------------------------------------------------------
# FUNCTION 3: classify_command
# ---------------------------------------------------------------------------
def classify_command(command: str) -> str:
"""
Classifies a single shell command into an attack stage.
Checks whether any keyword from STAGE_KEYWORDS appears in the command.
The check is case-insensitive so "WHOAMI" and "whoami" both match.
If no keyword matches, the stage is labeled "Unknown".
Args:
command (str): The shell command to classify.
Returns:
str: The name of the attack stage (e.g. "Reconnaissance").
"""
command_lower = command.lower()
for stage, keywords in STAGE_KEYWORDS.items():
for keyword in keywords:
if keyword in command_lower:
return stage
return "Unknown"
# ---------------------------------------------------------------------------
# FUNCTION 4: analyze_attack
# ---------------------------------------------------------------------------
def analyze_attack(commands: List[str]) -> List[Dict[str, str]]:
"""
Iterates through all attacker commands and classifies each one.
Builds a list of result dictionaries, one per command, that pair the
command text with its classified attack stage.
Args:
commands (list[str]): The list of command strings to analyse.
Returns:
list[dict]: A list like:
[
{"command": "whoami", "stage": "Reconnaissance"},
{"command": "ls", "stage": "Discovery"},
...
]
"""
analysis = []
for command in commands:
stage = classify_command(command)
analysis.append({"command": command, "stage": stage})
return analysis
# ---------------------------------------------------------------------------
# FUNCTION 5 (NEW): generate_llm_report
# ---------------------------------------------------------------------------
def generate_llm_report(
commands: List[str],
api_key: Optional[str] = None
) -> Optional[str]:
"""
Sends the attacker command list to the Google Gemini API and returns
an AI-generated cybersecurity incident report.
This function is OPTIONAL. If the API key is missing, or if the
google-generativeai package is not installed, or if the API call
fails for any reason, it returns None so the caller can fall back
to the rule-based generate_report() instead.
The API key is read from:
1. The 'api_key' argument (if provided directly).
2. The GEMINI_API_KEY environment variable.
Args:
commands (List[str]): The list of attacker shell commands.
api_key (str, optional): Gemini API key. Reads from env if None.
Returns:
str : The Gemini-generated report text, or
None : If LLM is unavailable or the call fails.
"""
# Step 1: Check that the google-generativeai package is installed.
if not GENAI_AVAILABLE:
return None
# Step 2: Resolve the API key.
# Re-load .env here with override=True so we always pick up the latest
# key even if the environment was set before this function was called.
try:
from dotenv import load_dotenv as _lde
_lde(override=True)
except ImportError:
pass
resolved_key = api_key or os.environ.get("GEMINI_API_KEY", "")
if not resolved_key:
# No API key found — silently return None to trigger fallback.
return None
# Step 3: Build a clear, focused prompt for Gemini.
command_list = "\n".join(f" - {cmd}" for cmd in commands)
prompt = (
"You are a cybersecurity analyst. "
"An attacker was observed running the following shell commands "
"inside a honeypot (decoy) environment:\n\n"
+ command_list
+ "\n\nAnalyze this attacker command sequence and produce a "
"structured cybersecurity incident report covering:\n"
"1. Attack stages observed (e.g. Reconnaissance, Discovery).\n"
"2. What the attacker was trying to achieve at each stage.\n"
"3. Overall attacker objective.\n"
"4. Recommended defensive actions.\n"
"Keep the report concise and beginner-friendly."
)
# Step 4: Call the Gemini API. Catch all errors so we can fall back.
try:
client = genai.Client(api_key=resolved_key)
response = client.models.generate_content(
model="gemini-2.0-flash",
contents=prompt,
)
return str(response.text)
except Exception: # noqa: BLE001
# Any API failure (bad key, quota, network) falls back to
# the rule-based report - the user sees no error.
return None
# ---------------------------------------------------------------------------
# FUNCTION 6: generate_report
# ---------------------------------------------------------------------------
def generate_report(analysis: List[Dict[str, str]]) -> str:
"""
Produces a human-readable plain-English attack behavior report.
The report covers:
- How many commands were run in total.
- Which attack stages were observed.
- Specific commands linked to each stage.
- A conclusion about the attacker's likely objective.
Args:
analysis (list[dict]): Output of analyze_attack() - list of
{command, stage} dicts.
Returns:
str: The full report as a multi-line string.
"""
if not analysis:
return "No attacker commands found. Cannot generate a report."
total_commands = len(analysis)
# Build a mapping of stage -> list of commands seen in that stage.
stage_commands: Dict[str, List[str]] = {}
for item in analysis:
stage = item["stage"]
command = item["command"]
if stage not in stage_commands:
stage_commands[stage] = []
stage_commands[stage].append(command)
# Collect unique stages in the order they first appeared.
seen_stages: List[str] = []
for item in analysis:
if item["stage"] not in seen_stages:
seen_stages.append(item["stage"])
# Build the report as clean paragraphs — no separator lines.
stages_str = ", ".join(seen_stages)
report = "Attack Analysis Report\n\n"
report += (
"Commands analysed: " + str(total_commands)
+ "\nStages detected: " + stages_str + "\n\n"
)
report += "Narrative Summary\n\n"
# Reconnaissance stage
if "Reconnaissance" in stage_commands:
cmds = ", ".join(stage_commands["Reconnaissance"])
report += (
"The attacker began with Reconnaissance commands ("
+ cmds
+ ") to identify the current user, system "
"privileges, and OS version.\n"
)
# Discovery stage
if "Discovery" in stage_commands:
cmds = ", ".join(stage_commands["Discovery"])
report += (
"\nThey then explored the file system using "
"Discovery commands ("
+ cmds
+ ") to locate interesting files and directories.\n"
)
# Credential Access stage
if "Credential Access" in stage_commands:
cmds = ", ".join(stage_commands["Credential Access"])
report += (
"\nThe attacker attempted to access sensitive "
"credential-related files ("
+ cmds
+ "), indicating a possible credential "
"harvesting attempt.\n"
)
# Malware Deployment stage
if "Malware Deployment" in stage_commands:
cmds = ", ".join(stage_commands["Malware Deployment"])
report += (
"\nA Malware Deployment action was observed ("
+ cmds
+ "). This suggests the attacker tried to download "
"malicious tools or payloads onto the system.\n"
)
# Privilege Escalation stage
if "Privilege Escalation" in stage_commands:
cmds = ", ".join(stage_commands["Privilege Escalation"])
report += (
"\nPrivilege Escalation was attempted ("
+ cmds
+ "), indicating the attacker tried to gain "
"superuser (root) access.\n"
)
# Unknown stage
if "Unknown" in stage_commands:
cmds = ", ".join(stage_commands["Unknown"])
report += (
"\nSome commands could not be classified ("
+ cmds
+ "). These may require manual investigation.\n"
)
# Conclusion
report += "\nConclusion\n\n"
report += (
"This pattern of behavior is consistent with a targeted "
"intrusion attempt. The attacker followed a methodical "
"kill-chain: gathering system information, exploring the "
"environment, harvesting credentials, and attempting to "
"escalate privileges. This is characteristic of a "
"credential harvesting and system compromise attempt.\n\n"
)
report += (
"Recommended action: Escalate to the Security "
"Operations Center (SOC).\n"
)
return report
# ---------------------------------------------------------------------------
# FUNCTION 6: main
# ---------------------------------------------------------------------------
def main():
"""
Orchestrates the full analysis pipeline:
1. Load all events from logs.json.
2. Extract attacker command strings.
3. Classify each command into an attack stage.
4. Print the per-command classification table.
5. Print the full narrative report.
"""
print()
print("AI Analysis — classifying attack commands")
print()
# Step 1: Load events.
events = load_logs()
if not events:
print("No events to analyse.")
return
# Step 2: Extract attacker commands.
commands = extract_attacker_commands(events)
print(f" {len(commands)} attacker commands found.")
print()
# Step 3: Classify each command.
analysis = analyze_attack(commands)
# Step 4: Print the per-command classification table.
print(f" {'Command':<42} Stage")
print(f" {'-'*40}")
for item in analysis:
print(f" {item['command']:<42} {item['stage']}")
print()
# Step 5: Generate the attack report.
# Try the Gemini LLM report first.
# If the API key is missing or the call fails, use the rule-based report.
llm_report = generate_llm_report(commands)
if llm_report:
print("[LLM] Gemini AI report generated.")
print()
print(llm_report)
report = llm_report
else:
# No API key set or LLM unavailable — fall back silently.
print("[INFO] Gemini API key not set or unavailable.")
print("[INFO] Using rule-based report instead.")
print()
report = generate_report(analysis)
print(report)
# Return analysis and report so app.py can use them directly.
return {
"analysis": analysis,
"report": report
}
# ---------------------------------------------------------------------------
# ENTRY POINT
# ---------------------------------------------------------------------------
if __name__ == "__main__":
main()