iptables实现字符串匹配
通过string匹配域名来过滤,范例如下:
iptables -I OUTPUT -p tcp -m string --string "qq.com" --algo bm -j DROPiptables -I OUTPUT -p udp -m string --string "qq.com" --algo bm -j DROP
这样就无法访问与QQ相关的业务了,但是代理好像还是可以
系统要求:
1:内核在2.6.18以上
2:iptables在1.3.5以上
3:内核中包含字符串匹配模块。在.config文件中应有
CONFIG_NETFILTER_XT_MATCH_STRING=m
man手册
iptables -m string -helpstring This modules matches a given string by using somepattern matching strategy. It requires a linux kernel >= 2.6.14. --algo bm|kmp Select the pattern matching strategy. (bm = Boyer-Moore, kmp = Knuth-Pratt-Morris) --from offset Set the offset from which it starts looking for anymatching. If not passed, default is 0. --to offset Set the offset to which it starts looking for anymatching. If not passed, default is the packet size. --string pattern Matches the given pattern. --hex-string patternMatches the given pattern in hex notation.常见的应用程序和有用的示例规则1:防止入侵的方法iptables -I INPUT 1 -p tcp --dport 80 -m string --string "cmd.exe" --algo bm -j DROPiptables -I INPUT -j DROP -p tcp -s 0.0.0.0/0 -m string --algo kmp --string "cmd.exe"
2:保护DDOS服务iptables -I INPUT 1 -p tcp --dport 80 -m string --string "domain.com" --algo kmp -j DROP3:防止电子邮件欺骗iptables -I INPUT -p tcp --dport 25 -m string --string "Subject" --algo bm -j DROP做个最优规则iptables -I INPUT -p tcp --dport 25 -m string --string "Subject" --algo bm --to 15000 -j DROP