binsleuth 0.4.0

ELF/PE binary security hardening checker and section-level entropy analyzer
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
use anyhow::Result;
use object::Endianness;
use object::read::elf::{Dyn, FileHeader, ProgramHeader};
use object::{FileKind, Object, ObjectSection, ObjectSymbol, elf};
use serde::Serialize;

// ── Result types ─────────────────────────────────────────────────────────────

/// Tri-state for a hardening check whose presence can be partial.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CheckResult {
    /// Protection is present and fully enabled
    Enabled,
    /// Protection is partially present (e.g. Partial RELRO vs Full RELRO)
    Partial(String),
    /// Protection is absent
    Disabled,
    /// Could not determine (binary format doesn't apply)
    NotApplicable,
}

impl Serialize for CheckResult {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match self {
            CheckResult::Enabled => serializer.serialize_str("enabled"),
            CheckResult::Disabled => serializer.serialize_str("disabled"),
            CheckResult::NotApplicable => serializer.serialize_str("n/a"),
            CheckResult::Partial(msg) => serializer.serialize_str(&format!("partial: {msg}")),
        }
    }
}

/// All hardening checks collected from one binary.
#[derive(Debug, Clone, Serialize)]
pub struct HardeningInfo {
    pub format: String,
    pub architecture: String,
    /// NX / DEP — non-executable stack/data
    pub nx: CheckResult,
    /// PIE — position-independent executable
    pub pie: CheckResult,
    /// RELRO — read-only relocations (ELF-only)
    pub relro: CheckResult,
    /// Stack canary — `__stack_chk_fail` symbol present
    pub stack_canary: CheckResult,
    /// FORTIFY_SOURCE — fortified libc wrappers (`__*_chk`) present
    pub fortify_source: CheckResult,
    /// RPATH/RUNPATH — ELF library search-path injection risk (Enabled = safe, no path set)
    pub rpath: CheckResult,
    /// Debug symbols stripped (Enabled = stripped = good; Disabled = debug info present)
    pub stripped: CheckResult,
    /// Dangerous symbols found in the import/symbol table
    pub dangerous_symbols: Vec<DangerousSymbol>,
}

/// Category of a dangerous symbol, used for colour-coded visualisation.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum SymbolCategory {
    /// Process / shell execution (system, execve, WinExec, …)
    Exec,
    /// Network I/O (connect, socket, WinHttpOpen, …)
    Net,
    /// Memory permission manipulation (mprotect, VirtualAlloc, …)
    Mem,
}

/// A dangerous symbol found in the binary's symbol / import table.
#[derive(Debug, Clone, Serialize)]
pub struct DangerousSymbol {
    pub name: String,
    pub category: SymbolCategory,
}

// ── Dangerous symbol lists ────────────────────────────────────────────────────

const DANGEROUS_EXEC: &[&str] = &[
    "system",
    "popen",
    "execve",
    "execl",
    "execle",
    "execlp",
    "execvp",
    "execvpe",
    "posix_spawn",
    "ShellExecute",
    "ShellExecuteA",
    "ShellExecuteW",
    "WinExec",
    "CreateProcessA",
    "CreateProcessW",
];

const DANGEROUS_NET: &[&str] = &[
    "connect",
    "socket",
    "WSAConnect",
    "WSASocket",
    "WSAStartup",
    "gethostbyname",
    "getaddrinfo",
    "URLDownloadToFile",
    "WinHttpOpen",
    "InternetOpen",
    "InternetOpenA",
    "InternetOpenW",
];

const DANGEROUS_MEM: &[&str] = &[
    "mprotect",
    "VirtualProtect",
    "VirtualAlloc",
    "mmap",
    "NtAllocateVirtualMemory",
    "NtProtectVirtualMemory",
];

// ── Main analysis ─────────────────────────────────────────────────────────────

