-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport.py
More file actions
194 lines (164 loc) · 6.87 KB
/
Copy pathreport.py
File metadata and controls
194 lines (164 loc) · 6.87 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
"""PDF report generation for StealthOps."""
from __future__ import annotations
import re
from datetime import datetime, timezone
from pathlib import Path
from typing import Any
# Matches "Label: value" lines (with optional leading whitespace / "- " prefix)
_LABEL_VALUE_RE = re.compile(r'^(\s*(?:-\s+)?)([A-Za-z][^:\n]{0,45}?)(: )(.+)$')
# Matches standalone sub-section labels like "Registrant:" or "Name Servers:"
_SUBLABEL_RE = re.compile(r'^(\s*(?:-\s+)?)([A-Za-z][^:\n]{0,45}?):$')
# Matches enrichment provider headers like [virustotal]
_PROVIDER_HDR_RE = re.compile(r'^\[([a-zA-Z0-9_]+)\]$')
# Colors
_NAVY = (15, 45, 90) # title block
_TEAL = (0, 75, 110) # section headers
_TEAL_L = (215, 232, 240) # provider sub-header background
_LIGHT = (220, 235, 245) # text on dark backgrounds
_BODY = (20, 20, 20) # body text
def _strip_ansi(text: str) -> str:
return re.sub(r'\x1b\[[0-9;]*m', '', text)
def _safe(text: str) -> str:
"""Strip ANSI codes and coerce to Latin-1 for PDF core fonts."""
return _strip_ansi(text).encode("latin-1", errors="replace").decode("latin-1")
def _resolve_path(target: str, out_path: str | None) -> Path:
if out_path:
return Path(out_path).expanduser().resolve()
safe_target = re.sub(r'[^\w.\-]', '_', target)
ts = datetime.now().strftime("%Y%m%d-%H%M%S")
downloads = Path.home() / "Downloads"
base_dir = downloads if downloads.is_dir() else Path.home()
return base_dir / f"stealthops-{safe_target}-{ts}.pdf"
def _load_fonts(pdf: Any) -> tuple[str, str]:
"""
Try to load modern system fonts. Returns (header_font, mono_font).
Falls back to Helvetica/Courier (always available as PDF core fonts).
"""
font_dirs = [Path("C:/Windows/Fonts"), Path("/mnt/c/Windows/Fonts")]
win_fonts = next((d for d in font_dirs if d.is_dir()), font_dirs[0])
candidates = [
(win_fonts / "segoeui.ttf", win_fonts / "segoeuib.ttf",
win_fonts / "consola.ttf", win_fonts / "consolab.ttf",
"SegoeUI", "Consolas"),
(win_fonts / "calibri.ttf", win_fonts / "calibrib.ttf",
win_fonts / "consola.ttf", win_fonts / "consolab.ttf",
"Calibri", "Consolas"),
]
for hf, hfb, mf, mfb, hname, mname in candidates:
if all(p.is_file() for p in (hf, hfb, mf, mfb)):
try:
pdf.add_font(hname, "", str(hf))
pdf.add_font(hname, "B", str(hfb))
pdf.add_font(mname, "", str(mf))
pdf.add_font(mname, "B", str(mfb))
return hname, mname
except Exception:
pass
return "Helvetica", "Courier"
def _section_label(line: str) -> str:
"""Extract readable label from a '=== SECTION === [source: ...]' line."""
parts = [p.strip() for p in line.split("===") if p.strip()]
if not parts:
return line.strip()
title = parts[0]
if len(parts) >= 2:
source = parts[1].strip("[] ").strip()
return f"{title} | {source}"
return title
def generate_report(
target: str,
result: dict[str, Any],
out_path: str | None = None,
route_mode: str = "public",
) -> Path:
"""Generate a PDF intelligence report from a query result dict. Returns the saved path."""
try:
from fpdf import FPDF
except ImportError as exc:
raise RuntimeError(
"PDF generation requires fpdf2. Install with: pip install fpdf2"
) from exc
from formatter import format_cli_report
body_text = _safe(format_cli_report(result, full=True))
consensus_marker = "=== ENRICHMENT CONSENSUS ==="
if consensus_marker in body_text:
body_text = body_text[:body_text.index(consensus_marker)].rstrip()
ts_str = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
line_h = 4.5
class _Doc(FPDF):
def footer(self) -> None:
self.set_y(-12)
self.set_font("Helvetica", "", 7)
self.set_text_color(150, 150, 150)
self.cell(0, 6, txt=_safe(f"Page {self.page_no()} | StealthOps | {target}"),
align="C", new_x="LMARGIN", new_y="NEXT")
pdf = _Doc(orientation="P", unit="mm", format="A4")
pdf.set_auto_page_break(auto=True, margin=18)
pdf.set_margins(left=12, top=12, right=12)
hdr_font, mono_font = _load_fonts(pdf)
pdf.add_page()
# ---- Title block ----
pdf.set_fill_color(*_NAVY)
pdf.set_text_color(*_LIGHT)
pdf.set_font(hdr_font, "B", 15)
pdf.cell(0, 11, txt="StealthOps Intelligence Report", fill=True,
new_x="LMARGIN", new_y="NEXT", align="C")
pdf.set_font(hdr_font, "", 8)
pdf.cell(0, 7,
txt=_safe(f"Target: {target} | Generated: {ts_str} | Route: {route_mode}"),
fill=True, new_x="LMARGIN", new_y="NEXT", align="C")
pdf.set_fill_color(255, 255, 255)
pdf.set_text_color(*_BODY)
pdf.ln(5)
# ---- Body ----
for raw_line in body_text.splitlines():
line = raw_line.replace("\t", " ")
if line.startswith("==="):
pdf.ln(1)
pdf.set_font(hdr_font, "B", 9)
pdf.set_fill_color(*_TEAL)
pdf.set_text_color(*_LIGHT)
pdf.cell(0, 6, txt=_section_label(line), fill=True,
new_x="LMARGIN", new_y="NEXT")
pdf.set_text_color(*_BODY)
pdf.ln(1)
elif _PROVIDER_HDR_RE.match(line):
pdf.ln(1)
pdf.set_font(hdr_font, "B", 8)
pdf.set_fill_color(*_TEAL_L)
pdf.set_text_color(*_TEAL)
pdf.cell(0, 5, txt=line, fill=True, new_x="LMARGIN", new_y="NEXT")
pdf.set_text_color(*_BODY)
pdf.ln(0.5)
elif not line.strip():
pdf.ln(2)
else:
lv = _LABEL_VALUE_RE.match(line)
sl = _SUBLABEL_RE.match(line)
if lv:
prefix, label, _, value = lv.groups()
if prefix:
pdf.set_font(mono_font, "", 7.5)
pdf.write(line_h, txt=prefix)
pdf.set_font(mono_font, "B", 7.5)
pdf.write(line_h, txt=label + ":")
pdf.set_font(mono_font, "", 7.5)
pdf.write(line_h, txt=" " + value)
pdf.ln(line_h)
elif sl:
prefix, label = sl.groups()
if prefix:
pdf.set_font(mono_font, "", 7.5)
pdf.write(line_h, txt=prefix)
pdf.set_font(mono_font, "B", 7.5)
pdf.write(line_h, txt=label + ":")
pdf.ln(line_h)
else:
pdf.set_font(mono_font, "", 7.5)
pdf.cell(0, line_h, txt=line, new_x="LMARGIN", new_y="NEXT")
dest = _resolve_path(target, out_path)
dest.parent.mkdir(parents=True, exist_ok=True)
import contextlib, io
with contextlib.redirect_stderr(io.StringIO()):
pdf.output(str(dest))
return dest