Skip to content

Commit 3a1a18f

Browse files
committed
Create hex_converter.rs
1 parent 6fbcefb commit 3a1a18f

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
//! Convert EXE files to hex format for embedding
2+
//! Author: BlackTechX
3+
4+
use process_ghosting::{
5+
init, print_exe_hex, exe_to_hex_string, exe_to_hex_array,
6+
bytes_to_hex_string,
7+
};
8+
use std::env;
9+
use std::fs;
10+
11+
fn print_usage(program: &str) {
12+
println!("Usage: {} <command> <file>", program);
13+
println!();
14+
println!("Commands:");
15+
println!(" print Print hex array to console (for embedding in code)");
16+
println!(" string Output as single line hex string");
17+
println!(" array Output as formatted array with line breaks");
18+
println!(" save Save hex to .txt file");
19+
println!();
20+
println!("Examples:");
21+
println!(" {} print payload.exe", program);
22+
println!(" {} save payload.exe", program);
23+
}
24+
25+
fn 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+
}

0 commit comments

Comments
 (0)