Linux系统下使用iptables管理防火墙

一、iptables基础信息

1.介绍
iptables命令是Linux上常用的防火墙软件,是netfilter项目的一部分

防火墙在做信息包过滤决定时,有一套遵循和组成的规则,这些规则存储在专用的信 息包过滤表中,而这些表集成在 Linux 内核中。在信息包过滤表中,规则被分组放在我们所谓的链(chain)中。而netfilter/iptables IP 信息包过滤系统是一款功能强大的工具,可用于添加、编辑和移除规则

netfilter 组件也称为内核空间(kernelspace),是内核的一部分,由一些信息包过滤表组成,这些表包含内核用来控制信息包过滤处理的规则集。
2.netfilter表、链的信息
这里写图片描述
- 处理流入的数据包(INPUT);
- 处理流出的数据包(OUTPUT);
- 处理转发的数据包(FORWARD);
- 在进行路由选择前处理数据包(PREROUTING);
- 在进行路由选择后处理数据包(POSTROUTING)。
- Raw:高级功能,如:网址过滤。
- mangle:数据包修改(QOS),用于实现服务质量。
- nat:地址转换,用于网关路由器。
- filter:包过滤,用于防火墙规则。

3.基本命令

iptables(选项)(参数)

-t<表>:指定要操纵的表;
-A:向规则链中添加条目;
-D:从规则链中删除条目;
-i:向规则链中插入条目;
-R:替换规则链中的条目;
-L:显示规则链中已有的条目;
-F:清楚规则链中已有的条目;
-Z:清空规则链中的数据包计算器和字节计数器;
-N:创建新的用户自定义规则链;
-P:定义规则链中的默认目标;
-h:显示帮助信息;
-p:指定要匹配的数据包协议类型;
-s:指定要匹配的数据包源ip地址;
-j<目标>:指定要跳转的目标;
-i<网络接口>:指定数据包进入本机的网络接口;
-o<网络接口>:指定数据包要离开本机所使用的网络接口。

二、iptables命令的使用

1.打开服务

[root@client ~]# systemctl stop firewalld.service 
[root@client ~]# systemctl disable firewalld.service 
rm '/etc/systemd/system/basic.target.wants/firewalld.service'
rm '/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service'
[root@client ~]# systemctl start iptables
[root@client ~]# systemctl enable iptables.service 

2.清空规则

[root@client ~]# iptables -nL  ##查看当前防火墙策略信息
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
[root@client ~]# iptables -F  ##清空信息
[root@client ~]# iptables -nL ##再次查看策略,清空完成
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
[root@client ~]# service iptables save  ##保存规则信息
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]

这里写图片描述
iptables的配置文件

[root@client ~]# vim  /etc/sysconfig/iptables

这里写图片描述
所有主机都可以访问105的apache
这里写图片描述
这里写图片描述
3.设置为拒绝模式

