@@ -7,12 +7,18 @@ module Msf
7
7
class Post
8
8
module Windows
9
9
module MSSQL
10
+
11
+ # @return [String, nil] contains the identified SQL command line client
10
12
attr_accessor :sql_client
11
13
12
14
include Msf ::Exploit ::Remote ::MSSQL_COMMANDS
13
15
include Msf ::Post ::Windows ::Services
14
16
include Msf ::Post ::Windows ::Priv
15
17
18
+ # Identifies the Windows Service matching the SQL Server instance name
19
+ #
20
+ # @param [String] instance the SQL Server instance name to locate
21
+ # @return [Hash, nil] the Windows Service instance
16
22
def check_for_sqlserver ( instance = nil )
17
23
target_service = nil
18
24
each_service do |service |
@@ -39,6 +45,11 @@ def check_for_sqlserver(instance = nil)
39
45
target_service
40
46
end
41
47
48
+ # Identifies a valid SQL Server command line client on the host and sets
49
+ # @sql_client
50
+ #
51
+ # @see #sql_client
52
+ # @return [String, nil] the SQL command line client
42
53
def get_sql_client
43
54
client = nil
44
55
@@ -52,12 +63,18 @@ def get_sql_client
52
63
client
53
64
end
54
65
66
+ # Attempts to run the osql command line tool
67
+ #
68
+ # @return [Boolean] true if osql is present
55
69
def check_osql
56
70
running_services1 = run_cmd ( "osql -?" )
57
71
services_array1 = running_services1 . split ( "\n " )
58
72
services_array1 . join =~ /(SQL Server Command Line Tool)|(usage: osql)/i
59
73
end
60
74
75
+ # Attempts to run the sqlcmd command line tool
76
+ #
77
+ # @return [Boolean] true if sqlcmd is present
61
78
def check_sqlcmd
62
79
running_services = run_cmd ( "sqlcmd -?" )
63
80
services_array = running_services . split ( "\n " )
@@ -66,6 +83,12 @@ def check_sqlcmd
66
83
end
67
84
end
68
85
86
+ # Runs a SQL query using the identified command line tool
87
+ #
88
+ # @param [String] query the query to execute
89
+ # @param [String] instance the SQL instance to target
90
+ # @param [String] server the SQL server to target
91
+ # @return [String] the result of query
69
92
def run_sql ( query , instance = nil , server = '.' )
70
93
target = server
71
94
if instance && instance . downcase != 'mssqlserver'
@@ -76,13 +99,15 @@ def run_sql(query, instance = nil, server = '.')
76
99
run_cmd ( cmd )
77
100
end
78
101
79
- ## ----------------------------------------------
80
- ## Method for executing cmd and returning the response
81
- ##
82
- ## Note: This may fail as SYSTEM if the current process
83
- ## doesn't have sufficient privileges to duplicate a token,
84
- ## e.g. SeAssignPrimaryToken
85
- ##----------------------------------------------
102
+ # Executes a hidden command
103
+ #
104
+ # @param [String] cmd the command line to execute
105
+ # @param [Boolean] token use the current thread token
106
+ # @return [String] the results from the command
107
+ #
108
+ # @note This may fail as SYSTEM if the current process
109
+ # doesn't have sufficient privileges to duplicate a token,
110
+ # e.g. SeAssignPrimaryToken
86
111
def run_cmd ( cmd , token = true )
87
112
opts = { 'Hidden' => true , 'Channelized' => true , 'UseThreadToken' => token }
88
113
process = session . sys . process . execute ( "cmd.exe /c #{ cmd } " , nil , opts )
@@ -97,6 +122,15 @@ def run_cmd(cmd, token = true)
97
122
res
98
123
end
99
124
125
+ # Attempts to impersonate the user of the supplied service
126
+ # If the process has the appropriate privileges it will attempt to
127
+ # steal a token to impersonate, otherwise it will attempt to migrate
128
+ # into the service process.
129
+ #
130
+ # @note This may cause the meterpreter session to migrate!
131
+ #
132
+ # @param [Hash] service the service to target
133
+ # @return [Boolean] true if impersonated successfully
100
134
def impersonate_sql_user ( service )
101
135
pid = service [ :pid ]
102
136
vprint_status ( "Current user: #{ session . sys . config . getuid } " )
@@ -140,11 +174,15 @@ def impersonate_sql_user(service)
140
174
true
141
175
end
142
176
177
+ # Attempts to escalate the meterpreter session to SYSTEM
178
+ #
179
+ # @return [Boolean] true if escalated successfully or user is already SYSTEM
143
180
def get_system
144
181
print_status ( "Checking if user is SYSTEM..." )
145
182
146
183
if is_system?
147
184
print_good ( "User is SYSTEM" )
185
+ return true
148
186
else
149
187
# Attempt to get LocalSystem privileges
150
188
print_warning ( "Attempting to get SYSTEM privileges..." )
0 commit comments