impl HardeningInfo {
    pub fn analyze(data: &[u8]) -> Result<HardeningInfo> {
        let kind = FileKind::parse(data)?;
        match kind {
            FileKind::Elf32 | FileKind::Elf64 => analyze_elf(data),
            FileKind::Pe32 | FileKind::Pe64 => analyze_pe(data),
            other => {
                anyhow::bail!("Unsupported binary format: {:?}", other);
            }
        }
    }
}

// ── ELF analysis ─────────────────────────────────────────────────────────────

fn analyze_elf(data: &[u8]) -> Result<HardeningInfo> {
    let obj = object::File::parse(data)?;
    let format = "ELF".to_owned();
    let architecture = format!("{:?}", obj.architecture());

    let kind = FileKind::parse(data)?;
    let is_64 = kind == FileKind::Elf64;

    let nx = if is_64 {
        detect_nx_64(data)
    } else {
        detect_nx_32(data)
    };
    let pie = if is_64 {
        detect_pie_64(data)
    } else {
        detect_pie_32(data)
    };
    let relro = if is_64 {
        detect_relro_64(data)
    } else {
        detect_relro_32(data)
    };

    // Stack Canary: search for well-known canary symbols in symbol tables
    let canary_syms = [
        "__stack_chk_fail",
        "__stack_chk_guard",
        "__stack_smash_handler",
    ];
    let stack_canary = if obj.symbols().chain(obj.dynamic_symbols()).any(|sym| {
        sym.name()
            .map(|n| canary_syms.iter().any(|&c| n.contains(c)))
            .unwrap_or(false)
    }) {
        CheckResult::Enabled
    } else {
        CheckResult::Disabled
    };

    let dangerous_symbols = collect_dangerous_symbols(&obj);

    let fortify_source = detect_fortify_source(&obj);

    let rpath = if is_64 {
        detect_rpath_64(data)
    } else {
        detect_rpath_32(data)
    };

    // Stripped: check for embedded DWARF debug sections
    let has_debug = obj
        .sections()
        .any(|s| s.name().map(|n| n.starts_with(".debug_")).unwrap_or(false));
    let stripped = if has_debug {
        CheckResult::Disabled
    } else {
        CheckResult::Enabled
    };

    Ok(HardeningInfo {
        format,
        architecture,
        nx,
        pie,
        relro,
        stack_canary,
        fortify_source,
        rpath,
        stripped,
        dangerous_symbols,
    })
}

// ── NX helpers ────────────────────────────────────────────────────────────────

fn detect_nx_64(data: &[u8]) -> CheckResult {
    let header = match object::elf::FileHeader64::<Endianness>::parse(data) {
        Ok(h) => h,
        Err(_) => return CheckResult::Disabled,
    };
    let endian = match header.endian() {
        Ok(e) => e,
        Err(_) => return CheckResult::Disabled,
    };
    let phdrs = match header.program_headers(endian, data) {
        Ok(p) => p,
        Err(_) => return CheckResult::Disabled,
    };
    nx_from_phdrs(phdrs, endian)
}

fn detect_nx_32(data: &[u8]) -> CheckResult {
    let header = match object::elf::FileHeader32::<Endianness>::parse(data) {
        Ok(h) => h,
        Err(_) => return CheckResult::Disabled,
    };
    let endian = match header.endian() {
        Ok(e) => e,
        Err(_) => return CheckResult::Disabled,
    };
    let phdrs = match header.program_headers(endian, data) {
        Ok(p) => p,
        Err(_) => return CheckResult::Disabled,
    };
    nx_from_phdrs(phdrs, endian)
}

fn nx_from_phdrs<P: ProgramHeader<Endian = Endianness>>(
    phdrs: &[P],
    endian: Endianness,
) -> CheckResult {
    for phdr in phdrs {
        if phdr.p_type(endian) == elf::PT_GNU_STACK {
            // PF_X = 0x1 — if NOT set, the stack is non-executable → NX enabled
            return if phdr.p_flags(endian) & elf::PF_X == 0 {
                CheckResult::Enabled
            } else {
                CheckResult::Disabled
            };
        }
    }
    // No PT_GNU_STACK found — treat as NX disabled (conservative)
    CheckResult::Disabled
}

