-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathbindiff_query.py
More file actions
298 lines (267 loc) · 11.3 KB
/
Copy pathbindiff_query.py
File metadata and controls
298 lines (267 loc) · 11.3 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
"""Read-only query surface over the per-pair `.BinDiff` SQLite files.
The pipeline drops one `.BinDiff` SQLite next to each primary binary
(`{primary_path}.{secondary_kb}.BinDiff`) plus matching `.BinExport`
files for both sides. Chat-agent tools call into this service to ask
post-pipeline questions about function matches, unmatched functions,
basic-block matches, and similarity statistics — without re-running
BinDiff or re-loading IDA.
Two layers of caching:
- `BindiffFile` (cheap) — SQLite-only; matches and counts.
- `BinDiff` (heavy) — adds the two BinExport programs so we can
enumerate unmatched functions and walk
basic blocks.
The cheap layer covers `stats` and `function_matches`; the heavy layer
is opened lazily on first `unmatched` / `basic_blocks` request. Same
pattern as `DataframeRegistry`'s polars-frame cache.
"""
from __future__ import annotations
from pathlib import Path
from typing import Any
from bindiff import BinDiff
from bindiff.file import BindiffFile
from patchdiff_ai.schemas.analysis import Artifact
def _bindiff_path(art: Artifact) -> Path:
"""Mirror of the convention in graphs/reverse_engineering/nodes.py."""
return Path(f"{art.primary_file.path}.{art.secondary_file.kb}.BinDiff")
def _binexport_paths(art: Artifact) -> tuple[Path, Path]:
return (
Path(art.primary_file.path + ".BinExport"),
Path(art.secondary_file.path + ".BinExport"),
)
def _parse_addr(value: str) -> int:
"""Accept '0x18001b2080', '18001B2080', '18001b2080'."""
s = value.strip().lower().removeprefix("0x")
return int(s, 16)
def _bucket(value: float, edges: list[float]) -> str:
"""Map `value` into a labelled bucket (descending edges)."""
for edge in edges:
if value >= edge:
return f">={edge}"
return f"<{edges[-1]}"
class BindiffQueryService:
"""Lazy reader over the per-artifact `.BinDiff` files for one chat session."""
# Bucket boundaries (descending) for similarity / confidence histograms.
_SIM_EDGES: list[float] = [1.0, 0.9, 0.5]
_CONF_EDGES: list[float] = [0.9, 0.5, 0.1]
def __init__(self, artifacts: list[Artifact]) -> None:
self._artifacts: list[Artifact] = list(artifacts)
self._by_name: dict[str, Artifact] = {
a.primary_file.name.lower(): a for a in self._artifacts
}
self._files: dict[str, BindiffFile] = {}
self._diffs: dict[str, BinDiff] = {}
# ------------------------------------------------------------------ public
def list_pairs(self) -> list[dict[str, Any]]:
"""One row per artifact with primary/secondary identity + paths."""
out: list[dict[str, Any]] = []
for art in self._artifacts:
bd = _bindiff_path(art)
primary_be, secondary_be = _binexport_paths(art)
out.append({
"file": art.primary_file.name,
"primary_kb": art.primary_file.kb,
"secondary_kb": art.secondary_file.kb,
"primary_path": str(art.primary_file.path),
"secondary_path": str(art.secondary_file.path),
"bindiff_path": str(bd),
"primary_binexport": str(primary_be),
"secondary_binexport": str(secondary_be),
"exists": bd.is_file(),
"changed_count": len(art.changed),
})
return out
def stats(self, file: str) -> dict[str, Any]:
bf = self._open_file(file)
sim_buckets = {f">={e}": 0 for e in self._SIM_EDGES} | {
f"<{self._SIM_EDGES[-1]}": 0
}
conf_buckets = {f">={e}": 0 for e in self._CONF_EDGES} | {
f"<{self._CONF_EDGES[-1]}": 0
}
for fm in bf.primary_functions_match.values():
sim_buckets[_bucket(fm.similarity, self._SIM_EDGES)] += 1
conf_buckets[_bucket(fm.confidence, self._CONF_EDGES)] += 1
return {
"file": self._resolve(file).primary_file.name,
"matched": len(bf.primary_functions_match),
"unmatched_primary": bf.unmatched_primary_count,
"unmatched_secondary": bf.unmatched_secondary_count,
"primary_total_functions": (
bf.primary_file.functions + bf.primary_file.libfunctions
),
"secondary_total_functions": (
bf.secondary_file.functions + bf.secondary_file.libfunctions
),
"basic_block_matches": len(bf.basicblock_matches),
"overall_similarity": bf.similarity,
"overall_confidence": bf.confidence,
"similarity_buckets": sim_buckets,
"confidence_buckets": conf_buckets,
}
def function_matches(
self,
file: str,
*,
min_similarity: float = 0.0,
max_similarity: float = 1.0,
min_confidence: float = 0.0,
name_contains: str = "",
address: str = "",
limit: int = 100,
offset: int = 0,
) -> dict[str, Any]:
bf = self._open_file(file)
needle = name_contains.lower()
addr_filter = _parse_addr(address) if address else None
rows: list[dict[str, Any]] = []
for fm in bf.primary_functions_match.values():
if fm.similarity < min_similarity or fm.similarity > max_similarity:
continue
if fm.confidence < min_confidence:
continue
if needle and needle not in (fm.name1 or "").lower() and needle not in (fm.name2 or "").lower():
continue
if addr_filter is not None and fm.address1 != addr_filter and fm.address2 != addr_filter:
continue
rows.append({
"address1": f"{fm.address1:X}",
"address2": f"{fm.address2:X}",
"name1": fm.name1,
"name2": fm.name2,
"similarity": fm.similarity,
"confidence": fm.confidence,
})
rows.sort(key=lambda r: (r["similarity"], r["address1"]))
total = len(rows)
page = rows[offset : offset + max(1, int(limit))]
next_offset = offset + len(page) if (offset + len(page)) < total else None
return {
"file": self._resolve(file).primary_file.name,
"data": page,
"total": total,
"next_offset": next_offset,
}
def unmatched(
self,
file: str,
*,
side: str = "both",
limit: int = 100,
offset: int = 0,
) -> dict[str, Any]:
if side not in ("primary", "secondary", "both"):
return {"kind": "error", "error": f"side must be primary|secondary|both, got {side!r}"}
diff = self._open_diff(file)
rows: list[dict[str, Any]] = []
if side in ("primary", "both"):
for fn in diff.primary_unmatched_function():
rows.append({"address": f"{fn.addr:X}", "name": fn.name, "side": "primary"})
if side in ("secondary", "both"):
for fn in diff.secondary_unmatched_function():
rows.append({"address": f"{fn.addr:X}", "name": fn.name, "side": "secondary"})
rows.sort(key=lambda r: (r["side"], r["address"]))
total = len(rows)
page = rows[offset : offset + max(1, int(limit))]
next_offset = offset + len(page) if (offset + len(page)) < total else None
return {
"file": self._resolve(file).primary_file.name,
"side": side,
"data": page,
"total": total,
"next_offset": next_offset,
}
def basic_blocks(self, file: str, function_address: str) -> dict[str, Any]:
diff = self._open_diff(file)
addr1 = _parse_addr(function_address)
fm = diff.primary_functions_match.get(addr1)
if fm is None:
return {
"kind": "error",
"error": (
f"No function match at primary address {function_address!r} "
f"in {self._resolve(file).primary_file.name}. Use "
"bindiff_function_matches to discover matched addresses."
),
}
# iter_basicblock_matches needs the BinExport function objects.
try:
f1 = diff.primary[fm.address1]
f2 = diff.secondary[fm.address2]
except KeyError:
return {
"kind": "error",
"error": (
f"Function present in match table but missing from BinExport "
f"at {fm.address1:X} / {fm.address2:X}."
),
}
bbs: list[dict[str, Any]] = []
for _bb1, _bb2, m in diff.iter_basicblock_matches(f1, f2):
bbs.append({
"address1": f"{m.address1:X}",
"address2": f"{m.address2:X}",
"algorithm": str(m.algorithm),
})
return {
"file": self._resolve(file).primary_file.name,
"function": {
"address1": f"{fm.address1:X}",
"address2": f"{fm.address2:X}",
"name1": fm.name1,
"name2": fm.name2,
"similarity": fm.similarity,
"confidence": fm.confidence,
},
"data": bbs,
"total": len(bbs),
}
# ----------------------------------------------------------------- internal
def _resolve(self, file: str) -> Artifact:
art = self._by_name.get(file.lower())
if art is None:
available = sorted(self._by_name.values(), key=lambda a: a.primary_file.name)
names = [a.primary_file.name for a in available]
raise ValueError(
f"No artifact for {file!r}. Available: {names}"
)
return art
def _open_file(self, file: str) -> BindiffFile:
art = self._resolve(file)
key = art.primary_file.name.lower()
cached = self._files.get(key)
if cached is not None:
return cached
path = _bindiff_path(art)
if not path.is_file():
raise FileNotFoundError(
f"BinDiff database missing on disk: {path}. The pipeline "
"may have skipped this pair (re-run with --force)."
)
bf = BindiffFile(path, permission="ro")
self._files[key] = bf
return bf
def _open_diff(self, file: str) -> BinDiff:
art = self._resolve(file)
key = art.primary_file.name.lower()
cached = self._diffs.get(key)
if cached is not None:
return cached
bd_path = _bindiff_path(art)
primary_be, secondary_be = _binexport_paths(art)
for label, p in (
("BinDiff", bd_path),
("primary BinExport", primary_be),
("secondary BinExport", secondary_be),
):
if not p.is_file():
raise FileNotFoundError(
f"{label} missing on disk: {p}. Required for unmatched / "
"basic-block queries."
)
diff = BinDiff(str(primary_be), str(secondary_be), str(bd_path))
self._diffs[key] = diff
# `BinDiff` super-init already loaded the same SQLite tables; share
# it as the file-level cache too so a later `stats`/`function_matches`
# call doesn't reopen the SQLite.
self._files[key] = diff
return diff