天天看点

iptables 包过滤防火墙

防火墙的结构

dmz(非军事区):允许外网随时访问

linux的防火墙:

ipfawdn ---> ipchains ---> iptables

iptables

7.0以前默认开启的是iptables

7.0以后默认开启的是firewall

linux中防火墙都属于包过滤防火墙,对经过的数据包中的ip地址、协议、port、标记位进行审核

实验拓扑图:客户端a:202.0.0.1 ------ re0:202.0.0.2;re1:192.168.10.1 ------- b:192.168.10.2

实验:

1、安装软件包

iptables.xxxx.xxxx.rpm

iptables-services.xxxx.xxxx.rpm

2、启动服务

systemctl mask firewalld

systemctl unmask iptables

systemctl restart iptables

systemctl enable iptables

iptables通过规则集实现保护功能,规则集称为链,每个接口都可以设置这三种链:

input链:通信目标为本设备时,审核相应接口的input链      a ping r

forward链:路由器实现转发功能,审核发送方近口的forward链

output链:当通信发起方为本设备时,审核相应接口的output链  r ping  a

内网 防火墙限制 

【限制ip】

iptables -l 查看所有链的规则集

iptables -f 清空所有链的规则集

iptables -p input drop(拒绝) 修改链的默认规则(policy)   accept(接受)

按添加规则顺序,进行判断通信是否可行 (审核规则按照顺序审核)

制定规则指定网卡 

-a 制定规则链

-i 指定插入位置   

-d 指定删除位置

-i 指定网卡 (eno16777736)

-j 指定操作规则 (drop拒绝)(accept接受)

-s 指定发送方ip地址

-d 指定接受方ip地址 

制定规则指定网卡   -i 指定插入位置  -a 制定规则链 -d 指定删除位置   -i 指定网卡   -j 指定操作  -d 指定接受方ip地址  

iptables -i forward 1 -i eno16777736 -s 202.0.0.1 -d 192.168.10.2 -j accept 在指定位置插入规则

iptables -d forward 3    删除指定位置的规则

telnet ip  远程连接控制

【协议和端口】  

-p 指定协议  tcp  udp  icmp  拒绝所有外网的tcp通信

--dport 指定目的端口

--sport 指定发送端口

iptables -a forward -i eno16777736 -p tcp -j drop                 拒绝所有tcp通信

iptables -a forward -i eno16777736 -p tcp --dport 23 -j drop       只拒绝23号端口

iptables -a forward -i eno16777736 -p tcp -d 192.168.10.2 --dport 23 -j drop   只拒绝10.2上的23号端口

iptables -a forward -i eno16777736 -p udp --sport 53 -j drop       使客户端无法获得dns解析

设置a到b不可以ping通   b到a通 

【标记位】icmp

拒绝外网所有的主动连接,但是外网的回复信息能收到

c ---> s syn     请求

s ---> c syn,ack 回复

c ---> s ack  回复

iptables -a forward -i eno16777736 -p tcp --syn -j drop

【icmp:内置命令ping、tracert】请求(requset 8)  回复(relay 0)

iptables -a forward -i eno16777736 -p icmp --icmp-type 8 -j drop

内网访问外网    开启 <nat网络地址转换(内访外)postrouting链>

iptables -t nat -a postrouting -o eno16777736 -s 192.168.1.197 -j snat --to-source 36.111.136.8

-t nat 指定规则

-o 指定外网卡

--to-source 外网卡ip

iptables -l -t nat 查看nat链规则

iptables -f -t nat 清空nat链规则

实验验证:

客户端和服务器端 netstat -n  查看通信端口

外网访问内网    开启 <端口映射(外访内)prerouting>

iptables -t nat -a prerouting -i eno16777736 -d 202.0.0.2 -p tcp --dport 2300 -j dnat --to-destination 192.168.10.2:23

内网上:telent 202.0.0.2 2300

        netstat -n

iptables -t nat -vnl 查看

iptables -t nat -d prerouting 1 删除

上述命令重启iptables服务将失效,永久生效将命令写入配置文件

vim /etc/rc.d/rc.local

iptables -f

iptables -f -t nat

书写相应的防火墙规则

保存退出

给该文件增加执行权限

chmod a+x /etc/rc.d/rc.local

下一篇: NSF共享服务

继续阅读