Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/auxiliary/admin/mssql/mssql_enum.rb
Views: 11784
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Auxiliary6include Msf::Exploit::Remote::MSSQL7include Msf::Auxiliary::Report8include Msf::OptionalSession::MSSQL910def initialize(info = {})11super(update_info(info,12'Name' => 'Microsoft SQL Server Configuration Enumerator',13'Description' => %q{14This module will perform a series of configuration audits and15security checks against a Microsoft SQL Server database. For this16module to work, valid administrative user credentials must be17supplied.18},19'Author' => [ 'Carlos Perez <carlos_perez[at]darkoperator.com>' ],20'License' => MSF_LICENSE21))22end2324def run25print_status("Running MS SQL Server Enumeration...")26if session27set_mssql_session(session.client)28else29unless mssql_login_datastore30print_error("Login was unsuccessful. Check your credentials.")31disconnect32return33end34end3536# Get Version37print_status("Version:")38vernum =""39ver = mssql_query("select @@version")40sqlversion = ver[:rows].join41sqlversion.each_line do |row|42print "[*]\t#{row}"43end44vernum = sqlversion.gsub("\n"," ").scan(/SQL Server\s*(200\d)/m)45report_note(:host => mssql_client.peerhost,46:proto => 'TCP',47:port => mssql_client.peerport,48:type => 'MSSQL_ENUM',49:data => "Version: #{sqlversion}")5051#---------------------------------------------------------52# Check Configuration Parameters and check what is enabled53print_status("Configuration Parameters:")54if vernum.join != "2000"55query = "SELECT name, CAST(value_in_use AS INT) from sys.configurations"56ver = mssql_query(query)[:rows]57sysconfig = {}58ver.each do |l|59sysconfig[l[0].strip] = l[1].to_i60end61else62# enable advanced options63mssql_query("EXEC sp_configure \'show advanced options\', 1; RECONFIGURE")[:rows]64query = "EXECUTE sp_configure"65ver = mssql_query(query)[:rows]66ver.class67sysconfig = {}68ver.each do |l|69sysconfig[l[0].strip] = l[3].to_i70end71end7273#-------------------------------------------------------74# checking for C2 Audit Mode75if sysconfig['c2 audit mode'] == 176print_status("\tC2 Audit Mode is Enabled")77report_note(:host => mssql_client.peerhost,78:proto => 'TCP',79:port => mssql_client.peerport,80:type => 'MSSQL_ENUM',81:data => "C2 Audit Mode is Enabled")82else83print_status("\tC2 Audit Mode is Not Enabled")84report_note(:host => mssql_client.peerhost,85:proto => 'TCP',86:port => mssql_client.peerport,87:type => 'MSSQL_ENUM',88:data => "C2 Audit Mode is Not Enabled")89end9091#-------------------------------------------------------92# check if xp_cmdshell is enabled93if vernum.join != "2000"94if sysconfig['xp_cmdshell'] == 195print_status("\txp_cmdshell is Enabled")96report_note(:host => mssql_client.peerhost,97:proto => 'TCP',98:port => mssql_client.peerport,99:type => 'MSSQL_ENUM',100:data => "xp_cmdshell is Enabled")101else102print_status("\txp_cmdshell is Not Enabled")103report_note(:host => mssql_client.peerhost,104:proto => 'TCP',105:port => mssql_client.peerport,106:type => 'MSSQL_ENUM',107:data => "xp_cmdshell is Not Enabled")108end109else110xpspexist = mssql_query("select sysobjects.name from sysobjects where name = \'xp_cmdshell\'")[:rows]111if xpspexist != nil112print_status("\txp_cmdshell is Enabled")113report_note(:host => mssql_client.peerhost,114:proto => 'TCP',115:port => mssql_client.peerport,116:type => 'MSSQL_ENUM',117:data => "xp_cmdshell is Enabled")118else119print_status("\txp_cmdshell is Not Enabled")120report_note(:host => mssql_client.peerhost,121:proto => 'TCP',122:port => mssql_client.peerport,123:type => 'MSSQL_ENUM',124:data => "xp_cmdshell is Not Enabled")125end126end127128#-------------------------------------------------------129# check if remote access is enabled130if sysconfig['remote access'] == 1131print_status("\tremote access is Enabled")132report_note(:host => mssql_client.peerhost,133:proto => 'TCP',134:port => mssql_client.peerport,135:type => 'MSSQL_ENUM',136:data => "remote access is Enabled")137else138print_status("\tremote access is Not Enabled")139report_note(:host => mssql_client.peerhost,140:proto => 'TCP',141:port => mssql_client.peerport,142:type => 'MSSQL_ENUM',143:data => "remote access is not Enabled")144end145146#-------------------------------------------------------147#check if updates are allowed148if sysconfig['allow updates'] == 1149print_status("\tallow updates is Enabled")150report_note(:host => mssql_client.peerhost,151:proto => 'TCP',152:port => mssql_client.peerport,153:type => 'MSSQL_ENUM',154:data => "allow updates is Enabled")155else156print_status("\tallow updates is Not Enabled")157report_note(:host => mssql_client.peerhost,158:proto => 'TCP',159:port => mssql_client.peerport,160:type => 'MSSQL_ENUM',161:data => "allow updates is not Enabled")162end163164#-------------------------------------------------------165# check if Mail stored procedures are enabled166if vernum.join != "2000"167if sysconfig['Database Mail XPs'] == 1168print_status("\tDatabase Mail XPs is Enabled")169report_note(:host => mssql_client.peerhost,170:proto => 'TCP',171:port => mssql_client.peerport,172:type => 'MSSQL_ENUM',173:data => "Database Mail XPs is Enabled")174else175print_status("\tDatabase Mail XPs is Not Enabled")176report_note(:host => mssql_client.peerhost,177:proto => 'TCP',178:port => mssql_client.peerport,179:type => 'MSSQL_ENUM',180:data => "Database Mail XPs is not Enabled")181end182else183mailexist = mssql_query("select sysobjects.name from sysobjects where name like \'%mail%\'")[:rows]184if mailexist != nil185print_status("\tDatabase Mail XPs is Enabled")186report_note(:host => mssql_client.peerhost,187:proto => 'TCP',188:port => mssql_client.peerport,189:type => 'MSSQL_ENUM',190:data => "Database Mail XPs is Enabled")191else192print_status("\tDatabase Mail XPs is Not Enabled")193report_note(:host => mssql_client.peerhost,194:proto => 'TCP',195:port => mssql_client.peerport,196:type => 'MSSQL_ENUM',197:data => "Database Mail XPs is not Enabled")198end199end200201#-------------------------------------------------------202# check if OLE stored procedures are enabled203if vernum.join != "2000"204if sysconfig['Ole Automation Procedures'] == 1205print_status("\tOle Automation Procedures are Enabled")206report_note(:host => mssql_client.peerhost,207:proto => 'TCP',208:port => mssql_client.peerport,209:type => 'MSSQL_ENUM',210:data => "Ole Automation Procedures are Enabled")211else212print_status("\tOle Automation Procedures are Not Enabled")213report_note(:host => mssql_client.peerhost,214:proto => 'TCP',215:port => mssql_client.peerport,216:type => 'MSSQL_ENUM',217:data => "Ole Automation Procedures are not Enabled")218end219else220oleexist = mssql_query("select sysobjects.name from sysobjects where name like \'%sp_OA%\'")[:rows]221if oleexist != nil222print_status("\tOle Automation Procedures is Enabled")223report_note(:host => mssql_client.peerhost,224:proto => 'TCP',225:port => mssql_client.peerport,226:type => 'MSSQL_ENUM',227:data => "Ole Automation Procedures are Enabled")228else229print_status("\tOle Automation Procedures are Not Enabled")230report_note(:host => mssql_client.peerhost,231:proto => 'TCP',232:port => mssql_client.peerport,233:type => 'MSSQL_ENUM',234:data => "Ole Automation Procedures are not Enabled")235end236end237238#-------------------------------------------------------239# Get list of Databases on System240print_status("Databases on the server:")241dbs = mssql_query("select name from master..sysdatabases")[:rows].flatten242if dbs != nil243dbs.each do |dbn|244print_status("\tDatabase name:#{dbn.strip}")245print_status("\tDatabase Files for #{dbn.strip}:")246if vernum.join != "2000"247db_ind_files = mssql_query("select filename from #{dbn.strip}.sys.sysfiles")[:rows]248if db_ind_files != nil249db_ind_files.each do |fn|250print_status("\t\t#{fn.join}")251report_note(:host => mssql_client.peerhost,252:proto => 'TCP',253:port => mssql_client.peerport,254:type => 'MSSQL_ENUM',255:data => "Database: #{dbn.strip} File: #{fn.join}")256end257end258else259db_ind_files = mssql_query("select filename from #{dbn.strip}..sysfiles")[:rows]260if db_ind_files != nil261db_ind_files.each do |fn|262print_status("\t\t#{fn.join.strip}")263report_note(:host => mssql_client.peerhost,264:proto => 'TCP',265:port => mssql_client.peerport,266:type => 'MSSQL_ENUM',267:data => "Database: #{dbn.strip} File: #{fn.join}")268end269end270end271end272end273274#-------------------------------------------------------275# Get list of syslogins on System276print_status("System Logins on this Server:")277if vernum.join != "2000"278syslogins = mssql_query("select loginname from master.sys.syslogins")[:rows]279else280syslogins = mssql_query("select loginname from master..syslogins")[:rows]281end282if syslogins != nil283syslogins.each do |acc|284print_status("\t#{acc.join}")285report_note(:host => mssql_client.peerhost,286:proto => 'TCP',287:port => mssql_client.peerport,288:type => 'MSSQL_ENUM',289:data => "Database: Master User: #{acc.join}")290end291else292print_error("\tCould not enumerate System Logins!")293report_note(:host => mssql_client.peerhost,294:proto => 'TCP',295:port => mssql_client.peerport,296:type => 'MSSQL_ENUM',297:data => "Could not enumerate System Logins")298end299300#-------------------------------------------------------301# Get list of disabled accounts on System302if vernum.join != "2000"303print_status("Disabled Accounts:")304disabledsyslogins = mssql_query("select name from master.sys.server_principals where is_disabled = 1")[:rows]305if disabledsyslogins != nil306disabledsyslogins.each do |acc|307print_status("\t#{acc.join}")308report_note(:host => mssql_client.peerhost,309:proto => 'TCP',310:port => mssql_client.peerport,311:type => 'MSSQL_ENUM',312:data => "Disabled User: #{acc.join}")313end314else315print_status("\tNo Disabled Logins Found")316report_note(:host => mssql_client.peerhost,317:proto => 'TCP',318:port => mssql_client.peerport,319:type => 'MSSQL_ENUM',320:data => "No Disabled Logins Found")321end322end323324#-------------------------------------------------------325# Get list of accounts for which password policy does not apply on System326if vernum.join != "2000"327print_status("No Accounts Policy is set for:")328nopolicysyslogins = mssql_query("select name from master.sys.sql_logins where is_policy_checked = 0")[:rows]329if nopolicysyslogins != nil330nopolicysyslogins.each do |acc|331print_status("\t#{acc.join}")332report_note(:host => mssql_client.peerhost,333:proto => 'TCP',334:port => mssql_client.peerport,335:type => 'MSSQL_ENUM',336:data => "None Policy Checked User: #{acc.join}")337end338else339print_status("\tAll System Accounts have the Windows Account Policy Applied to them.")340report_note(:host => mssql_client.peerhost,341:proto => 'TCP',342:port => mssql_client.peerport,343:type => 'MSSQL_ENUM',344:data => "All System Accounts have the Windows Account Policy Applied to them")345end346end347348#-------------------------------------------------------349# Get list of accounts for which password expiration is not checked350if vernum.join != "2000"351print_status("Password Expiration is not checked for:")352passexsyslogins = mssql_query("select name from master.sys.sql_logins where is_expiration_checked = 0")[:rows]353if passexsyslogins != nil354passexsyslogins.each do |acc|355print_status("\t#{acc.join}")356report_note(:host => mssql_client.peerhost,357:proto => 'TCP',358:port => mssql_client.peerport,359:type => 'MSSQL_ENUM',360:data => "None Password Expiration User: #{acc.join}")361end362else363print_status("\tAll System Accounts are checked for Password Expiration.")364report_note(:host => mssql_client.peerhost,365:proto => 'TCP',366:port => mssql_client.peerport,367:type => 'MSSQL_ENUM',368:data => "All System Accounts are checked for Password Expiration")369end370end371372#-------------------------------------------------------373# Get list of sysadmin logins on System374print_status("System Admin Logins on this Server:")375if vernum.join != "2000"376sysadmins = mssql_query("select name from master.sys.syslogins where sysadmin = 1")[:rows]377else378sysadmins = mssql_query("select name from master..syslogins where sysadmin = 1")[:rows]379end380if sysadmins != nil381sysadmins.each do |acc|382print_status("\t#{acc.join}")383report_note(:host => mssql_client.peerhost,384:proto => 'TCP',385:port => mssql_client.peerport,386:type => 'MSSQL_ENUM',387:data => "Sysdba: #{acc.join}")388end389else390print_error("\tCould not enumerate sysadmin accounts!")391report_note(:host => mssql_client.peerhost,392:proto => 'TCP',393:port => mssql_client.peerport,394:type => 'MSSQL_ENUM',395:data => "Could not enumerate sysadmin accounts")396end397398#-------------------------------------------------------399# Get list of Windows logins on System400print_status("Windows Logins on this Server:")401if vernum.join != "2000"402winusers = mssql_query("select name from master.sys.syslogins where isntuser = 1")[:rows]403else404winusers = mssql_query("select name from master..syslogins where isntuser = 1")[:rows]405end406407if winusers != nil408winusers.each do |acc|409print_status("\t#{acc.join}")410report_note(:host => mssql_client.peerhost,411:proto => 'TCP',412:port => mssql_client.peerport,413:type => 'MSSQL_ENUM',414:data => "Windows Logins: #{acc.join}")415end416else417print_status("\tNo Windows logins found!")418report_note(:host => mssql_client.peerhost,419:proto => 'TCP',420:port => mssql_client.peerport,421:type => 'MSSQL_ENUM',422:data => "No Windows logins found")423end424425#-------------------------------------------------------426# Get list of windows groups that can logins on the System427print_status("Windows Groups that can logins on this Server:")428if vernum.join != "2000"429wingroups = mssql_query("select name from master.sys.syslogins where isntgroup = 1")[:rows]430else431wingroups = mssql_query("select name from master..syslogins where isntgroup = 1")[:rows]432end433434if wingroups != nil435wingroups.each do |acc|436print_status("\t#{acc.join}")437report_note(:host => mssql_client.peerhost,438:proto => 'TCP',439:port => mssql_client.peerport,440:type => 'MSSQL_ENUM',441:data => "Windows Groups: #{acc.join}")442end443else444print_status("\tNo Windows Groups where found with permission to login to system.")445report_note(:host => mssql_client.peerhost,446:proto => 'TCP',447:port => mssql_client.peerport,448:type => 'MSSQL_ENUM',449:data => "No Windows Groups where found with permission to login to system")450451end452453#-------------------------------------------------------454# Check for local accounts with same username as password455sameasuser = []456if vernum.join != "2000"457sameasuser = mssql_query("SELECT name FROM sys.sql_logins WHERE PWDCOMPARE\(name, password_hash\) = 1")[:rows]458else459sameasuser = mssql_query("SELECT name FROM master.dbo.syslogins WHERE PWDCOMPARE\(name, password\) = 1")[:rows]460end461462print_status("Accounts with Username and Password being the same:")463if sameasuser != nil464sameasuser.each do |up|465print_status("\t#{up.join}")466report_note(:host => mssql_client.peerhost,467:proto => 'TCP',468:port => mssql_client.peerport,469:type => 'MSSQL_ENUM',470:data => "Username: #{up.join} Password: #{up.join}")471end472else473print_status("\tNo Account with its password being the same as its username was found.")474report_note(:host => mssql_client.peerhost,475:proto => 'TCP',476:port => mssql_client.peerport,477:type => 'MSSQL_ENUM',478:data => "No Account with its password being the same as its username was found")479end480481#-------------------------------------------------------482# Check for local accounts with empty password483blankpass = []484if vernum.join != "2000"485blankpass = mssql_query("SELECT name FROM sys.sql_logins WHERE PWDCOMPARE\(\'\', password_hash\) = 1")[:rows]486else487blankpass = mssql_query("SELECT name FROM master.dbo.syslogins WHERE password IS NULL AND isntname = 0")[:rows]488end489490print_status("Accounts with empty password:")491if blankpass != nil492blankpass.each do |up|493print_status("\t#{up.join}")494report_note(:host => mssql_client.peerhost,495:proto => 'TCP',496:port => mssql_client.peerport,497:type => 'MSSQL_ENUM',498:data => "Username: #{up.join} Password: EMPTY ")499end500else501print_status("\tNo Accounts with empty passwords where found.")502report_note(:host => mssql_client.peerhost,503:proto => 'TCP',504:port => mssql_client.peerport,505:type => 'MSSQL_ENUM',506:data => "No Accounts with empty passwords where found")507end508509#-------------------------------------------------------510# Check for dangerous stored procedures511fountsp = []512dangeroussp = [513'sp_createorphan',514'sp_droporphans',515'sp_execute_external_script',516'sp_getschemalock',517'sp_prepexec',518'sp_prepexecrpc',519'sp_refreshview',520'sp_releaseschemalock',521'sp_replpostschema',522'sp_replsendtoqueue',523'sp_replsetsyncstatus',524'sp_replwritetovarbin',525'sp_resyncexecute',526'sp_resyncexecutesql',527'sp_resyncprepare',528'sp_resyncuniquetable',529'sp_unprepare',530'sp_xml_preparedocument',531'sp_xml_removedocument',532'sp_fulltext_getdata',533'sp_getbindtoken',534'sp_replcmds',535'sp_replcounters',536'sp_repldone',537'sp_replflush',538'sp_replincrementlsn',539'sp_replpostcmd',540'sp_replsetoriginator',541'sp_replstatus',542'sp_repltrans',543'sp_replupdateschema',544'sp_reset_connection',545'sp_sdidebug',546'xp_availablemedia',547'xp_check_query_results',548'xp_cleanupwebtask',549'xp_cmdshell',550'xp_convertwebtask',551'xp_deletemail',552'xp_dirtree',553'xp_displayparamstmt',554'xp_dropwebtask',555'xp_dsninfo',556'xp_enum_activescriptengines',557'xp_enum_oledb_providers',558'xp_enumcodepages',559'xp_enumdsn',560'xp_enumerrorlogs',561'xp_enumgroups',562'xp_enumqueuedtasks',563'xp_eventlog',564'xp_execresultset',565'xp_fileexist',566'xp_findnextmsg',567'xp_fixeddrives',568'xp_get_mapi_default_profile',569'xp_get_mapi_profiles',570'xp_get_tape_devices',571'xp_getfiledetails',572'xp_getnetname',573'xp_grantlogin',574'xp_initcolvs',575'xp_intersectbitmaps',576'xp_logevent',577'xp_loginconfig',578'xp_logininfo',579'xp_makewebtask',580'xp_mergexpusage',581'xp_monitorsignal',582'xp_msver any user',583'xp_msx_enlist',584'xp_ntsec_enumdomains',585'xp_ntsec_enumgroups',586'xp_ntsec_enumusers',587'xp_oledbinfo',588'xp_perfend',589'xp_perfmonitor',590'xp_perfsample',591'xp_perfstart',592'xp_printstatements',593'xp_prop_oledb_provider',594'xp_proxiedmetadata',595'xp_qv',596'xp_readerrorlog',597'xp_readmail',598'xp_readwebtask',599'xp_regaddmultistring',600'xp_regdeletekey',601'xp_regdeletevalue',602'xp_regenumvalues',603'xp_regread',604'xp_regremovemultistring',605'xp_regwrite',606'xp_repl_encrypt',607'xp_revokelogin',608'xp_runwebtask',609'xp_schedulersignal',610'xp_sendmail',611'xp_servicecontrol',612'xp_showcolv',613'xp_showlineage',614'xp_snmp_getstate',615'xp_snmp_raisetrap',616'xp_sprintf any user', # huh?617'xp_sqlagent_enum_jobs',618'xp_sqlagent_is_starting',619'xp_sqlagent_monitor',620'xp_sqlagent_notify',621'xp_sqlinventory',622'xp_sqlmaint',623'xp_sqlregister',624'xp_sqltrace',625'xp_startmail',626'xp_stopmail',627'xp_subdirs',628'xp_terminate_process',629'xp_test_mapi_profile',630'xp_trace_addnewqueue',631'xp_trace_deletequeuedefinition',632'xp_trace_destroyqueue',633'xp_trace_enumqueuedefname',634'xp_trace_enumqueuehandles',635'xp_trace_eventclassrequired',636'xp_trace_flushqueryhistory',637'xp_trace_generate_event',638'xp_trace_getappfilter',639'xp_trace_getconnectionidfilter',640'xp_trace_getcpufilter',641'xp_trace_getdbidfilter',642'xp_trace_getdurationfilter',643'xp_trace_geteventfilter',644'xp_trace_geteventnames',645'xp_trace_getevents',646'xp_trace_gethostfilter',647'xp_trace_gethpidfilter',648'xp_trace_getindidfilter',649'xp_trace_getntdmfilter',650'xp_trace_getntnmfilter',651'xp_trace_getobjidfilter',652'xp_trace_getqueueautostart',653'xp_trace_getqueuecreateinfo',654'xp_trace_getqueuedestination',655'xp_trace_getqueueproperties',656'xp_trace_getreadfilter',657'xp_trace_getserverfilter',658'xp_trace_getseverityfilter',659'xp_trace_getspidfilter',660'xp_trace_getsysobjectsfilter',661'xp_trace_gettextfilter',662'xp_trace_getuserfilter',663'xp_trace_getwritefilter',664'xp_trace_loadqueuedefinition',665'xp_trace_opentracefile',666'xp_trace_pausequeue',667'xp_trace_restartqueue',668'xp_trace_savequeuedefinition',669'xp_trace_setappfilter',670'xp_trace_setconnectionidfilter',671'xp_trace_setcpufilter',672'xp_trace_setdbidfilter',673'xp_trace_setdurationfilter',674'xp_trace_seteventclassrequired',675'xp_trace_seteventfilter',676'xp_trace_sethostfilter',677'xp_trace_sethpidfilter',678'xp_trace_setindidfilter',679'xp_trace_setntdmfilter',680'xp_trace_setntnmfilter',681'xp_trace_setobjidfilter',682'xp_trace_setqueryhistory',683'xp_trace_setqueueautostart',684'xp_trace_setqueuecreateinfo',685'xp_trace_setqueuedestination',686'xp_trace_setreadfilter',687'xp_trace_setserverfilter',688'xp_trace_setseverityfilter',689'xp_trace_setspidfilter',690'xp_trace_setsysobjectsfilter',691'xp_trace_settextfilter',692'xp_trace_setuserfilter',693'xp_trace_setwritefilter',694'xp_trace_startconsumer',695'xp_unc_to_drive',696'xp_updatecolvbm',697'xp_updateFTSSQLAccount',698'xp_updatelineage',699'xp_varbintohexstr',700'xp_writesqlinfo',701'xp_MSplatform',702'xp_MSnt2000',703'xp_MSLocalSystem',704'xp_IsNTAdmin',705'xp_mapdown_bitmap'706]707708query = <<-EOS709SELECT CAST(SYSOBJECTS.NAME AS CHAR) FROM SYSOBJECTS, SYSPROTECTS WHERE SYSPROTECTS.UID = 0 AND XTYPE IN ('X','P')710AND SYSOBJECTS.ID = SYSPROTECTS.ID711EOS712fountsp = mssql_query(query)[:rows]713if fountsp != nil714fountsp.flatten!715print_status("Stored Procedures with Public Execute Permission found:")716fountsp.each do |strp|717if dangeroussp.include?(strp.strip)718print_status("\t#{strp.strip}")719report_note(:host => mssql_client.peerhost,720:proto => 'TCP',721:port => mssql_client.peerport,722:type => 'MSSQL_ENUM',723:data => "Stored Procedures with Public Execute Permission #{strp.strip}")724end725end726else727print_status("\tNo Dangerous Stored Procedure found with Public Execute.")728report_note(:host => mssql_client.peerhost,729:proto => 'TCP',730:port => mssql_client.peerport,731:type => 'MSSQL_ENUM',732:data => "No Dangerous Stored Procedure found with Public Execute")733end734735#-------------------------------------------------------736# Enumerate Instances737instances =[]738if vernum.join != "2000"739querykey = "EXEC master..xp_regenumvalues \'HKEY_LOCAL_MACHINE\',\'SOFTWARE\\Microsoft\\Microsoft SQL Server\\Instance Names\\SQL\'"740instance_res = mssql_query(querykey)[:rows]741if instance_res != nil742instance_res.each do |i|743instances << i[0]744end745end746else747querykey = "exec xp_regread \'HKEY_LOCAL_MACHINE\',\'SOFTWARE\\Microsoft\\Microsoft SQL Server\', \'InstalledInstances\'"748instance_res = mssql_query(querykey)[:rows]749if instance_res != nil750instance_res.each do |i|751instances << i[1]752end753end754end755756print_status("Instances found on this server:")757instancenames = []758if instances != nil759instances.each do |i|760print_status("\t#{i}")761instancenames << i.strip762report_note(:host => mssql_client.peerhost,763:proto => 'TCP',764:port => mssql_client.peerport,765:type => 'MSSQL_ENUM',766:data => "Instance Name: #{i}")767end768else769print_status("No instances found, possible permission problem")770end771772#---------------------------------------------------------773# Enumerate under what accounts the instance services are running under774print_status("Default Server Instance SQL Server Service is running under the privilege of:")775privdflt = mssql_query("EXEC master..xp_regread \'HKEY_LOCAL_MACHINE\' ,\'SYSTEM\\CurrentControlSet\\Services\\MSSQLSERVER\',\'ObjectName\'")[:rows]776if privdflt != nil777privdflt.each do |priv|778print_status("\t#{priv[1]}")779report_note(:host => mssql_client.peerhost,780:proto => 'TCP',781:port => mssql_client.peerport,782:type => 'MSSQL_ENUM',783:data => "Default Instance SQL Server running as: #{priv[1]}")784end785else786print_status("\txp_regread might be disabled in this system")787end788789#------------------------------------------------------------790if instancenames.length > 1791instancenames.each do |i|792if i.strip != "MSSQLSERVER"793privinst = mssql_query("EXEC master..xp_regread \'HKEY_LOCAL_MACHINE\' ,\'SYSTEM\\CurrentControlSet\\Services\\MSSQL$#{i.strip}\',\'ObjectName\'")[:rows]794if privinst != nil795print_status("Instance #{i} SQL Server Service is running under the privilege of:")796privinst.each do |p|797print_status("\t#{p[1]}")798report_note(:host => mssql_client.peerhost,799:proto => 'TCP',800:port => mssql_client.peerport,801:type => 'MSSQL_ENUM',802:data => "#{i} Instance SQL Server running as: #{p[1]}")803end804else805print_status("\tCould not enumerate credentials for Instance.")806end807end808end809end810811disconnect812end813end814815816