-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
executable file
·2979 lines (2679 loc) · 153 KB
/
Copy pathapp.py
File metadata and controls
executable file
·2979 lines (2679 loc) · 153 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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import streamlit as st
import subprocess
import sys
import time
import re
import base64
import math
import requests
from pathlib import Path
import os
import json
import yaml
import pandas as pd
import numpy as np
import plotly.graph_objects as go
import plotly.express as px
from streamlit.components.v1 import html
from datetime import datetime
# --- Configuration ---
BASE_DIR = Path(__file__).parent
CLI_SCRIPT_PATH = BASE_DIR / "runner.py"
MEMORY_FOLDER = BASE_DIR / "memory"
OUTPUT_FOLDER = BASE_DIR / "out" # Base output directory
BASELINE_FILE_PATH = BASE_DIR / "baseline.yaml"
DETECTIONS_FILE_PATH = BASE_DIR / "detections.yaml"
# Ollama — reads from env var set by docker-compose; falls back to localhost for dev
def _resolve_ollama_host() -> str:
"""
Pick the right Ollama base URL depending on where the app is running.
Priority:
1. OLLAMA_HOST env var (set by docker-compose to http://ollama:11434) — always wins.
2. If we're inside a Docker container (/.dockerenv exists) and no explicit var was
given, reach the host machine via the special DNS name `host.docker.internal`.
This covers the case where someone runs the DeepProbe container standalone
while Ollama is running on the host.
3. Plain localhost — for native / dev runs outside Docker.
"""
explicit = os.environ.get("OLLAMA_HOST", "")
if explicit:
return explicit
if Path("/.dockerenv").exists():
return "http://host.docker.internal:11434"
return "http://localhost:11434"
OLLAMA_HOST = _resolve_ollama_host()
DEFAULT_MODEL = os.environ.get("DEFAULT_MODEL", "llama3.2:3b")
RECOMMENDED_MODELS = [
"llama3.2:3b", # 2 GB — fast, works on any machine
"llama3.1:8b", # 5 GB — better reasoning
"mistral:7b", # 4 GB — excellent instruction following
"phi3:mini", # 2.3 GB — lightest option
"gemma2:2b", # 1.6 GB — Google Gemma, very lightweight
]
# set_page_config MUST be the first Streamlit command executed.
# It lives here at module scope so it runs before any other st.* call
# (including the CSS st.markdown block below).
st.set_page_config(
page_title="DeepProbe | Memory Forensics",
page_icon="🕵️",
layout="wide",
initial_sidebar_state="expanded",
)
# ---------------------------------------------------------------------------
# Ollama helper functions
# ---------------------------------------------------------------------------
def check_ollama_health() -> bool:
"""Return True if the Ollama service is reachable."""
try:
r = requests.get(f"{OLLAMA_HOST}/api/tags", timeout=3)
return r.status_code == 200
except Exception:
return False
def get_ollama_models() -> list:
"""Return list of model names already pulled in Ollama."""
try:
r = requests.get(f"{OLLAMA_HOST}/api/tags", timeout=3)
if r.status_code == 200:
return [m["name"] for m in r.json().get("models", [])]
except Exception:
pass
return []
def pull_ollama_model(model_name: str) -> tuple:
"""
Pull a model from the Ollama library.
Returns (success: bool, message: str).
Uses stream=False so the whole pull happens in one request (up to 10 min).
"""
try:
r = requests.post(
f"{OLLAMA_HOST}/api/pull",
json={"name": model_name, "stream": False},
timeout=600,
)
if r.status_code == 200:
return True, f"Model `{model_name}` downloaded successfully."
return False, f"Pull failed — HTTP {r.status_code}: {r.text[:200]}"
except requests.exceptions.Timeout:
return False, "Pull timed out (>10 min). Try a smaller model or check your connection."
except Exception as e:
return False, f"Pull error: {e}"
def query_ollama(model: str, prompt: str) -> str:
"""Send a prompt to Ollama and return the response text.
Determinism settings:
temperature=0 → greedy decoding, no randomness.
seed=42 → reproducible outputs across identical prompts.
top_p=1.0 → disable nucleus sampling (irrelevant at temp=0 but explicit).
These settings prevent hallucinations by making the model output the single
most probable token at every step instead of sampling randomly.
"""
try:
r = requests.post(
f"{OLLAMA_HOST}/api/generate",
json={
"model": model,
"prompt": prompt,
"stream": False,
"options": {
"temperature": 0,
"seed": 42,
"top_p": 1.0,
"num_predict": 512, # cap token budget — forensic summaries don't need to be long
},
},
timeout=120,
)
r.raise_for_status()
return r.json().get("response", "No response returned by model.")
except requests.exceptions.ConnectionError:
return "⚠️ Cannot reach Ollama service. Is it running? Check the sidebar status."
except requests.exceptions.Timeout:
return "⚠️ Ollama took too long to respond. Try a smaller/faster model."
except requests.exceptions.HTTPError as e:
# Try to get the real reason from Ollama's JSON body
body_msg = ""
if e.response is not None:
try:
body_msg = e.response.json().get("error", "")
except Exception:
body_msg = e.response.text or ""
if e.response.status_code == 404 or (
"not found" in body_msg.lower() or "pull" in body_msg.lower()
):
return (
f"⚠️ Model **{model}** is not downloaded yet. "
"Open the sidebar, select the model and click '⬇️ Download' to pull it first."
)
return f"⚠️ Ollama returned an error ({e.response.status_code if e.response else '?'}): {body_msg or str(e)}"
except Exception as e:
return f"⚠️ Ollama error: {e}"
def _clean_evidence_for_llm(raw_evidence: list, max_items: int = 3) -> list:
"""
Filter and sanitise evidence items before sending to an LLM.
Removes:
- Volatility internal messages (warnings, library path lines, progress output)
- Items that are plain strings containing no useful forensic data
- Keys whose values are empty, None, or noise strings
Returns a list of clean dicts with at most max_items entries.
"""
_NOISE_PATTERNS = re.compile(
r"(^WARNING|^ERROR|^INFO|Traceback|\.pyc|site-packages|volatility3|"
r"^\s*$|Progress:|Stacking attempts|incompatible:|PluginRequirements)",
re.IGNORECASE,
)
_NOISE_VALUES = {"", "None", "N/A", "nan", "0", "0.0", "-", "—"}
cleaned = []
for item in raw_evidence:
if not isinstance(item, dict):
# Skip raw string lines (Volatility console output)
continue
filtered = {
k: v for k, v in item.items()
if str(v).strip() not in _NOISE_VALUES
and not _NOISE_PATTERNS.search(str(v))
}
if filtered:
cleaned.append(filtered)
if len(cleaned) >= max_items:
break
return cleaned
def query_llm(finding: dict, model: str = DEFAULT_MODEL, gemini_key: str = "") -> str:
"""
Unified LLM call for individual (non-correlated) findings.
Evidence is pre-filtered through _clean_evidence_for_llm() before the prompt is built.
The prompt enforces strict evidence-bound output with no hallucination, no unsafe
remediation advice, and no MITRE IDs embedded inline in explanation text.
"""
title = finding.get('title', 'Unknown Finding')
weight = finding.get('weight', 0)
# MITRE IDs provided as reference metadata only — not for inline embedding
mitre_ref = ', '.join(finding.get('mitre', [])) or 'None provided'
clean_ev = _clean_evidence_for_llm(finding.get("evidence", []), max_items=3)
evidence_sample = (
json.dumps(clean_ev, indent=2, ensure_ascii=False)
if clean_ev else "(no structured evidence available)"
)
prompt = (
"You are a memory forensics analyst reviewing a single finding from an automated scan.\n\n"
"HARD CONSTRAINTS — non-negotiable rules for your response:\n"
"1. Use ONLY the finding title and evidence fields provided below. "
" Do NOT generate or infer file paths, process names, IP addresses, PIDs, "
" module names, registry keys, or memory values not present in the evidence.\n"
"2. Do NOT mention lateral movement, credential theft, persistence, C2 communication, "
" privilege escalation, or any attack technique UNLESS the finding title or evidence "
" explicitly names it.\n"
"3. Do NOT embed MITRE technique IDs inline in your explanation. "
" MITRE tags are provided as reference metadata only — do not quote them in body text.\n"
"4. Avoid hedging language: do NOT write 'may be used', 'could indicate', "
" 'might suggest', 'possibly', or 'likely' unless the evidence explicitly limits certainty. "
" State what the evidence shows directly, or omit the claim.\n"
"5. Remediation — ONLY use these safe forms: "
" 'isolate the system', 'investigate affected processes', 'collect memory artifacts', "
" 'preserve the memory image', 'escalate to the IR team'. "
" NEVER recommend killing, terminating, or stopping a specific process by name.\n"
"6. If the evidence is insufficient to support a statement, say so explicitly.\n\n"
f"Finding: {title}\n"
f"Severity Score: {weight} / 15\n"
f"MITRE reference (metadata only, do not quote inline): {mitre_ref}\n"
f"Evidence (filtered, up to 3 items):\n{evidence_sample}\n\n"
"Respond with exactly three short paragraphs:\n"
"**What this means:** Summarise what the evidence shows — state facts, not assumptions.\n"
"**Why it is dangerous:** Describe the confirmed risk based only on the evidence provided.\n"
"**Immediate actions:** List 2-3 steps using only the safe remediation forms above."
)
if gemini_key:
return query_gemini(gemini_key, {
**finding,
"evidence": clean_ev,
"_prompt_override": prompt,
})
return query_ollama(model, prompt)
def query_llm_correlated(finding: dict, model: str = DEFAULT_MODEL, gemini_key: str = "") -> str:
"""
LLM call specifically for correlated / system-wide findings.
Builds a chain-aware prompt from correlated_chains. Evidence items are
pre-filtered through _clean_evidence_for_llm() to remove Volatility noise
before any values reach the model.
"""
fid = finding.get("id", "")
title = finding.get("title", "Correlated Threat")
# Use MITRE IDs exactly as provided — never remap or reinterpret
mitre = ", ".join(finding.get("mitre", [])) or "None provided"
chains = finding.get("correlated_chains", [])
# ------------------------------------------------------------------
# Build sanitised chain text — only clean forensic signals reach LLM
# ------------------------------------------------------------------
chain_lines: list = []
for item in chains:
pid = item.get("correlated_pid", "?")
corr_type = item.get("correlation_type", "")
layers = item.get("layers_involved", [])
sub_findings = item.get("correlated_findings", [])
for sf in sub_findings[:8]:
layer_tag = f"[{sf.get('layer', corr_type or 'unknown').upper()}] " if sf.get("layer") else ""
role_tag = f" ({sf.get('process_role', '')})" if sf.get("process_role") else ""
# Clean the evidence sample — removes Volatility warnings, library paths, etc.
raw_ev = sf.get("evidence", [])
clean = _clean_evidence_for_llm(raw_ev, max_items=1)
if clean:
# Only include key-value pairs where the value is ≤60 chars (avoids memory blobs)
ev_parts = [
f"{k}: {str(v)[:60]}"
for k, v in list(clean[0].items())[:4]
if str(v).strip() not in ("", "None", "N/A", "nan")
and len(str(v)) <= 200 # skip raw hex / base64 blobs
and not str(v).startswith("0x") # skip memory addresses
]
ev_txt = ", ".join(ev_parts)
else:
ev_txt = ""
chain_lines.append(
f" • {layer_tag}{sf.get('title', sf.get('finding_id', '?'))}{role_tag}"
+ (f" [evidence: {ev_txt}]" if ev_txt else "")
)
chain_text = "\n".join(chain_lines) or " (no individual findings listed)"
# ------------------------------------------------------------------
# Hard constraint block — injected into every correlated prompt
# ------------------------------------------------------------------
_HARD_CONSTRAINTS = (
"HARD CONSTRAINTS — non-negotiable rules for your response:\n"
"1. Use ONLY the finding names and evidence fields explicitly listed below. "
" Do NOT generate, infer, or assume file paths, process names, PIDs, "
" memory addresses, module names, or registry keys not present in the data.\n"
"2. Do NOT mention lateral movement, credential theft, persistence, C2 communication, "
" privilege escalation, or any attack technique UNLESS a finding in the list below "
" explicitly names it. Do not add attack claims beyond what the findings state.\n"
"3. Do NOT embed MITRE technique IDs inline in your explanation. "
" MITRE tags are provided as reference metadata — do not quote them in body text.\n"
"4. Avoid hedging language: do NOT write 'may be used', 'could indicate', "
" 'might suggest', 'possibly', or 'likely' unless the evidence explicitly limits certainty. "
" State what the evidence shows directly, or omit the claim entirely.\n"
"5. Remediation — ONLY use these safe forms: "
" 'isolate the system', 'investigate affected processes', 'collect memory artifacts', "
" 'preserve the memory image', 'escalate to the IR team'. "
" NEVER name a specific process to kill, terminate, or stop.\n"
"6. Use 'analysis indicates' or 'evidence shows' — not 'the attacker did'.\n"
"7. If the evidence is insufficient to support a statement, say so explicitly "
" rather than filling the gap with plausible-sounding detail.\n\n"
)
# System-wide compromise
if fid == "correlation_system_wide":
all_layers = sorted({
layer
for item in chains
for layer in item.get("layers_involved", [])
})
prompt = (
"You are a memory forensics analyst producing an evidence-bound summary "
"of a system-wide compromise for an incident response team.\n\n"
+ _HARD_CONSTRAINTS +
f"Confirmed forensic layers ({len(all_layers)} simultaneously active): "
f"{', '.join(all_layers) or 'multiple layers'}\n"
f"MITRE reference (metadata only, do not quote inline): {mitre}\n\n"
f"Findings present in this memory image:\n{chain_text}\n\n"
"Respond with exactly three sections:\n\n"
"**What this means:** Summarise what each listed finding shows. "
"State only what the evidence above directly supports. "
"Do not infer a sequence of events, initial access method, or attacker motivation.\n\n"
"**Why it is dangerous:** Describe the confirmed risk of having high-severity "
"indicators across multiple forensic layers simultaneously. "
"Base every statement on a specific finding from the list above — "
"do not extrapolate or add attack claims not present in the findings.\n\n"
"**Immediate actions:** List exactly 3 steps. "
"Use only these forms: 'isolate the system', 'investigate affected processes', "
"'collect memory artifacts', 'preserve the memory image', 'escalate to the IR team'. "
"Do not name a specific process to terminate or stop."
)
else:
# Pair-based correlated finding
confidence = chains[0].get("confidence", "") if chains else ""
conf_scope = {
"strong": "within the same process (PID-level match)",
"medium": "across a parent-child process pair",
"weak": "as co-present behavioral indicators in the same image",
}.get(confidence, "across multiple correlated findings")
prompt = (
f"You are a memory forensics analyst reviewing a correlated finding.\n"
f"Correlation: {title}\n"
f"Scope: {conf_scope}\n\n"
+ _HARD_CONSTRAINTS +
f"MITRE reference (metadata only, do not quote inline): {mitre}\n\n"
f"Findings present in this memory image:\n{chain_text}\n\n"
"Respond with exactly three sections:\n\n"
"**What this means:** Describe what the correlated evidence shows — "
"which findings are linked, how they relate, and what the combination confirms. "
"State only what the listed findings directly support. "
"Do not add attack claims beyond what the findings name.\n\n"
"**Why it is dangerous:** Explain the confirmed risk this correlation represents "
"and why it is more severe than any single finding alone. "
"Every statement must be grounded in a specific finding from the list above.\n\n"
"**Immediate actions:** List 2-3 steps. "
"Use only these forms: 'isolate the system', 'investigate affected processes', "
"'collect memory artifacts', 'preserve the memory image', 'escalate to the IR team'. "
"Do not name a specific process to terminate or stop."
)
if gemini_key:
return query_gemini(gemini_key, {
"title": title,
"mitre": finding.get("mitre", []),
"weight": finding.get("weight", 0),
"evidence": [],
"_prompt_override": prompt,
})
return query_ollama(model, prompt)
# Ensure necessary directories exist at startup.
try:
MEMORY_FOLDER.mkdir(exist_ok=True)
OUTPUT_FOLDER.mkdir(exist_ok=True)
except Exception as e:
print(f"FATAL ERROR: Could not create necessary directories for DeepProbe. Please check file system permissions. Error: {e}", file=sys.stderr)
st.error(f"Initialization Error: DeepProbe could not create essential directories ('memory/', 'out/'). "
f"Please verify your file system permissions in the directory where you are running the app. Error: {e}")
st.stop()
# --- Custom CSS Fixes ---
# This CSS removes the Streamlit header and its associated spacing, and also the default top padding.
st.markdown(
"""
<style>
/* Remove Streamlit default padding */
.block-container {
padding-top: 0rem;
}
/* Hide Streamlit top header (Deploy, hamburger menu, etc.) */
header[data-testid="stHeader"] {
display: none;
}
/* Also hide empty space left by toolbar */
div[data-testid="stDecoration"] {
display: none;
}
/* --- Custom Table Styling --- */
/* This section is a stronger fix for table rendering */
div[data-testid="stTable"] table,
div[data-testid="stDataFrame"] table,
.stTable table,
.stDataFrame table {
background-color: #161b22 !important;
color: #c9d1d9 !important;
}
div[data-testid="stTable"] table td,
div[data-testid="stTable"] table th,
div[data-testid="stDataFrame"] table td,
div[data-testid="stDataFrame"] table th,
.stTable table td, .stTable table th,
.stDataFrame table td, .stDataFrame table th {
background-color: #161b22 !important;
color: #c9d1d9 !important;
border: 1px solid #30363d !important;
}
.stDataFrame thead th {
background-color: #161b22 !important;
color: #2ecc71 !important; /* Retain green header */
border-bottom: 2px solid #2ecc71 !important;
}
.stDataFrame tbody tr:hover {
background-color: #21262d !important;
}
/* Make sure scrollbar/scroll container also uses dark background */
.stDataFrame [data-testid="stTable"] {
background-color: #161b22 !important;
}
/* Also include styling for the artifacts tab tables */
[data-testid="stVerticalBlock"] [data-testid="stDataFrame"] thead th {
color: #2ecc71 !important;
}
[data-testid="stVerticalBlock"] [data-testid="stDataFrame"] tbody tr td {
color: #c9d1d9 !important;
}
</style>
""",
unsafe_allow_html=True
)
# --- Backend and Reporting Helper Functions ---
def html_escape(text):
"""Escapes HTML special characters in a string to prevent misinterpretation as HTML or links."""
if text is None:
return ""
return str(text).replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '"').replace("'", ''')
def get_friendly_scan_name(plugin_name):
"""Maps technical plugin names to user-friendly descriptions."""
mapping = {
"windows.info": "Detecting operating system...",
"windows.pslist": "Analyzing running processes...",
"windows.psxview": "Scanning for hidden processes...",
"windows.netscan": "Investigating network connections...",
"windows.netstat": "Checking network statistics...",
"windows.malfind": "Searching for injected code...",
"windows.hollowprocesses": "Checking for hollowed processes...",
"windows.ldrmodules": "Analyzing loaded modules for unlinked DLLs...",
"windows.handles": "Inspecting process handles for suspicious access...",
"windows.svcscan": "Scans system services...",
"windows.scheduled_tasks": "Reviewing scheduled tasks for persistence...",
"windows.filescan": "Scanning for suspicious files in memory...",
"windows.registry.printkey": "Querying registry keys for anomalies...",
"windows.registry.userassist": "Analyzing user execution history...",
"windows.sessions": "Inspecting user logon sessions...",
"linux.pslist": "Analyzing Linux processes...",
"linux.psscan": "Scanning Linux for hidden processes...",
"linux.lsof": "Checking Linux open files and network connections...",
"linux.sockstat": "Analyzing Linux socket statistics...",
"linux.check_syscall": "Checking system call table integrity for potential hooks on Linux.",
"linux.check_modules": "Checking Linux kernel modules...",
"linux.bash": "Analyzing Linux Bash history...",
"mac.pslist": "Analyzing macOS processes...",
"mac.lsof": "Checking macOS open files and network connections...",
"mac.netstat": "Checking macOS network statistics...",
"mac.malfind": "Searching macOS for injected code...",
"mac.bash": "Analyzing macOS Bash history...",
"windows.dlllist": "Listing loaded DLLs for each process, useful for identifying injected or suspicious modules.",
"windows.apihooks": "Detecting Windows API hooks...",
"windows.devicetree": "Analyzing Windows device tree...",
"windows.modscan": "Scanning Windows kernel modules...",
"windows.consoles": "Recovers Windows console history...",
"windows.clipboard": "Recovers clipboard contents...",
"windows.registry.shimcache": "Analyzing Windows Shimcache for execution artifacts...",
"windows.registry.amcache": "Analyzing Windows Amcache for execution artifacts...",
"windows.envars": "Listing Windows environment variables...",
"windows.callbacks": "Analyzing Windows kernel callbacks...",
"linux.envars": "Listing Linux environment variables...",
"linux.librarylist": "Enumerating Linux shared libraries...",
"linux.lsmod": "Listing Linux loaded kernel modules...",
"mac.sessions": "Listing macOS user sessions...",
"mac.mount": "Displaying macOS mounted filesystems...",
"mac.volumes": "Listing macOS volumes...",
"mac.dmesg": "Recovers macOS kernel ring buffer messages...",
}
return mapping.get(plugin_name, f"Running scan: {plugin_name}...")
def run_analysis_and_show_progress(case_name, memory_file_path, ip_enrichment_api_key, progress_bar, status_text):
"""
Executes the backend analysis script, captures its output in real-time,
and updates the UI with progress. Includes a timeout.
"""
# Dynamically define the output paths based on the project name
project_output_folder = OUTPUT_FOLDER / case_name
project_artifacts_folder = project_output_folder / "artifacts"
# Ensure the project-specific directories exist
project_output_folder.mkdir(exist_ok=True)
project_artifacts_folder.mkdir(exist_ok=True)
for p in [CLI_SCRIPT_PATH]:
if not p.exists():
status_text.error(f"FATAL ERROR: A required script '`{html_escape(p.name)}`' was not found.")
return False
# Check if detections.yaml and baseline.yaml exist at BASE_DIR
if not DETECTIONS_FILE_PATH.exists():
status_text.error(f"FATAL ERROR: '`detections.yaml`' not found at '`{html_escape(str(BASE_DIR))}`'.")
return False
if not BASELINE_FILE_PATH.exists():
status_text.error(f"FATAL ERROR: '`baseline.yaml`' not found at '`{html_escape(str(BASE_DIR))}`'.")
return False
cmd = [
sys.executable, '-u', str(CLI_SCRIPT_PATH),
"--image", str(memory_file_path), "--case", case_name,
"--detections", str(DETECTIONS_FILE_PATH),
"--baseline", str(BASELINE_FILE_PATH),
"--outdir", str(project_output_folder),
"--api-key", ip_enrichment_api_key
]
st.session_state.analysis_logs = []
process = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
text=True, encoding='utf-8', errors='replace', bufsize=1
)
TOTAL_STEPS = 20
current_step = 0
status_text.info("Preparing analysis environment…")
completed_plugins = []
checklist_placeholder = st.empty()
def _render_checklist(done_list, current_msg=""):
lines = []
for p in done_list[-12:]: # show last 12 to avoid overflow
lines.append(f"✅ `{p}`")
if current_msg:
lines.append(f"⟳ *{current_msg}*")
checklist_placeholder.markdown("\n\n".join(lines))
# Robust loop reading subprocess output line-by-line
while True:
line = process.stdout.readline()
if not line and process.poll() is not None:
break
if line:
st.session_state.analysis_logs.append(line)
if "[+] Running plugin:" in line:
current_step += 1
try:
plugin_name = line.split(":", 1)[1].strip().split(" ")[0]
friendly_name = get_friendly_scan_name(plugin_name)
status_text.info(friendly_name)
if completed_plugins:
completed_plugins[-1] = completed_plugins[-1] # keep prev
# mark the previous as done and show the current as in-progress
_render_checklist(completed_plugins, friendly_name)
completed_plugins.append(friendly_name)
except IndexError:
pass
progress_fraction = min(1.0, current_step / TOTAL_STEPS)
progress_bar.progress(progress_fraction, text=f"{int(progress_fraction*100)}% Complete")
elif "[i] Running detection engine:" in line:
msg = line.strip().replace("[i] Running detection engine: ", "Running engine: ")
status_text.info(msg)
_render_checklist(completed_plugins, msg)
elif "[i] Starting correlation analysis:" in line or "[i] Finished correlation" in line:
status_text.info("Running correlation analysis…")
_render_checklist(completed_plugins, "Correlating findings…")
elif "[WARN]" in line:
# Emit API fallback warning visibly
st.warning(line.strip().replace("[WARN] ", "⚠️ "))
try:
process.wait(timeout=900)
except subprocess.TimeoutExpired:
process.kill()
status_text.error("Analysis Timed Out after 15 minutes. The memory image may be corrupt or too complex.")
st.session_state.analysis_successful = False
return False
if process.returncode == 0:
progress_bar.progress(1.0, text="100% Complete")
status_text.success("Analysis Complete! Redirecting to results...")
st.session_state.analysis_successful = True
return True
else:
status_text.error(f"Analysis Failed. Exit code: `{html_escape(str(process.returncode))}`.")
st.session_state.analysis_successful = False
with st.expander("Show Error Log"):
st.code(''.join(st.session_state.analysis_logs), language='text')
return False
def load_findings(project_name):
"""Loads findings from the project-specific directory."""
project_output_folder = OUTPUT_FOLDER / project_name
findings_jsonl_path = project_output_folder / "findings.jsonl"
findings = []
if findings_jsonl_path.exists():
with open(findings_jsonl_path, 'r', encoding='utf-8') as f:
for line in f:
try:
findings.append(json.loads(line))
except json.JSONDecodeError:
print(f"Warning: Could not decode line: {line}")
return findings
def load_detections_config():
"""Loads the detections.yaml config from the base directory."""
if DETECTIONS_FILE_PATH.exists():
with open(DETECTIONS_FILE_PATH, 'r', encoding='utf-8') as f:
return yaml.safe_load(f)
return None
def load_baseline_config():
"""Loads the baseline.yaml config from the base directory."""
if BASELINE_FILE_PATH.exists():
with open(BASELINE_FILE_PATH, 'r', encoding='utf-8') as f:
return yaml.safe_load(f)
return None
_BUILTIN_NARRATIVES = {
"correlation_evasion_priv_esc": (
"Evasion techniques (AMSI bypass, ETW patching) were detected alongside privilege "
"escalation indicators (token impersonation, code injection). This combination suggests "
"an attacker actively suppressing defenses while elevating their access level — a "
"hallmark of post-exploitation activity following initial access."
),
"correlation_lolbin_chain": (
"Living-off-the-land binaries (LOLBins) or WMI were detected in conjunction with "
"network activity or persistence mechanisms. Attackers abuse built-in system tools "
"to blend with normal activity while establishing command-and-control channels or "
"maintaining long-term access."
),
"correlation_exfil_chain": (
"Archive staging artifacts were detected alongside outbound connections or suspicious "
"network activity. This pattern matches the Collection and Exfiltration phases of the "
"MITRE ATT&CK framework — data is gathered, compressed, and then transferred to "
"attacker-controlled infrastructure."
),
}
# ---------------------------------------------------------------------------
# Dynamic narrative helpers
# ---------------------------------------------------------------------------
_LAYER_FRIENDLY_NAMES = {
"process": "Process Layer",
"kernel": "Kernel Layer",
"network": "Network Layer",
"system": "System Artifact Layer",
}
def _friendly_layer_name(layer: str) -> str:
"""Return a human-readable layer name."""
return _LAYER_FRIENDLY_NAMES.get(str(layer).lower(), str(layer).replace("_", " ").title())
def _oxford_join(items: list) -> str:
"""Join a list with Oxford comma: 'a, b, and c'."""
items = [str(i) for i in items if i and str(i) not in ("", "None", "N/A")]
if not items: return "unknown activity"
if len(items) == 1: return items[0]
if len(items) == 2: return f"{items[0]} and {items[1]}"
return ", ".join(items[:-1]) + f", and {items[-1]}"
def build_dynamic_chain_narrative(finding: dict) -> str:
"""
Generate a dynamic, evidence-specific plain-English narrative for correlated findings.
Uses actual correlated_chains data so every report tells the story of THIS memory image,
not generic boilerplate.
"""
fid = finding.get("id", "")
chains = finding.get("correlated_chains", [])
# No chain data → fall back to static text
if not chains:
if fid in _BUILTIN_NARRATIVES:
return _BUILTIN_NARRATIVES[fid]
return "No correlation details available for this finding."
# ----------------------------------------------------------------
# System-wide: group findings by layer, tell each layer's story
# ----------------------------------------------------------------
if fid == "correlation_system_wide":
layer_findings: dict = {}
for item in chains:
for sf in item.get("correlated_findings", []):
layer = sf.get("layer", "unknown")
ev0 = (sf.get("evidence") or [{}])[0]
proc = (
ev0.get("name") or ev0.get("process") or
ev0.get("ImageFileName") or ev0.get("owner") or ""
)
layer_findings.setdefault(layer, []).append({
"title": sf.get("title") or sf.get("finding_id", "suspicious activity"),
"process": str(proc).strip(),
"pid": str(ev0.get("PID") or ev0.get("pid") or "").strip(),
})
layer_names = sorted(layer_findings.keys())
total = sum(len(v) for v in layer_findings.values())
layer_list = _oxford_join([_friendly_layer_name(l) for l in layer_names])
parts = [
f"This memory image contains {total} high-severity indicator"
f"{'s' if total != 1 else ''} spread across "
f"{len(layer_names)} separate parts of the system at the same time — "
f"the {layer_list}. "
f"When threats appear simultaneously across independent system layers like this, "
f"it means the attacker is not just visiting: they are deeply embedded and actively "
f"operating across the entire machine."
]
for layer in layer_names:
items = layer_findings[layer]
fname = _friendly_layer_name(layer)
titles = [i["title"] for i in items[:3]]
procs = list({
i["process"] for i in items
if i["process"] not in ("", "None", "N/A")
})[:2]
proc_clause = (f", specifically linked to {_oxford_join(procs)}" if procs else "")
title_clause = _oxford_join(titles)
parts.append(
f"In the {fname}{proc_clause}: {title_clause}."
)
parts.append(
"All of this is happening at the same time, which is the most alarming part. "
"A single suspicious process might be a false alarm. Suspicious activity across "
"the kernel, network, and system artifacts simultaneously points to a real, "
"active intrusion. The machine should be isolated immediately and not trusted "
"until a full forensic investigation is complete."
)
return " ".join(parts)
# ----------------------------------------------------------------
# Other correlated findings: brief chain-specific narrative
# ----------------------------------------------------------------
all_titles: list = []
pids: list = []
for item in chains:
pid = item.get("correlated_pid", "")
if pid and pid not in ("system-wide", "None", ""):
pids.append(str(pid))
for sf in item.get("correlated_findings", []):
t = sf.get("title") or sf.get("finding_id", "")
if t:
all_titles.append(t)
confidence = (chains[0].get("confidence", "") if chains else "")
pid_clause = (
f" (PID{'s' if len(pids) > 1 else ''} {', '.join(pids[:3])})" if pids else ""
)
conf_note = {
"strong": "These findings were detected inside the same process",
"medium": "These findings are linked through a parent-child process relationship",
"weak": "These findings co-exist as a behavioral pattern across this memory image",
}.get(confidence, "These findings were correlated")
finding_list = _oxford_join(all_titles[:5])
# Use the static narrative as the opening sentence if available
base = _BUILTIN_NARRATIVES.get(fid, "")
if base:
return f"{base} In this image, {conf_note.lower()}{pid_clause}: {finding_list}."
return (
f"{conf_note}{pid_clause}. The following indicators were detected together: "
f"{finding_list}. Their simultaneous presence suggests coordinated attacker activity "
f"rather than isolated anomalies."
)
def get_narrative(finding_id, detections_config):
# Programmatically-generated findings (correlations not in YAML) use built-in narratives
if finding_id in _BUILTIN_NARRATIVES:
return _BUILTIN_NARRATIVES[finding_id]
if not detections_config: return "Detections config not found."
for os_profile in detections_config.get('os_profiles', {}).values():
for rule in os_profile.get('detections', []):
if rule.get('id') == finding_id:
narrative = rule.get('narrative', 'No narrative available.')
narrative = narrative.replace("psxview mismatch", "hidden process detection anomaly")
narrative = narrative.replace("LdrModules", "loaded modules")
narrative = narrative.replace("ldrmodules", "loaded modules")
if ("network" in narrative.lower() or "c2" in narrative.lower()) and "command and control" not in narrative.lower():
narrative += " This communication often serves as a 'Command and Control' (C2) channel, allowing attackers to remotely send commands and receive data from compromised systems."
return narrative
return "Narrative not found."
def categorize_findings(findings, detections_config):
counts = {"Critical": 0, "High": 0, "Medium": 0, "Low": 0, "Informational": 0}
total_score = 0
severity_bands = detections_config.get('scoring', {}).get('severity_bands', []) if detections_config else []
for f in findings:
weight = f.get('weight', 0)
total_score += weight
for band in severity_bands:
if weight <= int(band['max']):
if band['label'] == "Critical": counts["Critical"] += 1
elif band['label'] == "High": counts["High"] += 1
elif band['label'] == "Medium": counts["Medium"] += 1
elif band['label'] == "Low": counts["Low"] += 1
elif band['label'] == "Informational": counts["Informational"] += 1
break
overall_severity = "Informational"
for band in severity_bands:
if total_score <= int(band['max']):
overall_severity = band['label']
break
return counts, overall_severity, total_score
# ---------------------------------------------------------------------------
# Gemini AI helper
# ---------------------------------------------------------------------------
def query_gemini(api_key: str, finding: dict) -> str:
"""Call Gemini API to explain a finding in plain English."""
url = (
"https://generativelanguage.googleapis.com/v1beta/models/"
f"gemini-2.0-flash:generateContent?key={api_key}"
)
# Allow callers to inject a fully-formed prompt (used by query_llm_correlated)
prompt = finding.get("_prompt_override") or None
if not prompt:
evidence_sample = json.dumps(finding.get("evidence", [])[:3], indent=2, ensure_ascii=False)
prompt = (
f"You are a memory forensics expert. A security analyst is reviewing this finding "
f"from a live memory image analysis. Explain it clearly and concisely.\n\n"
f"Finding Title: {finding.get('title', 'Unknown')}\n"
f"MITRE ATT&CK: {', '.join(finding.get('mitre', []))}\n"
f"Severity Score: {finding.get('weight', 0)}\n"
f"Evidence (sample):\n{evidence_sample}\n\n"
f"Provide exactly three short paragraphs:\n"
f"1. What this finding means in plain English\n"
f"2. Why it is dangerous and what the attacker is likely doing\n"
f"3. The top 2-3 immediate containment/investigation actions the analyst should take"
)
try:
resp = requests.post(
url,
json={"contents": [{"parts": [{"text": prompt}]}]},
timeout=20
)
resp.raise_for_status()
data = resp.json()
return data["candidates"][0]["content"]["parts"][0]["text"]
except requests.exceptions.HTTPError as e:
status = e.response.status_code if e.response is not None else "?"
print(f"[warn] Gemini API HTTP {status}: {e}", file=sys.stderr)
if status == 400:
return "⚠️ AI summary unavailable — Gemini rejected the request (invalid API key or model not available)."
if status == 429:
return "⚠️ AI summary unavailable — Gemini rate limit hit. Wait a moment and try again."
return "⚠️ AI summary unavailable — Gemini returned an error. Check your API key."
except requests.exceptions.Timeout:
return "⚠️ AI summary unavailable — Gemini took too long to respond. Try again shortly."
except Exception as e:
print(f"[warn] Gemini error: {e}", file=sys.stderr)
return "⚠️ AI summary unavailable. Use the local Ollama model as an alternative."
# ---------------------------------------------------------------------------
# Plotly attack-chain network graph
# ---------------------------------------------------------------------------
def render_attack_chain_graph(correlated_finding: dict):
"""Render attack chain as an interactive Plotly network graph.
Reads from `correlated_chains` — the dedicated key for correlation data —
rather than the generic `evidence` field which is used by non-correlation findings.
Each chain item is {"correlated_pid": str, "correlated_findings": [...]}.
"""
chains = correlated_finding.get("correlated_chains", [])
if not chains:
return
for item in chains:
pid = item.get("correlated_pid", "?")
chain_findings = item.get("correlated_findings", [])
confidence = item.get("confidence", "weak")
corr_type = item.get("correlation_type", "")
if not chain_findings:
continue
# Centre node colour encodes correlation strength
_centre_colors = {"strong": "#2ecc71", "medium": "#3498db", "weak": "#e67e22"}
centre_color = _centre_colors.get(confidence, "#8b949e")
# Centre label: special handling for non-PID correlation types
if corr_type == "system_wide":
centre_label = "🌐 System-Wide"
centre_color = "#9b59b6" # purple — distinct from PID-based chains
elif corr_type == "parent_child":
centre_label = f"PID {pid}\n(parent)"
else:
centre_label = f"PID {pid}"
n = len(chain_findings)
radius = 2.2
angles = [math.pi / 2 + 2 * math.pi * i / n for i in range(n)]
# Node data
node_x = [0.0]
node_y = [0.0]
node_labels = [centre_label]
node_colors = [centre_color]
node_sizes = [28]
node_hover = [f"<b>Correlated PID: {pid}</b><br>Confidence: {confidence}"]
severity_colors = {
"psxview_hidden": "#e74c3c", "malfind_injection": "#e74c3c",
"ldr_unlinked_module": "#e74c3c", "handles_lsass_access": "#e74c3c",
"lsass_credential_dump": "#e74c3c", "entropy_anomaly": "#e74c3c",
"suspicious_connection": "#f39c12", "suspicious_network_enrichment": "#f39c12",
"suspicious_port_activity": "#f39c12", "netscan_beacon_like": "#f39c12",
}
edge_x, edge_y = [], []
for i, cf in enumerate(chain_findings):
fx = radius * math.cos(angles[i])
fy = radius * math.sin(angles[i])
node_x.append(fx)
node_y.append(fy)
fid = cf.get("finding_id", "")
title = get_user_friendly_correlated_title(fid)
node_labels.append(title)
node_colors.append(severity_colors.get(fid, "#9b59b6"))
node_sizes.append(20)
ev_count = len(cf.get("evidence", []))
role = cf.get("process_role", "")
role_txt = f"<br>Role: {role}" if role else ""
co_pres = " (co-presence)" if cf.get("co_presence") else ""
node_hover.append(f"<b>{title}</b>{co_pres}<br>Evidence items: {ev_count}{role_txt}")
edge_x += [0, fx, None]
edge_y += [0, fy, None]
fig = go.Figure()
fig.add_trace(go.Scatter(
x=edge_x, y=edge_y, mode="lines",
line=dict(width=1.5, color="#30363d"),
hoverinfo="none", showlegend=False
))
fig.add_trace(go.Scatter(
x=node_x, y=node_y, mode="markers+text",
marker=dict(size=node_sizes, color=node_colors, line=dict(color="#0d1117", width=2)),
text=node_labels,