binscout 0.5.1

Headless, agent-first binary analysis: functions, CFGs, xrefs, strings, and IR over a JSON CLI or MCP server
Documentation
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
//! Bounded literal/wildcard byte search over mapped file bytes.
//!
//! [`BytePattern::parse`] accepts whitespace-separated full-byte tokens
//! (`48 8B ?? ??`); scanning walks each segment's file-backed prefix once in
//! ascending VA order, so gaps between mappings are never bridged, virtual
//! BSS is never materialized, and overlapping section views cannot report a
//! match twice. Sections only attribute matches (name plus file offset).

use crate::error::{Error, Result};
use crate::loader::{BinaryFormat, LoadedBinary};

/// Cap on pattern length in bytes.
pub const MAX_PATTERN_BYTES: usize = 4096;
/// Cap on unique file-backed mapped bytes scanned per request.
pub const MAX_SCAN_BYTES: u64 = crate::loader::MAX_MAPPED_BYTES;

/// Parsed search pattern: literal bytes plus `??` wildcards.
///
/// At least one literal is guaranteed: [`BytePattern::parse`] rejects empty
/// and all-wildcard input, so every pattern has a literal anchor byte.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BytePattern {
    bytes: Vec<Option<u8>>,
}

impl BytePattern {
    /// Parse whitespace-separated full-byte tokens.
    ///
    /// Each token is two hex digits (case-insensitive) or a `??` wildcard.
    ///
    /// # Errors
    /// [`Error::Invalid`] on empty, all-wildcard, odd-length, non-hex, or
    /// over-[`MAX_PATTERN_BYTES`] input.
    pub fn parse(text: &str) -> Result<Self> {
        let mut bytes = Vec::new();
        for tok in text.split_whitespace() {
            if tok.eq_ignore_ascii_case("??") {
                bytes.push(None);
            } else if tok.len() == 2
                && let Some(b) = parse_hex_byte(tok)
            {
                bytes.push(Some(b));
            } else {
                return Err(Error::Invalid(format!("bad search token: {tok:?}")));
            }
            if bytes.len() > MAX_PATTERN_BYTES {
                return Err(Error::Invalid(format!(
                    "search pattern exceeds {MAX_PATTERN_BYTES} bytes"
                )));
            }
        }
        if bytes.is_empty() {
            return Err(Error::Invalid("empty search pattern".to_string()));
        }
        if bytes.iter().all(Option::is_none) {
            return Err(Error::Invalid(
                "search pattern needs at least one literal byte".to_string(),
            ));
        }
        Ok(Self { bytes })
    }

    /// Pattern length in bytes, wildcards included.
    #[must_use]
    pub fn len(&self) -> usize {
        self.bytes.len()
    }

    /// Whether the pattern holds no tokens (unreachable via [`BytePattern::parse`]).
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.bytes.is_empty()
    }

    /// Pattern tokens in order (`None` is a wildcard).
    #[must_use]
    pub fn bytes(&self) -> &[Option<u8>] {
        &self.bytes
    }
}

/// Parse exactly two hex digits as one byte.
fn parse_hex_byte(tok: &str) -> Option<u8> {
    let mut v: u8 = 0;
    for c in tok.chars() {
        v = v.checked_mul(16)?.checked_add(c.to_digit(16)? as u8)?;
    }
    Some(v)
}

/// One pattern occurrence: virtual address plus file provenance.
///
/// JSON pairs `addr` with `addr_hex`; `addr_hex` is recomputed on serialize
/// and ignored on deserialize. `file_off` is the byte's file offset: from
/// the covering section for containers, from the blob start for raw
/// mappings, null when neither applies. `section` is the covering
/// section's name, null for raw blobs and section-less ranges.
#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize)]
pub struct ByteMatch {
    /// Virtual address of the first matched byte.
    pub addr: u64,
    /// File offset of the first matched byte, if known.
    pub file_off: Option<u64>,
    /// Covering section name, if any.
    pub section: Option<String>,
    /// Match length in bytes (the pattern length).
    pub len: usize,
}

