init

Function init 

Source
pub fn init()
Expand description

Initialize the library and print banner

Examples found in repository?
examples/from_file.rs (line 8)
7fn main() {
8    init();
9
10    println!("[*] From File Example\n");
11
12    // Get file path from command line or use default
13    let args: Vec<String> = env::args().collect();
14    let file_path = if args.len() > 1 {
15        &args[1]
16    } else {
17        "payload.exe"
18    };
19
20    println!("[*] Loading payload from: {}", file_path);
21
22    // Use from_file builder method
23    match GhostingBuilder::from_file(file_path) {
24        Ok(builder) => {
25            let result = builder
26                .x64()
27                .with_logging()
28                .execute();
29
30            match result {
31                Ok(_) => println!("\n[+] Success!"),
32                Err(e) => eprintln!("\n[-] Failed: {}", e),
33            }
34        }
35        Err(e) => {
36            eprintln!("[-] Failed to load file: {}", e);
37        }
38    }
39}
More examples
Hide additional examples
examples/basic_usage.rs (line 8)
6fn main() {
7    // Initialize library (prints banner)
8    init();
9
10    println!("[*] Basic Usage Example\n");
11
12    // Read payload from file
13    let payload_path = "payload.exe";
14    
15    match std::fs::read(payload_path) {
16        Ok(payload) => {
17            println!("[+] Loaded payload: {} bytes", payload.len());
18            
19            // Execute ghosting
20            let result = GhostingBuilder::new(&payload)
21                .x64()              // Target x64 architecture
22                .with_logging()     // Enable verbose output
23                .execute();
24
25            match result {
26                Ok(_) => println!("\n[+] Process ghosting completed successfully!"),
27                Err(e) => eprintln!("\n[-] Process ghosting failed: {}", e),
28            }
29        }
30        Err(e) => {
31            eprintln!("[-] Failed to read payload file '{}': {}", payload_path, e);
32            eprintln!("    Please provide a valid PE executable.");
33        }
34    }
35}
examples/from_hex.rs (line 7)
6fn main() {
7    init();
8
9    println!("[*] From Hex String Example\n");
10
11    // Example: Various hex formats supported
12    let hex_formats = [
13        ("Continuous", "4D5A90000300000004000000FFFF0000"),
14        ("Spaced", "4D 5A 90 00 03 00 00 00"),
15        ("C-style", "0x4D, 0x5A, 0x90, 0x00, 0x03, 0x00"),
16        ("Escaped", "\\x4D\\x5A\\x90\\x00\\x03\\x00"),
17    ];
18
19    println!("[*] Supported hex formats:\n");
20    
21    for (name, hex) in &hex_formats {
22        match parse_hex_string(hex) {
23            Ok(bytes) => {
24                println!("    {}: {} -> {:02X?}", name, hex, &bytes[..bytes.len().min(4)]);
25            }
26            Err(e) => {
27                println!("    {}: Error - {}", name, e);
28            }
29        }
30    }
31
32    println!();
33
34    // For actual execution, you would use a complete PE file in hex
35    // This is just a demonstration of the parsing
36    
37    let sample_hex = "0x4D, 0x5A, 0x90, 0x00"; // MZ header start
38    
39    match GhostingBuilder::from_hex_string(sample_hex) {
40        Ok(builder) => {
41            println!("[+] Successfully parsed hex string");
42            println!("[*] Payload size: {} bytes", builder.build().payload.len());
43            
44            // Note: This would fail with just the header
45            // In real usage, provide complete PE bytes
46            // builder.x64().execute();
47        }
48        Err(e) => {
49            eprintln!("[-] Failed to parse: {}", e);
50        }
51    }
52}
examples/architecture_selection.rs (line 8)
7fn main() {
8    init();
9
10    println!("[*] Architecture Selection Example\n");
11
12    let args: Vec<String> = env::args().collect();
13    
14    // Parse arguments
15    let (file_path, arch) = if args.len() >= 3 {
16        let arch = match args[2].to_lowercase().as_str() {
17            "x86" | "32" | "i386" => Architecture::X86,
18            "x64" | "64" | "amd64" => Architecture::X64,
19            _ => {
20                eprintln!("[-] Unknown architecture: {}", args[2]);
21                eprintln!("    Use: x86, x64, 32, or 64");
22                return;
23            }
24        };
25        (args[1].as_str(), arch)
26    } else if args.len() == 2 {
27        (args[1].as_str(), Architecture::X64) // Default to x64
28    } else {
29        println!("Usage: {} <payload.exe> [arch]", args[0]);
30        println!();
31        println!("Architectures:");
32        println!("  x86, 32, i386   - 32-bit");
33        println!("  x64, 64, amd64  - 64-bit (default)");
34        return;
35    };
36
37    println!("[*] File: {}", file_path);
38    println!("[*] Architecture: {}", arch);
39    println!();
40
41    match GhostingBuilder::from_file(file_path) {
42        Ok(builder) => {
43            let result = builder
44                .architecture(arch)
45                .with_logging()
46                .execute();
47
48            match result {
49                Ok(_) => println!("\n[+] Success!"),
50                Err(e) => eprintln!("\n[-] Failed: {}", e),
51            }
52        }
53        Err(e) => eprintln!("[-] Error: {}", e),
54    }
55}
examples/error_handling.rs (line 7)
6fn main() {
7    init();
8
9    println!("[*] Error Handling Example\n");
10
11    // Test 1: Invalid file
12    println!("[Test 1] Loading non-existent file:");
13    match GhostingBuilder::from_file("nonexistent.exe") {
14        Ok(_) => println!("    Unexpected success"),
15        Err(e) => println!("    Expected error: {}", e),
16    }
17    println!();
18
19    // Test 2: Invalid hex string
20    println!("[Test 2] Parsing invalid hex:");
21    match parse_hex_string("ZZZZ") {
22        Ok(_) => println!("    Unexpected success"),
23        Err(e) => println!("    Expected error: {}", e),
24    }
25    println!();
26
27    // Test 3: Invalid PE (not MZ header)
28    println!("[Test 3] Executing invalid PE:");
29    let invalid_pe = vec![0x00, 0x00, 0x00, 0x00]; // Not MZ
30    match GhostingBuilder::new(&invalid_pe).x64().execute() {
31        Ok(_) => println!("    Unexpected success"),
32        Err(e) => println!("    Expected error: {}", e),
33    }
34    println!();
35
36    // Test 4: Empty payload
37    println!("[Test 4] Empty payload:");
38    match GhostingBuilder::new(&[]).x64().execute() {
39        Ok(_) => println!("    Unexpected success"),
40        Err(e) => println!("    Expected error: {}", e),
41    }
42    println!();
43
44    // Test 5: Truncated PE
45    println!("[Test 5] Truncated PE (only MZ header):");
46    let truncated_pe = vec![0x4D, 0x5A]; // Just MZ
47    match GhostingBuilder::new(&truncated_pe).x64().execute() {
48        Ok(_) => println!("    Unexpected success"),
49        Err(e) => println!("    Expected error: {}", e),
50    }
51    println!();
52
53    println!("[*] Error handling tests complete.");
54}
examples/hex_converter.rs (line 26)
25fn main() {
26    init();
27
28    let args: Vec<String> = env::args().collect();
29
30    if args.len() < 3 {
31        print_usage(&args[0]);
32        return;
33    }
34
35    let command = &args[1];
36    let file_path = &args[2];
37
38    // Check if file exists
39    if !std::path::Path::new(file_path).exists() {
40        eprintln!("[-] File not found: {}", file_path);
41        return;
42    }
43
44    match command.as_str() {
45        "print" => {
46            println!("[*] Converting {} to Rust byte array:\n", file_path);
47            if let Err(e) = print_exe_hex(file_path) {
48                eprintln!("[-] Error: {}", e);
49            }
50        }
51
52        "string" => {
53            println!("[*] Converting {} to hex string:\n", file_path);
54            match exe_to_hex_string(file_path) {
55                Ok(hex) => println!("{}", hex),
56                Err(e) => eprintln!("[-] Error: {}", e),
57            }
58        }
59
60        "array" => {
61            println!("[*] Converting {} to formatted array:\n", file_path);
62            match exe_to_hex_array(file_path) {
63                Ok(hex) => println!("{}", hex),
64                Err(e) => eprintln!("[-] Error: {}", e),
65            }
66        }
67
68        "save" => {
69            let output_path = format!("{}.hex.txt", file_path);
70            println!("[*] Converting {} and saving to {}", file_path, output_path);
71
72            match fs::read(file_path) {
73                Ok(bytes) => {
74                    let hex = bytes_to_hex_string(&bytes);
75                    let content = format!(
76                        "// File: {}\n// Size: {} bytes\n\nconst PAYLOAD: &[u8] = &[\n    {}\n];\n",
77                        file_path,
78                        bytes.len(),
79                        hex
80                    );
81
82                    match fs::write(&output_path, content) {
83                        Ok(_) => println!("[+] Saved to {}", output_path),
84                        Err(e) => eprintln!("[-] Failed to save: {}", e),
85                    }
86                }
87                Err(e) => eprintln!("[-] Failed to read file: {}", e),
88            }
89        }
90
91        _ => {
92            eprintln!("[-] Unknown command: {}", command);
93            println!();
94            print_usage(&args[0]);
95        }
96    }
97}