[root@client ~]# iptables  -A  INPUT  -p tcp  --dport  80  -j REJECT  ##访问80端口为拒绝模式
[root@client ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
REJECT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80 reject-with icmp-port-unreachable

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 

这里写图片描述
访问测试:均不能访问
这里写图片描述
这里写图片描述
4.添加指定策略
使62主机可以访问80端口

[root@client ~]# iptables  -A  INPUT  -s 172.25.254.62 -p tcp --dport 80 -j ACCEPT
[root@client ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
REJECT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80 reject-with icmp-port-unreachable
ACCEPT     tcp  --  172.25.254.62        0.0.0.0/0            tcp dpt:80

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

上述操作未能实现预想的结果
因为读取时是从第一条开始读取,当第一条满足时不再读取第二条规则
所以删除第二条规则,将其插入到第一条,这样172.25.254.62主机就可以访问到本机httpd

[root@client ~]# iptables  -D INPUT 2  ##删除第二条规则
[root@client ~]# iptables  -I  INPUT  1   -s 172.25.254.62 -p tcp --dport 80 -j ACCEPT  ##在第一条的位置插入,使62可以访问apache
[root@client ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  172.25.254.62        0.0.0.0/0            tcp dpt:80
REJECT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80 reject-with icmp-port-unreachable

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

测试:62主机可以访问,其他主机不能访问
这里写图片描述
这里写图片描述
5.iptables的状态

[root@client ~]# iptables  -t filter -nL  ##查看filter表策略
[root@client ~]# iptables  -t nat -nL     ##查看nat表策略
[root@client ~]# iptables  -t mangle -nL  ##查看mangle表策略

设置输入数据为drop状态

[root@client ~]# iptables  -P INPUT DROP

测试:当设置为DROP状态的时候系统是处于丢弃状态,一直转圈却打不开网页,丢弃状态不返回值。
这里写图片描述
这里写图片描述
6.修改指定内容

[root@client ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80
REJECT     tcp  --  172.25.254.62        0.0.0.0/0            tcp dpt:80 reject-with icmp-port-unreachable

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
[root@client ~]# iptables  -R INPUT 1  -s 172.25.254.5  -p tcp  --dport 80 -j ACCEPT
[root@client ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  172.25.254.5         0.0.0.0/0            tcp dpt:80
REJECT     tcp  --  172.25.254.62        0.0.0.0/0            tcp dpt:80 reject-with icmp-port-unreachable

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination   

7.自定义链的管理

[root@client ~]# iptables  -F   ##清空策略
[root@client ~]# service iptables save  ##保存
[root@client ~]# iptables -N linux  ##新建linux链
[root@client ~]# iptables -E linux  LINUX ##更改为大写
[root@client ~]# iptables  -X  LINUX ##删除LINUX链

这里写图片描述
8.当第二次访问时不读取策略
NEW:第一次访问 | ESTABLISHED :第二次访问 | RELATED :关闭访问后再次访问

[root@client ~]# iptables  -A INPUT -m state --state  ESTABLISHED,RELATED  -j ACCEPT 
##将第二次访问和关闭访问后再次访问添加为ACCEPT模式,并添加在第一条,以便第二次访问时不用进行读取其他策略
[root@client ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 
[root@client ~]# iptables -A INPUT -m  state --state  NEW -p tcp --dport 22 -j ACCEPT ##添加ssh的端口为接受
[root@client ~]# iptables -A INPUT -m  state --state  NEW -p tcp --dport 53 -j ACCEPT ##添加dns端口为接受
[root@client ~]# iptables -A INPUT -m  state --state  NEW -p tcp --dport 80 -j ACCEPT ##添加http端口为接受
[root@client ~]# iptables -A INPUT -m  state --state  NEW -p tcp --dport 3260 -j ACCEPT ##iscsi端口为接受
[root@client ~]# iptables -A INPUT -m  state --state  NEW -p tcp --dport 443 -j ACCEPT ##https端口为接受
[root@client ~]# iptables -A INPUT -m  state --state  NEW -i lo -j ACCEPT  ##添加回环接口为接受
[root@client ~]# iptables  -A INPUT -j REJECT  ##其他的为拒绝
[root@client ~]# iptables  -nL
[root@client ~]# service iptables save

这里写图片描述

三、地址伪装

路由主机 eth0:172.25.4.105 eth1:172.25.254.105
测试主机:172.25.4.205 GATEWAY=172.25.4.105
在路由主机:当其他主机连接eth0时候伪装为172.25.254.105

[root@client ~]# iptables -F
[root@client ~]# iptables  -nL  -t nat
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination    
[root@client ~]# iptables  -t nat -A POSTROUTING  -o  eth0  -j SNAT  --to-source 172.25.254.105
[root@client ~]# iptables  -nL -t nat 
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
SNAT       all  --  0.0.0.0/0            0.0.0.0/0            to:172.25.254.105

测试:
这里写图片描述
这里写图片描述

四、地址转换

[root@client ~]# iptables  -t nat  -A PREROUTING  -i  eth1  -p tcp  --dport 22 -j  DNAT  --to-dest  172.25.4.205
[root@client ~]# iptables -nL -t nat 
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
DNAT       tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22 to:172.25.4.205

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
SNAT       all  --  0.0.0.0/0            0.0.0.0/0            to:172.25.254.105

在真机连接105–>205
这里写图片描述

猜你喜欢

转载自blog.csdn.net/weixin_41476978/article/details/80706453