dexsect 4.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dexsect/__init__.py +4 -0
- dexsect/__main__.py +5 -0
- dexsect/catalog/EXTRACTION_CATALOG.json +4057 -0
- dexsect/catalog/__init__.py +0 -0
- dexsect/cli.py +1272 -0
- dexsect/core/__init__.py +0 -0
- dexsect/core/config.py +177 -0
- dexsect/core/context.py +181 -0
- dexsect/core/i18n.py +40 -0
- dexsect/core/pipeline.py +153 -0
- dexsect/core/platform_utils.py +164 -0
- dexsect/core/runner.py +124 -0
- dexsect/core/theme.py +64 -0
- dexsect/core/ui.py +405 -0
- dexsect/core/utils.py +150 -0
- dexsect/inspectors/__init__.py +0 -0
- dexsect/inspectors/assets_db_inspector.py +235 -0
- dexsect/inspectors/base.py +46 -0
- dexsect/inspectors/bundle_inspector.py +55 -0
- dexsect/inspectors/code_inspector.py +341 -0
- dexsect/inspectors/custom_rules_inspector.py +178 -0
- dexsect/inspectors/dex_inspector.py +144 -0
- dexsect/inspectors/manifest_inspector.py +503 -0
- dexsect/inspectors/native_fw_inspector.py +167 -0
- dexsect/inspectors/resources_inspector.py +195 -0
- dexsect/inspectors/scanner_inspector.py +153 -0
- dexsect/inspectors/signing_inspector.py +423 -0
- dexsect/inspectors/text_scanner.py +336 -0
- dexsect/locales/__init__.py +0 -0
- dexsect/locales/ar.json +130 -0
- dexsect/locales/de.json +51 -0
- dexsect/locales/en.json +110 -0
- dexsect/locales/es.json +51 -0
- dexsect/locales/fr.json +51 -0
- dexsect/locales/ja.json +51 -0
- dexsect/locales/pt.json +51 -0
- dexsect/locales/ru.json +51 -0
- dexsect/locales/tr.json +51 -0
- dexsect/locales/zh.json +51 -0
- dexsect/parsers/__init__.py +0 -0
- dexsect/parsers/axml.py +264 -0
- dexsect/parsers/bundle.py +214 -0
- dexsect/parsers/elf.py +396 -0
- dexsect/phases/__init__.py +0 -0
- dexsect/phases/base.py +102 -0
- dexsect/phases/decode.py +507 -0
- dexsect/phases/decompile.py +408 -0
- dexsect/phases/fingerprint.py +341 -0
- dexsect/phases/frameworks.py +377 -0
- dexsect/phases/mapper.py +41 -0
- dexsect/phases/native.py +188 -0
- dexsect/plugins/__init__.py +0 -0
- dexsect/plugins/base.py +20 -0
- dexsect/plugins/diff.py +162 -0
- dexsect/plugins/inspect.py +172 -0
- dexsect/plugins/report.py +80 -0
- dexsect/plugins/trace.py +141 -0
- dexsect/reporting/__init__.py +0 -0
- dexsect/reporting/analysis_report.py +1190 -0
- dexsect/reporting/diff_report.py +144 -0
- dexsect/reporting/sarif_report.py +109 -0
- dexsect/reporting/unpack_map.py +149 -0
- dexsect/rules/__init__.py +0 -0
- dexsect/rules/anti_analysis.json +45 -0
- dexsect/rules/crypto_audit.json +41 -0
- dexsect/rules/secrets_extended.json +46 -0
- dexsect/storage/__init__.py +0 -0
- dexsect/storage/findings_db.py +460 -0
- dexsect-4.1.0.dist-info/METADATA +532 -0
- dexsect-4.1.0.dist-info/RECORD +74 -0
- dexsect-4.1.0.dist-info/WHEEL +5 -0
- dexsect-4.1.0.dist-info/entry_points.txt +2 -0
- dexsect-4.1.0.dist-info/licenses/LICENSE +21 -0
- dexsect-4.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import os
|
|
3
|
+
import time
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from typing import Any, Dict, List, Set
|
|
6
|
+
|
|
7
|
+
from ..core.context import ArtifactUnit, PipelineContext
|
|
8
|
+
from ..parsers.elf import ELF_MAGIC, parse_elf
|
|
9
|
+
from ..storage.findings_db import FindingItem, FindingsDB
|
|
10
|
+
from .base import BaseInspector, InspectorResult
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _is_elf(path: Path) -> bool:
|
|
14
|
+
try:
|
|
15
|
+
with path.open("rb") as f:
|
|
16
|
+
return f.read(4) == ELF_MAGIC
|
|
17
|
+
except OSError:
|
|
18
|
+
return False
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class NativeFwInspector(BaseInspector):
|
|
22
|
+
name = "native_fw"
|
|
23
|
+
description = "Inspect ELF hardening, BuildID, JNI cross-refs, and unpacked framework artifacts"
|
|
24
|
+
domains = ["Native Libraries", "Frameworks"]
|
|
25
|
+
catalog_ids = [
|
|
26
|
+
"NAT-001", "NAT-002", "NAT-003", "NAT-004", "NAT-005",
|
|
27
|
+
"FLT-001", "RN-001", "UNT-001", "WEB-001", "XAM-001",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
def run(
|
|
31
|
+
self,
|
|
32
|
+
ctx: PipelineContext,
|
|
33
|
+
db: FindingsDB,
|
|
34
|
+
units: List[ArtifactUnit],
|
|
35
|
+
profile: str,
|
|
36
|
+
) -> InspectorResult:
|
|
37
|
+
t0 = time.time()
|
|
38
|
+
findings: List[FindingItem] = []
|
|
39
|
+
summary: Dict[str, Any] = {}
|
|
40
|
+
|
|
41
|
+
java_native_methods: Dict[str, Set[str]] = {}
|
|
42
|
+
for r in db.query_findings(catalog_ids=["COD-003"]):
|
|
43
|
+
cls = r.get("class_name") or ""
|
|
44
|
+
mth = r.get("method_name") or ""
|
|
45
|
+
if cls and mth:
|
|
46
|
+
java_native_methods.setdefault(r["unit_id"], set()).add(f"{cls}.{mth}")
|
|
47
|
+
|
|
48
|
+
for unit in units:
|
|
49
|
+
dec = Path(unit.decode_dir) if unit.decode_dir else None
|
|
50
|
+
fw_dir = Path(unit.frameworks_dir) if unit.frameworks_dir else None
|
|
51
|
+
|
|
52
|
+
so_candidates: List[Path] = []
|
|
53
|
+
if unit.custom_so_paths:
|
|
54
|
+
so_candidates.extend(Path(p) for p in unit.custom_so_paths if Path(p).is_file())
|
|
55
|
+
if dec and dec.is_dir():
|
|
56
|
+
existing_set = set(so_candidates)
|
|
57
|
+
for root, _dirs, files in os.walk(dec):
|
|
58
|
+
rel_root = Path(root).relative_to(dec).as_posix()
|
|
59
|
+
if rel_root.startswith(("smali", "res", "META-INF")):
|
|
60
|
+
continue
|
|
61
|
+
for fn in sorted(files):
|
|
62
|
+
p = Path(root) / fn
|
|
63
|
+
if p in existing_set:
|
|
64
|
+
continue
|
|
65
|
+
if fn.lower().endswith(".so") or (p.suffix == "" and p.stat().st_size >= 64 and _is_elf(p)):
|
|
66
|
+
if not unit.custom_so_paths:
|
|
67
|
+
so_candidates.append(p)
|
|
68
|
+
elif not rel_root.startswith("lib"):
|
|
69
|
+
so_candidates.append(p)
|
|
70
|
+
|
|
71
|
+
jni_matched = 0
|
|
72
|
+
for so_p in sorted(set(so_candidates)):
|
|
73
|
+
rel = so_p.relative_to(dec).as_posix() if (dec and so_p.is_relative_to(dec)) else so_p.name
|
|
74
|
+
loc_tag = so_p.parent.name
|
|
75
|
+
elf = parse_elf(so_p)
|
|
76
|
+
|
|
77
|
+
findings.append(FindingItem(
|
|
78
|
+
catalog_id="NAT-001", domain="Native Libraries", subdomain="ELF",
|
|
79
|
+
unit_id=unit.unit_id, layer="native", title=so_p.name,
|
|
80
|
+
value=f"{rel} (dir={loc_tag}, size={elf['size_bytes']})", file_path=str(so_p),
|
|
81
|
+
))
|
|
82
|
+
findings.append(FindingItem(
|
|
83
|
+
catalog_id="NAT-002", domain="Native Libraries", subdomain="ELF",
|
|
84
|
+
unit_id=unit.unit_id, layer="native", title=f"ELF Header ({so_p.name})",
|
|
85
|
+
value=f"{elf['machine']} | {elf['elf_type']} | build_id={elf['build_id'] or '-'} | needed={elf['needed_libs']}",
|
|
86
|
+
file_path=str(so_p), extra={"needed": elf["needed_libs"], "soname": elf["soname"], "build_id": elf["build_id"]},
|
|
87
|
+
))
|
|
88
|
+
hard = {"nx": elf["nx"], "relro": elf["relro"], "pie": elf["pie"], "stripped": elf["stripped"]}
|
|
89
|
+
findings.append(FindingItem(
|
|
90
|
+
catalog_id="NAT-003", domain="Native Libraries", subdomain="Hardening",
|
|
91
|
+
unit_id=unit.unit_id, layer="native", title=f"ELF Hardening ({so_p.name})",
|
|
92
|
+
value=f"NX={elf['nx']}, RELRO={elf['relro']}, PIE={elf['pie']}, stripped={elf['stripped']}",
|
|
93
|
+
file_path=str(so_p), extra=hard,
|
|
94
|
+
))
|
|
95
|
+
|
|
96
|
+
if profile == "deep":
|
|
97
|
+
for sym in elf["exported_functions"][:500]:
|
|
98
|
+
findings.append(FindingItem(
|
|
99
|
+
catalog_id="NAT-004", domain="Native Libraries", subdomain="Symbols",
|
|
100
|
+
unit_id=unit.unit_id, layer="native", title=f"Export ({so_p.name})",
|
|
101
|
+
value=sym, file_path=str(so_p),
|
|
102
|
+
))
|
|
103
|
+
|
|
104
|
+
declared_set = java_native_methods.get(unit.unit_id, set())
|
|
105
|
+
for j in elf["jni_exports"]:
|
|
106
|
+
dem = j["demangled"]
|
|
107
|
+
base_sig = dem.split("(", 1)[0]
|
|
108
|
+
java_match = base_sig in declared_set
|
|
109
|
+
if java_match:
|
|
110
|
+
jni_matched += 1
|
|
111
|
+
findings.append(FindingItem(
|
|
112
|
+
catalog_id="NAT-005", domain="Native Libraries", subdomain="JNI",
|
|
113
|
+
unit_id=unit.unit_id, layer="native", title=f"JNI ({so_p.name})",
|
|
114
|
+
value=f"{dem} [raw={j['symbol']}, java_match={java_match}]",
|
|
115
|
+
file_path=str(so_p), extra={"raw": j["symbol"], "java_match": java_match},
|
|
116
|
+
))
|
|
117
|
+
|
|
118
|
+
for fw in unit.framework_plan:
|
|
119
|
+
ftype = fw.get("framework")
|
|
120
|
+
if ftype == "Flutter":
|
|
121
|
+
pkgs = []
|
|
122
|
+
if fw_dir and (fw_dir / "flutter" / "dart_packages.txt").is_file():
|
|
123
|
+
pkgs = (fw_dir / "flutter" / "dart_packages.txt").read_text(encoding="utf-8", errors="ignore").splitlines()
|
|
124
|
+
findings.append(FindingItem(
|
|
125
|
+
catalog_id="FLT-001", domain="Frameworks", subdomain="Flutter",
|
|
126
|
+
unit_id=unit.unit_id, layer="framework", title="Flutter Metadata",
|
|
127
|
+
value=f"mode={fw.get('mode')}, dart_packages={len(pkgs)}",
|
|
128
|
+
extra={"packages_sample": pkgs[:30]},
|
|
129
|
+
))
|
|
130
|
+
elif ftype == "ReactNative":
|
|
131
|
+
for b in fw.get("bundles", []):
|
|
132
|
+
findings.append(FindingItem(
|
|
133
|
+
catalog_id="RN-001", domain="Frameworks", subdomain="React Native",
|
|
134
|
+
unit_id=unit.unit_id, layer="framework", title=b.get("rel_path", "bundle"),
|
|
135
|
+
value=f"format={b.get('format')}, hermes_version={b.get('hermes_version')}",
|
|
136
|
+
file_path=b.get("path", ""), extra=b,
|
|
137
|
+
))
|
|
138
|
+
elif ftype in ("Unity_IL2CPP", "Unity_Mono"):
|
|
139
|
+
findings.append(FindingItem(
|
|
140
|
+
catalog_id="UNT-001", domain="Frameworks", subdomain="Unity",
|
|
141
|
+
unit_id=unit.unit_id, layer="framework", title=ftype,
|
|
142
|
+
value=json.dumps(fw.get("metadata_info") or {"dlls": len(fw.get("dll_paths", []))}),
|
|
143
|
+
extra=fw,
|
|
144
|
+
))
|
|
145
|
+
elif ftype == "WebHybrid":
|
|
146
|
+
findings.append(FindingItem(
|
|
147
|
+
catalog_id="WEB-001", domain="Frameworks", subdomain="Web Hybrid",
|
|
148
|
+
unit_id=unit.unit_id, layer="framework", title="Web Directories",
|
|
149
|
+
value=", ".join(fw.get("web_dirs", [])),
|
|
150
|
+
))
|
|
151
|
+
elif ftype == "Xamarin":
|
|
152
|
+
findings.append(FindingItem(
|
|
153
|
+
catalog_id="XAM-001", domain="Frameworks", subdomain="Xamarin",
|
|
154
|
+
unit_id=unit.unit_id, layer="framework", title="Xamarin Assemblies",
|
|
155
|
+
value=f"dlls={len(fw.get('dll_paths', []))}, blobs={len(fw.get('blobs', []))}",
|
|
156
|
+
))
|
|
157
|
+
|
|
158
|
+
summary[unit.unit_id] = {"so_inspected": len(so_candidates), "jni_java_matched": jni_matched}
|
|
159
|
+
|
|
160
|
+
inserted = db.insert_findings(findings)
|
|
161
|
+
return InspectorResult(
|
|
162
|
+
name=self.name,
|
|
163
|
+
status="COMPLETED",
|
|
164
|
+
duration_sec=round(time.time() - t0, 3),
|
|
165
|
+
findings_count=inserted,
|
|
166
|
+
summary=summary,
|
|
167
|
+
)
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import os
|
|
3
|
+
import re
|
|
4
|
+
import time
|
|
5
|
+
import xml.etree.ElementTree as ET
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Dict, List
|
|
8
|
+
|
|
9
|
+
from ..core.context import ArtifactUnit, PipelineContext
|
|
10
|
+
from ..storage.findings_db import FindingItem, FindingsDB
|
|
11
|
+
from .base import BaseInspector, InspectorResult
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
CLOUD_STRING_KEYS = {
|
|
15
|
+
"google_api_key", "google_app_id", "firebase_database_url", "project_id",
|
|
16
|
+
"gcm_defaultSenderId", "google_storage_bucket", "google_crash_reporting_api_key",
|
|
17
|
+
"default_web_client_id", "facebook_app_id", "fb_login_protocol_scheme",
|
|
18
|
+
"com.crashlytics.ApiKey", "io.fabric.ApiKey", "sentry_dsn", "appsflyer_dev_key",
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class ResourcesInspector(BaseInspector):
|
|
23
|
+
name = "resources"
|
|
24
|
+
description = "Extract RES-* resource maps/strings/locales and CFG-001..003 cloud/env configs"
|
|
25
|
+
domains = ["Resources", "Configuration & IDs"]
|
|
26
|
+
catalog_ids = ["RES-001", "RES-002", "RES-003", "RES-004", "RES-005", "CFG-001", "CFG-002", "CFG-003"]
|
|
27
|
+
|
|
28
|
+
def run(
|
|
29
|
+
self,
|
|
30
|
+
ctx: PipelineContext,
|
|
31
|
+
db: FindingsDB,
|
|
32
|
+
units: List[ArtifactUnit],
|
|
33
|
+
profile: str,
|
|
34
|
+
) -> InspectorResult:
|
|
35
|
+
t0 = time.time()
|
|
36
|
+
findings: List[FindingItem] = []
|
|
37
|
+
summary: Dict[str, Dict] = {}
|
|
38
|
+
|
|
39
|
+
for unit in units:
|
|
40
|
+
dec = Path(unit.decode_dir) if unit.decode_dir else None
|
|
41
|
+
if not dec or not dec.is_dir():
|
|
42
|
+
continue
|
|
43
|
+
|
|
44
|
+
res_dir = dec / "res"
|
|
45
|
+
locales: List[str] = []
|
|
46
|
+
ui_counts = {"layout": 0, "menu": 0, "navigation": 0, "drawable": 0, "font": 0}
|
|
47
|
+
xml_raw_files: List[str] = []
|
|
48
|
+
strings_count = 0
|
|
49
|
+
public_count = 0
|
|
50
|
+
|
|
51
|
+
if res_dir.is_dir():
|
|
52
|
+
for entry in sorted(res_dir.iterdir()):
|
|
53
|
+
if not entry.is_dir():
|
|
54
|
+
continue
|
|
55
|
+
dname = entry.name
|
|
56
|
+
if dname.startswith("values-"):
|
|
57
|
+
locales.append(dname[7:])
|
|
58
|
+
for cat in ui_counts:
|
|
59
|
+
if dname == cat or dname.startswith(cat + "-"):
|
|
60
|
+
ui_counts[cat] += sum(1 for f in entry.iterdir() if f.is_file())
|
|
61
|
+
if dname in ("xml", "raw") or dname.startswith(("xml-", "raw-")):
|
|
62
|
+
for f in sorted(entry.iterdir()):
|
|
63
|
+
if f.is_file():
|
|
64
|
+
rel = f.relative_to(dec).as_posix()
|
|
65
|
+
xml_raw_files.append(rel)
|
|
66
|
+
findings.append(FindingItem(
|
|
67
|
+
catalog_id="RES-004", domain="Resources", subdomain="Files",
|
|
68
|
+
unit_id=unit.unit_id, layer="resources", title=dname,
|
|
69
|
+
value=rel, file_path=str(f),
|
|
70
|
+
))
|
|
71
|
+
|
|
72
|
+
if locales:
|
|
73
|
+
findings.append(FindingItem(
|
|
74
|
+
catalog_id="RES-003", domain="Resources", subdomain="Qualifiers",
|
|
75
|
+
unit_id=unit.unit_id, layer="resources", title="Locales",
|
|
76
|
+
value=", ".join(sorted(set(locales))),
|
|
77
|
+
))
|
|
78
|
+
|
|
79
|
+
findings.append(FindingItem(
|
|
80
|
+
catalog_id="RES-005", domain="Resources", subdomain="UI",
|
|
81
|
+
unit_id=unit.unit_id, layer="resources", title="Resource Counts",
|
|
82
|
+
value=json.dumps(ui_counts), extra=ui_counts,
|
|
83
|
+
))
|
|
84
|
+
|
|
85
|
+
# Parse default strings.xml (and values-*/strings.xml in deep mode)
|
|
86
|
+
sxml_list = [res_dir / "values" / "strings.xml"]
|
|
87
|
+
if profile == "deep":
|
|
88
|
+
sxml_list = sorted(res_dir.glob("values*/strings.xml"))
|
|
89
|
+
for sxml in sxml_list:
|
|
90
|
+
if not sxml.is_file():
|
|
91
|
+
continue
|
|
92
|
+
try:
|
|
93
|
+
root = ET.parse(sxml).getroot()
|
|
94
|
+
for el in root.findall("string"):
|
|
95
|
+
k = el.attrib.get("name", "")
|
|
96
|
+
v = (el.text or "").strip()
|
|
97
|
+
if not k:
|
|
98
|
+
continue
|
|
99
|
+
strings_count += 1
|
|
100
|
+
if k in CLOUD_STRING_KEYS or any(tok in k.lower() for tok in ("firebase", "api_key", "client_id", "sentry", "dsn")):
|
|
101
|
+
cid = "CFG-001" if ("firebase" in k.lower() or "google" in k.lower() or "gcm" in k.lower()) else "CFG-002"
|
|
102
|
+
findings.append(FindingItem(
|
|
103
|
+
catalog_id=cid, domain="Configuration & IDs", subdomain="Cloud/Auth",
|
|
104
|
+
unit_id=unit.unit_id, layer="resources", title=k,
|
|
105
|
+
value=v, file_path=str(sxml),
|
|
106
|
+
))
|
|
107
|
+
if profile in ("standard", "deep") and strings_count <= (5000 if profile == "standard" else 50000):
|
|
108
|
+
findings.append(FindingItem(
|
|
109
|
+
catalog_id="RES-002", domain="Resources", subdomain="Values",
|
|
110
|
+
unit_id=unit.unit_id, layer="resources", title=k,
|
|
111
|
+
value=v[:500], file_path=str(sxml),
|
|
112
|
+
))
|
|
113
|
+
except Exception:
|
|
114
|
+
continue
|
|
115
|
+
|
|
116
|
+
# public.xml (standard/deep)
|
|
117
|
+
pub_xml = res_dir / "values" / "public.xml"
|
|
118
|
+
if profile in ("standard", "deep") and pub_xml.is_file():
|
|
119
|
+
try:
|
|
120
|
+
proot = ET.parse(pub_xml).getroot()
|
|
121
|
+
pub_items = []
|
|
122
|
+
for el in proot.findall("public"):
|
|
123
|
+
public_count += 1
|
|
124
|
+
if profile == "deep" or public_count <= 2000:
|
|
125
|
+
pub_items.append(FindingItem(
|
|
126
|
+
catalog_id="RES-001", domain="Resources", subdomain="Compiled",
|
|
127
|
+
unit_id=unit.unit_id, layer="resources",
|
|
128
|
+
title=f"{el.attrib.get('type', '')}/{el.attrib.get('name', '')}",
|
|
129
|
+
value=el.attrib.get("id", ""), file_path=str(pub_xml),
|
|
130
|
+
))
|
|
131
|
+
findings.extend(pub_items)
|
|
132
|
+
except Exception:
|
|
133
|
+
pass
|
|
134
|
+
|
|
135
|
+
# Check assets/ and unknown/ for google-services.json, .env, .properties, aws/amplify configs
|
|
136
|
+
for scan_dir in (dec / "assets", dec / "unknown", res_dir / "raw"):
|
|
137
|
+
if not scan_dir.is_dir():
|
|
138
|
+
continue
|
|
139
|
+
for f in scan_dir.rglob("*"):
|
|
140
|
+
if not f.is_file():
|
|
141
|
+
continue
|
|
142
|
+
low = f.name.lower()
|
|
143
|
+
if low == "google-services.json":
|
|
144
|
+
try:
|
|
145
|
+
gs = json.loads(f.read_text(encoding="utf-8", errors="replace"))
|
|
146
|
+
findings.append(FindingItem(
|
|
147
|
+
catalog_id="CFG-001", domain="Configuration & IDs", subdomain="Cloud",
|
|
148
|
+
unit_id=unit.unit_id, layer="assets", title="google-services.json",
|
|
149
|
+
value=json.dumps(gs.get("project_info", {})), file_path=str(f), extra=gs,
|
|
150
|
+
))
|
|
151
|
+
except Exception:
|
|
152
|
+
pass
|
|
153
|
+
elif low in (".env", "awsconfiguration.json", "amplifyconfiguration.json") or low.endswith(".properties"):
|
|
154
|
+
try:
|
|
155
|
+
raw_bytes = f.read_bytes()[:32768]
|
|
156
|
+
if b"\x00" in raw_bytes:
|
|
157
|
+
continue
|
|
158
|
+
txt = raw_bytes.decode("utf-8", errors="replace")
|
|
159
|
+
if "\ufffd" in txt or len(txt.strip()) == 0:
|
|
160
|
+
continue
|
|
161
|
+
cfg_kw = ("version=", "client=", "endpoint", "host=", "url=", "api_key", "apikey", "firebase", "aws", "project", "app_id", "secret", "token", "port=", "env=", "debug=")
|
|
162
|
+
if low.endswith(".properties"):
|
|
163
|
+
matched_lines = [
|
|
164
|
+
ln.strip() for ln in txt.splitlines()
|
|
165
|
+
if ln.strip() and not ln.strip().startswith("#") and "=" in ln and any(kw in ln.lower() for kw in cfg_kw)
|
|
166
|
+
]
|
|
167
|
+
if not matched_lines:
|
|
168
|
+
continue
|
|
169
|
+
txt_out = "\n".join(matched_lines)[:3000]
|
|
170
|
+
else:
|
|
171
|
+
txt_out = txt[:3000]
|
|
172
|
+
findings.append(FindingItem(
|
|
173
|
+
catalog_id="CFG-003", domain="Configuration & IDs", subdomain="Configs",
|
|
174
|
+
unit_id=unit.unit_id, layer="assets", title=f.name,
|
|
175
|
+
value=txt_out, file_path=str(f),
|
|
176
|
+
))
|
|
177
|
+
except Exception:
|
|
178
|
+
pass
|
|
179
|
+
|
|
180
|
+
summary[unit.unit_id] = {
|
|
181
|
+
"locales_count": len(set(locales)),
|
|
182
|
+
"strings_parsed": strings_count,
|
|
183
|
+
"public_ids": public_count,
|
|
184
|
+
"xml_raw_count": len(xml_raw_files),
|
|
185
|
+
"ui_counts": ui_counts,
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
inserted = db.insert_findings(findings)
|
|
189
|
+
return InspectorResult(
|
|
190
|
+
name=self.name,
|
|
191
|
+
status="COMPLETED",
|
|
192
|
+
duration_sec=round(time.time() - t0, 3),
|
|
193
|
+
findings_count=inserted,
|
|
194
|
+
summary=summary,
|
|
195
|
+
)
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import time
|
|
3
|
+
import zipfile
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from typing import Dict, List, Set, Tuple
|
|
6
|
+
|
|
7
|
+
from ..core.context import ArtifactUnit, PipelineContext
|
|
8
|
+
from ..core.utils import ASCII_STR_RE, extract_strings_chunked
|
|
9
|
+
from ..storage.findings_db import FindingItem, FindingsDB
|
|
10
|
+
from .base import BaseInspector, InspectorResult
|
|
11
|
+
from .text_scanner import record_domain_scope, scan_text_lines
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
TEXT_EXTS = {
|
|
15
|
+
".java", ".kt", ".xml", ".json", ".js", ".mjs", ".html", ".htm",
|
|
16
|
+
".properties", ".yml", ".yaml", ".txt", ".env", ".hasm",
|
|
17
|
+
}
|
|
18
|
+
MAX_SCAN_FILE_BYTES = 15 * 1024 * 1024
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class ScannerInspector(BaseInspector):
|
|
22
|
+
name = "scanner"
|
|
23
|
+
description = "Single-pass multi-layer scanner for URLs, custom schemes, IPv4/IPv6, emails, secrets, Base64 & strings"
|
|
24
|
+
domains = ["Network & URLs", "Configuration & IDs", "Strings & Constants", "Native Libraries"]
|
|
25
|
+
catalog_ids = [
|
|
26
|
+
"URL-001", "URL-002", "URL-003", "URL-004",
|
|
27
|
+
"NET-001", "NET-002", "NET-003", "NET-004",
|
|
28
|
+
"CFG-004", "CFG-005", "NAT-006",
|
|
29
|
+
"STR-001", "STR-002", "STR-003", "STR-004",
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
def _scan_raw_dex_strings(
|
|
33
|
+
self,
|
|
34
|
+
uid: str,
|
|
35
|
+
apk_path: Path,
|
|
36
|
+
profile: str,
|
|
37
|
+
findings: List[FindingItem],
|
|
38
|
+
strings_rows: List[tuple],
|
|
39
|
+
seen_domains: Dict[str, Dict[str, str]],
|
|
40
|
+
seen_values: Set[Tuple[str, str, str, str]],
|
|
41
|
+
) -> None:
|
|
42
|
+
if not apk_path.is_file() or not zipfile.is_zipfile(apk_path):
|
|
43
|
+
return
|
|
44
|
+
try:
|
|
45
|
+
with zipfile.ZipFile(apk_path, "r") as zf:
|
|
46
|
+
for zi in zf.infolist():
|
|
47
|
+
if not zi.filename.lower().endswith(".dex") or "/" in zi.filename:
|
|
48
|
+
continue
|
|
49
|
+
data = zf.read(zi)
|
|
50
|
+
dex_strs = [m.decode("ascii", errors="ignore") for m in ASCII_STR_RE.findall(data)]
|
|
51
|
+
scan_text_lines(
|
|
52
|
+
uid, "dex", f"{apk_path.name}!{zi.filename}", dex_strs, profile, "app",
|
|
53
|
+
findings, strings_rows, seen_domains, seen_values,
|
|
54
|
+
)
|
|
55
|
+
except Exception:
|
|
56
|
+
pass
|
|
57
|
+
|
|
58
|
+
def run(
|
|
59
|
+
self,
|
|
60
|
+
ctx: PipelineContext,
|
|
61
|
+
db: FindingsDB,
|
|
62
|
+
units: List[ArtifactUnit],
|
|
63
|
+
profile: str,
|
|
64
|
+
) -> InspectorResult:
|
|
65
|
+
t0 = time.time()
|
|
66
|
+
findings: List[FindingItem] = []
|
|
67
|
+
strings_rows: List[tuple] = []
|
|
68
|
+
seen_domains: Dict[str, Dict[str, str]] = {}
|
|
69
|
+
for uid, dom_map in ctx.runtime_cache.get("java_seen_domains", {}).items():
|
|
70
|
+
for d_host, d_scope in dom_map.items():
|
|
71
|
+
record_domain_scope(seen_domains, uid, d_host, d_scope)
|
|
72
|
+
already_java_units: Set[str] = ctx.runtime_cache.get("java_scanned_units", set())
|
|
73
|
+
|
|
74
|
+
seen_values: Set[Tuple[str, str, str, str]] = set()
|
|
75
|
+
scanned_files = 0
|
|
76
|
+
last_tick = time.time()
|
|
77
|
+
|
|
78
|
+
for unit in units:
|
|
79
|
+
dec = Path(unit.decode_dir) if unit.decode_dir else None
|
|
80
|
+
java_dir = Path(unit.java_dir) if unit.java_dir else None
|
|
81
|
+
fw_dir = Path(unit.frameworks_dir) if unit.frameworks_dir else None
|
|
82
|
+
nat_dir = Path(unit.native_dir) if unit.native_dir else None
|
|
83
|
+
|
|
84
|
+
if unit.source_path:
|
|
85
|
+
ctx.ui.push_stream(f"Scanning raw DEX string pools: {unit.name}")
|
|
86
|
+
ctx.ui.render()
|
|
87
|
+
self._scan_raw_dex_strings(
|
|
88
|
+
unit.unit_id, Path(unit.source_path), profile,
|
|
89
|
+
findings, strings_rows, seen_domains, seen_values,
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
layer_roots: List[Tuple[str, Path]] = []
|
|
93
|
+
# Skip Java disk walk if CodeInspector already scanned these exact files in memory!
|
|
94
|
+
if java_dir and java_dir.is_dir() and unit.unit_id not in already_java_units:
|
|
95
|
+
src_sub = java_dir / "sources"
|
|
96
|
+
layer_roots.append(("java", src_sub if src_sub.is_dir() else java_dir))
|
|
97
|
+
if dec and dec.is_dir():
|
|
98
|
+
for sub, lyr in (("res", "resources"), ("assets", "assets"), ("unknown", "assets")):
|
|
99
|
+
if (dec / sub).is_dir():
|
|
100
|
+
layer_roots.append((lyr, dec / sub))
|
|
101
|
+
if fw_dir and fw_dir.is_dir():
|
|
102
|
+
layer_roots.append(("framework", fw_dir))
|
|
103
|
+
if nat_dir and nat_dir.is_dir():
|
|
104
|
+
layer_roots.append(("native", nat_dir))
|
|
105
|
+
|
|
106
|
+
for layer, root_p in layer_roots:
|
|
107
|
+
for root, _dirs, files in os.walk(root_p):
|
|
108
|
+
for fn in files:
|
|
109
|
+
fp = Path(root) / fn
|
|
110
|
+
if fp.suffix.lower() not in TEXT_EXTS:
|
|
111
|
+
continue
|
|
112
|
+
try:
|
|
113
|
+
if fp.stat().st_size > MAX_SCAN_FILE_BYTES:
|
|
114
|
+
continue
|
|
115
|
+
lines = fp.read_text(encoding="utf-8", errors="ignore").splitlines()
|
|
116
|
+
except OSError:
|
|
117
|
+
continue
|
|
118
|
+
scanned_files += 1
|
|
119
|
+
if time.time() - last_tick >= 0.3:
|
|
120
|
+
last_tick = time.time()
|
|
121
|
+
ctx.ui.push_stream(f"[{layer}] {scanned_files} files | {fn}")
|
|
122
|
+
ctx.ui.render()
|
|
123
|
+
scan_text_lines(
|
|
124
|
+
unit.unit_id, layer, str(fp), lines, profile, "app",
|
|
125
|
+
findings, strings_rows, seen_domains, seen_values,
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
if (not nat_dir or not nat_dir.is_dir()) and unit.custom_so_paths and profile != "fast":
|
|
129
|
+
for so_str in unit.custom_so_paths:
|
|
130
|
+
so_p = Path(so_str)
|
|
131
|
+
if so_p.is_file():
|
|
132
|
+
strs = extract_strings_chunked(so_p, max_strings=30000)
|
|
133
|
+
scan_text_lines(
|
|
134
|
+
unit.unit_id, "native", str(so_p), strs, profile, "app",
|
|
135
|
+
findings, strings_rows, seen_domains, seen_values,
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
for uid, dom_map in seen_domains.items():
|
|
139
|
+
for d, d_scope in sorted(dom_map.items()):
|
|
140
|
+
findings.append(FindingItem(
|
|
141
|
+
catalog_id="NET-001", domain="Network & URLs", subdomain="Hosts",
|
|
142
|
+
unit_id=uid, layer="correlated", title="Domain", value=d, scope=d_scope,
|
|
143
|
+
))
|
|
144
|
+
|
|
145
|
+
inserted = db.insert_findings(findings)
|
|
146
|
+
str_inserted = db.insert_strings(strings_rows)
|
|
147
|
+
return InspectorResult(
|
|
148
|
+
name=self.name,
|
|
149
|
+
status="COMPLETED",
|
|
150
|
+
duration_sec=round(time.time() - t0, 3),
|
|
151
|
+
findings_count=inserted,
|
|
152
|
+
summary={"files_scanned": scanned_files, "strings_indexed": str_inserted},
|
|
153
|
+
)
|