iptables系列三

时间:2022-05-11
本文章向大家介绍iptables系列三,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

iptables系列之nat及其过滤功能

自定义规则链

iptables -N clean_in 
iptables -L -n 
iptables -A clean_in -d 255.255.255.255 -p icmp -j DROP 
iptables -L -n --line-number 
iptables -A clean_in -d 172.16.255.255 -p icmp -j DROP 
iptables -A clean_in -p tcp !--syn -m state --state NEW -j DROP 
iptables -A clean_in -p tcp --tcp-flags ALL ALL -j DROP 
iptables -A clean_in -p tcp tcp-flags ALL NONE -j DROP 
iptables -A clean_in -d 172.16.1.3 -j RETURN 
iptables -I INPUT -j claen_in 
iptables -L -n --line-numbers

iptables -X clean_in 
定义自定义链,以及链的引用于返回等。

利用iptables的recent模块来抵御DOS攻击

 ssh: 远程连接,
 iptables -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 3 -j DROP 
 iptables -I -p tcp --dport 22 -m state --state NEW -m recent --set --name SSH 
 iptables -IINPUT -p tcp --dport 22 -m state NEW -m recent --update --seconds 300 --hitcount 3 --name SSH -j DROP 
 
 -m recent :将最近向本机发起连接请求的ip地址记录下来。允许您动态创建一个地址列表,然后用几种不同的方式对该列表的匹配。
       --name 
           为模板命名。一个内存空间。


 1.利用connlimit模块将单IP的并发设置为3;会误杀使用NAT上网的用户,可以根据实际情况增大该值;

2.利用recent和state模块限制单IP在300s内只能与本机建立3个新连接。被限制五分钟后即可恢复访问。

下面对最后两句做一个说明:

1.第二句是记录访问tcp 22端口的新连接,记录名称为SSH
--set 记录数据包的来源IP,如果IP已经存在将更新已经存在的条目

2.第三句是指SSH记录中的IP,300s内发起超过3次连接则拒绝此IP的连接。
--update 是指每次建立连接都更新列表;
--seconds必须与--rcheck或者--update同时使用
--hitcount必须与--rcheck或者--update同时使用

3.iptables的记录:/proc/net/ipt_recent/SSH
/proc/net/xt_recent/


也可以使用下面的这句记录日志:
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --name SSH --second 300 --hitcount 3 -j LOG --log-prefix "SSH Attack"

image.png

NAT:Network Address Translation

SNAT:源地址转换  POSTROUTING,OUTPUT 
DNAT:目标地址转换 PREROUTING 

-j SNAT 
   --to-source ipaddr[-ipaddr][:port[-port]]
  
 可以做静态NAT,动态NAT,端口NAT
  
MASQUERADE 
这个目标只适用于nat表,在POSTROUTING连锁。它只能用于动态分配的IP(拨号)连接.自动查找一个可以上网的IP地址进行源地址转换。

-j  MASQUERADE 
     --to-ports port[-port]
     --radom 
 
ip_forward 
/proc/sys/net/ipv4/ip_forward:1 
route -n 
iptables -P INPUT ACCEPT 
iptables -P OUTPUT ACCEPT 
iptables -P FPRWARD ACCEPT 
注意:对于linux,地址是属于主机的,不属于网卡的,所以一台主机上,ip都是可以通信的。

echo "1" > /proc/sys/net/ipv4/ip_forward 

vim /etc/sysctl.conf 
net.ipv4.ip_forward=1 
sysctl -p 

route del -net 0.0.0.0 
route add default gw 172.16.100.7 


地址池表

TCP报文的序列号 
nat基于地址表+nf_conntrack...


iptables -t nat 
     -j SNAT 
         --to-source 
         
iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -j SNAT --to-source 172.16.100.7  

tcpdump -i  eth0 -nn -X -p icmp 
tcpdump -i eth0 -nn  |grep 192

iptables -t nat -A PREROUTING -d 192.168.1.10 -j DNAT --to-destination 172.16.10.6 
 
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o pppoe -j SNAT--to-source 123.2.3.2 

iptables -A FORWARD -s 192.168.0.0/24 -p icmp -j DROP 

iptables -P FORWARD DROP 
iptables -A FROWARD -m state ESTABLISHED -j ACCEPT 

iptables -A FORWARD -s 192.168.10.0/24 -p tcp --dport 80 -m state NEW -j ACCEPT 
iptables -A FORWARD 192.168.10.0/24 -p icmp --icmp-type 8 -m state --state NEW -j ACCEPT 
 
lsmod |grep ftp 
注意:网络防火墙,建立在FORWARD上的链。


 -j DNAT 
   --to-destination [ipaddr][-ipaddr][:port[-port]]
  --random
   --persistent
   

iptables -t nat -A PREROUTING -d 192.168.1.10  -p tcp --dport 21 

iptables -t nat -A PREROUTING -d 172.16.100.7 -p tcp --dport 80 -j DNAT  

请求内容过滤:
iptables -t filter -A FORWARD -m string --algo kmp --string "hello" -j DROP