// ── PIE helpers ───────────────────────────────────────────────────────────────

fn detect_pie_64(data: &[u8]) -> CheckResult {
    let header = match object::elf::FileHeader64::<Endianness>::parse(data) {
        Ok(h) => h,
        Err(_) => return CheckResult::Disabled,
    };
    let endian = match header.endian() {
        Ok(e) => e,
        Err(_) => return CheckResult::Disabled,
    };
    // ET_DYN (3) = position-independent; ET_EXEC (2) = non-PIE
    if header.e_type(endian) == elf::ET_DYN {
        CheckResult::Enabled
    } else {
        CheckResult::Disabled
    }
}

fn detect_pie_32(data: &[u8]) -> CheckResult {
    let header = match object::elf::FileHeader32::<Endianness>::parse(data) {
        Ok(h) => h,
        Err(_) => return CheckResult::Disabled,
    };
    let endian = match header.endian() {
        Ok(e) => e,
        Err(_) => return CheckResult::Disabled,
    };
    if header.e_type(endian) == elf::ET_DYN {
        CheckResult::Enabled
    } else {
        CheckResult::Disabled
    }
}

// ── RELRO helpers ─────────────────────────────────────────────────────────────

fn detect_relro_64(data: &[u8]) -> CheckResult {
    let header = match object::elf::FileHeader64::<Endianness>::parse(data) {
        Ok(h) => h,
        Err(_) => return CheckResult::NotApplicable,
    };
    let endian = match header.endian() {
        Ok(e) => e,
        Err(_) => return CheckResult::NotApplicable,
    };
    let phdrs = match header.program_headers(endian, data) {
        Ok(p) => p,
        Err(_) => return CheckResult::NotApplicable,
    };

    let mut has_relro = false;
    let mut has_bind_now = false;

    for phdr in phdrs {
        match phdr.p_type(endian) {
            elf::PT_GNU_RELRO => has_relro = true,
            elf::PT_DYNAMIC => {
                if let Ok(Some(entries)) = phdr.dynamic(endian, data) {
                    for entry in entries {
                        // Dyn64: d_tag → u64, d_val → u64
                        let tag = entry.d_tag(endian);
                        let val = entry.d_val(endian);
                        if tag == elf::DT_FLAGS as u64 && val & elf::DF_BIND_NOW as u64 != 0 {
                            has_bind_now = true;
                        }
                        if tag == elf::DT_FLAGS_1 as u64 && val & elf::DF_1_NOW as u64 != 0 {
                            has_bind_now = true;
                        }
                    }
                }
            }
            _ => {}
        }
    }

    relro_result(has_relro, has_bind_now)
}

fn detect_relro_32(data: &[u8]) -> CheckResult {
    let header = match object::elf::FileHeader32::<Endianness>::parse(data) {
        Ok(h) => h,
        Err(_) => return CheckResult::NotApplicable,
    };
    let endian = match header.endian() {
        Ok(e) => e,
        Err(_) => return CheckResult::NotApplicable,
    };
    let phdrs = match header.program_headers(endian, data) {
        Ok(p) => p,
        Err(_) => return CheckResult::NotApplicable,
    };

    let mut has_relro = false;
    let mut has_bind_now = false;

    for phdr in phdrs {
        match phdr.p_type(endian) {
            elf::PT_GNU_RELRO => has_relro = true,
            elf::PT_DYNAMIC => {
                if let Ok(Some(entries)) = phdr.dynamic(endian, data) {
                    for entry in entries {
                        // Dyn32: d_tag → u32, d_val → u32; widen both for uniform comparisons
                        let tag = u64::from(entry.d_tag(endian));
                        let val = u64::from(entry.d_val(endian));
                        if tag == elf::DT_FLAGS as u64 && val & elf::DF_BIND_NOW as u64 != 0 {
                            has_bind_now = true;
                        }
                        if tag == elf::DT_FLAGS_1 as u64 && val & elf::DF_1_NOW as u64 != 0 {
                            has_bind_now = true;
                        }
                    }
                }
            }
            _ => {}
        }
    }

    relro_result(has_relro, has_bind_now)
}

