Skip to main content

analyze_file

Function analyze_file 

Source
pub fn analyze_file(path: &PathBuf) -> Result<SecurityCheck>
Expand description

Analyze a single executable file for security features

This function detects the executable format and performs appropriate security analysis.

§Arguments

  • path - Path to the executable file to analyze

§Returns

Returns a SecurityCheck struct containing the analysis results, or an error if the file cannot be read or is not a supported executable format.

§Example

use execheck::analyze_file;
use std::path::PathBuf;

let result = analyze_file(&PathBuf::from("/bin/ls"))?;
println!("Security status: {}", result.overall_status);
Examples found in repository?
examples/simple_analysis.rs (line 15)
9fn main() -> Result<(), Box<dyn std::error::Error>> {
10    // Analyze a single executable file
11    let file_path = PathBuf::from("/bin/ls");
12    
13    println!("Analyzing: {}", file_path.display());
14    
15    match analyze_file(&file_path) {
16        Ok(result) => {
17            println!("\n=== Analysis Results ===");
18            println!("File: {}", result.file_path);
19            println!("Type: {}", result.file_type);
20            println!("Status: {}", result.overall_status);
21            
22            println!("\n=== Security Checks ===");
23            for (check, value) in &result.checks {
24                println!("  {}: {}", check, value);
25            }
26            
27            // Create a report and print it in human-readable format
28            println!("\n=== Formatted Report ===");
29            let report = SecurityReport {
30                files: vec![result],
31                summary: ReportSummary {
32                    total_files: 1,
33                    secure_files: 1,
34                    insecure_files: 0,
35                    unsupported_files: 0,
36                },
37            };
38            
39            print_report(&report, &OutputFormat::Human, None)?;
40        }
41        Err(e) => {
42            eprintln!("Failed to analyze file: {}", e);
43            std::process::exit(1);
44        }
45    }
46    
47    Ok(())
48}
More examples
Hide additional examples
examples/json_output.rs (line 25)
9fn main() -> Result<(), Box<dyn std::error::Error>> {
10    // Analyze multiple files
11    let files = vec![
12        PathBuf::from("/bin/ls"),
13        PathBuf::from("/bin/cat"),
14        PathBuf::from("/bin/echo"),
15    ];
16    
17    let mut results = Vec::new();
18    let mut secure_count = 0;
19    let mut insecure_count = 0;
20    let mut unsupported_count = 0;
21    
22    println!("Analyzing {} files...", files.len());
23    
24    for file_path in files {
25        match analyze_file(&file_path) {
26            Ok(result) => {
27                match result.overall_status.as_str() {
28                    "Secure" => secure_count += 1,
29                    "Mostly Secure" | "Insecure" => insecure_count += 1,
30                    _ => unsupported_count += 1,
31                }
32                results.push(result);
33            }
34            Err(e) => {
35                eprintln!("Warning: Failed to analyze {}: {}", file_path.display(), e);
36                unsupported_count += 1;
37            }
38        }
39    }
40    
41    // Create report
42    let report = SecurityReport {
43        files: results,
44        summary: ReportSummary {
45            total_files: secure_count + insecure_count + unsupported_count,
46            secure_files: secure_count,
47            insecure_files: insecure_count,
48            unsupported_files: unsupported_count,
49        },
50    };
51    
52    println!("\n=== JSON Output ===");
53    print_report(&report, &OutputFormat::Json, None)?;
54    
55    Ok(())
56}
examples/fat_binary_analysis.rs (line 34)
11fn main() -> Result<(), Box<dyn std::error::Error>> {
12    println!("=== ExeCheck Fat Binary Analysis Example ===");
13    println!();
14    
15    // Example fat binary paths (these may not exist on all systems)
16    let potential_fat_binaries = vec![
17        "/Applications/Xcode.app/Contents/MacOS/Xcode",
18        "/System/Applications/Calculator.app/Contents/MacOS/Calculator",
19        "/usr/bin/python3",
20        "/bin/bash",
21    ];
22    
23    println!("Searching for fat binaries on the system...");
24    println!();
25    
26    let mut found_examples = false;
27    
28    for binary_path in potential_fat_binaries {
29        let path = PathBuf::from(binary_path);
30        
31        if path.exists() {
32            println!("Analyzing: {}", binary_path);
33            
34            match analyze_file(&path) {
35                Ok(result) => {
36                    // Check if this is a fat binary
37                    if result.file_type.contains("Fat") {
38                        found_examples = true;
39                        
40                        println!("✅ Found fat binary!");
41                        println!("   File Type: {}", result.file_type);
42                        println!("   Status: {}", result.overall_status);
43                        
44                        if let Some(arch_count) = result.checks.get("architectures") {
45                            println!("   Architectures: {}", arch_count);
46                        }
47                        
48                        // Show per-architecture security details
49                        if let Some(total_archs) = result.checks.get("total_architectures") {
50                            println!("   Total Architectures: {}", total_archs);
51                        }
52                        if let Some(secure_archs) = result.checks.get("secure_architectures") {
53                            println!("   Secure Architectures: {}", secure_archs);
54                        }
55                        if let Some(arch_list) = result.checks.get("architectures") {
56                            println!("   Architecture List: {}", arch_list);
57                        }
58                        
59                        // Show individual architecture security checks
60                        println!("   Per-Architecture Security:");
61                        for (key, value) in &result.checks {
62                            if key.contains("_pie") || key.contains("_canary") || key.contains("_nx") {
63                                println!("     {}: {}", key, value);
64                            }
65                        }
66                        
67                        println!();
68                        
69                        // Show detailed report in JSON format
70                        println!("Detailed JSON Report:");
71                        print_report(&execheck::SecurityReport {
72                            files: vec![result],
73                            summary: execheck::ReportSummary {
74                                total_files: 1,
75                                secure_files: 0,
76                                insecure_files: 0,
77                                unsupported_files: 1,
78                            },
79                        }, &OutputFormat::Json, None)?;
80                        
81                        println!("\n{}\n", "=".repeat(60));
82                    } else {
83                        println!("   Not a fat binary ({})", result.file_type);
84                    }
85                }
86                Err(e) => {
87                    println!("   Error analyzing: {}", e);
88                }
89            }
90        } else {
91            println!("Skipping: {} (not found)", binary_path);
92        }
93    }
94    
95    if !found_examples {
96        // Create a demonstration with mock data
97        println!("No fat binaries found on system. Showing example output:");
98        println!();
99        
100        demonstrate_fat_binary_output()?;
101    }
102    
103    println!("=== Fat Binary Analysis Notes ===");
104    println!();
105    println!("Current Status:");
106    println!("• ✅ Fat binary detection and architecture counting");
107    println!("• ✅ Per-architecture security analysis"); 
108    println!("• ✅ Architecture-specific security checks (e.g., 'x86_64_pie', 'arm64_canary')");
109    println!("• ✅ Combined security status (Secure/Mixed/Insecure)");
110    println!();
111    println!("Fat binaries now show detailed architecture-specific security analysis,");
112    println!("allowing you to see security features for each architecture independently.");
113    
114    Ok(())
115}