iptables之iptables表、链、规则 、匹配模式、扩展模块、连接追踪模块(一)

109 篇文章 10 订阅
7 篇文章 1 订阅
本文详细介绍了iptables的各个部分,包括链的概念、表的功能及其关系,规则的增删改查,以及iptables的匹配模式、扩展模块如multiport、iprange、string、time等,并展示了如何使用这些功能进行网络访问控制和连接追踪。
摘要由CSDN通过智能技术生成

目录

一、iptables的链

二、iptables的表

2.1表的功能

2.2 表与链的关系

2.3 问题

三、iptables规则的增、删、改、查、存

3.1 iptables操作举例

1.如何查看

2.如何添加规则:

3.如何修改规则:

4.如何清空计数器:

5.备份规则;

6.清空规则:

7.恢复规则:

8.永久生效:

四、iptables匹配模式与案例

4.1 iptables匹配参数

4.2 示例问题

五、Iptables扩展匹配与案例

5.1 Iptables扩展模块-multiport

5.2 lptables扩展模块-iprange

5.3 Iptables扩展模块string

5.4 lptables扩展模块-time

5.5 Iptables扩展模块icmp

5.6 Iptables扩展模块connlimit

5.7 Iptables扩展模块limit

5.8 Iptables扩展模块tcp-flags

六、iptables连接追踪模块案例


一、iptables的链

1.请求到达本机: PREROUTING --> INPUT --> Local Process (本机)
2.请求经过本机: PREROUTING --> FORWARD --> POSTROUTING
3.请求从本机发出:local Process(本机) --> OUTPUT --> POSTROUTING

二、iptables的表


2.1表的功能

平常主要用的是filter表和nat表
    filter: 过滤,网络安全;
    nat: 路由;地址转换;

2.2 表与链的关系

简单解释:
注意图中的向下箭头,这是访问顺序

1.请求到达本机: PREROUTING --> INPUT --> Local Process (本机)
2.请求经过本机: PREROUTING --> FORWARD --> POSTROUTING
3.请求从本机发出:local Process(本机) --> OUTPUT --> POSTROUTING

2.3 问题

问题1:来自`10.0.0.1`的地址,访问本机的`web`服务请求不允许,应该在哪个表的哪个链上设定规则?
答:filter表中的INPUT链上设定规则。

问题2:所有由本机发往`10.0.0.0/24`网段的`TCP`服务都不允许?
答:filter表中的OUTPUT链上设定规则。

问题3:所有来自己本地内部网络的主机,向互联网发送`web`服务器请求都允许?
答:filter表中的FORWARD链设定规则。
c  -->  iptables  --> s



三、iptables规则的增、删、改、查、存

iptables [-t 表名] 选项 [链名] [规则] [动作]


3.1 iptables操作举例


默认不同-t指定表,则默认为filter表。

1.如何查看

-L:查看 -n:不解析 -v 详细 --line-numbers 编号

iptables -L -n -v --line-numbers
2.如何添加规则:

禁止10.0.0.10 ping 10.0.0.200

-I:插入Insert

iptables -t filter -I INPUT -p icmp -j REJECT
iptables -L -n

随后ping 10.0.0.200看效果 , REJECT还会有反馈内容。 如果是DROP则,没有反馈内容


3.如何修改规则:


-R:修改 需要指定规则的编号

iptables -t filter -R INPUT 1 -p icmp -j DROP


4.如何清空计数器:
iptables -Z


5.备份规则;
iptables-save > /etc/iptables.rule


6.清空规则:
iptables -F    #等价于 iptables -t filter -F
iptables -t nat -F


7.恢复规则:
iptables-restore < /etc/iptables.rule


8.永久生效:

将 iptables-restore < /etc/iptables.rule 加入开机自启动文件 /etc/rc.local


四、iptables匹配模式与案例


4.1 iptables匹配参数

4.2 示例问题


1 、 仅允许10.0.0.1 访问 10.0.0.200 服务器的80端口、其他地址全部拒绝。
filter 表;
INPUT;

iptables -t filter -I INPUT -p tcp -s 10.0.0.10 -d 10.0.0.200 --dport 80 -j DROP

