Repository navigation
Expand file tree
/
Copy pathquick_scan.py
More file actions
256 lines (202 loc) · 8.43 KB
/
Copy pathquick_scan.py
File metadata and controls
256 lines (202 loc) · 8.43 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
"""quick_scan.py — Programmatic async interface for SecureNET tools.
Each quick_scan_* function returns a job_id immediately and runs the tool
in a background thread. Use get_result(job_id) to poll status/results.
"""
import json
import os
import subprocess
import sys
import threading
import time
import uuid
# quick_scan.py lives in SecureNET-Control-Panel/, tools live in sibling dirs under the toolkit root
TOOLKIT_ROOT = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
TOOL_MODULES = {
"headerscan": os.path.join(TOOLKIT_ROOT, "HeaderScan", "main.py"),
"portmap": os.path.join(TOOLKIT_ROOT, "PortMap", "main.py"),
"hashdetect": os.path.join(TOOLKIT_ROOT, "HashDetect", "main.py"),
"tlscan": os.path.join(TOOLKIT_ROOT, "TLScan", "main.py"),
"dnsaudit": os.path.join(TOOLKIT_ROOT, "DNSAudit", "main.py"),
"subprobe": os.path.join(TOOLKIT_ROOT, "SubProbe", "main.py"),
"jwtinspect": os.path.join(TOOLKIT_ROOT, "JWTInspect", "main.py"),
"secretsniff": os.path.join(TOOLKIT_ROOT, "SecretSniff", "main.py"),
"vulnprobe": os.path.join(TOOLKIT_ROOT, "VulnProbe", "main.py"),
"pathprobe": os.path.join(TOOLKIT_ROOT, "PathProbe", "main.py"),
"cloudsentry": os.path.join(TOOLKIT_ROOT, "CloudSentry", "main.py"),
"imgscan": os.path.join(TOOLKIT_ROOT, "ImgScan", "main.py"),
"techfinger": os.path.join(TOOLKIT_ROOT, "TechFinger", "main.py"),
"apiguard": os.path.join(TOOLKIT_ROOT, "APIGuard", "main.py"),
}
_jobs = {}
_jobs_lock = threading.Lock()
def _run_tool(job_id, tool_key, args):
"""Run a tool subprocess and store its output in the job dict."""
script = os.path.normpath(TOOL_MODULES[tool_key])
workdir = os.path.dirname(script)
# Use list form + cwd to avoid shell-escaping issues on Windows
cmd = [sys.executable, script] + args
with _jobs_lock:
_jobs[job_id]["status"] = "running"
try:
proc = subprocess.run(
cmd,
cwd=workdir,
capture_output=True,
text=True,
timeout=300,
# Avoid inherited stdin/stderr handles that can trigger WinError 267
stdin=subprocess.DEVNULL,
)
with _jobs_lock:
_jobs[job_id]["status"] = "complete"
_jobs[job_id]["returncode"] = proc.returncode
_jobs[job_id]["stdout"] = proc.stdout
_jobs[job_id]["stderr"] = proc.stderr
_jobs[job_id]["completed_at"] = time.time()
except subprocess.TimeoutExpired:
with _jobs_lock:
_jobs[job_id]["status"] = "complete"
_jobs[job_id]["returncode"] = -1
_jobs[job_id]["stdout"] = ""
_jobs[job_id]["stderr"] = "Timed out after 300s"
_jobs[job_id]["completed_at"] = time.time()
except Exception as e:
with _jobs_lock:
_jobs[job_id]["status"] = "complete"
_jobs[job_id]["returncode"] = -1
_jobs[job_id]["stdout"] = ""
_jobs[job_id]["stderr"] = str(e)
_jobs[job_id]["completed_at"] = time.time()
def _start_job(tool_key, args):
"""Create a job entry and launch the tool in a background thread."""
job_id = str(uuid.uuid4())[:12]
with _jobs_lock:
_jobs[job_id] = {
"status": "pending",
"tool": tool_key,
"args": args,
"created_at": time.time(),
"returncode": None,
"stdout": "",
"stderr": "",
"completed_at": None,
}
t = threading.Thread(target=_run_tool, args=(job_id, tool_key, args), daemon=True)
t.start()
return job_id
def quick_scan_headerscan(url):
"""Start a HeaderScan on a URL. Returns job_id."""
return _start_job("headerscan", [url, "--json"])
def quick_scan_portmap(host):
"""Start a PortMap scan on a host. Returns job_id."""
return _start_job("portmap", [host, "--yes"])
def quick_scan_hashdetect(hash_str):
"""Start a HashDetect on a hash string. Returns job_id."""
return _start_job("hashdetect", [hash_str])
def quick_scan_tlscan(domain):
"""Start a TLScan on a domain. Returns job_id."""
return _start_job("tlscan", [domain, "--no-disclaimer", "--json"])
def quick_scan_dnsaudit(domain):
"""Start a DNSAudit on a domain. Returns job_id."""
return _start_job("dnsaudit", [domain, "--json", "--no-disclaimer"])
def quick_scan_subprobe(domain):
"""Start a SubProbe on a domain. Returns job_id."""
return _start_job("subprobe", [domain, "--no-disclaimer"])
def quick_scan_jwtinspect(token):
"""Start a JWTInspect on a JWT token. Returns job_id."""
return _start_job("jwtinspect", [token, "--no-disclaimer"])
def quick_scan_secretsniff(path):
"""Start a SecretSniff scan on a path. Returns job_id."""
return _start_job("secretsniff", ["scan", "--path", path])
def quick_scan_vulnprobe(url, severity="HIGH,CRITICAL"):
"""Start a VulnProbe template scan on a URL. Returns job_id.
Args:
url: target URL (or bare domain).
severity: comma-separated severities to include (default HIGH,CRITICAL).
"""
args = [url, "--no-disclaimer", "--severity", severity]
return _start_job("vulnprobe", args)
def quick_scan_pathprobe(url, wordlist="common"):
"""Start a PathProbe content-discovery scan on a URL. Returns job_id.
Args:
url: target URL (or bare domain).
wordlist: built-in name (common/api/files/large) or path to a file.
"""
wl = wordlist or "common"
args = [url, "--wordlist", wl, "--no-disclaimer"]
return _start_job("pathprobe", args)
def quick_scan_cloudsentry(provider="aws", profile=None):
"""Start a CloudSentry multi-cloud posture audit. Returns job_id.
Args:
provider: aws | gcp | azure (or comma-separated for multi-cloud).
profile: optional AWS profile / GCP project / Azure subscription.
"""
prov = provider or "aws"
args = ["--provider", prov.replace(",", " "), "--no-disclaimer"]
if profile:
args += ["--profile", profile]
return _start_job("cloudsentry", args)
def quick_scan_imgscan(path="./myproject"):
"""Start an ImgScan dependency/container CVE scan. Returns job_id.
Args:
path: target directory, manifest, SBOM, or Dockerfile to scan.
"""
return _start_job("imgscan", ["scan", "--path", path, "--no-disclaimer"])
def quick_scan_techfinger(url="https://example.com"):
"""Start a TechFinger fingerprint scan. Returns job_id.
Args:
url: target URL to fingerprint.
"""
return _start_job("techfinger", [url, "--no-disclaimer"])
def quick_scan_apiguard(url="https://api.example.com", auth="none", spec=""):
"""Start an APIGuard scan on an API URL. Returns job_id.
Args:
url: target API base URL.
auth: auth string (bearer TOKEN, etc.).
spec: optional OpenAPI spec file path.
"""
args = [url, "--auth", auth, "--no-disclaimer"]
if spec:
args += ["--spec", spec]
return _start_job("apiguard", args)
def get_result(job_id):
"""Get the status/results for a job.
Returns a dict with keys: status, tool, stdout, stderr, returncode, created_at, completed_at.
Status is one of: pending, running, complete.
"""
with _jobs_lock:
job = _jobs.get(job_id)
if job is None:
return {"status": "not_found", "error": f"No job with id {job_id}"}
return {
"status": job["status"],
"tool": job["tool"],
"stdout": job["stdout"],
"stderr": job["stderr"],
"returncode": job["returncode"],
"created_at": job["created_at"],
"completed_at": job["completed_at"],
}
def list_jobs():
"""Return a summary of all jobs."""
with _jobs_lock:
return {
jid: {"status": j["status"], "tool": j["tool"], "created_at": j["created_at"]}
for jid, j in _jobs.items()
}
if __name__ == "__main__":
# ponytail: minimal self-check, remove when wiring into the dashboard
print("quick_scan.py — SecureNET async tool runner")
print(f"Jobs store: {len(_jobs)} jobs")
# Quick smoke test
jid = quick_scan_headerscan("https://example.com")
print(f"Started headerscan job: {jid}")
for _ in range(30):
r = get_result(jid)
if r["status"] == "complete":
print(f"Job complete. Return code: {r['returncode']}")
print(r["stdout"][:500] if r["stdout"] else "(no output)")
break
time.sleep(1)
else:
print("Job did not complete in 30s")