fn relro_result(has_relro: bool, has_bind_now: bool) -> CheckResult {
    match (has_relro, has_bind_now) {
        (true, true) => CheckResult::Enabled,
        (true, false) => CheckResult::Partial("Partial RELRO".to_owned()),
        _ => CheckResult::Disabled,
    }
}

// ── PE analysis ───────────────────────────────────────────────────────────────

fn analyze_pe(data: &[u8]) -> Result<HardeningInfo> {
    let obj = object::File::parse(data)?;
    let format = "PE".to_owned();
    let architecture = format!("{:?}", obj.architecture());

    let (nx, pie) = detect_pe_characteristics(data);
    let relro = CheckResult::NotApplicable;

    let canary_syms = [
        "__security_cookie",
        "__security_check_cookie",
        "__stack_chk_fail",
    ];
    let stack_canary = if obj.symbols().chain(obj.dynamic_symbols()).any(|sym| {
        sym.name()
            .map(|n| canary_syms.iter().any(|&c| n.contains(c)))
            .unwrap_or(false)
    }) {
        CheckResult::Enabled
    } else {
        CheckResult::Disabled
    };

    let dangerous_symbols = collect_dangerous_symbols(&obj);

    let fortify_source = detect_fortify_source(&obj);

    // Stripped: check PE debug directory and embedded .debug_* sections
    let stripped = detect_pe_stripped(data, &obj);

    Ok(HardeningInfo {
        format,
        architecture,
        nx,
        pie,
        relro,
        stack_canary,
        fortify_source,
        rpath: CheckResult::NotApplicable,
        stripped,
        dangerous_symbols,
    })
}

/// Extract DllCharacteristics from the PE optional header via raw bytes.
fn detect_pe_characteristics(data: &[u8]) -> (CheckResult, CheckResult) {
    if data.len() < 0x40 {
        return (CheckResult::Disabled, CheckResult::Disabled);
    }
    let e_lfanew = u32::from_le_bytes([data[0x3c], data[0x3d], data[0x3e], data[0x3f]]) as usize;
    if data.len() < e_lfanew + 4 || &data[e_lfanew..e_lfanew + 4] != b"PE\0\0" {
        return (CheckResult::Disabled, CheckResult::Disabled);
    }

    // Optional header starts at e_lfanew + 4 (PE sig) + 20 (COFF header)
    let opt_offset = e_lfanew + 24;
    if data.len() < opt_offset + 2 {
        return (CheckResult::Disabled, CheckResult::Disabled);
    }

    let magic = u16::from_le_bytes([data[opt_offset], data[opt_offset + 1]]);
    // DllCharacteristics: offset 70 from start of optional header (same for PE32 and PE32+)
    let dll_char_offset = match magic {
        0x010b | 0x020b => opt_offset + 70,
        _ => return (CheckResult::Disabled, CheckResult::Disabled),
    };

    if data.len() < dll_char_offset + 2 {
        return (CheckResult::Disabled, CheckResult::Disabled);
    }
    let dll_chars = u16::from_le_bytes([data[dll_char_offset], data[dll_char_offset + 1]]);

    // IMAGE_DLLCHARACTERISTICS_NX_COMPAT    = 0x0100
    // IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE = 0x0040 (PIE/ASLR)
    let nx = if dll_chars & 0x0100 != 0 {
        CheckResult::Enabled
    } else {
        CheckResult::Disabled
    };
    let pie = if dll_chars & 0x0040 != 0 {
        CheckResult::Enabled
    } else {
        CheckResult::Disabled
    };
    (nx, pie)
}

// ── Helpers ───────────────────────────────────────────────────────────────────

