目录
缺点:如果目标在内网,你是无法直接ping到它的
iptables -t nat -N HTTP_TO_SSH
自定义了一条链条
iptables -t nat -A HTTP_TO_SSH -p tcp -j REDIRECT --to-port 22
将自定义链条的流量转发到22端口
iptables -t nat -A PREROUTING -p icmp --icmp-type 8 -m length --length 1139 -m recent --set --name oupeng --rsource -j ACCEPT
如果收到一个长为1139的icmp包,则将来源ip添加到loupeng的列表中
iptables -t nat -A PREROUTING -p icmp --icmp-type 8 -m length --length 1140 -mrecent --name oupeng --remove -j ACCEPT
如果收到到一个长为1140的icmp包,则将来源ip从oupeng列表中去掉
iptables -t nat -A PREROUTING -p tcp --dport 80 --syn -m recent --rcheck --seconds 3600 --name oupeng --rsource -j HTTP_TO_SSH
如果发现了syn包的来源处于letmein列表中,则将跳转到letmein链进行处理,有效时间为3600秒
可以查看一下五条链的情况:
- [root@centos111 ~]# iptables -t nat -nvxL
- Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
- pkts bytes target prot opt in out source destination
- 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 8 length 1139 recent: SET name: oupeng side: source mask: 255.255.255.255
- 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 8 length 1140 recent: REMOVE name: oupeng side: source mask: 255.255.255.255
- 0 0 HTTP_TO_SSH tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 flags:0x17/0x02 recent: CHECK seconds: 3600 name: oupeng side: source mask: 255.255.255.255
-
- Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
- pkts bytes target prot opt in out source destination
-
- Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
- pkts bytes target prot opt in out source destination
-
- Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
- pkts bytes target prot opt in out source destination
- 26 1804 RETURN all -- * * 192.168.122.0/24 224.0.0.0/24
- 0 0 RETURN all -- * * 192.168.122.0/24 255.255.255.255
- 0 0 MASQUERADE tcp -- * * 192.168.122.0/24 !192.168.122.0/24 masq ports: 1024-65535
- 0 0 MASQUERADE udp -- * * 192.168.122.0/24 !192.168.122.0/24 masq ports: 1024-65535
- 0 0 MASQUERADE all -- * * 192.168.122.0/24 !192.168.122.0/24
-
- Chain HTTP_TO_SSH (1 references)
- pkts bytes target prot opt in out source destination
- 0 0 REDIRECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 redir ports 22
开启复用前,web是可以正常访问的:
现在我们可以尝试开启复用,即在192.168.159.201ping192.168.159.200(本机)
ping -c 1 -s 1111 192.168.159.200
向目标发送了一个长度为1111的icmp数据包(加上包头28,总长度实际为1139)
因为ip数据包=ip包头+icmp
所以这里的28个字节包括:ip包头的20字节+icmp包头的8字节
尝试使用80端口进行ssh,(这里实际访问的是22端口)
可以看到,我们成功的使用80端口实现22端口的登陆
我们可以在200上面查看一下 22端口
netstat -antp |grep 22
这里显示有两个ip地址和本主机的22端口建立了连接。
我们现在在201主机上上可以尝试访问一下80端口
可以看到访问失败了,因为我们将200主机的80端口用作22的端口了
查看nat表也可以看到规则中是1139个字节的数据包:
那现在我们关闭复用
ping -c 1 -s 1112 192.168.159.200
向目标发送了一个长度为1111的icmp数据包(加上包头28,总长度实际为1140)
在centos1上再查看一下nat表的规则
关闭复用后,我们再尝试登录
可以看到,现在如果去尝试登录就会失败!
优点:不怕目标不在内网
iptables -t nat -N HTTP_TO_SSH
iptables -t nat -A HTTP_TO_SSH -p tcp -j REDIRECT --to-port 22
iptables -A INPUT -p tcp -m string --string 'oupeng' --algo bm -m recent --set --name HTTP_TO_SSH --rsource -j ACCEPT
iptables -A INPUT -p tcp -m string --string 'close' --algo bm -m recent --name HTTP_TO_SSH --remove -j ACCEPT
iptables -t nat -A PREROUTING -p tcp --dport 80 --syn -m recent --rcheck --seconds 3600 --name HTTP_TO_SSH --rsource -j HTTP_TO_SSH
开启复用,开启本机到目标80端口的流量,将转发至目标的SSH ,80将无法再被主机访问;
- [root@centos222 ~]# echo oupeng | socat - tcp:192.168.159.200:80
- HTTP/1.1 400 Bad Request
- Server: nginx/1.20.1
- Date: Sun, 19 Nov 2023 04:29:46 GMT
- Content-Type: text/html
- Content-Length: 157
- Connection: close
-
- <head>
400 Bad Request 400 Bad Request
nginx/1.20.1