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,235 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import os
|
|
3
|
+
import sqlite3
|
|
4
|
+
import time
|
|
5
|
+
from collections import Counter, defaultdict
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Any, Dict, List
|
|
8
|
+
|
|
9
|
+
from ..core.context import ArtifactUnit, PipelineContext
|
|
10
|
+
from ..core.utils import compute_sha256
|
|
11
|
+
from ..storage.findings_db import FindingItem, FindingsDB
|
|
12
|
+
from .base import BaseInspector, InspectorResult
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
MAGIC_SIGS = [
|
|
16
|
+
(b"SQLite format 3\x00", "SQLite 3"),
|
|
17
|
+
(b"PK\x03\x04", "ZIP/APK/JAR"),
|
|
18
|
+
(b"dex\n", "Android DEX"),
|
|
19
|
+
(b"\x7fELF", "ELF Binary"),
|
|
20
|
+
(b"\x89PNG\r\n\x1a\n", "PNG Image"),
|
|
21
|
+
(b"\xff\xd8\xff", "JPEG Image"),
|
|
22
|
+
(b"RIFF", "RIFF/WEBP/WAV"),
|
|
23
|
+
(b"%PDF-", "PDF Document"),
|
|
24
|
+
(b"\x1f\x8b", "GZIP Archive"),
|
|
25
|
+
(b"-----BEGIN ", "PEM Certificate/Key"),
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _detect_magic(path: Path) -> str:
|
|
30
|
+
try:
|
|
31
|
+
with path.open("rb") as f:
|
|
32
|
+
hdr = f.read(32)
|
|
33
|
+
for sig, label in MAGIC_SIGS:
|
|
34
|
+
if hdr.startswith(sig):
|
|
35
|
+
return label
|
|
36
|
+
return "Unknown/Data"
|
|
37
|
+
except OSError:
|
|
38
|
+
return "Unreadable"
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class AssetsDbInspector(BaseInspector):
|
|
42
|
+
name = "assets_db"
|
|
43
|
+
description = "Inventory assets/binaries/hashes and inspect embedded SQLite databases"
|
|
44
|
+
domains = ["Assets & Files", "Databases"]
|
|
45
|
+
catalog_ids = ["AST-001", "AST-002", "AST-003", "AST-004", "AST-005", "DB-001", "DB-002", "DB-003"]
|
|
46
|
+
|
|
47
|
+
def _inspect_sqlite(
|
|
48
|
+
self, uid: str, db_file: Path, profile: str, findings: List[FindingItem]
|
|
49
|
+
) -> Dict[str, Any]:
|
|
50
|
+
info: Dict[str, Any] = {"file": str(db_file), "tables": []}
|
|
51
|
+
try:
|
|
52
|
+
conn = sqlite3.connect(f"file:{db_file}?mode=ro", uri=True)
|
|
53
|
+
cur = conn.cursor()
|
|
54
|
+
objects = cur.execute(
|
|
55
|
+
"SELECT type, name, sql FROM sqlite_master WHERE name NOT LIKE 'sqlite_%' ORDER BY type, name"
|
|
56
|
+
).fetchall()
|
|
57
|
+
except Exception as exc:
|
|
58
|
+
return {"file": str(db_file), "error": str(exc)}
|
|
59
|
+
|
|
60
|
+
try:
|
|
61
|
+
for obj_type, obj_name, obj_sql in objects:
|
|
62
|
+
if obj_type == "table":
|
|
63
|
+
cols = [
|
|
64
|
+
{"name": r[1], "type": r[2], "pk": bool(r[5])}
|
|
65
|
+
for r in cur.execute(f'PRAGMA table_info("{obj_name.replace(chr(34), chr(34)*2)}")').fetchall()
|
|
66
|
+
]
|
|
67
|
+
fks = [
|
|
68
|
+
{"table": r[2], "from": r[3], "to": r[4]}
|
|
69
|
+
for r in cur.execute(f'PRAGMA foreign_key_list("{obj_name.replace(chr(34), chr(34)*2)}")').fetchall()
|
|
70
|
+
]
|
|
71
|
+
try:
|
|
72
|
+
row_cnt = cur.execute(f'SELECT COUNT(*) FROM "{obj_name.replace(chr(34), chr(34)*2)}"').fetchone()[0]
|
|
73
|
+
except Exception:
|
|
74
|
+
row_cnt = -1
|
|
75
|
+
|
|
76
|
+
t_meta = {"name": obj_name, "rows": row_cnt, "columns": cols, "foreign_keys": fks}
|
|
77
|
+
info["tables"].append(t_meta)
|
|
78
|
+
|
|
79
|
+
findings.append(FindingItem(
|
|
80
|
+
catalog_id="DB-002", domain="Databases", subdomain="SQLite",
|
|
81
|
+
unit_id=uid, layer="assets", title=f"Table: {obj_name}",
|
|
82
|
+
value=f"rows={row_cnt}, cols={[c['name'] for c in cols]}",
|
|
83
|
+
file_path=str(db_file), extra={"schema": obj_sql or "", **t_meta},
|
|
84
|
+
))
|
|
85
|
+
|
|
86
|
+
if profile == "deep" and row_cnt > 0:
|
|
87
|
+
try:
|
|
88
|
+
sample = cur.execute(f'SELECT * FROM "{obj_name.replace(chr(34), chr(34)*2)}" LIMIT 10').fetchall()
|
|
89
|
+
findings.append(FindingItem(
|
|
90
|
+
catalog_id="DB-003", domain="Databases", subdomain="SQLite",
|
|
91
|
+
unit_id=uid, layer="assets", title=f"Sample Rows: {obj_name}",
|
|
92
|
+
value=json.dumps(sample, default=str, ensure_ascii=False)[:2000],
|
|
93
|
+
file_path=str(db_file),
|
|
94
|
+
))
|
|
95
|
+
except Exception:
|
|
96
|
+
pass
|
|
97
|
+
elif obj_type in ("index", "view", "trigger"):
|
|
98
|
+
findings.append(FindingItem(
|
|
99
|
+
catalog_id="DB-002", domain="Databases", subdomain="SQLite",
|
|
100
|
+
unit_id=uid, layer="assets", title=f"{obj_type.title()}: {obj_name}",
|
|
101
|
+
value=obj_sql or obj_name, file_path=str(db_file),
|
|
102
|
+
))
|
|
103
|
+
finally:
|
|
104
|
+
conn.close()
|
|
105
|
+
return info
|
|
106
|
+
|
|
107
|
+
def run(
|
|
108
|
+
self,
|
|
109
|
+
ctx: PipelineContext,
|
|
110
|
+
db: FindingsDB,
|
|
111
|
+
units: List[ArtifactUnit],
|
|
112
|
+
profile: str,
|
|
113
|
+
) -> InspectorResult:
|
|
114
|
+
t0 = time.time()
|
|
115
|
+
findings: List[FindingItem] = []
|
|
116
|
+
summary: Dict[str, Any] = {}
|
|
117
|
+
|
|
118
|
+
# AST-001: Summary from Stage 1 files.tsv if present
|
|
119
|
+
files_tsv = ctx.state.inv_dir / "files.tsv"
|
|
120
|
+
if files_tsv.is_file():
|
|
121
|
+
ext_counts: Counter = Counter()
|
|
122
|
+
ext_bytes: Counter = Counter()
|
|
123
|
+
total_files = 0
|
|
124
|
+
try:
|
|
125
|
+
with files_tsv.open("r", encoding="utf-8", errors="replace") as f:
|
|
126
|
+
for idx, line in enumerate(f):
|
|
127
|
+
if idx == 0:
|
|
128
|
+
continue
|
|
129
|
+
parts = line.rstrip("\n").split("\t")
|
|
130
|
+
if len(parts) >= 4:
|
|
131
|
+
total_files += 1
|
|
132
|
+
sz = int(parts[2]) if parts[2].isdigit() else 0
|
|
133
|
+
ext = parts[3]
|
|
134
|
+
ext_counts[ext] += 1
|
|
135
|
+
ext_bytes[ext] += sz
|
|
136
|
+
findings.append(FindingItem(
|
|
137
|
+
catalog_id="AST-001", domain="Assets & Files", subdomain="Inventory",
|
|
138
|
+
unit_id="0000", layer="workspace", title="Workspace File Summary",
|
|
139
|
+
value=f"total_files={total_files}, top_exts={dict(ext_counts.most_common(12))}",
|
|
140
|
+
file_path=str(files_tsv), extra={"top_extensions": dict(ext_counts.most_common(25))},
|
|
141
|
+
))
|
|
142
|
+
except OSError:
|
|
143
|
+
pass
|
|
144
|
+
|
|
145
|
+
for unit in units:
|
|
146
|
+
dec = Path(unit.decode_dir) if unit.decode_dir else None
|
|
147
|
+
if not dec or not dec.is_dir():
|
|
148
|
+
continue
|
|
149
|
+
|
|
150
|
+
hash_map: Dict[str, List[str]] = defaultdict(list)
|
|
151
|
+
cats: Dict[str, List[str]] = defaultdict(list)
|
|
152
|
+
sqlite_dbs: List[Dict[str, Any]] = []
|
|
153
|
+
|
|
154
|
+
for sub_name in ("assets", "res/raw", "unknown"):
|
|
155
|
+
root_dir = dec / sub_name
|
|
156
|
+
if not root_dir.is_dir():
|
|
157
|
+
continue
|
|
158
|
+
for fpath in sorted(root_dir.rglob("*")):
|
|
159
|
+
if not fpath.is_file():
|
|
160
|
+
continue
|
|
161
|
+
rel = fpath.relative_to(dec).as_posix()
|
|
162
|
+
ext = fpath.suffix.lower()
|
|
163
|
+
sz = fpath.stat().st_size
|
|
164
|
+
magic = _detect_magic(fpath)
|
|
165
|
+
|
|
166
|
+
if ext in (".json", ".xml", ".html", ".htm", ".js", ".css"):
|
|
167
|
+
cats["web_and_config"].append(rel)
|
|
168
|
+
elif ext in (".pem", ".crt", ".cer", ".der", ".bks", ".p12", ".pfx", ".jks") or magic == "PEM Certificate/Key":
|
|
169
|
+
cats["certificates_keystores"].append(rel)
|
|
170
|
+
elif ext in (".tflite", ".onnx", ".pt", ".pb"):
|
|
171
|
+
findings.append(FindingItem(
|
|
172
|
+
catalog_id="AST-005", domain="Assets & Files", subdomain="Special",
|
|
173
|
+
unit_id=unit.unit_id, layer="assets", title="ML Model",
|
|
174
|
+
value=rel, file_path=str(fpath), extra={"size": sz, "magic": magic},
|
|
175
|
+
))
|
|
176
|
+
elif ext in (".dex", ".jar", ".apk", ".zip") or magic in ("Android DEX", "ZIP/APK/JAR"):
|
|
177
|
+
findings.append(FindingItem(
|
|
178
|
+
catalog_id="AST-005", domain="Assets & Files", subdomain="Special",
|
|
179
|
+
unit_id=unit.unit_id, layer="assets", title=f"Embedded {magic}",
|
|
180
|
+
value=rel, file_path=str(fpath), extra={"size": sz, "magic": magic},
|
|
181
|
+
))
|
|
182
|
+
|
|
183
|
+
if magic == "SQLite 3" or ext in (".db", ".sqlite", ".sqlite3", ".realm"):
|
|
184
|
+
fhash = compute_sha256(fpath)
|
|
185
|
+
findings.append(FindingItem(
|
|
186
|
+
catalog_id="DB-001", domain="Databases", subdomain="Discovery",
|
|
187
|
+
unit_id=unit.unit_id, layer="assets", title=f"{magic} DB",
|
|
188
|
+
value=f"{rel} (size={sz}, sha256={fhash[:12]})",
|
|
189
|
+
file_path=str(fpath), extra={"sha256": fhash, "size": sz, "magic": magic},
|
|
190
|
+
))
|
|
191
|
+
if magic == "SQLite 3" and profile in ("standard", "deep"):
|
|
192
|
+
sqlite_dbs.append(self._inspect_sqlite(unit.unit_id, fpath, profile, findings))
|
|
193
|
+
|
|
194
|
+
if profile in ("standard", "deep") and sz <= 25 * 1024 * 1024:
|
|
195
|
+
fhash = compute_sha256(fpath)
|
|
196
|
+
hash_map[fhash].append(rel)
|
|
197
|
+
if profile == "deep" or magic != "Unknown/Data":
|
|
198
|
+
findings.append(FindingItem(
|
|
199
|
+
catalog_id="AST-002", domain="Assets & Files", subdomain="Signatures",
|
|
200
|
+
unit_id=unit.unit_id, layer="assets", title=magic,
|
|
201
|
+
value=f"{rel} | sha256={fhash[:16]} | size={sz}",
|
|
202
|
+
file_path=str(fpath), extra={"sha256": fhash, "size": sz, "magic": magic},
|
|
203
|
+
))
|
|
204
|
+
|
|
205
|
+
for cat_k, items in cats.items():
|
|
206
|
+
findings.append(FindingItem(
|
|
207
|
+
catalog_id="AST-004", domain="Assets & Files", subdomain="Categories",
|
|
208
|
+
unit_id=unit.unit_id, layer="assets", title=cat_k,
|
|
209
|
+
value=f"{len(items)} files: {', '.join(items[:15])}",
|
|
210
|
+
extra={"count": len(items), "sample": items[:50]},
|
|
211
|
+
))
|
|
212
|
+
|
|
213
|
+
for fhash, rels in hash_map.items():
|
|
214
|
+
if len(rels) > 1:
|
|
215
|
+
findings.append(FindingItem(
|
|
216
|
+
catalog_id="AST-003", domain="Assets & Files", subdomain="Deduplication",
|
|
217
|
+
unit_id=unit.unit_id, layer="assets", title=f"Duplicate Asset ({len(rels)}x)",
|
|
218
|
+
value=f"{fhash[:16]} -> {', '.join(rels[:6])}",
|
|
219
|
+
extra={"sha256": fhash, "paths": rels},
|
|
220
|
+
))
|
|
221
|
+
|
|
222
|
+
summary[unit.unit_id] = {
|
|
223
|
+
"sqlite_dbs": len(sqlite_dbs),
|
|
224
|
+
"categories": {k: len(v) for k, v in cats.items()},
|
|
225
|
+
"duplicate_groups": sum(1 for v in hash_map.values() if len(v) > 1),
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
inserted = db.insert_findings(findings)
|
|
229
|
+
return InspectorResult(
|
|
230
|
+
name=self.name,
|
|
231
|
+
status="COMPLETED",
|
|
232
|
+
duration_sec=round(time.time() - t0, 3),
|
|
233
|
+
findings_count=inserted,
|
|
234
|
+
summary=summary,
|
|
235
|
+
)
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
from dataclasses import dataclass, field
|
|
3
|
+
from typing import Any, Dict, List, Set
|
|
4
|
+
|
|
5
|
+
from ..core.context import ArtifactUnit, PipelineContext
|
|
6
|
+
from ..storage.findings_db import FindingsDB
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass
|
|
10
|
+
class InspectorResult:
|
|
11
|
+
name: str
|
|
12
|
+
status: str # COMPLETED, SKIPPED, FAILED
|
|
13
|
+
duration_sec: float = 0.0
|
|
14
|
+
findings_count: int = 0
|
|
15
|
+
summary: Dict[str, Any] = field(default_factory=dict)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class BaseInspector(ABC):
|
|
19
|
+
name: str = "base_inspector"
|
|
20
|
+
description: str = ""
|
|
21
|
+
domains: List[str] = []
|
|
22
|
+
catalog_ids: List[str] = []
|
|
23
|
+
|
|
24
|
+
def is_selected(self, only_filters: Set[str], skip_filters: Set[str]) -> bool:
|
|
25
|
+
tokens = {self.name.lower()}
|
|
26
|
+
tokens.update(d.lower() for d in self.domains)
|
|
27
|
+
tokens.update(cid.lower() for cid in self.catalog_ids)
|
|
28
|
+
# Prefix wildcards like ACT-*, PER-*
|
|
29
|
+
prefixes = {cid.split("-")[0].lower() for cid in self.catalog_ids if "-" in cid}
|
|
30
|
+
tokens.update(prefixes)
|
|
31
|
+
|
|
32
|
+
if skip_filters and (tokens & skip_filters):
|
|
33
|
+
return False
|
|
34
|
+
if only_filters:
|
|
35
|
+
return bool(tokens & only_filters)
|
|
36
|
+
return True
|
|
37
|
+
|
|
38
|
+
@abstractmethod
|
|
39
|
+
def run(
|
|
40
|
+
self,
|
|
41
|
+
ctx: PipelineContext,
|
|
42
|
+
db: FindingsDB,
|
|
43
|
+
units: List[ArtifactUnit],
|
|
44
|
+
profile: str,
|
|
45
|
+
) -> InspectorResult:
|
|
46
|
+
raise NotImplementedError
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import time
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from typing import Any, Dict, List
|
|
4
|
+
|
|
5
|
+
from ..core.context import ArtifactUnit, PipelineContext
|
|
6
|
+
from ..storage.findings_db import FindingItem, FindingsDB
|
|
7
|
+
from .base import BaseInspector, InspectorResult
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class BundleInspector(BaseInspector):
|
|
11
|
+
name = "bundle"
|
|
12
|
+
description = "Correlate Main, Split, and Embedded APKs in the workspace (BND-001..002)"
|
|
13
|
+
domains = ["Bundles & Embedded"]
|
|
14
|
+
catalog_ids = ["BND-001", "BND-002"]
|
|
15
|
+
|
|
16
|
+
def run(
|
|
17
|
+
self,
|
|
18
|
+
ctx: PipelineContext,
|
|
19
|
+
db: FindingsDB,
|
|
20
|
+
units: List[ArtifactUnit],
|
|
21
|
+
profile: str,
|
|
22
|
+
) -> InspectorResult:
|
|
23
|
+
t0 = time.time()
|
|
24
|
+
findings: List[FindingItem] = []
|
|
25
|
+
|
|
26
|
+
for u in units:
|
|
27
|
+
src_size = 0
|
|
28
|
+
try:
|
|
29
|
+
p = Path(u.source_path)
|
|
30
|
+
if p.is_file():
|
|
31
|
+
src_size = p.stat().st_size
|
|
32
|
+
except OSError:
|
|
33
|
+
pass
|
|
34
|
+
|
|
35
|
+
findings.append(FindingItem(
|
|
36
|
+
catalog_id="BND-001", domain="Bundles & Embedded", subdomain="Structure",
|
|
37
|
+
unit_id=u.unit_id, layer="bundle", title=f"{u.role} (depth={u.depth})",
|
|
38
|
+
value=u.name, file_path=u.source_path,
|
|
39
|
+
extra={"role": u.role, "depth": u.depth, "decode_dir": u.decode_dir},
|
|
40
|
+
))
|
|
41
|
+
findings.append(FindingItem(
|
|
42
|
+
catalog_id="BND-002", domain="Bundles & Embedded", subdomain="Integrity",
|
|
43
|
+
unit_id=u.unit_id, layer="bundle", title="APK SHA256 & Size",
|
|
44
|
+
value=f"sha256={u.sha256} | size={src_size}", file_path=u.source_path,
|
|
45
|
+
extra={"sha256": u.sha256, "size_bytes": src_size},
|
|
46
|
+
))
|
|
47
|
+
|
|
48
|
+
inserted = db.insert_findings(findings)
|
|
49
|
+
return InspectorResult(
|
|
50
|
+
name=self.name,
|
|
51
|
+
status="COMPLETED",
|
|
52
|
+
duration_sec=round(time.time() - t0, 3),
|
|
53
|
+
findings_count=inserted,
|
|
54
|
+
summary={"units_total": len(units)},
|
|
55
|
+
)
|