Spaces:
Running
Running
feat: Integrate PDF, Office, APK, ELF analyzers & updated ML classifiers
Browse files- Integrated PDF, MS Office, Android APK, and Linux ELF heuristic analyzers
- Added multiclass CSV injection model (csv_injection_multiclass_model.joblib)
- Deployed retrained URL models and custom features scaler
- Hardened routers and services for robust API handling
- Updated Dockerfile to containerize all models
backend/app/services/pdf_analyzer.py
CHANGED
|
@@ -11,8 +11,8 @@ from typing import Optional, List, Dict, Any
|
|
| 11 |
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
|
| 14 |
-
# Suspicious PDF
|
| 15 |
-
|
| 16 |
b"/JS": ("Embedded JavaScript", 15.0, "High"),
|
| 17 |
b"/JavaScript": ("Embedded JavaScript Object", 15.0, "High"),
|
| 18 |
b"/Launch": ("External Application Launch", 30.0, "Critical"),
|
|
@@ -24,11 +24,31 @@ SUSPICIOUS_PDF_PATTERNS = {
|
|
| 24 |
b"/XFA": ("XML Forms Architecture", 10.0, "Medium"),
|
| 25 |
b"/URI": ("Embedded Hyperlink Action", 0.0, "Low"),
|
| 26 |
b"/SubmitForm": ("Form submission (exfiltration risk)", 5.0, "Low"),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
b"/Colors >": ("Color table anomaly (heap exploit pattern)", 30.0, "Critical"),
|
| 28 |
}
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
|
| 34 |
class PDFAnalysisResult:
|
|
@@ -111,36 +131,65 @@ class PDFAnalyzerService:
|
|
| 111 |
})
|
| 112 |
risk_score += 5.0
|
| 113 |
|
| 114 |
-
# 3.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
pattern_counts = {}
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
count = content.count(pattern)
|
| 118 |
if count > 0:
|
| 119 |
-
pattern_counts[pattern.decode("ascii")] = count
|
| 120 |
-
# Cap contribution of repeated identical threats to score
|
| 121 |
multiplier = min(count, 3)
|
| 122 |
threats.append({
|
| 123 |
"type": name,
|
| 124 |
"severity": severity,
|
| 125 |
-
"description": f"Found {count} occurrences of {pattern.decode('ascii')} tag/action.",
|
| 126 |
})
|
| 127 |
risk_score += points * multiplier
|
| 128 |
|
| 129 |
-
#
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
count = len(obfuscated_names)
|
| 133 |
-
pattern_counts["obfuscated_names"] = count
|
| 134 |
threats.append({
|
| 135 |
"type": "Obfuscated Tags",
|
| 136 |
"severity": "High",
|
| 137 |
-
"description": f"Detected {
|
| 138 |
})
|
| 139 |
-
risk_score += 25.0 * min(
|
| 140 |
|
| 141 |
# 5. Embedded File / Launch Exploit Signatures
|
| 142 |
# CVE-2010-1240 / Launch commands checking
|
| 143 |
-
if b"/Launch" in
|
| 144 |
threats.append({
|
| 145 |
"type": "Active Launch Exploit",
|
| 146 |
"severity": "Critical",
|
|
@@ -182,9 +231,9 @@ class PDFAnalyzerService:
|
|
| 182 |
"header_valid": header_valid,
|
| 183 |
"eof_valid": eof_valid,
|
| 184 |
"pattern_counts": pattern_counts,
|
| 185 |
-
"has_javascript": b"/JS" in
|
| 186 |
-
"has_auto_execution": b"/OpenAction" in
|
| 187 |
-
"has_embedded_files": b"/EmbeddedFile" in
|
| 188 |
}
|
| 189 |
|
| 190 |
return PDFAnalysisResult(
|
|
|
|
| 11 |
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
|
| 14 |
+
# Suspicious PDF Name tags (must be exact matches of decoded PDF Name objects)
|
| 15 |
+
SUSPICIOUS_PDF_NAMES = {
|
| 16 |
b"/JS": ("Embedded JavaScript", 15.0, "High"),
|
| 17 |
b"/JavaScript": ("Embedded JavaScript Object", 15.0, "High"),
|
| 18 |
b"/Launch": ("External Application Launch", 30.0, "Critical"),
|
|
|
|
| 24 |
b"/XFA": ("XML Forms Architecture", 10.0, "Medium"),
|
| 25 |
b"/URI": ("Embedded Hyperlink Action", 0.0, "Low"),
|
| 26 |
b"/SubmitForm": ("Form submission (exfiltration risk)", 5.0, "Low"),
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
# Suspicious PDF raw binary patterns (checked via direct substring count)
|
| 30 |
+
SUSPICIOUS_PDF_BINARY_PATTERNS = {
|
| 31 |
b"/Colors >": ("Color table anomaly (heap exploit pattern)", 30.0, "Critical"),
|
| 32 |
}
|
| 33 |
|
| 34 |
+
|
| 35 |
+
def decode_pdf_name(name_bytes: bytes) -> bytes:
|
| 36 |
+
"""Decodes hex-encoded characters in a PDF Name object (e.g., /J#61vaScript -> /JavaScript)."""
|
| 37 |
+
result = bytearray()
|
| 38 |
+
i = 0
|
| 39 |
+
n = len(name_bytes)
|
| 40 |
+
while i < n:
|
| 41 |
+
if name_bytes[i] == ord('#') and i + 2 < n:
|
| 42 |
+
try:
|
| 43 |
+
hex_val = int(name_bytes[i+1:i+3], 16)
|
| 44 |
+
result.append(hex_val)
|
| 45 |
+
i += 3
|
| 46 |
+
continue
|
| 47 |
+
except ValueError:
|
| 48 |
+
pass
|
| 49 |
+
result.append(name_bytes[i])
|
| 50 |
+
i += 1
|
| 51 |
+
return bytes(result)
|
| 52 |
|
| 53 |
|
| 54 |
class PDFAnalysisResult:
|
|
|
|
| 131 |
})
|
| 132 |
risk_score += 5.0
|
| 133 |
|
| 134 |
+
# 3. Extract and decode all Name tokens
|
| 135 |
+
NAME_RE = re.compile(b"/[^\\s()<>[\\]{}/%]+")
|
| 136 |
+
raw_names = NAME_RE.findall(content)
|
| 137 |
+
|
| 138 |
+
name_counts = {}
|
| 139 |
+
obfuscated_count = 0
|
| 140 |
+
|
| 141 |
+
for raw_name in raw_names:
|
| 142 |
+
decoded = decode_pdf_name(raw_name)
|
| 143 |
+
|
| 144 |
+
# Check if the decoded Name matches any of our suspicious Name tags
|
| 145 |
+
if decoded in SUSPICIOUS_PDF_NAMES:
|
| 146 |
+
name_counts[decoded] = name_counts.get(decoded, 0) + 1
|
| 147 |
+
if decoded != raw_name:
|
| 148 |
+
# An active suspicious tag is hex-obfuscated (e.g. /J#61vaScript)
|
| 149 |
+
obfuscated_count += 1
|
| 150 |
+
|
| 151 |
+
# 4. Compile threats from name counts and binary patterns
|
| 152 |
pattern_counts = {}
|
| 153 |
+
|
| 154 |
+
# Add Name-based suspicious patterns
|
| 155 |
+
for pattern, (name, points, severity) in SUSPICIOUS_PDF_NAMES.items():
|
| 156 |
+
count = name_counts.get(pattern, 0)
|
| 157 |
+
if count > 0:
|
| 158 |
+
pattern_counts[pattern.decode("ascii", errors="replace")] = count
|
| 159 |
+
multiplier = min(count, 3)
|
| 160 |
+
threats.append({
|
| 161 |
+
"type": name,
|
| 162 |
+
"severity": severity,
|
| 163 |
+
"description": f"Found {count} occurrences of {pattern.decode('ascii', errors='replace')} tag/action.",
|
| 164 |
+
})
|
| 165 |
+
risk_score += points * multiplier
|
| 166 |
+
|
| 167 |
+
# Add binary-based suspicious patterns
|
| 168 |
+
for pattern, (name, points, severity) in SUSPICIOUS_PDF_BINARY_PATTERNS.items():
|
| 169 |
count = content.count(pattern)
|
| 170 |
if count > 0:
|
| 171 |
+
pattern_counts[pattern.decode("ascii", errors="replace")] = count
|
|
|
|
| 172 |
multiplier = min(count, 3)
|
| 173 |
threats.append({
|
| 174 |
"type": name,
|
| 175 |
"severity": severity,
|
| 176 |
+
"description": f"Found {count} occurrences of {pattern.decode('ascii', errors='replace')} tag/action.",
|
| 177 |
})
|
| 178 |
risk_score += points * multiplier
|
| 179 |
|
| 180 |
+
# Add obfuscated tags threat
|
| 181 |
+
if obfuscated_count > 0:
|
| 182 |
+
pattern_counts["obfuscated_names"] = obfuscated_count
|
|
|
|
|
|
|
| 183 |
threats.append({
|
| 184 |
"type": "Obfuscated Tags",
|
| 185 |
"severity": "High",
|
| 186 |
+
"description": f"Detected {obfuscated_count} instances of hex-encoded tag obfuscation (e.g., /#4a#53) used to bypass filters.",
|
| 187 |
})
|
| 188 |
+
risk_score += 25.0 * min(obfuscated_count, 3)
|
| 189 |
|
| 190 |
# 5. Embedded File / Launch Exploit Signatures
|
| 191 |
# CVE-2010-1240 / Launch commands checking
|
| 192 |
+
if b"/Launch" in name_counts and b"/Cmd" in name_counts:
|
| 193 |
threats.append({
|
| 194 |
"type": "Active Launch Exploit",
|
| 195 |
"severity": "Critical",
|
|
|
|
| 231 |
"header_valid": header_valid,
|
| 232 |
"eof_valid": eof_valid,
|
| 233 |
"pattern_counts": pattern_counts,
|
| 234 |
+
"has_javascript": b"/JS" in name_counts or b"/JavaScript" in name_counts,
|
| 235 |
+
"has_auto_execution": b"/OpenAction" in name_counts or b"/AA" in name_counts,
|
| 236 |
+
"has_embedded_files": b"/EmbeddedFile" in name_counts or b"/Launch" in name_counts,
|
| 237 |
}
|
| 238 |
|
| 239 |
return PDFAnalysisResult(
|
urlmodel_info/validate_models.py
CHANGED
|
@@ -122,6 +122,19 @@ def test_pdf_analyzer():
|
|
| 122 |
b"%%EOF\n"
|
| 123 |
)
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
# Create dangerous PDF content containing JavaScript and Launch exploit commands
|
| 126 |
malicious_pdf = (
|
| 127 |
b"%PDF-1.4\n"
|
|
@@ -140,6 +153,13 @@ def test_pdf_analyzer():
|
|
| 140 |
for t in res_b.threats_found:
|
| 141 |
print(f" - Threat: {t['type']} ({t['severity']}) - {t['description']}")
|
| 142 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
# Malicious PDF scan
|
| 144 |
res_m = analyzer.analyze(malicious_pdf, "invoice_exploit.pdf")
|
| 145 |
status_m = "PASS" if res_m.verdict == "Malicious" else "FAIL"
|
|
@@ -149,11 +169,12 @@ def test_pdf_analyzer():
|
|
| 149 |
|
| 150 |
print("\n" + "-"*50)
|
| 151 |
print(f" PDF Validation Summary:")
|
| 152 |
-
print(f" - Benign Document Verdict
|
| 153 |
-
print(f" -
|
|
|
|
| 154 |
print("-"*50)
|
| 155 |
|
| 156 |
-
return res_b.verdict == "Benign" and res_m.verdict == "Malicious"
|
| 157 |
|
| 158 |
|
| 159 |
if __name__ == "__main__":
|
|
|
|
| 122 |
b"%%EOF\n"
|
| 123 |
)
|
| 124 |
|
| 125 |
+
# Create a complex benign PDF with subset fonts and standard hex name escapes that previously caused false positives
|
| 126 |
+
complex_benign_pdf = (
|
| 127 |
+
b"%PDF-1.4\n"
|
| 128 |
+
b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R /AcroForm 3 0 R >>\nendobj\n"
|
| 129 |
+
# Font subset name with "/AAAAAA+Arial" (should NOT trigger /AA)
|
| 130 |
+
b"2 0 obj\n<< /Type /Font /Subtype /Type1 /BaseFont /AAAAAA+Arial >>\nendobj\n"
|
| 131 |
+
# Standard hex escape in font name "/My#20Custom#20Font" (should NOT trigger "Obfuscated Tags")
|
| 132 |
+
b"3 0 obj\n<< /Name /My#20Custom#20Font /Encoding /WinAnsiEncoding >>\nendobj\n"
|
| 133 |
+
# Hex-escaped parameter that is NOT a suspicious tag (should NOT trigger "Obfuscated Tags")
|
| 134 |
+
b"4 0 obj\n<< /Base#20Font /Custom#20Name >>\nendobj\n"
|
| 135 |
+
b"%%EOF\n"
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
# Create dangerous PDF content containing JavaScript and Launch exploit commands
|
| 139 |
malicious_pdf = (
|
| 140 |
b"%PDF-1.4\n"
|
|
|
|
| 153 |
for t in res_b.threats_found:
|
| 154 |
print(f" - Threat: {t['type']} ({t['severity']}) - {t['description']}")
|
| 155 |
|
| 156 |
+
# Complex Benign PDF scan
|
| 157 |
+
res_cb = analyzer.analyze(complex_benign_pdf, "complex_benign.pdf")
|
| 158 |
+
status_cb = "PASS" if res_cb.verdict == "Benign" else "FAIL"
|
| 159 |
+
print(f" [{status_cb}] complex_benign.pdf | Verdict: {res_cb.verdict:<10} | Risk Score: {res_cb.risk_score:<5} | Threats: {len(res_cb.threats_found)}")
|
| 160 |
+
for t in res_cb.threats_found:
|
| 161 |
+
print(f" - Threat: {t['type']} ({t['severity']}) - {t['description']}")
|
| 162 |
+
|
| 163 |
# Malicious PDF scan
|
| 164 |
res_m = analyzer.analyze(malicious_pdf, "invoice_exploit.pdf")
|
| 165 |
status_m = "PASS" if res_m.verdict == "Malicious" else "FAIL"
|
|
|
|
| 169 |
|
| 170 |
print("\n" + "-"*50)
|
| 171 |
print(f" PDF Validation Summary:")
|
| 172 |
+
print(f" - Benign Document Verdict : {res_b.verdict} (Risk: {res_b.risk_score})")
|
| 173 |
+
print(f" - Complex Benign Document Verdict: {res_cb.verdict} (Risk: {res_cb.risk_score})")
|
| 174 |
+
print(f" - Malicious Document Verdict : {res_m.verdict} (Risk: {res_m.risk_score})")
|
| 175 |
print("-"*50)
|
| 176 |
|
| 177 |
+
return res_b.verdict == "Benign" and res_cb.verdict == "Benign" and res_m.verdict == "Malicious"
|
| 178 |
|
| 179 |
|
| 180 |
if __name__ == "__main__":
|