防火墙配置详解

目录

 

一、首先了解一下查询防火墙的各种状态的命令:

二、iptables命令

实验一、实现让自己可以ping别人,而别人却无法ping自己。

实验二、网络防火墙

实验三、实现日志记录功能


一、首先了解一下查询防火墙的各种状态的命令:
 

1、查看防火墙状态:

[root@localhost ~]# systemctl status firewalld.service

2、开启防火墙

[root@localhost ~]# systemctl start firewalld.service

3、关闭防火墙:

[root@localhost ~]# systemctl stop firewalld.service

4、禁止firewall开机自启动:

[root@localhost ~]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

5、在开机时自动启用:

[root@localhost ~]# systemctl enable firewalld.service
Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.

6、查看服务是否开机自启动:

[root@localhost ~]# systemctl is-enabled firewalld.service 
enabled

7、查看已经启用的服务列表:

[root@localhost ~]# systemctl list-unit-files | grep fire
firewalld.service                             enabled 

二、iptables命令

1、列出防火墙规则:iptables -L

2、列出防火墙规则,不进行域名解析:iptables -Ln

3、列出防火墙规则的详细信息:iptables -Lnv

4、列出防火墙规则对应是第几条:iptables -nL --line-numbers

5、删除INPUT表的第二条规则:iptables -D INPUT 2

6、清空防火墙:iptables -F。清空所有策略,但不会影响链上的默认策略

7、替换INPUT表第100条策略:iptables -D INPUT -s 192.168.153.1 -j REJECT

8、设置链策略:-P,DROP  ,也可以设置为ACCEPT,不能为REJECT。不建议改这个设置!

9、将当前防火墙策略保存至iptables文件:iptables-save > iptables

10、将保存的防火墙文件iptables添加至策略:iptables-restore < iptables

三、案例

实验一、实现让自己可以ping别人,而别人却无法ping自己。

主机一:

IP172.18.254.131

(在给主机一添加策略前,先试试主机二能不能ping通主机一,)

做实验前我们最好清空一下防火墙,以免其相互影响(iptables -F)。

添加防火墙策略:

[root@localhost ~]# iptables -A INPUT -p icmp -j REJECT
[root@localhost ~]# iptables -A OUTPUT -p icmp -j REJECT
[root@localhost ~]# iptables -I OUTPUT 1 -p icmp --icmp-type 8 -j ACCEPT
[root@localhost ~]# iptables -I INPUT 1 -p icmp --icmp-type 0 -j ACCEPT

这里我们用到了icmp模块。icmp说直白一点,就是我们调用ping请求用到的模块。icmp-type 8  请求,icmp-type 0  回应。

简单解释一下这几行代码:

(1)拒绝INPUT的ping请求

(2)拒绝OUTPUT的ping请求

(3)插入策略为第一条,允许本机发送ping的请求报文。

(4)插入策略为第一条,允许接收回应报文。

也就是说,我发送的请求报文,别的主机给我发回应报文,我接收;

别人给我发的请求报文,我直接拒绝接收,拒绝回应。

主机二:

IP:172.18.252.162

[root@localhost ~]# ping 172.18.254.131
PING 172.18.254.131 (172.18.254.131) 56(84) bytes of data.

他会卡到这里不动。这是因为我们把报文发给主机一,主机一没有接受,没有回应报文,这样我们的主机二会一直卡着,直到请求超时才自动结束。

用主机二ping主机一,自然是没有一点儿问题。

实验二、网络防火墙

实验目的:实现内网可以ping外网,外网不可以ping内网。

实验环境:三台机器:一台作为防火墙(firewall),一台作为我们的内网机器(in),一台作为我们的外网机器(out)。防火墙机器两个网卡,一个连接内网,一个连接外网;内网机器一个网卡,外网机器一个网卡。最好自己把网卡设置成同一个网段,注意掩码一定要相匹配,不然会影响实验。

firewall:172.18.252.162/16  192.168.32.222/24
in  192.168.32.216/24   out   172.18.254.66/16

实验前,请先清理一下防火墙,以确保本实验不受其他条件的影响。

一、在firewall机器上开启IP转发功能

这里有三种方法提供给大家:

1、临时开启IP转发,重启后失效。

sysctl -w net.ipv4.ip_forward=1

2、直接写/proc文件系统 

echo 1 > /proc/sys/net/ipv4/ip_forward

3、编写配置文件