#INPUT链默认是接受  policy ACCEPT 。 当走完Input链下的所有规则,一旦符合则不会往下走,如果都不符合的,则走默认规则 policy ACCEPT。


2 、 仅允许10.0.0.1 访问 10.0.0.200 服务器的22端口、其他地址全部拒绝。

iptables -t filter -I INPUT -s 10.0.0.1 -d 10.0.0.200 -p tcp --dport 22 -j ACCEPT
iptables -t filter -A INPUT -d 10.0.0.200 -p tcp --dport 22 -j DROP


 


3 、所有来访问本机的协议,属于TCP协议的我们通通都放行;

iptables -t filter -I INPUT -p tcp -j ACCEPT
iptables -t filter -A INPUT -j DROP

#测试方法: 时间同步chrony有走udp协议 、 ping走icmp协议


4、凡是由本机发出的TCP协议报文,都允许出去,其他协议不行;

iptables -t filter -I OUTPUT -p tcp -j ACCEPT
iptables -t filter -A OUTPUT -j DROP



5、禁止其他主机从eth0像本机发送ping请求

iptables -t filter -I INPUT -i eth0 -p icmp -j DROP


 


6、允许从本机发送ping请求,其他任何协议都不允许; 【执行完后 ssh会掉线】【注意!】

iptables -t filter -I OUTPUT -p icmp -j ACCEPT
iptables -t filter -A OUTPUT -j DROP



五、Iptables扩展匹配与案例


5.1 Iptables扩展模块-multiport


multiport模块可以添加多个不连续的端口;
示例 :10.0.0.10 访问本机20、21、80、443允许通过;

iptables -I INPUT -s 10.0.0.10 -d 10.0.0.200 -p tcp -m multiport --dport 20:22,80,443,873 -j ACCEPT


 


5.2 lptables扩展模块-iprange


iprange模块可以指定”一段连续的IP地址范围";用于匹配报文的源地址或者目标地址,iprange扩展模块中有两个扩展匹配条件可以使用。

示例:10.0.0.5-10.0.0.10地址段访问ping本机,则丢弃;

iptables -t filter -I INPUT -p icmp -m iprange --src-range 10.0.0.5-10.0.0.10 --dst-range 10.0.0.200 -j DROP


 

5.3 Iptables扩展模块string


●string模块,可以指定要匹配的字符串,如果报文中包含对应的字符串,则符合匹配条件。
。–algo {bm|kmp}:字符匹配的查询算法;
。[!] --string pattern: 字符匹配的字符串;

情景准备:

yum install httpd -y
echo "hello" > /var/www/html/index.html
echo " video " > /var/www/html/test.html
systemctl start httpd



情景1示例:应用返回的报文中包含字符"video",我们就丢弃当前报文,其余正常通过。

#OUTPUT链
iptables -t filter -I OUTPUT -p tcp -m string --algo kmp --string "video" -j DROP


 

情景2 任意时间段都拒绝,用户请求iptables节点,如果请求中包含 “jd.oldxu.net” 则拒绝;

iptables -t filter -F
iptables -t filter -I INPUT -p tcp -m string --algo kmp --string "jd.oldxu.net" -j DROP



5.4 lptables扩展模块-time


time模块,可以根据时间段区匹配报文,如果报文到达的时间在指定的时间范围内,则符合匹配条件。
。–timestart hh:mm[:ss]:开始时间
。–timestop hh:mm[:ss]: 结束时间
。[!] --monthdays day[, day…]:指定-一个月的某- 天
。[!] --weekdays day[, day…]: 指定周一-到周天
。–kerneltz:使用内核时区而不是UTC时间 (所以要减8小时)

示例1:拒绝每天8:30~12:30 (00: 3004:30)、13:3018:30 (05:30~10:30), 任何主机发送icmp协
议;

#utc时间,比北京时间快8小时,所以要减8小时。
iptables -t filter -I INPUT -p icmp -m time --timestart 00:30 --timestop 04:30 -j DROP
iptables -t filter -I INPUT-p icmp -m time --timestart 05:30 --timestop 10:30 -j DROP



限制用户在上班时间段访问jd、qq等资源,其他时间可以正常放行:
time和string一起用: (路由器)
限制早上:8:00 ~ 12:00 (00:00-04:00)
限制下午:14:00 ~ 18:00 (06:00-10:00)

