bytes_to_hex_string

Function bytes_to_hex_string 

Source
pub fn bytes_to_hex_string(bytes: &[u8]) -> String
Expand description

Convert bytes to hex string format: “0x4D, 0x5A, 0x90, …”

Examples found in repository?
examples/hex_converter.rs (line 74)
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}
More examples
Hide additional examples
examples/full_demo.rs (line 54)
12fn main() {
13    // ========================================
14    // INITIALIZATION
15    // ========================================
16    init();
17    
18    println!("╔════════════════════════════════════════════╗");
19    println!("║     ProcessGhosting Full Demo              ║");
20    println!("║     By BlackTechX                          ║");
21    println!("╚════════════════════════════════════════════╝\n");
22
23    let args: Vec<String> = env::args().collect();
24    let test_file = if args.len() > 1 { &args[1] } else { "C:\\Windows\\System32\\notepad.exe" };
25
26    // ========================================
27    // SECTION 1: HEX UTILITIES
28    // ========================================
29    println!("┌────────────────────────────────────────────┐");
30    println!("│ Section 1: Hex Utilities                   │");
31    println!("└────────────────────────────────────────────┘\n");
32
33    // 1.1 Parse different hex formats
34    println!("[1.1] Parsing different hex formats:\n");
35    
36    let formats = [
37        "4D5A9000",
38        "4D 5A 90 00",
39        "0x4D, 0x5A, 0x90, 0x00",
40        "\\x4D\\x5A\\x90\\x00",
41    ];
42
43    for fmt in &formats {
44        match parse_hex_string(fmt) {
45            Ok(bytes) => println!("      '{}' -> {:02X?}", fmt, bytes),
46            Err(e) => println!("      '{}' -> Error: {}", fmt, e),
47        }
48    }
49    println!();
50
51    // 1.2 Convert bytes to hex
52    println!("[1.2] Converting bytes to hex string:");
53    let sample_bytes = vec![0x4D, 0x5A, 0x90, 0x00, 0x03, 0x00];
54    let hex_string = bytes_to_hex_string(&sample_bytes);
55    println!("      {:?} -> {}\n", sample_bytes, hex_string);
56
57    // 1.3 Read file as hex (first 64 bytes)
58    println!("[1.3] Reading file header as hex:");
59    match read_exe_bytes(test_file) {
60        Ok(bytes) => {
61            let preview: Vec<u8> = bytes.iter().take(32).cloned().collect();
62            println!("      File: {}", test_file);
63            println!("      Size: {} bytes", bytes.len());
64            println!("      First 32 bytes: {}", bytes_to_hex_string(&preview));
65        }
66        Err(e) => println!("      Error: {}", e),
67    }
68    println!();
69
70    // ========================================
71    // SECTION 2: BUILDER PATTERNS
72    // ========================================
73    println!("┌────────────────────────────────────────────┐");
74    println!("│ Section 2: Builder Patterns                │");
75    println!("└────────────────────────────────────────────┘\n");
76
77    // 2.1 From raw bytes
78    println!("[2.1] Builder from raw bytes:");
79    let sample = vec![0x4D, 0x5A];
80    let config = GhostingBuilder::new(&sample)
81        .x64()
82        .with_logging()
83        .build();
84    println!("      Payload size: {} bytes", config.payload.len());
85    println!("      Architecture: {}", config.architecture);
86    println!("      Verbose: {}\n", config.verbose);
87
88    // 2.2 From file
89    println!("[2.2] Builder from file:");
90    match GhostingBuilder::from_file(test_file) {
91        Ok(builder) => {
92            let config = builder.x64().build();
93            println!("      Loaded: {} bytes\n", config.payload.len());
94        }
95        Err(e) => println!("      Error: {}\n", e),
96    }
97
98    // 2.3 From hex string
99    println!("[2.3] Builder from hex string:");
100    match GhostingBuilder::from_hex_string("0x4D, 0x5A, 0x90, 0x00") {
101        Ok(builder) => {
102            let config = builder.build();
103            println!("      Parsed: {} bytes\n", config.payload.len());
104        }
105        Err(e) => println!("      Error: {}\n", e),
106    }
107
108    // 2.4 Architecture selection
109    println!("[2.4] Architecture selection:");
110    
111    let x64_config = GhostingBuilder::new(&[0x4D, 0x5A])
112        .x64()
113        .build();
114    println!("      .x64() -> {}", x64_config.architecture);
115
116    let x86_config = GhostingBuilder::new(&[0x4D, 0x5A])
117        .x86()
118        .build();
119    println!("      .x86() -> {}", x86_config.architecture);
120
121    let arch_config = GhostingBuilder::new(&[0x4D, 0x5A])
122        .architecture(Architecture::X64)
123        .build();
124    println!("      .architecture(X64) -> {}\n", arch_config.architecture);
125
126    // 2.5 Verbose control
127    println!("[2.5] Verbose control:");
128    
129    let verbose = GhostingBuilder::new(&[0x4D, 0x5A])
130        .with_logging()
131        .build();
132    println!("      .with_logging() -> verbose: {}", verbose.verbose);
133
134    let silent = GhostingBuilder::new(&[0x4D, 0x5A])
135        .silent()
136        .build();
137    println!("      .silent() -> verbose: {}\n", silent.verbose);
138
139    // ========================================
140    // SECTION 3: QUICK FUNCTIONS
141    // ========================================
142    println!("┌────────────────────────────────────────────┐");
143    println!("│ Section 3: Quick Functions                 │");
144    println!("└────────────────────────────────────────────┘\n");
145
146    println!("[3.1] Available quick functions:");
147    println!("      ghost_payload(&bytes)      - Execute from bytes");
148    println!("      ghost_payload_file(path)   - Execute from file");
149    println!("      ghost_payload_hex(hex_str) - Execute from hex string\n");
150
151    // ========================================
152    // SECTION 4: EXECUTION (DISABLED BY DEFAULT)
153    // ========================================
154    println!("┌────────────────────────────────────────────┐");
155    println!("│ Section 4: Execution Demo                  │");
156    println!("└────────────────────────────────────────────┘\n");
157
158    println!("[4.1] To execute process ghosting:");
159    println!();
160    println!("      // Method 1: Builder pattern");
161    println!("      GhostingBuilder::from_file(\"payload.exe\")?");
162    println!("          .x64()");
163    println!("          .with_logging()");
164    println!("          .execute()?;");
165    println!();
166    println!("      // Method 2: Quick function");
167    println!("      ghost_payload_file(\"payload.exe\")?;");
168    println!();
169    println!("      // Method 3: From embedded bytes");
170    println!("      const PAYLOAD: &[u8] = include_bytes!(\"payload.exe\");");
171    println!("      ghost_payload(PAYLOAD)?;");
172    println!();
173
174    // Uncomment below to actually execute
175    /*
176    println!("[4.2] Executing ghosting...\n");
177    
178    match GhostingBuilder::from_file("your_payload.exe") {
179        Ok(builder) => {
180            match builder.x64().with_logging().execute() {
181                Ok(_) => println!("\n[+] Ghosting successful!"),
182                Err(e) => println!("\n[-] Ghosting failed: {}", e),
183            }
184        }
185        Err(e) => println!("[-] Failed to load payload: {}", e),
186    }
187    */
188
189    println!("┌────────────────────────────────────────────┐");
190    println!("│ Demo Complete                              │");
191    println!("└────────────────────────────────────────────┘\n");
192}