基本上,我试图在防火墙级别限制对邮件服务器的请求,但将几个公司拥有的IP范围列入白名单。 我可以将一个 IP地址范围列入白名单,而不是几个,因为“第一个匹配规则占用数据包”的性质。
这是我目前的…(对于-I和-A的混合感到抱歉)
# Send traffic to rate-limiter before allowing it to continue to the mail server. iptables -t nat -I PREROUTING -d 20.87.6.71 -p tcp --destination-port 25 -j DNAT --to-destination 192.168.250.71:25 iptables -t nat -I PREROUTING -d 20.87.6.71 -p tcp --destination-port 143 -j DNAT --to-destination 192.168.250.71:143 iptables -t nat -I PREROUTING -d 20.87.6.71 -p tcp --destination-port 993 -j DNAT --to-destination 192.168.250.71:993 iptables -t mangle -I PREROUTING -d 20.87.6.71 -p tcp --destination-port 25 -j rate-limiter iptables -t mangle -I PREROUTING -d 20.87.6.71 -p tcp --destination-port 143 -j rate-limiter iptables -t mangle -I PREROUTING -d 20.87.6.71 -p tcp --destination-port 993 -j rate-limiter # Create a new chain, which logs all traffic then drops it. # We make sure that localnets are not part of this... iptables -t mangle -N rate-limiter iptables -t mangle -A rate-limiter -p tcp --syn -d 20.87.6.71 ! -s 192.168.254.0/24 -m state --state NEW -m recent --set --name MailRateLimiter --rsource iptables -t mangle -A rate-limiter -p tcp --syn -d 20.87.6.71 ! -s 20.87.6.0/24 -m state --state NEW -m recent --set --name MailRateLimiter --rsource iptables -t mangle -A rate-limiter -p tcp --syn -d 20.87.6.71 ! -s 192.168.250.0/24 -m state --state NEW -m recent --rcheck --seconds 60 --hitcount 20 --name MailRateLimiter --rsource -j LOG --log-prefix 'RATE EXCEEDED' --log-level 4 iptables -t mangle -A rate-limiter -p tcp --syn -d 20.87.6.71 ! -s 192.168.250.0/24 -m state --state NEW -m recent --rcheck --seconds 60 --hitcount 20 --name MailRateLimiter --rsource -j DROP iptables -t mangle -A rate-limiter -p tcp --syn -d 20.87.6.71 ! -s 20.87.6.0/24 -m state --state NEW -m recent --rcheck --seconds 60 --hitcount 20 --name MailRateLimiter --rsource -j LOG --log-prefix 'RATE EXCEEDED' --log-level 4 iptables -t mangle -A rate-limiter -p tcp --syn -d 20.87.6.71 ! -s 20.87.6.0/24 -m state --state NEW -m recent --rcheck --seconds 60 --hitcount 20 --name MailRateLimiter --rsource -j DROP
所以,第一个规则匹配数据包,然后在最近的表中标记,所以当我cat /proc/self/net/xt_recent/MailRateLimiter ,我看到应列入白名单的IP:
src=20.87.6.200 ttl: 57 last_seen: 56654988107 oldest_pkt: 8 56654320106, 56654370481, 56654426548, 56654480458, 56654564349, 56654565421, 56654952936, 56654988107 src=20.87.6.5 ttl: 63 last_seen: 56655077970 oldest_pkt: 9 56654962465, 56654962465, 56654978465, 56655024835, 56655031038, 56655037096, 56655048206, 56655059305, 56655077970, 56654768325, 56654777150, 56654802008, 56654807677, 56654816481, 56654842497, 56654846996, 56654855809, 56654886875, 5665489136
请原谅我,如果这是愚蠢的…这是一个漫长的一天,我有点被扔进这个…
你可以将任何你想要的东西写在链的顶部:
iptables -t mangle -A rate-limiter <rule to match whitelisted> -j RETURN
匹配白名单networking的数据包将不经修改而返回到POSTROUTING 。