impl serde::Serialize for ByteMatch {
    fn serialize<S: serde::Serializer>(
        &self,
        serializer: S,
    ) -> std::result::Result<S::Ok, S::Error> {
        use serde::ser::SerializeStruct;
        let mut st = serializer.serialize_struct("ByteMatch", 5)?;
        st.serialize_field("addr", &self.addr)?;
        st.serialize_field("addr_hex", &crate::addr_hex(self.addr))?;
        st.serialize_field("file_off", &self.file_off)?;
        st.serialize_field("section", &self.section)?;
        st.serialize_field("len", &self.len)?;
        st.end()
    }
}

/// Scan `binary` for `pattern`, returning the exact total plus the requested page.
///
/// `section` keeps matches fully inside a file-backed section of that exact
/// name ([`Error::NotFound`] when no section matches); `range` is half-open
/// `[lo, hi)` over match VAs (a match must end at or before `hi`; `lo > hi`
/// is normalized, `lo == hi` is [`Error::Invalid`]). `limit` is pre-clipped
/// by the caller to the page cap; matches materialize lazily so only the
/// page window is built while `total` still counts every match. Matches
/// never span segments, gaps, or BSS tails.
///
/// # Errors
/// Pattern, section, and range validation plus [`Error::Limit`] past
/// [`MAX_SCAN_BYTES`] unique scanned bytes.
pub(crate) fn run(
    binary: &LoadedBinary,
    pattern: &BytePattern,
    section: Option<&str>,
    range: Option<(u64, u64)>,
    offset: usize,
    limit: usize,
) -> Result<(usize, Vec<ByteMatch>)> {
    run_limited(
        binary,
        pattern,
        section,
        range,
        offset,
        limit,
        MAX_SCAN_BYTES,
    )
}

/// `run` with an explicit scan budget (tests drive [`Error::Limit`] with small caps).
fn run_limited(
    binary: &LoadedBinary,
    pattern: &BytePattern,
    section: Option<&str>,
    range: Option<(u64, u64)>,
    offset: usize,
    limit: usize,
    max_scan: u64,
) -> Result<(usize, Vec<ByteMatch>)> {
    let allowed = allowed_ranges(binary, section, range)?;
    let mut pieces: Vec<(u64, &[u8], u64)> = Vec::new();
    for seg in &binary.segments {
        if seg.data.is_empty() {
            continue;
        }
        let start = seg.vaddr;
        let end = start.saturating_add(seg.data.len() as u64);
        for (lo, hi) in &allowed {
            let s = start.max(*lo);
            let e = end.min(*hi);
            if s < e {
                let a = (s - start) as usize;
                let b = (e - start) as usize;
                pieces.push((s, &seg.data[a..b], seg.vaddr));
            }
        }
    }
    pieces.sort_by_key(|p| p.0);

    let anchor = pattern
        .bytes
        .iter()
        .position(Option::is_some)
        .map(|k| (k, pattern.bytes[k].unwrap_or(0)));
    let Some((k, byte)) = anchor else {
        return Err(Error::Invalid(
            "search pattern needs at least one literal byte".to_string(),
        ));
    };
    let m = pattern.len();

    let mut total = 0usize;
    let mut items = Vec::new();
    let mut covered = 0u64;
    let mut scanned = 0u64;
    for (va, hay, seg_vaddr) in pieces {
        let skip = covered.saturating_sub(va) as usize;
        if skip >= hay.len() {
            continue;
        }
        let (hay, va) = (&hay[skip..], va + skip as u64);
        scanned = scanned
            .checked_add(hay.len() as u64)
            .filter(|s| *s <= max_scan)
            .ok_or_else(|| Error::Limit("byte search exceeds 256 MiB scan budget".to_string()))?;
        scan_piece(
            binary, pattern, hay, va, seg_vaddr, k, byte, m, offset, limit, &mut total, &mut items,
        );
        covered = covered.max(va.saturating_add(hay.len() as u64));
    }
    Ok((total, items))
}

/// Half-open VA intervals a match must lie fully inside.
fn allowed_ranges(
    binary: &LoadedBinary,
    section: Option<&str>,
    range: Option<(u64, u64)>,
) -> Result<Vec<(u64, u64)>> {
    let mut intervals = if let Some(name) = section {
        let mut iv: Vec<(u64, u64)> = binary
            .sections
            .iter()
            .filter(|s| s.name == name && !s.data.is_empty())
            .map(|s| (s.vaddr, s.vaddr.saturating_add(s.data.len() as u64)))
            .collect();
        if !binary.sections.iter().any(|s| s.name == name) {
            return Err(Error::NotFound(format!("no section {name:?}")));
        }
        iv.sort_unstable();
        iv
    } else {
        vec![(0, u64::MAX)]
    };
    if let Some((mut lo, mut hi)) = range {
        if lo > hi {
            std::mem::swap(&mut lo, &mut hi);
        }
        if lo == hi {
            return Err(Error::Invalid("empty search range".to_string()));
        }
        intervals = intervals
            .into_iter()
            .filter_map(|(a, b)| {
                let s = a.max(lo);
                let e = b.min(hi);
                (s < e).then_some((s, e))
            })
            .collect();
    }
    Ok(intervals)
}