#utc时间,比北京时间快8小时,所以要减8小时。
#网络策略   FORWARD链
#上午
iptables -t filter -I FORWARD -p tcp -m string --string "qq" --algo kmp -m time --timestart 00:00 --timestop 04:00 -j DROP
iptables -t filter -I FORWARD -p tcp -m string --string "tb" --algo kmp -m time --timestart 00:00 --timestop 04:00 -j DROP#下午
iptables -t filter -I FORWARD -p tcp -m string --string "qq" --algo kmp -m time --timestart 06:00 --timestop 10:00 -j DROP
iptables -t filter -I FORWARD -p tcp -m string --string "tb" --algo kmp -m time --timestart 06:00 --timestop 10:00 -j DROP


 

#utc时间,比北京时间快8小时,所以要减8小时。
#主机策略   INPUT链
#上午
iptables -t filter -I  INPUT -p tcp -m string --string "qq" --algo kmp -m time --timestart 00:00 --timestop 10:00 -j DROP
iptables -t filter -I  INPUT -p tcp -m string --string "tb" --algo kmp -m time --timestart 00:00 --timestop 04:00 -j DROP

#下午:                 
iptables -t filter -I  INPUT -p tcp -m string --string "qq" --algo kmp -m time --timestart 06:00 --timestop 10:00 -j DROP
iptables -t filter -I  INPUT -p tcp -m string --string "tb" --algo kmp -m time --timestart 06:00 --timestop 10:00 -j DROP



5.5 Iptables扩展模块icmp


· icmp模块:可以控制其他主机无法ping同本机,但本机可以ping同其他主机;
·默认情况当禁止ping后,其他主机无法ping通本主机,本主机也无法ping通其他主机,现需要本主机可以ping通其他主机,而其他主机依然无法ping同本主机。
语法: [!] --icmp-type {type[/code] / typename}
指定ICMP类型,echo-request(8请求)、echo-reply (0回应)

iptables -t filter -I INPUT -p icmp -m icmp --icmp-type "echo-request" -j REJECT


5.6 Iptables扩展模块connlimit


connlimit扩展模块,限制每个客户端P地址到服务器的并行连接数。
–connlimit-upto n:如果现有连接数小于或等于n,则匹配。
–connlimit-above n:如果现有连接数大于n,则匹配。

#模拟网站和大流量访问
yum install httpd -y
gcc f_connect.c
./a.out 10.0.0.200


#限制同一IP的并发连接数,超过10个并发连接数则拒绝。
iptabels -t filter -I INPUT -p tcp --dport 80 -m connlimit --connlimit-above 10 -j DROP



5.7 Iptables扩展模块limit


5.8 Iptables扩展模块tcp-flags


六、iptables连接追踪模块案例


相关概念:

比如记录这些:

conntrack:
查看链接追踪详情: /proc/net/nf_conntrack
调整链接追踪大小: echo "100000" > /proc/sys/net/nf_conntrack_max


应用场景:

  1. 正常情况下服务器的80端口不会主动连接其他服务器,如果出现了80端口连接其他服务器,那么说明出现了
  2. 异常行为,或者可以理解为中了木马程序病毒。反弹端口型木马
  3. 如果关闭80端口的响应报文,就会造成请求进来无法响应;如果开放80端口则又会出现异常行为。
  4. 所以我们需要对80端口做连接追踪限制,凡事从80端口出去的就必须是对某个请求的响应,也就是说通过80
  5. 端口出去的状态必须是ESTABLISHED,不能是NEW


1、允许接收远程主机像本机发送的SSH与HTTP请求(NEW、 ESTABLISHED)
2、同时也仅允许本机像其他主机回应SSH以及HTTP响应(ESTABLISHED)
3、但不允许本机通过22、80端口主动向外发起连接。
 

#INPUT链
iptables -t filter -I INPUT -p tcp -m multiport --dport 80,22 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -t filter -A INPUT -p tcp -m multiport --dport 80,22 -j DROP

#OUTPUT链
iptables -t filter -I OUTPUT -p tcp -m multiport --sport 22,80 -m state --state ESTABLISHED -j ACCEPT
iptables -t filter -A OUTPUT -p tcp -m multiport --sport 22,80 -j DROP

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值