Skip to content

Commit b4fd341

Browse files
committed
Add shellcode for RC4 decoding
Provided as a block to be included into stagers and/or decoder stubs. Also included is a test shellcode that can be used for verifying that the algorithm is compatible to Ruby's OpenSSL RC4 algorithm.
1 parent f7543e1 commit b4fd341

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
;-----------------------------------------------------------------------------;
2+
; Author: Michael Schierl (schierlm[at]gmx[dot]de)
3+
; Version: 1.0 (29 December 2012)
4+
;-----------------------------------------------------------------------------;
5+
[BITS 32]
6+
7+
; Input: EBP - Data to decode
8+
; ECX - Data length
9+
; ESI - Key (16 bytes for simplicity)
10+
; EDI - pointer to 0x100 bytes scratch space for S-box
11+
; Direction flag has to be cleared
12+
; Output: None. Data is decoded in place.
13+
; Clobbers: EAX, EBX, ECX, EDX, ESI, EBP (stack is not used)
14+
15+
; Initialize S-box
16+
xor eax, eax ; Start with 0
17+
init:
18+
stosb ; Store next S-Box byte S[i] = i
19+
inc al ; increase byte to write (EDI is increased automatically)
20+
jnz init ; loop until we wrap around
21+
sub edi, 0x100 ; restore EDI
22+
23+
; permute S-box according to key
24+
xor ebx, ebx ; Clear EBX (EAX is already cleared)
25+
permute:
26+
add bl, [edi+eax] ; BL += S[AL] + KEY[AL % 16]
27+
mov edx, eax
28+
and dl, 0xF
29+
add bl, [esi+edx]
30+
mov dl, [edi+eax] ; swap S[AL] and S[BL]
31+
xchg dl, [edi+ebx]
32+
mov [edi+eax], dl
33+
inc al ; AL += 1 until we wrap around
34+
jnz permute
35+
36+
37+
; decryption loop
38+
xor ebx, ebx ; Clear EBX and EDX (EAX is already cleared)
39+
xor edx, edx
40+
decrypt:
41+
inc al ; AL += 1
42+
add bl, [edi+eax] ; BL += S[AL]
43+
mov dl, [edi+eax] ; swap S[AL] and S[BL]
44+
xchg dl, [edi+ebx]
45+
mov [edi+eax], dl
46+
add dl, [edi+ebx] ; DL = S[AL]+S[BL]
47+
mov dl, [edi+edx] ; DL = S[DL]
48+
xor [ebp], dl ; [EBP] ^= DL
49+
inc ebp ; advance data pointer
50+
dec ecx ; reduce counter
51+
jnz decrypt ; until finished
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
;-----------------------------------------------------------------------------;
2+
; Author: Michael Schierl (schierlm[at]gmx[dot]de)
3+
; Version: 1.0 (29 December 2012)
4+
;-----------------------------------------------------------------------------;
5+
6+
;
7+
; c1 = OpenSSL::Cipher::Cipher.new('RC4')
8+
; c1.encrypt
9+
; c1.key="Hello, my world!"
10+
; c1.update("This is some magic data you may want to have encoded and decoded again").unpack("H*")
11+
;
12+
; => "882353c5de0f5e6b10bf0d25c432c5d16424dc797e895f37f261c893b31d577e7e69f77e07aa576d58c7f757164e7d74988feb10f972b28dcfa1e3a2b1cc0b0fa1a8b116294b"
13+
;
14+
; c1 = OpenSSL::Cipher::Cipher.new('RC4')
15+
; c1.decrypt
16+
; c1.key="Hello, my world!"
17+
; c1.update(["882353c5de0f5e6b10bf0d25c432c5d16424dc797e895f37f261c893b31d577e7e69f77e07aa576d58c7f757164e7d74988feb10f972b28dcfa1e3a2b1cc0b0fa1a8b116294b"].pack("H*"))
18+
;
19+
; => "This is some magic data you may want to have encoded and decoded again"
20+
;
21+
22+
[BITS 32]
23+
[ORG 0]
24+
25+
cld ; Clear the direction flag.
26+
call pushkey ; push the address of the key onto the stack
27+
db "Hello, my world!"
28+
pushkey:
29+
pop esi ; and store it into ESI
30+
call pushdata ; push the address of the encrypted data on the stack
31+
db 0x88, 0x23, 0x53, 0xc5, 0xde, 0x0f, 0x5e, 0x6b, 0x10, 0xbf, 0x0d, 0x25, 0xc4, 0x32, 0xc5, 0xd1, 0x64, 0x24, 0xdc, 0x79, 0x7e, 0x89, 0x5f, 0x37, 0xf2, 0x61, 0xc8, 0x93, 0xb3, 0x1d, 0x57, 0x7e, 0x7e, 0x69, 0xf7, 0x7e, 0x07, 0xaa, 0x57, 0x6d, 0x58, 0xc7, 0xf7, 0x57, 0x16, 0x4e, 0x7d, 0x74, 0x98, 0x8f, 0xeb, 0x10, 0xf9, 0x72, 0xb2, 0x8d, 0xcf, 0xa1, 0xe3, 0xa2, 0xb1, 0xcc, 0x0b, 0x0f, 0xa1, 0xa8, 0xb1, 0x16, 0x29, 0x4b
32+
pushdata:
33+
pop ebp ; and store it into EBP
34+
mov ecx, 70 ; store data length into ECX
35+
sub esp, 0x100 ; make space on stack for S-Box
36+
mov edi, esp ; and store address into EDI
37+
nop
38+
nop
39+
nop
40+
int 3 ; for stepping through the code
41+
; let's run the RC4 decoder
42+
%include "./src/block/block_rc4.asm"
43+
int 3 ; EBP should point to decoded data now

0 commit comments

Comments
 (0)