/// Anchor occurrences in one deduplicated piece, verifying full windows.
#[allow(clippy::too_many_arguments)]
fn scan_piece(
    binary: &LoadedBinary,
    pattern: &BytePattern,
    hay: &[u8],
    va: u64,
    seg_vaddr: u64,
    anchor_idx: usize,
    anchor: u8,
    m: usize,
    offset: usize,
    limit: usize,
    total: &mut usize,
    items: &mut Vec<ByteMatch>,
) {
    if hay.len() < m {
        return;
    }
    let mut i = 0usize;
    while i + m <= hay.len() {
        let Some(j) = hay[i + anchor_idx..].iter().position(|&b| b == anchor) else {
            break;
        };
        i += j;
        if i + m > hay.len() {
            break;
        }
        if pattern
            .bytes
            .iter()
            .zip(&hay[i..i + m])
            .all(|(p, b)| p.is_none_or(|x| x == *b))
        {
            let addr = va + i as u64;
            if *total >= offset && items.len() < limit {
                let (file_off, section) = attribute(binary, addr, seg_vaddr);
                items.push(ByteMatch {
                    addr,
                    file_off,
                    section,
                    len: m,
                });
            }
            *total += 1;
        }
        i += 1;
    }
}

/// Section name plus file offset for `addr`, if attributable.
fn attribute(binary: &LoadedBinary, addr: u64, seg_vaddr: u64) -> (Option<u64>, Option<String>) {
    for sec in &binary.sections {
        if sec.data.is_empty() || addr < sec.vaddr {
            continue;
        }
        let d = addr - sec.vaddr;
        if d < sec.data.len() as u64 {
            return (sec.file_off.checked_add(d), Some(sec.name.clone()));
        }
    }
    if binary.format == BinaryFormat::Raw {
        return (Some(addr - seg_vaddr), None);
    }
    (None, None)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::disasm::Arch;
    use crate::loader::{BinaryFormat, RawOptions, Section, Segment, load_raw};

    fn raw_bin(bytes: &[u8]) -> LoadedBinary {
        load_raw(
            bytes,
            RawOptions {
                base: 0x1000,
                arch: Arch::X86_64,
                entry: None,
            },
        )
        .expect("raw fixture loads")
    }

    #[test]
    fn parse_accepts_mixed_case_and_whitespace() {
        let p = BytePattern::parse("  48 8b\t??\nAB ").expect("valid");
        assert_eq!(
            p.bytes(),
            &[Some(0x48), Some(0x8b), None, Some(0xAB)],
            "hex is case-insensitive"
        );
        assert_eq!(p.len(), 4);
        assert!(!p.is_empty());
    }

    #[test]
    fn parse_rejects_bad_input() {
        for bad in [
            "", "   ", "??", "?? ?? ??", "5", "abc", "zz", "0x55", "5?", "?5", "555", "48 8B ?",
        ] {
            assert!(
                BytePattern::parse(bad).is_err(),
                "must reject {bad:?} (odd/non-hex/empty/all-wildcard)"
            );
        }
    }

    #[test]
    fn parse_enforces_length_cap() {
        let ok = vec!["ab"; MAX_PATTERN_BYTES].join(" ");
        assert_eq!(BytePattern::parse(&ok).expect("4096 passes").len(), 4096);
        let over = vec!["ab"; MAX_PATTERN_BYTES + 1].join(" ");
        assert!(matches!(
            BytePattern::parse(&over).unwrap_err(),
            Error::Invalid(_)
        ));
    }

    #[test]
    fn scan_budget_counts_unique_bytes() {
        let bin = raw_bin(&[0x90; 64]);
        let pat = BytePattern::parse("90").expect("parse");
        let (total, _) =
            run_limited(&bin, &pat, None, None, 0, usize::MAX, 64).expect("exact budget passes");
        assert_eq!(total, 64);
        assert!(matches!(
            run_limited(&bin, &pat, None, None, 0, usize::MAX, 63).unwrap_err(),
            Error::Limit(_)
        ));
    }

    #[test]
    fn scan_never_bridges_segments() {
        let bin = LoadedBinary {
            format: BinaryFormat::Raw,
            arch: Arch::X86_64,
            entry: None,
            segments: vec![
                Segment {
                    vaddr: 0x1000,
                    mem_size: 2,
                    data: vec![0xAA, 0xAA],
                    readable: true,
                    writable: true,
                    executable: true,
                },
                Segment {
                    vaddr: 0x2000,
                    mem_size: 2,
                    data: vec![0xBB, 0xBB],
                    readable: true,
                    writable: true,
                    executable: true,
                },
            ],
            sections: Vec::new(),
            imports: Vec::new(),
            exports: Vec::new(),
        };
        let spanning = BytePattern::parse("AA AA BB BB").expect("parse");
        let (total, _) = run(&bin, &spanning, None, None, 0, 100).expect("scan");
        assert_eq!(total, 0, "gap bytes must not fuse segments");
        let (total, items) = run(
            &bin,
            &BytePattern::parse("AA").expect("parse"),
            None,
            None,
            0,
            100,
        )
        .expect("scan");
        assert_eq!(total, 2);
        assert_eq!(items[0].addr, 0x1000);
    }

    #[test]
    fn overlapping_segment_views_scan_once() {
        let bin = LoadedBinary {
            format: BinaryFormat::Raw,
            arch: Arch::X86_64,
            entry: None,
            segments: vec![
                Segment {
                    vaddr: 0x1000,
                    mem_size: 4,
                    data: vec![0x90, 0x90, 0x90, 0x90],
                    readable: true,
                    writable: true,
                    executable: true,
                },
                Segment {
                    vaddr: 0x1002,
                    mem_size: 4,
                    data: vec![0x90, 0x90, 0x90, 0x90],
                    readable: true,
                    writable: true,
                    executable: true,
                },
            ],
            sections: Vec::new(),
            imports: Vec::new(),
            exports: Vec::new(),
        };
        let (total, _) = run(
            &bin,
            &BytePattern::parse("90").expect("parse"),
            None,
            None,
            0,
            100,
        )
        .expect("scan");
        assert_eq!(total, 6, "union 0x1000..0x1006 scanned once, not 4+4");
    }

    #[test]
    fn bss_section_filters_to_empty_page() {
        let bin = LoadedBinary {
            format: BinaryFormat::Elf,
            arch: Arch::X86_64,
            entry: None,
            segments: vec![Segment {
                vaddr: 0x400000,
                mem_size: 0x110,
                data: vec![0x90; 16],
                readable: true,
                writable: false,
                executable: true,
            }],
            sections: vec![Section {
                name: ".bss".to_string(),
                vaddr: 0x400010,
                size: 0x100,
                file_off: 0,
                data: Vec::new(),
                readable: true,
                writable: true,
                executable: false,
            }],
            imports: Vec::new(),
            exports: Vec::new(),
        };
        let pat = BytePattern::parse("90").expect("parse");
        let (total, _) = run(&bin, &pat, Some(".bss"), None, 0, 10).expect("bss filter");
        assert_eq!(
            total, 0,
            "virtual-only section contributes no searchable bytes"
        );
        let err = run(&bin, &pat, Some(".text"), None, 0, 10).unwrap_err();
        assert!(
            matches!(err, Error::NotFound(_)),
            "unknown section: {err:?}"
        );
    }

    #[test]
    fn range_normalizes_and_rejects_empty() {
        let bin = raw_bin(&[0x55, 0x89, 0xE5]);
        let pat = BytePattern::parse("55 89 e5").expect("parse");
        let swapped = run(&bin, &pat, None, Some((0x1003, 0x1000)), 0, 10).expect("swapped");
        let straight = run(&bin, &pat, None, Some((0x1000, 0x1003)), 0, 10).expect("straight");
        assert_eq!(swapped, straight);
        assert!(matches!(
            run(&bin, &pat, None, Some((0x1000, 0x1000)), 0, 10).unwrap_err(),
            Error::Invalid(_)
        ));
    }
}