/// Detect whether a PE binary has debug information (via debug directory or .debug_* sections).
/// Returns Enabled (stripped) or Disabled (debug info present).
fn detect_pe_stripped(data: &[u8], obj: &object::File) -> CheckResult {
    // Check for embedded .debug_* sections (MinGW/GCC DWARF-in-PE)
    let has_debug_sections = obj
        .sections()
        .any(|s| s.name().map(|n| n.starts_with(".debug_")).unwrap_or(false));
    if has_debug_sections {
        return CheckResult::Disabled;
    }

    // Check PE debug directory (IMAGE_DIRECTORY_ENTRY_DEBUG, index 6)
    // DataDirectory for PE32  starts at opt_offset + 96
    // DataDirectory for PE32+ starts at opt_offset + 112
    // Each entry is 8 bytes; entry 6 is at DataDirectory + 48
    if data.len() < 0x40 {
        return CheckResult::Enabled;
    }
    let e_lfanew = u32::from_le_bytes([data[0x3c], data[0x3d], data[0x3e], data[0x3f]]) as usize;
    if data.len() < e_lfanew + 4 || &data[e_lfanew..e_lfanew + 4] != b"PE\0\0" {
        return CheckResult::Enabled;
    }
    let opt_offset = e_lfanew + 24;
    if data.len() < opt_offset + 2 {
        return CheckResult::Enabled;
    }
    let magic = u16::from_le_bytes([data[opt_offset], data[opt_offset + 1]]);
    let debug_entry_offset = match magic {
        0x010b => opt_offset + 96 + 48,  // PE32:  DataDirectory + entry 6
        0x020b => opt_offset + 112 + 48, // PE32+: DataDirectory + entry 6
        _ => return CheckResult::Enabled,
    };
    if data.len() < debug_entry_offset + 4 {
        return CheckResult::Enabled;
    }
    let rva = u32::from_le_bytes([
        data[debug_entry_offset],
        data[debug_entry_offset + 1],
        data[debug_entry_offset + 2],
        data[debug_entry_offset + 3],
    ]);
    if rva != 0 {
        CheckResult::Disabled // debug directory present → not stripped
    } else {
        CheckResult::Enabled
    }
}

// ── FORTIFY_SOURCE detection ──────────────────────────────────────────────────

/// Detect `-D_FORTIFY_SOURCE` by looking for fortified libc wrapper symbols (`__*_chk`).
///
/// When compiled with `_FORTIFY_SOURCE=1` or `=2`, the compiler replaces unsafe
/// libc calls (memcpy, strcpy, sprintf, …) with bounds-checking variants whose
/// names end in `_chk` and are prefixed with `__` (e.g. `__memcpy_chk`).
fn detect_fortify_source(obj: &object::File) -> CheckResult {
    let has_chk = obj
        .symbols()
        .chain(obj.dynamic_symbols())
        .filter_map(|sym| sym.name().ok())
        .any(|name| name.starts_with("__") && name.ends_with("_chk"));
    if has_chk {
        CheckResult::Enabled
    } else {
        CheckResult::Disabled
    }
}

// ── RPATH / RUNPATH detection ─────────────────────────────────────────────────

/// Scan the ELF dynamic section for `DT_RPATH` (tag 15) or `DT_RUNPATH` (tag 29).
///
/// An embedded RPATH/RUNPATH can allow an attacker to inject a malicious shared
/// library into the process if the path includes a world-writable or relative
/// directory.  Returns:
/// - `Enabled`  — no RPATH/RUNPATH present (safe)
/// - `Disabled` — one or more entries found (potential hijacking vector)
/// - `NotApplicable` — binary has no dynamic segment (static binary)
fn rpath_from_dynamic<D: Dyn<Endian = Endianness>>(entries: &[D], endian: Endianness) -> bool {
    entries.iter().any(|entry| {
        let tag = entry.d_tag(endian).into();
        let val: u64 = entry.d_val(endian).into();
        (tag == elf::DT_RPATH as u64 || tag == elf::DT_RUNPATH as u64) && val != 0
    })
}

