Skip to content

Commit

Permalink
adjust session logic in modules
Browse files Browse the repository at this point in the history
  • Loading branch information
zgoldman-r7 committed Feb 12, 2024
1 parent 94223f0 commit 42bfa3d
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 61 deletions.
4 changes: 2 additions & 2 deletions lib/msf/core/exploit/remote/mssql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def initialize(info = {})
register_autofilter_services(%W{ ms-sql-s ms-sql2000 sybase })
end

def set_session(session)
@mssql_client = session.client
def set_session(client)
@mssql_client = client
end

#
Expand Down
16 changes: 8 additions & 8 deletions modules/auxiliary/admin/mssql/mssql_enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ module to work, valid administrative user credentials must be

def run
print_status("Running MS SQL Server Enumeration...")
if (datastore['SESSION'] && session)
set_session(session)
end

unless (datastore['SESSION'] && session) || mssql_login_datastore
print_error("Login was unsuccessful. Check your credentials.")
disconnect
return
if session
set_session(session.client)
else
unless mssql_login_datastore
print_error("Login was unsuccessful. Check your credentials.")
disconnect
return
end
end

# Get Version
Expand Down
21 changes: 11 additions & 10 deletions modules/auxiliary/admin/mssql/mssql_escalate_dbowner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@ def initialize(info = {})

def run
# Check connection and issue initial query
if (datastore['SESSION'] && session)
set_session(session)
end

print_status("Attempting to connect to the database server at #{rhost}:#{rport} as #{datastore['USERNAME']}...")
if (datastore['SESSION'] && session) || mssql_login_datastore
print_good('Connected.')
if session
set_session(session.client)
print_good("Using session #{datastore['SESSION']}.")
else
print_error('Login was unsuccessful. Check your credentials.')
disconnect
return
print_status("Attempting to connect to the database server at #{rhost}:#{rport} as #{datastore['USERNAME']}...")
if mssql_login_datastore
print_good('Connected.')
else
print_error("Login was unsuccessful. Check your credentials.")
disconnect
return
end
end

# Query for sysadmin status
Expand Down
22 changes: 11 additions & 11 deletions modules/auxiliary/admin/mssql/mssql_escalate_execute_as.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ def initialize(info = {})
end

def run
if (datastore['SESSION'] && session)
set_session(session)
end
# Check connection and issue initial query
print_status("Attempting to connect to the database server at #{datastore['RHOST']}:#{datastore['RPORT']} as #{datastore['USERNAME']}...")

if (datastore['SESSION'] && session) || mssql_login_datastore
print_good('Connected.')
if session
set_session(session.client)
print_good("Using session #{datastore['SESSION']}.")
else
print_error('Login was unsuccessful. Check your credentials.')
disconnect
return
print_status("Attempting to connect to the database server at #{rhost}:#{rport} as #{datastore['USERNAME']}...")
if mssql_login_datastore
print_good('Connected.')
else
print_error("Login was unsuccessful. Check your credentials.")
disconnect
return
end
end

# Query for sysadmin status
Expand Down
10 changes: 7 additions & 3 deletions modules/auxiliary/admin/mssql/mssql_exec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ def initialize(info = {})
end

def run
if (datastore['SESSION'] && session)
set_session(session)
if session
set_session(session.client)
print_good("Using session #{datastore['SESSION']}.")
else
unless mssql_login_datastore
return
end
end
return unless (datastore['SESSION'] && session) || mssql_login_datastore

technique = datastore['TECHNIQUE']
case technique
Expand Down
8 changes: 5 additions & 3 deletions modules/auxiliary/admin/mssql/mssql_findandsampledata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,12 @@ def sql_statement()

# CREATE DATABASE CONNECTION AND SUBMIT QUERY WITH ERROR HANDLING
begin
if (datastore['SESSION'] && session)
set_session(session)
if session
set_session(session.client)
result = mssql_query(sql, false)
elsif mssql_login_datastore
result = mssql_query(sql, false)
end
result = mssql_query(sql, false) if (datastore['SESSION'] && session) || mssql_login_datastore

column_data = result[:rows]
print_good("Successfully connected to #{rhost}:#{rport}")
Expand Down
8 changes: 4 additions & 4 deletions modules/auxiliary/admin/mssql/mssql_idf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ def run
sql += "DEALLOCATE table_cursor "

begin
if (datastore['SESSION'] && session)
set_session(session)
if session
set_session(session.client)
end

if (datastore['SESSION'] && session) || mssql_login_datastore
require 'pry-byebug'; binding.pry
if session || mssql_login_datastore
result = mssql_query(sql, false)
else
print_error('Login failed')
Expand Down
6 changes: 3 additions & 3 deletions modules/auxiliary/admin/mssql/mssql_sql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ def cmd_select(*args)
end

def run
if (datastore['SESSION'] && session)
set_session(session)
if session
set_session(session.client)
end

mssql_query(datastore['SQL'], true) if (datastore['SESSION'] && session) || mssql_login_datastore
mssql_query(datastore['SQL'], true) if session || mssql_login_datastore
end
end
6 changes: 3 additions & 3 deletions modules/auxiliary/admin/mssql/mssql_sql_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ def run
suffix = datastore['QUERY_SUFFIX']

begin
if (datastore['SESSION'] && session)
set_session(session)
if session
set_session(session.client)
end
queries.each do |sql_query|
vprint_status("Executing: #{sql_query}")
mssql_query(prefix+sql_query.chomp+suffix,true) if (datastore['SESSION'] && session) || mssql_login_datastore
mssql_query(prefix+sql_query.chomp+suffix,true) if session || mssql_login_datastore
end
rescue Rex::ConnectionRefused, Rex::ConnectionTimeout
print_error "Error connecting to server: #{$!}"
Expand Down
7 changes: 2 additions & 5 deletions modules/auxiliary/scanner/mssql/mssql_hashdump.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@ def initialize
end

def run_host(ip)
if (datastore['SESSION'] && session)
set_session(session)
elsif (datastore['SESSION'] && !session)
print_error('Unable to connect to session')
return
if session
set_session(session.client)
elsif !mssql_login(datastore['USERNAME'], datastore['PASSWORD'])
print_error('Invalid SQL Server credentials')
return
Expand Down
6 changes: 3 additions & 3 deletions modules/auxiliary/scanner/mssql/mssql_schemadump.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ def initialize
end

def run_host(ip)
if (datastore['SESSION'] && session)
set_session(session)
if session
set_session(session.client)
end

unless (datastore['SESSION'] && session) || mssql_login_datastore
unless session || mssql_login_datastore
print_error("#{datastore['RHOST']}:#{datastore['RPORT']} - Invalid SQL Server credentials")
return
end
Expand Down
12 changes: 6 additions & 6 deletions modules/exploits/windows/mssql/mssql_payload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ def initialize(info = {})
end

def check
if (datastore['SESSION'] && session)
set_session(session)
if session
set_session(session.client)
end

unless (datastore['SESSION'] && session) || mssql_login_datastore
unless session || mssql_login_datastore
vprint_status("Invalid SQL Server credentials")
return Exploit::CheckCode::Detected
end
Expand All @@ -97,11 +97,11 @@ def execute_command(cmd, opts)

def exploit

if (datastore['SESSION'] && session)
set_session(session)
if session
set_session(session.client)
end

unless (datastore['SESSION'] && session) || mssql_login_datastore
unless session || mssql_login_datastore
print_status("Invalid SQL Server credentials")
return
end
Expand Down

0 comments on commit 42bfa3d

Please sign in to comment.