firewall

I. Introduction firewall

In linux firewall strategy is based on netfilter implementation. netfilter kernel firewall architecture, enabling security policies in many functions such as packet filtering, packet processing, masquerading, transparent proxy, Dynamic Network address translation (Network Address Translation, NAT), and a control based on a user and media access filtration filter (Media Access Control, MAC) address and a state-based, packet rate limiting.
IPTABLES is integrated with the Linux kernel IP packet filtering system. If the Linux system is connected to the Internet or LAN, and the Internet server or LAN connection proxy server, the system facilitates better control of the IP packet filter on the Linux system and firewall configuration.
netfilter / iptables IP packet filtering system consists of two components netfilter and iptables composition.
netfilter components are also called kernel space (kernelspace), is part of the kernel, the number of packet filtering tables, these tables contain the core to control the packet filtering rule sets processing.
iptables component is a tool, also referred to as user space (the userspace), which allows to insert, modify, and remove rules in the packet filter table becomes easy.

Second, the firewall and switch management tools

There are two firewall management tool in linux: firewalld and iptables
default in rhel8 is firewalld, if you need to download using iptables
Here Insert Picture Description

dnf search iptables
dnf install iptables-services.x86_64 -y

Here Insert Picture DescriptionThere is a firewall management tools only in the system, first put firewalld fully closed before all to use iptables

systemctl stop firewalld.service   #关闭火墙
systemctl disable firewalld.service  #设置开机不启动
systemctl mask firewalld.service    #冻结火墙

Here Insert Picture Description

Three, iptables use

View: iptables -L
Refresh: iptables -F
log file policy for the / etc / sysconfig / iptables
by default iptables -Lsituation View below
Here Insert Picture DescriptionHere Insert Picture Descriptionbut in view of the default content service iptables restart before they can be seen, because the policy is stored in the policy document
Here Insert Picture Description
refresh to change the policy file can be saved after the restart to refresh, so the iptables policy saved permanently to use the following command:

service iptables save
iptables-save > /etc/sysconfig/iptables

Here Insert Picture Description

Four, iptables three in Table V chain

The default policy pentachain

input Entry
output Export
forward Forwarded
postrouting After converting the source address generating SNAT route
prerouting Conversion will occur before the destination address routing DNAT

The default policy three tables

filter After the native kernel data (input, output, forward)
nat Without kernel data (postrouting, prerouting, output, input)
missing Additional tables, used when the filter is not enough and nat table, including the five chain

Use the 5, iptables command

5.1 In the filter table

Strategies to view the table
Here Insert Picture Descriptionby default, the test host can communicate between the host and, by default, all hosts can access this host
Here Insert Picture Descriptionrefused access to all hosts
Here Insert Picture DescriptionHere Insert Picture Descriptiondeletion policy
Here Insert Picture Descriptionwill be changed to DROP action
Here Insert Picture DescriptionHere Insert Picture Descriptiononly allow 192.168.0.12 this host data access
Here Insert Picture DescriptionHere Insert Picture DescriptionHere Insert Picture Descriptiontest
Here Insert Picture Descriptiononly allows access to host data port 22 of the host 12, that can not communicate (ping nowhere) but you can use ssh connection
Here Insert Picture Descriptiontest
Here Insert Picture Descriptionto change the rules
Here Insert Picture Descriptionto change the default policy, the default policy can only be changed to ACCEPT or DROP
Here Insert Picture Descriptiontests
Here Insert Picture Descriptionadd a new the chain
Here Insert Picture Descriptionchange chain name
Here Insert Picture Descriptiondeleted chain
Here Insert Picture Descriptiona small example: an existing host 192.168.0.11, only allows access to other hosts dns and apache services, other visits are not possible, configure iptables to achieve

iptables -A INPUT -i lo -j ACCEPT   #将本地回环接口的访问打开
iptables -A INPUT -p tcp --dport 80 -j ACCEPT  # 将apache服务的80端口打开
iptables -A INPUT -p tcp --dport 443 -j ACCEPT  #将apache服务的443端口打开 
iptables -A INPUT -p tcp --dport 53 -j ACCEPT   #将dns服务的53端口打开
iptables -A INPUT -j REJECT      #其他的所有情况都拒绝

Here Insert Picture Description
The above-described arrangement configuration requirements can be achieved, but because the firewall is a top-down strategy read, particularly when the strategy is a data packet to each of the investigation will be carried out from the policy, the result is a long delay, so you need to state data packet plus the time allocation strategy that effectively reduce the time delay, greatly improving the efficiency of access
packet common state

RELATED Connection has been established
ESTABLISHED Establishing Connection
NEW The new data packet access

Connection has been established, and packet connection being established can perform actions ACCEPT

iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -m state --state NEW -i lo -j ACCEPT
iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -m state --state NEW -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -m state --state NEW -p tcp --dport 53 -j ACCEPT
iptables -A INPUT -m state --state NEW -j REJECT