[root@localhost ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1

二、在in机器上添加路由

[root@localhost ~]# route add default gw 192.168.32.0/24

三、在out机器添加路由

[root@localhost ~]# route add default gw 172.18.0.0/16

四、在防火墙机器添加策略

这里又有三种方法:

1、

[root@localhost ~]# iptables -A FORWARD -j REJECT
[root@localhost ~]# iptables -I FORWARD -s 192.168.32.216/24 -d 172.18.254.66/16 -p icmp --icmp-type 8 -j ACCEPT
[root@localhost ~]# iptables -I FORWARD -s 172.18.254.66/16 -d 192.168.32.222/24 -p icmp --icmp-type 0 -j ACCEPT​

2、

[root@localhost ~]# iptables -A FORWARD -j REJECT
[root@localhost ~]# iptables -I FORWARD -s 192.168.32.216/24 -d 172.18.254.66/16 -p icmp --icmp-type 8 -j ACCEPT
[root@localhost ~]#  iptables -I FORWARD -m state --state ESTABLISHED -j ACCEPT​

3、

[root@localhost ~]# iptables -A FORWARD -j REJECT
[root@localhost ~]#  iptables -I FORWARD -s 192.168.32.216/24 -d 172.18.0.0/16 -p icmp -m state --state NEW -j ACCEPT
[root@localhost ~]#  iptables -I FORWARD -m state --state ESTABLISHED -j ACCEPT​

五,测试

在内网主机ping外网主机,在外网主机ping内网主机。

实验三、实现日志记录功能

命令参数很简单:-j LOG

实验要求:两台主机

一、主机一

[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
setenforce: SELinux is disabled
[root@localhost ~]# iptables -I INPUT -p tcp --dport 80 -j LOG --log-prefix "NEW CONNECTION:"
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# tailf /var/log/messages

 清空防火墙,关闭selinux,为80端口添加一条策略:启用LOG功能,前缀是NEW CONNECTION。启动httpd服务,动态查看日志文件

 主机二、

访问主机一的80端口

[root@localhost ~]# curl 172.18.252.162

这时候我们再去看主机一

[root@localhost ~]# tailf /var/log/messages
Oct 26 22:40:01 localhost systemd: Started Session 18 of user root.
Oct 26 22:40:01 localhost systemd: Starting Session 18 of user root.
Oct 26 22:50:01 localhost systemd: Started Session 19 of user root.
Oct 26 22:50:01 localhost systemd: Starting Session 19 of user root.
Oct 26 22:56:30 localhost kernel: NEW CONNECTION:IN=ens34 OUT= MAC=00:0c:29:4e:8d:a5:00:0c:29:54:fb:bc:08:00 SRC=192.168.32.216 DST=172.18.252.162 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=40342 DF PROTO=TCP SPT=51866 DPT=80 WINDOW=29200 RES=0x00 SYN URGP=0 
Oct 26 22:56:30 localhost kernel: NEW CONNECTION:IN=ens34 OUT= MAC=00:0c:29:4e:8d:a5:00:0c:29:54:fb:bc:08:00 SRC=192.168.32.216 DST=172.18.252.162 LEN=52 TOS=0x00 PREC=0x00 TTL=64 ID=40343 DF PROTO=TCP SPT=51866 DPT=80 WINDOW=229 RES=0x00 ACK URGP=0 

 看到了吧,日志已经记录在案,并且已经加上了前缀。

实验环境:两台机器,一台作为防火墙(两个网卡),一台做内网机器(一个网卡)。

firewall:172.18.252.162/16  192.168.32.222/24
in  192.168.32.216/24

一、在防火墙机器上操作

[root@localhost ~]# iptables -F
[root@localhost ~]# iptables -t nat -F
[root@localhost ~]#  iptables -t nat -A POSTROUTING -s 192.168.32.0/24 -j SNAT --to-source 172.18.252.162

二、在内网机器测试

[root@localhost ~]# ping 119.75.217.109
PING 119.75.217.109 (119.75.217.109) 56(84) bytes of data.
64 bytes from 119.75.217.109: icmp_seq=1 ttl=53 time=22.1 ms

这里我们给了一个百度的IP地址:119.75.217.109

三、附加操作

我们ping通了百度,但是是以谁的身份呢?为了加深理解,我们再添加一台试验机,这一次我们的网卡设置为172.18.254.66/24

拿我们的内网机器ping一下我们添加的试验机:

[root@localhost ~]# ping 172.18.254.66
PING 172.18.254.66 (172.18.254.66) 56(84) bytes of data.
64 bytes from 172.18.254.66: icmp_seq=1 ttl=63 time=0.383 ms
64 bytes from 172.18.254.66: icmp_seq=2 ttl=63 time=0.359 ms

就这么让他ping着。这时候,我们再去我们添加的这台试验机上操作:

[root@localhost ~]# tcpdump -i ens33 -nn icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
15:18:06.498058 IP 172.18.252.162 > 172.18.254.66: ICMP echo request, id 6142, seq 1, length 64
15:18:06.498140 IP 172.18.254.66 > 172.18.252.162: ICMP echo reply, id 6142, seq 1, length 64

看到了吧,他显示的地址正是我们防火墙机器的IP,而不是我们内部网络的IP地址!

知识共享,允许转载!不足之处希望您能完善完善!

猜你喜欢

转载自blog.csdn.net/qq_34208467/article/details/83384815