iptables
iptables(1)
Firewall : 隔离工具;Packets Filter Firewall;工作于主机或网络的边缘,对经由的报文根据预先定义的规则(匹配条件)进行检测,对于能够被规则匹配到的报文实行某预定义的处理机制的一套组件;
硬件防火墙 : 在硬件级别实现部分功能的防火墙;另一个部分功能基于软件实现;
软件防火墙 : 应用软件处理逻辑运行于通用硬件平台之上的防火墙;
主机防火墙 : 服务范围为当前主机;
网络防火墙 : 服务范围为防火墙被的局域网;
iptables/netfilter :
netfilter : 防火墙框架,framework;位于内核空间;
iptables : 命令行工具程序,位于用户空间;规则管理工具;
netfilter :
hooks function
prerouting
input
forward
output
postrouting
iptables :
CHAINS :
PREROUTING
INPUT
FORWARD
OUTPUT
POSTROUTING
报文流向 :
到本机某进程的报文 : PREROUTING –> INPUT
由本机转发的报文 : PREROUTING –> FORWARD –> POSTROUTING
由本机的某进程发出报文 : OUTPUT –> POSTROUTING
tables :
filter : 过滤,防火墙;
nat : network address translation,网络地址转换;
mangle : 拆解报文,做出修改,并重新封装;
raw : 关闭nat表上启用的连接追踪机制;
优先级次序(由高而低) :
raw –> mangle –> nat –> filter
功能<–>钩子 :
raw : PREROUTING,OUTPUT
mangle : PREROUTING,INPUT,FORWARD,OUTPUT,POSTROUTING
nat : PREROUTING,INPUT,OUTPUT,POSTROUTING
filter : INPUT,FORWARD,OUTPUT
iptables规则的组成部分 :
匹配条件 :
网络层首部 : Source IP, Destination IP
传输层首部 : Source Port, Destination Port
扩展检查机制 :
处理动作 : target
ACCEPT,DROP,REJECT
安装 :
netfilter : 位于内核中的tcp/ip协议栈报文处理框架;
iptables :
CentOS 5/6 : iptables命令编写规则;
# iptables -t filter -F
# service iptables save
CentOS 7 : firewalld,firewall-cmd, firewall-config
# systemctl disable firewalld
程序包 : iptables, iptstate
iptables命令 :
规则 : 根据指定的匹配条件来尝试匹配每个流经此处的报文,一旦匹配成功,则由规则后面指定的处理动作进行处理;
匹配条件 :
基本匹配条件 : 源地址,目标地址,传输层协议
扩展匹配条件 : 需要借助于扩展模块进行指定的匹配条件
隐式扩展 : 已经在基本匹配条件中指明的协议相关的扩展;
显式扩展 : 隐式扩展之外的其它扩展匹配条件;
处理动作 :
基本动作 : ACCEPT,DROP,…
扩展动作 : 需要借助于扩展模块进行,但无须显式指定,仅需指明动作;
添加规则时需要考量的问题 :
(1) 报文流经的位置 : 用于判断将规则添加至哪个链;
(2) 实现的功能 : 用于判断将规则添加至哪个表;
(3) 报文的方向 : 用于判断哪个为“源”,哪个为“目标”;
(4) 匹配条件 : 用于编写能够正确匹配目标报文的规则;
iptabls命令的使用格式 :
iptables [-t table] {-A|-C|-D} chain rule-specification
iptables [-t table] -I chain [rulenum] rule-specification
iptables [-t table] -R chain rulenum rule-specification
iptables [-t table] -D chain rulenum
iptables [-t table] -S [chain [rulenum]]
iptables [-t table] {-F|-L|-Z} [chain [rulenum]] [options…]
iptables [-t table] -N chain
iptables [-t table] -X [chain]
iptables [-t table] -P chain target
iptables [-t table] -E old-chain-name new-chain-name
rule-specification = [matches…] [target]
match = -m matchname [per-match-options]
target = -j targetname [per-target-options]
规则管理格式 : iptables [-t table] COMMAND chain cretieria [-m -m matchname [per-match-options]] [-j targetname [per-target-options]]
-t table : 指明要管理的表; 默认为filter;
COMMAND :
链管理:
-P : iptables [-t table] -P chain target ,定义链的默认策略
# iptables -t filter -P INPUT DROP #将INPUT策略默认为DROP,所有进站连接全部禁止
-N : iptables [-t table] -N chain :自定义规则链,仅在默认链通过某规则进行调用方可生效
# iptables iN web_da
-X : iptables [-t table] -X [chain] : 删除自定义,空的,引用计数为0的链
# iptables -X :清空自定义,空的,引用计数为0的链
-F : iptables [-t table] -F [chain []] :清空指定的链,或删除指定链上的规则
-E : iptables [-t table] -E old-chain-name new-chain-name :重命名,计数器必须为零,否则改不了
# iptables -t filter -E web_da in_web
-Z : iptables [-t table] -Z [chain [rulenum]] [options…] :计数器重置为零
# iptables -t filter -Z INPUT
计数器:
每条规则以及链的默认策略分别有各自的两个计数器
(1) 匹配到的报文的个数:pkts
(2) 匹配到的所有报文的
规则:
-A :append , iptables [-t table] -A chain rule-specification 追加规则到指定的链,追加到末尾
-I : insert , iptables [-t table] -I chain [rulenum] rule-specification 插入规则到链中的指定位置,默认为链首
-D ,delete , iptables [-t table] -D chain rulenum 或 iptables [-t table] -D chain rule-specification :
-R ,replace , iptables [-t table] -R chain rulenum rule-specification :将指定链的指定规则替换为新的规则
查看 :
-L : list, iptables [-t table] -L [chain [rulenum]] [options…]
-n : 数字格式;
-v : verbose,详细格式信息;
-vv, -vvv
–line-numbers : 显示链上的规则的编号;
-x : exactly,显示计数器的精确值;
匹配条件:
基本匹配条件
扩展匹配条件
隐式扩展
显式扩展
基本匹配条件:
[!] -s, –source address[/mask][,…] : 检查报文中的源IP地址是否符合此处指定的地址或地址范围;
# iptables -t filter -A INPUT -s 192.168.153.0/24 -j ACCEPT :允许该网段进站 # iptables -t filter -I INPUT -s 192.168.153.71 -j REJECT :拒绝该ip进站 # iptables -t filter -R INPUT 1 -s 192.168.153.71 -j DROP:删除 # iptables -vnL --line-numbers # iptables -t filter -D INPUT 1 或者 # iptables -t filter -D INPUT -s 192.168.153.71 -j DROP:删除
[!] -d, –destination address[/mask][,…] : 检查报文中的目标IP地址是否符合此处指定的地址或地址范围;
[!] -p, –protocol protocol : 检查报文中传输层的协议类型,支持tcp, udp, udplite, icmp, icmpv6,esp, ah, sctp, mh,或者 "all";
# iptables -t filter -A INPUT -s 192.168.153.0/24 -d 192.168.153.128 -t tcp -j ACCEPT :tcp协议,在该网段可以放心 # iptables -t filter -A INPUT -s 192.168.153.0/24 -d 192.168.153.128 -t icmp p -j ACCEPT :可以ping通了
[!] -i, –in-interface name : 检查报文进入本机时的接口是否符合本处指定的接口,仅适合 INPUT, FORWARD and PREROUTING ;
# iptables -t filter -i eth0 -I INPUT -s 192.168.153.71 -j REJECT :从eth0网卡进入
[!] -o, –out-interface name : 检查报文即将离开本机时经由的接口是否符合本处指定的接口;FORWARD, OUTPUT and POSTROUTING;
# iptables -t filter -o eth0 -I OUTPUT -d 192.168.153.71 -j REJECT
-m, –match match : 显式指明要使用的扩展模块;
-j, –jump target : 跳转目标;
扩展匹配条件
隐式扩展:不用-m选项明确给出要使用的扩展机制的扩展;此处主要指使用-p {tcp|udp|icmp}给定协议后可直接对给定的协议所进行的扩展
-p tcp:可直接使用tcp协议对应的扩展选项;
[!] –source-port,–sport port[:port]:匹配报文中的传输层的源端口;可给出多个连接的端口;
# iptables -t filter -A INPUT -s 192.168.153.0/24 -d 192.168.153.131 -p tcp --dport 22 -j ACCEPT # iptables -t filter -A INPUT -s 192.168.153.0/24 -d 192.168.153.131 -p tcp --dport 22:26 -j ACCEPT #22到26端口
[!] –destination-port,–dport port[:port]:匹配报文中的传输层的目标端口;可给出多个连接的端口;
# iptables -t filter -A INPUT -s 192.168.153.0/24 -d 192.168.153.131 -p tcp --dport 22 -j ACCEPT # iptables -t filter -A OUTPUT -d 192.168.153.0/24 -s 192.168.153.131 -p tcp --sport 22 -j ACCEPT
[!] –tcp-flags mask comp
SYN,ACK,FIN,RST,URG,PSH;
mask:要检查的标志位列表,以逗号分隔,例如SYN,ACK,FIN,RST
comp:mask给定的众标志位中,其值必须为1的标志位列表,余下的必须为0;
–tcp-flags SYN,ACK,FIN,RST SYN #检查四个位SYN,ACK,FIN,RST 其中 SYN必须为1,ACK,FIN,RST都为0 :为tcp握手的第一次
[!] –syn:相当于–tcp-flags SYN,ACK,FIN,RST SYN
-p udp:可直接使用udp协议对应的扩展选项;
[!] –source-port,–sport port[:port]:匹配报文中的传输层的源端口;可给出多个连接的端口;
[!] –destination-port,–dport port[:port]:匹配报文中的传输层的目标端口;可给出多个连接的端口;
# iptables -t filter -A INPUT -s 192.168.153.0/24 -d 192.168.153.131 -p udp --dport 139 -j ACCEPT
-p icmp:可直接使用icmp协议对应的扩展选项;
[!] –icmp-type {type[/code]|typename}
–icmp-type 0/0:匹配对ping请求的响应报文
–icmp-type 8/0:匹配ping请求报文
# 对方ping你 # iptables -t filter -A INPUT -s 192.168.153.128 -d 192.168.153.131 -p icmp --icmp-type 8/0 -j ACCEPT # tcpdump -i eno33554960 -nn icmp ;抓包查看 # iptables -t filter -A OUTPUT -d 192.168.153.128 -s 192.168.153.131 -p icmp --icmp-type 0/0 -j ACCEPT # tcpdump -i eno33554960 -nn icmp ;抓包查看 # 你ping对方 # iptables -t filter -A OUTPUT -s 192.168.153.131 -p icmp --icmp-type 8 -j ACCEPT # iptables -t filter -A INPUT -d 192.168.153.131 -p icmp --icmp-type 0 -j ACCEPT
显式扩展:必须使用-m选项给出matchname的扩展,而且有些扩展都还存在专用选项;man iptables-extensions
1、multiport
以离散或连续的方式定义的多端口匹配条件; Up to 15 ports can be specified.
[!] –source-ports,–sports port[,port|,port:port]…:指定多个源端口;
[!] –destination-ports,–dports port[,port|,port:port]…:指定多个目标端口;
[!] –ports port[,port|,port:port]…:匹配此处指定的源或目标端口;
#iptables -A INPUT -p tcp -m multiport --ports 22,21,23,80 -j ACCEPT #iptables -A OUTPUT -p tcp -m multiport --ports 21:23,80 -j ACCEPT
2、iprange
以连续的ip地址范围指明多地址匹配条件;
[!] –src-range from[-to]
[!] –dst-range from[-to]
#from to 都是指的IP # iptables -A OUTPUT -p tcp -m iprange --src-range 192.168.153.20-192.168.153.100 -j ACCEPT
3、string
对报文中的应用层数据做字符串匹配检测;
[!] –string pattern
[!] –hex-string pattern
–algo {bm|kmp}:字符串匹配检查算法;
–from offset
–to offset
# iptables -I OUTPUT -p tcp -m string --string "sex" --algo kmp -j REJECT #报文中有sex字符的全部拒绝出站
4、time
根据报文到达的时间与指定的时间范围进行匹配度检测;
–datestart YYYY[-MM[-DD[Thh[:mm[:ss]]]]]
–datestop YYYY[-MM[-DD[Thh[:mm[:ss]]]]]
–timestart hh:mm[:ss]
–timestop hh:mm[:ss]
[!] –monthdays day[,day…]
[!] –weekdays day[,day…]
# iptables -I INPUT -p tcp --dport 23 -m time --timestart 07:47 -j REJECT #从7:47开始拒绝telnet连接 # iptables -I INPUT -p tcp --dport 23 -m time --timestart 07:47 --timestop 05:22 -j REJECT # iptables -I INPUT -p tcp --dport 23 -m time --datestart 2016-10-20T07:55 --datestop 2016-10-20T07:56 -j REJECT
5、connlimit
根据每客户端IP做并发连接数限制,即限制单IP可同时发起连接请求;
–connlimit-upto n:连接数小于等于阈值;
–connlimit-above n:连接数超出阈值;
# iptables -I INPUT -p icmp -m connlimit --connlimit-above 2 -j REJECT
6、limit
基于收发报文的速率进行匹配;
–limit rate[/second|/minute|/hour|/day]
–limit-burst number
# iptables -A INPUT -p icmp --icmp-type 8 -m limit --limit-burst 3 --limit 20/minute -j ACCEPT
7、state
状态检测:连接追踪机制(conntrack)
NEW:新连接
ESTABLISHED:已建立的连接
RELATED:相关联的连接
INVALID:无法识别的连接
UNTRACKED:未被追踪连接;
相关的内核模块:
nf_conntrack
nf_conntrack_ipv4
nf_conntrack_ftp
modinfo nf_conntrack
modprobe nf_conntrack_ftp
追踪到的连接:/proc/net/nf_conntrack文件中;
能追踪的最大连接数量定义在:/proc/sys/net/nf_conntrack_max
建议调整至足够大;
不同的协议的连接追踪时长:
/proc/sys/net/netfilter/
[!] –state state
# iptables -I INPUT -m state --state ESTABLISHED -j ACCEPT # iptables -A OUTPUT -m state --state ESTABLISHED -j ACCEPT # iptables -A INPUT -p tcp -m multiport --dports 22,23,80 -m state --state NEW,ESTABLISHED -j ACCEPT # iptables -A OUTPUT -m state --state ESTABLISHED -j ACCEPT # iptables -A INPUT -p icmp --icmp-type 8 -m state --state NEW,ESTABLISHED -j ACCEPT #ping命令 # iptables -A OUTPUT -p tcp --dport 80 -j ACCEPT
如何开放被动模式的ftp服务:
(1) 装载追踪ftp协议的模块;
# modprobe nf_conntrack_ftp
(2) 放行入站命令连接
# iptables -A INPUT -d SERVER_IP -p tcp --dport 21 -m state --state NEW,ESTABLISHED -j ACCEPT
(3) 放行入站数据连接
# iptables -A INPUT -d SERVER_IP -p tcp -m state --state RELATED,ESTABLISHED -j ACCEPT
(4) 放行出站的ESTABLISHED连接
# iptabls -A OUTPUT -s SERVER_IP -m state --state ESTABLISHED -j ACCEPT
处理动作(跳转目标):
-j tagetname [per-target-options]
简单target:
ACCEPT,DROP
扩展target:
REJECT:
–reject-with type
icmp-net-unreachable, icmp-host-unreachable, icmp-port-unreachable, icmp-proto-unreach‐able, icmp-net-prohibited, icmp-host-prohibited, or icmp-admin-prohibited,默认为icmp-port-unreachable;
LOG:
Turn on kernel logging of matching packets.
–log-level level
–log-prefix prefix:日志信息的前导信息;
# iptables -I INPUT 2 -p tcp --dport 22 -j LOG --log-prefix "openssh from kernel: " # cat /var/log/message
保存和载入规则:
保存:iptables-save > /PATH/TO/SOME_RULE_FILE
重载:iptables-restore < /PATH/FROM/SOME_RULE_FILE
-n, –noflush:不清除原有规则
-t, –test:仅分析生成规则集,但不予提交;
注意:重载文件中的规则,会清除已有规则;
CentOS 6:
保存规则:service iptables save
保存规则于/etc/sysconfig/iptables,保存操作会清除文件中原有的内容;
# vim /etc/sysconfig/iptables-config # IPTABLES_MODULES="nf_conntrack_ftp"
重载规则:server iptables restart
默认重载/etc/sysconfig/iptables文件中的规则
脚本配置文件:/etc/sysconfig/iptables-config
用于指明要装载的模块;
CentOS 7开机自动生效规则:
(1) firewalld服务;
(2) shell脚本,直接记录iptables命令;
(3) 自定义unit file或init script;
规则优化的思路:
(1) 优先放行双方向状态为ESTABLISHED的报文;
(2) 服务于不同类别的功能的规则,匹配到报文可能性更大的放前面;
(3) 服务于同一类别的功能的规则,匹配条件较为严格的放前面;
(4) 设置默认策略:白名单机制
(a) 可使用iptables -P设定默认策略;
(b) 建议在规则链的最后定义规则做为默认策略;
练习:基于状态放行telnet, ftp, ssh, http, samba, icmp等服务;
(1) 对本机的ping请求每分钟不得超出20个;
(2) 每客户端对本机的ssh的并发连接数不得超过3个;
(3) 本机的telnet服务仅允许工作时间内访问;
iptables -F iptables -X modprobe nf_conntrack_ftp iptables -A INPUT -m state --state ESTABLISHED -j ACCEPT iptables -A INPUT -m state --state NEW -j ACCEPT iptables -A INPUT -m state --state RELATED -j ACCEPT iptables -I INPUT -p icmp --icmp-type 8 -m limit --limit-burst 3 --limit 40/minute -j REJECT iptables -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 3 -j REJECT iptables -I INPUT -p tcp --dport 23 -m time --timestart 18:00 --timestop 09:00 -j REJECT iptables -I INPUT -p tcp --dport 23 -m time --weekdays 6,7 -j REJECT iptables -A INPUT -j REJECT
iptables -F iptables -X modprobe nf_conntrack_ftp iptables -A INPUT -p tcp -m state --state ESTABLISHED -j ACCEPT iptables -A INPUT -p udp -m state --state ESTABLISHED -j ACCEPT iptables -A INPUT -p tcp -m multiport --dport 21:23,139,445,80 -m state --state NEW -j ACCEPT iptables -A INPUT -p udp -m multiport --dport 138,137 -m state --state NEW -j ACCEPT iptables -A INPUT -m state --state RELATED -j ACCEPT iptables -A INPUT -p icmp -m limit --limit 20/minute --limit-burst 2 -j ACCEPT iptables -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 3 -j REJECT iptables -I INPUT -p tcp --dport 23 -m time --timestart 18:00 --timestop 09:00 -j REJECT iptables -I INPUT -p tcp --dport 23 -m time --weekdays 6,7 -j REJECT iptables -A INPUT -j REJECT
iptables/netfilter网络防火墙:
添加规则于FORWARD链,注意几个问题:
(1) 请求和响应报文均会经由FORWARD链,要注意规则的方向性;
第一条:iptables -I FORWARD -m state –state ESTABLISHED,RELATED -j ACCEPT
# iptables -I FORWARD -s 192.168.153.0/24 -p icmp -m state --state NEW -j ACCEPT # iptables -I FORWARD -s 192.168.153.0/24 -m state --state ESTABLISHED -j ACCEPT # iptables -A FORWARD -s 192.168.153.0/24 -p tcp -m multiport --sports 21:23,80,139,445 -m state --state NEW -j ACCEPT
(2) 如果可以启用conntrack机制,注意网关主机所能够追踪的连接数的最大数量要符合需要
NAT:(可以和filter共同使用)
源地址转换:SNAT,POSTROUTING
静态转换:
动态转换:
目标地址转换:DNAT,PREROUTING
PAT:Port Address Translation
SNAT:
This target is only valid in the nat table, in the POSTROUTING and INPUT chains, and user-defined chains which are only called from those chains.
–to-source [ipaddr[-ipaddr]]
# iptables -t nat -A POSTROUTING -s 192.168.153.0/24 -j SNAT --to-source 10.1.249.79
MASQUERADE:
This target is only valid in the nat table, in the POSTROUTING chain. It should only be used with dynamically assigned IP (dialup) connections: if you have a static IP address, you should use the SNAT target.
DNAT:
This target is only valid in the nat table, in the PREROUTING and OUTPUT chains, and user-defined chains which are only called from those chains.
–to-destination [ipaddr[-ipaddr]][:port[-port]]
# iptables -t nat -A PREROUTING -d 10.1.249.79 -p tcp --dport 22 -j DNAT --to-destination 192.168.153.131 # iptables -t nat -I PREROUTING -d 10.1.249.79 -p tcp --dport 2002 -j DNAT --to-destination 192.168.153.131:22
REDIRECT:端口映射
This target is only valid in the nat table, in the PREROUTING and OUTPUT chains, and user-defined chains which are only called from those chains.
–to-ports port[-port]
# iptables -t nat -A PREROUTING -d 192.168.153.131,192.168.153.132 -p tcp --dport 80 -j REDIRECT --to-port 8080 #服务器两个ip,开的是8080端口,但是对外称是80端口
RETURN:返回
自定义规则链:
# iptables -N web_in # iptables -A web_in -d 192.168.153.131 -p tcp --dport 80 -j ACCEPT # iptables -A FORWARD -j web_in
练习:INPUT和OUTPUT默认策略为DROP;
1、限制本地主机的web服务器在周一不允许访问;新请求的速率不能超过100个每秒;web服务器包含了admin字符串的页面不允许访问;web服务器仅允许响应报文离开本机;
2、在工作时间,即周一到周五的8:30-18:00,开放本机的ftp服务给172.16.0.0网络中的主机访问;数据下载请求的次数每分钟不得超过5个;
3、开放本机的ssh服务给172.16.x.1-172.18.x.100中的主机,x为你的学号,新请求建立的速率一分钟不得超过2个;仅允许响应报文通过其服务端口离开本机;
4、拒绝TCP标志位全部为1及全部为0的报文访问本机;
# iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP # iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
5、允许本机ping别的主机;但不开放别的主机ping本机;
iptables -A INPUT 1 -p icmp --icmp-type 8 -j REJECT
练习:判断下述规则的意义:
# iptables -N clean_in #新建一个链 # iptables -A clean_in -d 255.255.255.255 -p icmp -j DROP #将拒绝对255.255.255.255 的icmp规则添加到clean_in链上 # 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.100.7 -j RETURN
# iptables -A INPUT -d 172.16.100.7 -j clean_in
# iptables -A INPUT -i lo -j ACCEPT # iptables -A OUTPUT -o lo -j ACCEPT
# iptables -A INPUT -i eth0 -m multiport -p tcp --dports 53,113,135,137,139,445 -j DROP # iptables -A INPUT -i eth0 -m multiport -p udp --dports 53,113,135,137,139,445 -j DROP # iptables -A INPUT -i eth0 -p udp --dport 1026 -j DROP # iptables -A INPUT -i eth0 -m multiport -p tcp --dports 1433,4899 -j DROP
# iptables -A INPUT -p icmp -m limit --limit 10/second -j ACCEPT
补充:利用iptables的recent模块来抵御DOS攻击: 22,建立一个列表,保存有所有访问过指定的服务的客户端IP
ssh: 远程连接,
iptables -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 3 -j DROP iptables -I INPUT -p tcp --dport 22 -m state --state NEW -m recent --set --name SSH iptables -I INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 300 --hitcount 3 --name SSH -j LOG --log-prefix "SSH Attach: " iptables -I INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 300 --hitcount 3 --name SSH -j DROP
1.利用connlimit模块将单IP的并发设置为3;会误杀使用NAT上网的用户,可以根据实际情况增大该值;
2.利用recent和state模块限制单IP在300s内只能与本机建立2个新连接。被限制五分钟后即可恢复访问。
下面对最后两句做一个说明:
1.第二句是记录访问tcp 22端口的新连接,记录名称为SSH
–set 记录数据包的来源IP,如果IP已经存在将更新已经存在的条目
2.第三句是指SSH记录中的IP,300s内发起超过3次连接则拒绝此IP的连接。
–update 是指每次建立连接都更新列表;
–seconds必须与–rcheck或者–update同时使用
–hitcount必须与–rcheck或者–update同时使用
3.iptables的记录:/proc/net/xt_recent/SSH
也可以使用下面的这句记录日志:
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"
CentOS 6:
http://ftp.redhat.com/redhat/linux/enterprise/6Server/en/os/SRPMS/
layer7:第三方扩展;
iptables实现七层访问过滤:
模块:layer7
识别应用层协议
iptables/netfilter
iptables -m state,
netfilter state
对内核中的netfilter,打补丁layer7,重新编译内核
对iptables打补丁,补上layer7模块,重新iptables
diff/patch:文本操作工具
diff是Unix系统的一个很重要的工具程序。它用来比较两个文本文件的差异,是代码版本管理的核心工具之一。其用法非常简单:
# diff <变动前的文件> <变动后的文件>
由于历史原因,diff有三种格式:
* 正常格式(normal diff)
* 上下文格式(context diff)
* 合并格式(unified diff)
1、正常格式的diff
例如,对file1(变动前的文件)和file2(变动后的文件)进行比较可使用如下命令:
# diff file1 file2
显示结果中,第一行是一个提示,用来说明变动位置。它分成三个部分:前面的数字,表示file1的第n行有变化;中间的"c"表示变动的模式是内容改变(change),其他模式还有"增加"(a,代表addition)和"删除"(d,代表deletion);
2、上下文格式的diff
上个世纪80年代初,加州大学伯克利分校推出BSD版本的Unix时,觉得diff的显示结果太简单,最好加入上下文,便于了解发生的变动。因此,推出了上下文格式的diff。它的使用方法是加入-c选项(即context)。
# diff -c f1 f2
结果分成四个部分。第一部分的两行,显示两个文件的基本情况:文件名和时间信息,"***"表示变动前的文件,"—"表示变动后的文件。第二部分是15个星号,将文件的基本情况与变动内容分割开。第三部分显示变动前的文件,即file1。
另外,文件内容的每一行最前面,还有一个标记位。如果为空,表示该行无变化;如果是感叹号(!),表示该行有改动;如果是减号(-),表示该行被删除;如果是加号(+),表示该行为新增。
第四部分显示变动后的文件,即file2。
3、合并格式的diff
如果两个文件相似度很高,那么上下文格式的diff,将显示大量重复的内容,很浪费空间。1990年,GNU diff率先推出了"合并格式"的diff,将f1和f2的上下文合并在一起显示。
它的使用方法是加入u参数(代表unified)。
# diff -u f1 f2
其结果的第一部分,也是文件的基本信息。"—"表示变动前的文件,"+++"表示变动后的文件。第二部分,变动的位置用两个@作为起首和结束。第三部分是变动的具体内容。
除了有变动的那些行以外,也是上下文各显示3行。它将两个文件的上下文,合并显示在一起,所以叫做"合并格式"。每一行最前面的标志位,空表示无变动,减号表示第一个文件删除的行,加号表示第二个文件新增的行。
diff
-u
patch
尽管并没有指定patch和diff的关系,但通常patch都使用diff的结果来完成打补丁的工作,这和patch本身支持多种diff输出文件格式有很大关系。patch通过读入patch命令文件(可以从标准输入),对目标文件进行修改。通常先用diff命令比较新老版本,patch命令文件则采用diff的输出文件,从而保持原版本与新版本一致。
patch的标准格式为
patch [options] [originalfile] [patchfile]
如果patchfile为空则从标准输入读取patchfile内容;如果originalfile也为空,则从patchfile(肯定来自标准输入)中读取需要打补丁的文件名。因此,如果需要修改的是目录,一般都必须在patchfile中记录目录下的各个文件名。绝大多数情况下,patch都用以下这种简单的方式使用:
patch命令可以忽略文件中的冗余信息,从中取出diff的格式以及所需要patch的文件名,文件名按照diff参数中的"源文件"、"目标文件"以及冗余信息中的"Index:"行中所指定的文件的顺序来决定。
-p参数决定了是否使用读出的源文件名的前缀目录信息,不提供-p参数,则忽略所有目录信息,-p0(或者-p 0)表示使用全部的路径信息,-p1将忽略第一个"/"以前的目录,依此类推。如/usr/src/linux-2.4.15/Makefile这样的文件名,在提供-p3参数时将使用linux-2.4.15/Makefile作为所要patch的文件。
patch
-p
-R
mockbuild
总结:操作步骤
1、获取并编译内核
# useradd mockbuild # rpm -ivh kernel-2.6.32-431.5.1.x86_64.el6.src.rpm # cd rpmbuild/SOURCES # tar linux-2.6.32-*.tar.gz -C /usr/src # cd /usr/src # ln -sv
2、给内核打补丁
# tar xf netfilter-layer7-v2.23.tar.bz2 # cd /usr/src/linux # patch -p1 < /root/netfilter-layer7-v2.23/kernel-2.6.32-layer7-2.23.patch # cp /boot/config-* .config # make menuconfig
按如下步骤启用layer7模块
Networking support → Networking Options →Network packet filtering framework → Core Netfilter Configuration
<M> “layer7” match support
3、编译并安装内核
# make # make modules_install # make install
4、重启系统,启用新内核
5、编译iptables
# tar xf iptables-1.4.20.tar.gz # cp /root/netfilter-layer7-v2.23/iptables-1.4.3forward-for-kernel-2.6.20forward/* /root/iptables-1.4.20/extensions/ # cp /etc/rc.d/init.d/iptales /root # cp /etc/sysconfig/iptables-config /root # rpm -e iptables iptables-ipv6 --nodeps # ./configure --prefix=/usr --with-ksource=/usr/src/linux # make && make install # cp /root/iptables /etc/rc.d/init.d # cp /root/iptables-config /etc/sysconfig
6、为layer7模块提供其所识别的协议的特征码
# tar zxvf l7-protocols-2009-05-28.tar.gz # cd l7-protocols-2009-05-28 # make install
7、如何使用layer7模块
ACCT的功能已经可以在内核参数中按需启用或禁用。此参数需要装载nf_conntrack模块后方能生效。
net.netfilter.nf_conntrack_acct = 1
l7-filter uses the standard iptables extension syntax
# iptables [specify table & chain] -m layer7 –l7proto [protocol name] -j [action]
# iptables -A FORWARD -m layer7 --l7proto qq -j REJECT
编译内核:
make menuconfig make -j # make modules_install make install
清理内核源码树:
提示:xt_layer7.ko依赖于nf_conntrack.ko模块