|
| 1 | +/* |
| 2 | + Payload Shuffling Technique .. |
| 3 | +
|
| 4 | + Original Credit Goes to Cocomelonc |
| 5 | + => https://cocomelonc.github.io/malware/2024/09/30/malware-trick-43.html#byte-shuffling-technique |
| 6 | + By @5mukx |
| 7 | +*/ |
| 8 | + |
| 9 | + |
| 10 | +use std::fs::File; |
| 11 | +use std::io::{self, Read, Write}; |
| 12 | +use rand::{rngs::StdRng, SeedableRng, Rng}; |
| 13 | + |
| 14 | +fn shuffle_file_and_save(input_file: &str, output_file: &str, seed: u64) -> io::Result<()> { |
| 15 | + shuffle_bytes(input_file, output_file, seed) |
| 16 | +} |
| 17 | + |
| 18 | +fn main() -> io::Result<()> { |
| 19 | + let input_file = "w64-exec-calc-shellcode.bin"; |
| 20 | + let shuffled_file = "shuffled.bin"; |
| 21 | + let deshuffled_file = "deshuffled.bin"; |
| 22 | + let seed = 12345; |
| 23 | + |
| 24 | + // Lets shuffle the data and save it in seperate file ! |
| 25 | + shuffle_file_and_save(input_file, shuffled_file, seed)?; |
| 26 | + |
| 27 | + println!("[+] shuffled and stored at [shuffled.bin]"); |
| 28 | + // Read the shuffled data |
| 29 | + let mut shuffled_data = Vec::new(); |
| 30 | + let mut shuffled_fp = File::open(shuffled_file)?; |
| 31 | + shuffled_fp.read_to_end(&mut shuffled_data)?; |
| 32 | + |
| 33 | + // At last. Deshuffle the data. |
| 34 | + deshuffle_bytes(&shuffled_data, shuffled_data.len(), deshuffled_file, seed)?; |
| 35 | + println!("[+] Desuffled and stored at [deshuffled.bin]"); |
| 36 | + Ok(()) |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +fn shuffle_bytes(input_file: &str, output_file: &str, seed: u64) -> io::Result<()> { |
| 41 | + // open input file for reading |
| 42 | + let mut in_file = File::open(input_file)?; |
| 43 | + let size = in_file.metadata()?.len() as usize; |
| 44 | + |
| 45 | + // read data from the input file |
| 46 | + let mut data = vec![0u8; size]; |
| 47 | + in_file.read_exact(&mut data)?; |
| 48 | + |
| 49 | + // create a list of indices |
| 50 | + let mut indices: Vec<usize> = (0..size).collect(); |
| 51 | + |
| 52 | + // shuffle the indices |
| 53 | + let mut rng = StdRng::seed_from_u64(seed); |
| 54 | + for i in (1..size).rev() { |
| 55 | + let j = rng.gen_range(0..=i); |
| 56 | + indices.swap(i, j); |
| 57 | + } |
| 58 | + |
| 59 | + // rearrange data based on shuffled indices |
| 60 | + let mut shuffled_data = vec![0u8; size]; |
| 61 | + for (i, &idx) in indices.iter().enumerate(){ |
| 62 | + shuffled_data[i] = data[idx]; |
| 63 | + } |
| 64 | + |
| 65 | + // Write the shuffled data to the output file |
| 66 | + let mut out_file = File::create(output_file)?; |
| 67 | + out_file.write_all(&shuffled_data)?; |
| 68 | + |
| 69 | + Ok(()) |
| 70 | +} |
| 71 | + |
| 72 | +fn deshuffle_bytes(data: &[u8], size: usize, output_file: &str, seed: u64) -> io::Result<()> { |
| 73 | + // Create a list of indices |
| 74 | + let mut indices: Vec<usize> = (0..size).collect(); |
| 75 | + |
| 76 | + // Shuffle the indices to get the same order |
| 77 | + let mut rng = StdRng::seed_from_u64(seed); |
| 78 | + for i in (1..size).rev() { |
| 79 | + let j = rng.gen_range(0..=i); |
| 80 | + indices.swap(i, j); |
| 81 | + } |
| 82 | + |
| 83 | + // rearrang the data according to shuffled indices |
| 84 | + let mut deshuffled_data = vec![0u8; size]; |
| 85 | + for (i, &idx) in indices.iter().enumerate() { |
| 86 | + deshuffled_data[idx] = data[i]; |
| 87 | + } |
| 88 | + |
| 89 | + // write the deshuffled data to the output file |
| 90 | + let mut out_file = File::create(output_file)?; |
| 91 | + out_file.write_all(&deshuffled_data)?; |
| 92 | + |
| 93 | + Ok(()) |
| 94 | +} |
| 95 | + |
| 96 | + |
0 commit comments