fn detect_rpath_64(data: &[u8]) -> CheckResult {
    let header = match object::elf::FileHeader64::<Endianness>::parse(data) {
        Ok(h) => h,
        Err(_) => return CheckResult::NotApplicable,
    };
    let endian = match header.endian() {
        Ok(e) => e,
        Err(_) => return CheckResult::NotApplicable,
    };
    let phdrs = match header.program_headers(endian, data) {
        Ok(p) => p,
        Err(_) => return CheckResult::NotApplicable,
    };

    let mut has_dynamic = false;
    for phdr in phdrs {
        if phdr.p_type(endian) == elf::PT_DYNAMIC {
            has_dynamic = true;
            if let Ok(Some(entries)) = phdr.dynamic(endian, data) {
                if rpath_from_dynamic(entries, endian) {
                    return CheckResult::Disabled;
                }
            }
        }
    }
    if has_dynamic {
        CheckResult::Enabled
    } else {
        CheckResult::NotApplicable
    }
}

fn detect_rpath_32(data: &[u8]) -> CheckResult {
    let header = match object::elf::FileHeader32::<Endianness>::parse(data) {
        Ok(h) => h,
        Err(_) => return CheckResult::NotApplicable,
    };
    let endian = match header.endian() {
        Ok(e) => e,
        Err(_) => return CheckResult::NotApplicable,
    };
    let phdrs = match header.program_headers(endian, data) {
        Ok(p) => p,
        Err(_) => return CheckResult::NotApplicable,
    };

    let mut has_dynamic = false;
    for phdr in phdrs {
        if phdr.p_type(endian) == elf::PT_DYNAMIC {
            has_dynamic = true;
            if let Ok(Some(entries)) = phdr.dynamic(endian, data) {
                if rpath_from_dynamic(entries, endian) {
                    return CheckResult::Disabled;
                }
            }
        }
    }
    if has_dynamic {
        CheckResult::Enabled
    } else {
        CheckResult::NotApplicable
    }
}

fn collect_dangerous_symbols(obj: &object::File) -> Vec<DangerousSymbol> {
    let mut found: Vec<DangerousSymbol> = obj
        .symbols()
        .chain(obj.dynamic_symbols())
        .filter_map(|sym| sym.name().ok())
        .filter_map(categorize_dangerous_symbol)
        .collect();

    found.sort_by(|a, b| a.name.cmp(&b.name));
    found.dedup_by(|a, b| a.name == b.name);
    found
}

/// Return the category of a symbol if it is considered dangerous, otherwise `None`.
fn categorize_dangerous_symbol(name: &str) -> Option<DangerousSymbol> {
    let category = if DANGEROUS_EXEC
        .iter()
        .any(|&d| name == d || name.contains(d))
    {
        SymbolCategory::Exec
    } else if DANGEROUS_NET.iter().any(|&d| name == d || name.contains(d)) {
        SymbolCategory::Net
    } else if DANGEROUS_MEM.iter().any(|&d| name == d || name.contains(d)) {
        SymbolCategory::Mem
    } else {
        return None;
    };
    Some(DangerousSymbol {
        name: name.to_owned(),
        category,
    })
}