In addition to 22 ports can not access the 192.168.0.12 host, other hosts can be
Here Insert Picture Descriptiontested: 12 hosts
Here Insert Picture Descriptionin the 10 host
Here Insert Picture Descriptionsummary, the following common parameters iptables

-t Specify the table name (filter (default), nat, mangle)
-n Is not resolved
-L View
-A Add Policy
-p Designated agreement
-Dport Destination port
-s source
-j Specify actions ACCEPT, DROP, REJECT, SNAT, DNAT
-N New Chain
-X Delete chain
-E Change the name chain
-D Delete Rule
-I Insertion rules
-R Changing the rules
-P Change the default rule (ACCEPT, DROP)
5.2 nat table

A first host configured for dual card host
Here Insert Picture Descriptiontest machine 1 and the gateway ip
Here Insert Picture Descriptiontest host ip of 2
Here Insert Picture Descriptiontest machine 1 and 2 in two different segments, default communication is not
Here Insert Picture Description
disposed in a dual NIC host firewall policy: ens160 card data out from the mouth will translate the source address 192.168.0.11
Here Insert Picture Descriptiontest
Here Insert Picture Descriptionto see which hosts the test even his host 2 host, display is dual-NIC host ip
Here Insert Picture Description
want to connect the test host 1 host 2 in the test also need to configure dual-NIC host strategy: coming from the host ens160 card destination address data have changed (ip test host 1) 172.25.254.12
Here Insert Picture DescriptionHere Insert Picture Description

Six, firewalld enabled and firewalld domain

6.1 firewalld open iptables tool needs to be shut down
systemctl stop iptables
systemctl disable iptables
systemctl mask iptables
systemctl unmask firewalld
systemctl start firewalld

firewalld domain and service included by default as follows

Domain Type Allows connection service
trusted Accept all network connections
home Home network, ssh, mdns, ipp-client, samba-client, dhcp-client
work Work Network, ssh, ipp-client, dhcp-client
public Public network, ssh, dhcp-client
dmz Military-grade network, ssh
block Reject all
drop Discard all data will be discarded without any reply
internal Intranet, and home network allows the same service
external ipv4 network address masquerading forward ssh

Firewall configuration directory / etc / firewalld
firewall module directory / usr / lib / firewalld, which has allowed the definition and description of the firewall services

6.2 firewalld management command

About the domain management command

firewall-cmd --state  #查看火墙状态
firewall-cmd --get-active-zones #查看生效的域
firewall-cmd --get-default-zone #查看默认域
firewall-cmd --liat-all  #查看默认域中的详细火墙信息
firewall-cmd --list-all --zone=work #查看指定域(work)中的详细火墙信息
firewall-cmd --set-default-zone=drop #设定默认域

Here Insert Picture DescriptionWhen set to the default domain to drop, it did not respond when connecting a host
Here Insert Picture Descriptionon service management command
to view all services that can be set

firewall-cmd --get-service

Here Insert Picture DescriptionAllowed to view the default domain firewall information
Here Insert Picture DescriptionRemoves the specified service

firewall-cmd --remove-service=http
firewall-cmd --reload   #刷新

Here Insert Picture DescriptionSpecifies the data source to access the specified domain

firewall-cmd --permanent --add-source=192.168.0.11 --zone=block #指定从192.168.0.11中来的数据访问block域

Test ping11 host unreasonable in the host
Here Insert Picture Descriptiondelete data sources to access the specified domain

firewall-cmd --permanent --remove-source=192.168.0.11 --zone=block

Here Insert Picture DescriptionChange the network interface to the specified domain

firewall-cmd --permanent --change-interface=ens160 --zone=block 

Here Insert Picture DescriptionDelete the specified network interface

firewall-cmd --permanent --remove-interface=ens160 --zone=block 

Here Insert Picture DescriptionAdds the specified network interface

firewall-cmd --permanent --add-interface=ens160 --zone=drop

Here Insert Picture Description

The advanced rule 6.3 firewalld

The method is similar to using the iptables command to specify the chain specified in the table to add firewall policy
to see all of the direct rule
will host firewall dual-NIC in the ssh service to remove, add new direct rule
Here Insert Picture Descriptionhost 192 can not connect network segments
Here Insert Picture Description

6.3 firewalld of NAT

1, the source address forwarding (SNAT)

firewall-cmd --permanent --add-masquerade  #打开地址伪装功能
 firewall-cmd --reload 

Here Insert Picture DescriptionIn 172.25.254.30 host ping192.25.254.12 successfully host
Here Insert Picture Description1, the destination address forwarding (DNAT)

firewall-cmd --permanent --add-forward-port=port=22:proto=tcp:toaddr=172.25.254.30

Here Insert Picture DescriptionTest: Connect dual-NIC host ip192.168.0.11 in the host 192.168.0.12 actually connect to the larger of the host 172.25.254.30
Here Insert Picture Description

Published 28 original articles · won praise 0 · Views 757

Guess you like

Origin blog.csdn.net/weixin_43834060/article/details/104995568