// ── Unit tests ────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    // ── Helpers ───────────────────────────────────────────────────────────────

    /// Build a minimal syntactically-valid PE32 with the given DllCharacteristics.
    ///
    /// Layout (all little-endian):
    ///   0x00        DOS magic "MZ"
    ///   0x3c..0x40  e_lfanew = 0x40
    ///   0x40..0x44  PE signature "PE\0\0"
    ///   0x44..0x58  COFF header (20 zero bytes)
    ///   0x58..0x5a  Optional header magic = 0x010b (PE32)
    ///   0x58+70 ..  DllCharacteristics (2 bytes)
    fn make_pe32(dll_chars: u16) -> Vec<u8> {
        const E_LFANEW: usize = 0x40;
        const OPT_OFFSET: usize = E_LFANEW + 4 + 20; // PE sig + COFF header
        const DLL_OFFSET: usize = OPT_OFFSET + 70;

        let mut data = vec![0u8; DLL_OFFSET + 2];
        data[0] = b'M';
        data[1] = b'Z';
        data[0x3c..0x40].copy_from_slice(&(E_LFANEW as u32).to_le_bytes());
        data[E_LFANEW..E_LFANEW + 4].copy_from_slice(b"PE\0\0");
        data[OPT_OFFSET..OPT_OFFSET + 2].copy_from_slice(&0x010bu16.to_le_bytes()); // PE32
        data[DLL_OFFSET..DLL_OFFSET + 2].copy_from_slice(&dll_chars.to_le_bytes());
        data
    }

    // ── PE characteristic tests ───────────────────────────────────────────────

    #[test]
    fn pe_nx_and_pie_enabled() {
        let (nx, pie) = detect_pe_characteristics(&make_pe32(0x0140));
        assert_eq!(nx, CheckResult::Enabled);
        assert_eq!(pie, CheckResult::Enabled);
    }

    #[test]
    fn pe_nx_only_enabled() {
        let (nx, pie) = detect_pe_characteristics(&make_pe32(0x0100));
        assert_eq!(nx, CheckResult::Enabled);
        assert_eq!(pie, CheckResult::Disabled);
    }

    #[test]
    fn pe_pie_only_enabled() {
        let (nx, pie) = detect_pe_characteristics(&make_pe32(0x0040));
        assert_eq!(nx, CheckResult::Disabled);
        assert_eq!(pie, CheckResult::Enabled);
    }

    #[test]
    fn pe_no_protections() {
        let (nx, pie) = detect_pe_characteristics(&make_pe32(0x0000));
        assert_eq!(nx, CheckResult::Disabled);
        assert_eq!(pie, CheckResult::Disabled);
    }

    #[test]
    fn pe_too_short_returns_disabled() {
        let (nx, pie) = detect_pe_characteristics(&[0u8; 10]);
        assert_eq!(nx, CheckResult::Disabled);
        assert_eq!(pie, CheckResult::Disabled);
    }

    #[test]
    fn pe_empty_returns_disabled() {
        let (nx, pie) = detect_pe_characteristics(&[]);
        assert_eq!(nx, CheckResult::Disabled);
        assert_eq!(pie, CheckResult::Disabled);
    }

    #[test]
    fn pe_bad_signature_returns_disabled() {
        let mut data = make_pe32(0x0140);
        // Corrupt the PE signature
        data[0x40] = 0x00;
        let (nx, pie) = detect_pe_characteristics(&data);
        assert_eq!(nx, CheckResult::Disabled);
        assert_eq!(pie, CheckResult::Disabled);
    }

    // ── RELRO helper tests ────────────────────────────────────────────────────

    #[test]
    fn relro_full_when_both_flags_set() {
        assert_eq!(relro_result(true, true), CheckResult::Enabled);
    }

    #[test]
    fn relro_partial_when_only_segment_present() {
        assert!(
            matches!(relro_result(true, false), CheckResult::Partial(_)),
            "expected Partial RELRO"
        );
    }

    #[test]
    fn relro_disabled_when_no_segment() {
        assert_eq!(relro_result(false, false), CheckResult::Disabled);
    }

    #[test]
    fn relro_disabled_when_bind_now_without_segment() {
        // BIND_NOW alone (no PT_GNU_RELRO) should still be Disabled
        assert_eq!(relro_result(false, true), CheckResult::Disabled);
    }

    // ── CheckResult display invariants ────────────────────────────────────────

    #[test]
    fn check_result_partial_carries_message() {
        let msg = "Partial RELRO".to_owned();
        match CheckResult::Partial(msg.clone()) {
            CheckResult::Partial(s) => assert_eq!(s, msg),
            other => panic!("expected Partial, got {:?}", other),
        }
    }

    // ── FORTIFY_SOURCE tests ──────────────────────────────────────────────────

    #[test]
    fn fortify_source_disabled_for_empty_object() {
        // A minimal ELF with no symbols → FORTIFY_SOURCE must be Disabled
        let path = std::env::current_exe().expect("current exe");
        let data = std::fs::read(&path).expect("read self");
        let obj = object::File::parse(data.as_slice()).expect("parse");
        // We can't guarantee the test binary has __*_chk; just verify no panic
        let result = detect_fortify_source(&obj);
        assert!(matches!(
            result,
            CheckResult::Enabled | CheckResult::Disabled
        ));
    }

    // ── RPATH helpers tests ───────────────────────────────────────────────────

    #[test]
    #[cfg(target_os = "linux")]
    fn rpath_result_on_self_is_valid() {
        let path = std::env::current_exe().expect("current exe");
        let data = std::fs::read(&path).expect("read self");
        let result = detect_rpath_64(&data);
        // A freshly built Rust binary should have no RPATH or be NotApplicable
        assert!(matches!(
            result,
            CheckResult::Enabled | CheckResult::Disabled | CheckResult::NotApplicable
        ));
    }

    // ── categorize_dangerous_symbol ───────────────────────────────────────────

    #[test]
    fn exec_symbol_is_categorized_as_exec() {
        let sym = categorize_dangerous_symbol("system").expect("system must be dangerous");
        assert_eq!(sym.name, "system");
        assert_eq!(sym.category, SymbolCategory::Exec);
    }

    #[test]
    fn net_symbol_is_categorized_as_net() {
        let sym = categorize_dangerous_symbol("connect").expect("connect must be dangerous");
        assert_eq!(sym.category, SymbolCategory::Net);
    }

    #[test]
    fn mem_symbol_is_categorized_as_mem() {
        let sym = categorize_dangerous_symbol("mprotect").expect("mprotect must be dangerous");
        assert_eq!(sym.category, SymbolCategory::Mem);
    }

    #[test]
    fn windows_exec_symbol_is_exec() {
        let sym = categorize_dangerous_symbol("WinExec").expect("WinExec must be dangerous");
        assert_eq!(sym.category, SymbolCategory::Exec);
    }

    #[test]
    fn windows_net_symbol_is_net() {
        let sym = categorize_dangerous_symbol("WSAConnect").expect("WSAConnect must be dangerous");
        assert_eq!(sym.category, SymbolCategory::Net);
    }

    #[test]
    fn windows_mem_symbol_is_mem() {
        let sym =
            categorize_dangerous_symbol("VirtualAlloc").expect("VirtualAlloc must be dangerous");
        assert_eq!(sym.category, SymbolCategory::Mem);
    }

    #[test]
    fn safe_symbols_return_none() {
        assert!(categorize_dangerous_symbol("printf").is_none());
        assert!(categorize_dangerous_symbol("malloc").is_none());
        assert!(categorize_dangerous_symbol("strlen").is_none());
        assert!(categorize_dangerous_symbol("memcpy").is_none());
        assert!(categorize_dangerous_symbol("").is_none());
    }

    #[test]
    fn dangerous_symbol_carries_name() {
        let sym = categorize_dangerous_symbol("execve").unwrap();
        assert_eq!(sym.name, "execve");
    }

    // ── Full HardeningInfo round-trip on the test binary (ELF) ───────────────

    #[test]
    #[cfg(target_os = "linux")]
    fn analyze_self_succeeds_on_linux() {
        // Read the binsleuth test binary that cargo placed next to us.
        // The binary will be valid ELF on Linux.
        let path = std::env::current_exe().expect("current exe");
        let data = std::fs::read(&path).expect("read self");
        let info = HardeningInfo::analyze(&data).expect("analyze");
        assert_eq!(info.format, "ELF");
        // At minimum the enum variants should be the expected types
        assert!(matches!(
            info.nx,
            CheckResult::Enabled | CheckResult::Disabled | CheckResult::Partial(_)
        ));
    }
}