Principles and Applications of iptables firewall

Source: Internet
Author: User
Iptables is an excellent nat + firewall tool in Linux. I used this tool to configure a flexible and powerful firewall + nat system in a low-configuration traditional pc, I have read many articles in this area on the Internet, but it seems that there are only a few or more biased articles. the content is incomplete and easy to mislead. I have studied iptables for a while. IptablesIs an excellent nat + firewall tool in Linux. I used this tool to configure a flexible and powerful firewall + nat system in a low-configuration traditional pc, I have read many articles in this area on the Internet, but it seems that there are few or more biased articles. the content is incomplete and easy to mislead. I have studied this article for a while. IptablesIt has also been used for a long time and has some experience to write for your reference. at the same time, you can also read it on your own later.
The first thing to note is that iptables operates the netfilter of more than 2.4 kernels. Therefore, linux kernel 2.4 or more is required. Its functions and security are far more powerful than those of its predecessors, ipfwadm and ipchains. iptables generally works on layer 2, Layer 3, and layer 4 of OSI Layer 7, its predecessors, ipchains, cannot independently implement tcp/udpport and mac address definitions and operations. Therefore, I think ipchains should only work on three layers.
Let's first briefly introduce the general workflow of netfilter, that is, when a packet (or packet) arrives at the network interface of linux (NIC) how to handle this packet, and then introduce how to use iptables to change or control the operation on this packet.
Netfilter is divided into three tables: filter, nat, and mangle. Each table has different operation Chains (Chains ). In the filter table, which is the firewall function, three chains are defined, namely INPUT, FORWARD, and OUTPUT. That is, the three filters that define the inbound, forward, and outbound packets. The operation and control of this filter table is also an important means for us to implement the firewall function. in the nat (NetworkAddressTranslation, network address translation) table, that is, the table used to implement address translation and port forwarding functions defines three links: PREROUTING, POSTROUTING, and OUTPUT. The following describes these three links in detail; the netfilter mangle table is a custom table, which includes the above filters and various chains in the nat table. it allows us to perform some custom operations, at the same time, the chains in this mangle table are at a higher priority in the netfilter packet processing process, the following figure clearly depicts the netfilter packet processing process (this figure is taken from the internet and I do not know who the author is. I would like to pay close attention to it !), Generally, we cannot use this mangle table. we will not introduce it here.

We can see that the PREROUTING chain is at the beginning. when a package comes to the network interface of linux, it first goes through the PREROUTING of mangle, and then the PREROUTING of nat, from the chain name, we can see that this chain is required before the routing (pre-routing. Why should I use it before routing? As you can see in this figure, there is a diamond part named ROUTING. This ROUTING part is the Linux routebox, that is, the ROUTING system. it also has very advanced functions, policy Routing and other advanced features can be implemented. we will not explain them in detail here. Let alone this PREROUTING chain, because in this chain, we operate the package in DNAT, that is, changing the destination address and (or port), which is usually used for port forwarding, or nat to the DMZ area of the intranet. that is to say, when a package comes in, we need to change its destination address. you can think about it, if a package is thrown into the routebox before the destination address is changed, and the system selects the route and then changes the destination address, the route may be wrong or meaningless, PREROUTING this Chain must be done before the Routing. For example, our public ip address is 60.1.1.1/24, which is eth0 in linux and the intranet ip address is 10.1.1.1/24 in eth1 in linux. our intranet has a web server, the address is 10.1.1.2/24. how can we allow internet users to access our internal web server through this public ip address? We can define a rule on this PREROUTING chain to change the destination address of the user accessing 60.1.1.1: 80 to 10.1.1.2: 80, in this way, internet users can access the intranet server. of course, this port is flexible. we can define any port forwarding, not necessarily 80 --> 80, for specific commands, we will introduce them in the following example. here we will only talk about the process and concept implementation methods. Okay, let's go down. this package has already passed two PREROUTING chains. at this point, there is a branch turning point, that is, the diamond (FORWARD) below the figure ), forward! There is a judgment on the target address (this also shows that PREROUTING must be the first, not only before routebox, but even before the judgment on the target address, because we may make a rule to go to a certain ip address to transfer to our own ip address, so PREROUTING is the first Chain to process this package )! If the destination of the package is the local ip address, the package goes up and enters the INPUT chain for processing, and then enters LOCALPROCESS. if it is not local, the FORWARD chain is used for filtering. we will not introduce the INPUT here, OUTPUT processing, because it is mainly a kind of processing for local security. here we mainly talk about forwarding filtering and nat implementation.
Here, FORWARD is a simple example. when linux receives a packet whose destination IP address is not a local IP address, Linux discards the packet because, by default, the three-tier packet forwarding function in Linux is disabled. to enable forwarding in linux, you need to enable this forwarding function. you can change one of its system parameters and use sysctlnet. ipv4.ip _ forward = 1 or echo "1">/proc/sys/net/ipv4/ip_forward command to enable the forwarding function. Okay, here we allow linux to allow forwarding. the destination address of this package is not the local machine, so it will go into the FORWARD chain. in the FORWARD chain, we can define detailed rules, that is, whether the package is allowed to pass through or make some changes to the package's direction flow. this is also the place where we implement access control. this is also Mangle_FORWARD and then filter_FORWARD, the operation of any chain will affect the fate of this package. in the following introduction, we will ignore the mangle table, and we will basically not use it, so we assume it is transparent. Assume that the rule for this package has been put, that is, ACCEPT. it will enter the POSTROUTING part. Note! Here I noticed a detailed problem, that is, the packet in the figure above goes through the FORWARD chain and directly enters the POSTROUITNG chain. I think there is no link in the middle, that is, routebox, for a forwarded packet, linux also needs to route the packet before it can be sent out, but this figure does not indicate this, I think it is a POSTROUITNG that enters after routebox. of course, this is not very important for us to discuss iptables filtering and forwarding, but I think there is a problem in the process, it should be noted. Similarly, we can see from the name here that this POSTROUTING chain should be a chain after the route, that is, the package should be sent out of the last link of this Linux, this is an extremely important step !! At this time, linux has been completed (hasdone .. ^_^) the routing (routing) of this package has been found, and the appropriate interface has been found to send this package. in this chain, we need to perform important operations, it is an action called SNAT in Linux. modify the source IP address! Why modify the source IP address? In many cases, you need to modify the source address. The most common problem is that multiple machines on the intranet need to share one or more public ip addresses to access the internet, because our intranet address is private, if you want linux to route out, the source address will remain unchanged. this package should be able to access the destination, but it will not be able to return, because N route nodes on the internet do not forward data packets with private addresses, that is, our data packets do not have to go back without a legal ip address. Some people may say, "in this case, I don't need a private ip address. can I allocate my own legitimate ip addresses? Then the package will be back, right ?" The answer is no. the IP address is allocated by ICANN. your data packets may be sent to your destination, but when you get back, they will not be transferred to you, the routing information in the router on the internet will send the returned packet to the valid ip address, and you cannot receive the packet. However, this behavior may be defined as an ip spoofing, many devices will filter out such packages on the access end, and may not be able to access the server.
So how does Linux implement SNAT? For example, if a pc in the intranet 10.1.1.11 accesses a web server in 202.2.2.2, the linux intranet interface 10.1.1.1 changes the IP address of the original PC to the valid IP address of 60.1.1.1 after receiving the package and then sends it out, at the same time, make a record in your ip_conntrack table, remember which port of the intranet ip address is used to access the web server, and change its source address to a certain number, the port number is changed so that when the web server returns a data packet, linux returns it to the pc that sends the request.
The general data forwarding process is complete. let's see what parameters iptables uses to complete these operations. Before describing these specific operations, I would also like to explain some of my understanding of iptables (which may not be completely correct), which will help you understand these rules for more precise control. As we mentioned above, the control over packages is implemented by adding different rules to different chains. for example, we can filter tables) add rules to control the package. Now that a chain is composed of one or more rules, there is a problem. if multiple rules define the same package, what will happen? In the Chain, all the rules are executed from the top down. that is to say, if the first row is matched, the rule is executed according to the first row and the row is searched down, until the rule that matches this type of package is found. What if I find the rule that does not match this package? There is a concept in itpables, that is, Policy. When talking about this thing, you may find it troublesome. what strategy is it? my understanding is that this policy is the last rule in the chain, that is to say, if the rule for processing the package cannot be found again, the rule will be handled according to the policy. This makes it much easier to understand. Iptables uses-P to set the Chain policy.
Well, let's get down to the point and talk about how iptables controls packets.
First, we will introduce how iptables operates the chain:
There are several operations on the chain:-I (insert)-A (append)-R (replace)-D (delete)-L (list display) here we should note that-I will place the rule in the first line, and-A will be placed in the last line. For example, we want to add a rule to the FORWARD chain of the filter table.
Iptables-t filter-a forward-s 10.1.1.11-d 202.1.1.1-jACCEPT
The above Command means: append a rule to the end of the FORWARD chain in the filter table, and allow (-jACCEPT) data packets whose source address is 10.1.1.11 and whose destination address is 202.1.1.1 to pass through. -T is followed by the table name,-A is followed by the Chain name,-s is the source address,-d is the destination address, and-j is the processing direction.
In iptables, the default table name is filter. Therefore, you can omit-t filter and write it as iptables-a forward-s 10.1.1.11-d 202.1.1.1-j ACCEPT.
Matching parameters in iptables:
Here we will introduce several common parameters. for detailed usage, you can refer to man iptables's online documentation, which will surprise you.
-S match source address
-D: match the destination address
-P protocol matching
-I-inbound interface match
-O-out interface match
-- Sport, -- dport source and destination port match
-J jump, that is, the packet direction
There is also one! Parameter, use! This is the opposite. The following is a simple example.
The-s parameter specifies the source address. if you use this parameter to tell netfilter how to handle a packet that matches the source address, you can specify a unicast IP address,
You can also specify a network. if a single IP address actually implies a 32-bit subnet mask, for example,-s 10.1.1.11 is actually-s10.1.1.11/32
We can also specify different masks to implement the source network address rules. for example, we can use-s 10.1.1.0/24 to specify a Class C address.
The-d parameter is in the same format as the-s parameter.
-The I parameter specifies the network interface of the inbound interface. for example, if I only allow packets from the eth3 interface to pass the FORWARD chain, you can specify iptables-AFORWARD-I eth3-j ACCEPT.
-O is the output interface.
Let's take a look at the specific configuration method of iptables with some simple examples.
Example 1: a simple nat router
Environment introduction:
Linux 2.4 +
2 Network interfaces
Lan port: 10.1.1.254/24 eth0
Wan port: 60.1.1.1/24 eth1
Objective: to achieve controllable internet access for nodes (10.1.1.0/24) in the intranet.
First, point the gateway of the Lan node pc to 10.1.1.254.
Make sure that your linux ip address configuration is correct. you can ping the internal and external addresses correctly. Run the route command to view the local route table of linux and confirm that the default gateway provided by the available ISP is specified.
Use sysctl net. ipv4.ip _ forward = 1 to enable the linux forwarding function.
Iptables-P FORWARD DROP
Set the FORWARD chain policy to DROP to control the intranet ip address. you can add a rule when you allow access to the internet, ip addresses that are not in the rule cannot access the internet.
Iptables-a forward-m state -- state ESTABLISHED, RELATED-jACCEPT
This rule allows confirmation packages and associated packages from any address to pass. You must add this line. Otherwise, it is useless to allow only access from the lanIP. for the reason, let's go into details.
Iptables-t nat-a postrouting-s 10.1.1.0/24-j SNAT -- to60.1.1.1
This rule implements an SNAT, that is, source address conversion, which converts the address from 10.1.1.0/24 to 60.1.1.1.
With these rules, a simple nat router is implemented. In this case, you can add the ip addresses that are allowed to access the FORWARD chain so that they can access the internet.
For example, if I want the address 10.1.1.9 to access the internet, you can add the following command.
Iptables-a forward-s 10.1.1.9-j ACCEPT
You can also precisely control the access address. for example, I allow 10.1.1.99 to access the ip address 3.3.3.3.
Iptables-a forward-s 10.1.1.99-d 3.3.3.3-j ACCEPT
Or only allow them to access port 80.
Iptables-a forward-s 10.1.1.0/24-p tcp -- dport http-jACCEPT
You can perform more control on your own or refer to the online documentation of iptables.
Example 2: port forwarding
Environment introduction:
Linux 2.4 +
2 Network interfaces
Lan port: 10.1.1.254/24 eth0
Web server in Lan: 10.1.1.1: 80
Ftp server in Lan: 10.1.1.2: 21
Wan port: 60.1.1.1/24 eth1
Objective: to enable internet users to access intranet servers through port forwarding on internal servers
Make sure that your linux configurations are normal and you can access the intranet and internet.
Iptables-P FORWARD DROP
Iptables-a forward-m state -- state ESTABLISHED, RELATED-jACCEPT
You also need to add the validation package and associated package to allow
If you want to forward data packets accessing 60.1.1.1: 80 to the web server in the Lan, run the following command:
Iptables-t nat-a prerouting-d 60.1.1.1-p tcp -- dport 80-j DNAT -- to 10.1.1.1: 80
The same is true for the ftp service. The command is as follows:
Iptables-t nat-a prerouting-d 60.1.1.1-p tcp -- dport 21-j DNAT -- to 10.1.1.2: 21
All right, the command is complete, and port forwarding is complete. can I forward it in this example? No. why? I will analyze it in detail below. For iptables, it seems that the configuration for external access is easier, but for internal
The forwarding seems to have some problems. at the beginning, I first talked about some netfilter process problems, then I will briefly explain why these configurations may not work yet?
There are many reasons that can cause this configuration failure. one by one:
First, in this example, our FORWARD policy is DROP. that is to say, packets that do not comply with the rules will be discarded, no matter whether they are inside or outside, we will not discuss the problem of confirming the package and associated package here. we don't need to worry about it. I will explain it in detail below. so how can we make this example successful? Add the following rules.
Iptables-a forward-d 10.1.1.1-p tcp -- dport 80-j ACCEPT
Iptables-a forward-d 10.1.1.2-p tcp -- dport 21-j ACCEPT
Do you feel dizzy? Why is the destination address 10. xxx, not 60. xxx, accessed by internet users? Let's go back to the figure above. where is the FORWARD chain? it is after PREROUTING. that is to say, when the package reaches the FORWARD chain, the destination address has changed to 10. xxx. if the internet user's request is like this: 202.1.1.1: 1333 --> 60.1.1.1: 80, it will become 202.1.1.1: 1333 after our PREROUTING chain --> 10.1.1.1: 80, at this time, if you set a destination address to 60. is the xxx rule useful? This is problem 1. In this case, access to port forwarding should be completed, but sometimes it will not work? Why? Check question 2.
Second, the ip address configuration of the intranet server. Here we take webserver as an example to describe it (ftp has some special circumstances. we will discuss it in detail below, I will discuss this issue when confirming the package and associated package). as mentioned above, sometimes it can be accessed, but sometimes it won't work. this is the IP address setting problem of the webserver, if the web server does not specify the default gateway, the web server will receive an internet request after the above configuration, but he does not know where to go back, your local route table does not know your internet ip address. If you use the packet capture tool to check the webserver, you will find that the server received a request from 202.1.1.1: 1333 --> 10.1.1.1: 80, because you did not configure the default gateway for the webserver, it does not know how to go back, so it becomes disconnected. What should we do?
Two solutions:
First, configure a default gateway for the server. of course, you must point to the linux server configured for port forwarding. In this example, 10.1.1.254 is configured and the server can be accessed. Have a question? Do you not need to set a rule on the FORWARD chain to allow the IP address of the webserver to access the Internet? Can its package go out? The answer is yes. Because our rule allows validation of the package and associated package, otherwise it will not be available.
The second method is more troublesome, but it seems safer for the server. The method is to re-execute SNAT for this package, that is, add rules on the POSTROUTING chain. The command is as follows:
Iptables-t nat-a postrouting-d 10.1.1.1-p tcp -- dport 80-jSNAT -- to 10.1.1.254
The ftp method is the same. This command is not easy to understand ?? In fact, it is very simple. if you use this command, your webserver can receive this request without setting the default gateway, as long as the lan IP addresses of the web server and linux can communicate with each other (that is, the Lanip addresses of the web server and Linux are in the same broadcast domain ), according to the netfilter flowchart above, we will analyze how this package was handled. first, a request 202.1.1.1: 1333 --> 60.1.1.1: 80 was received by linux and entered PREROUTING, A rule (iptables-t nat-a prerouting-d 60.1.1.1-p tcp -- dport 80-j DNAT -- to10.1.1.1: 80) is found to be correct, so this package is changed to 202.1.1.1: 1333 --> 10.1.1.1: 80. go FORWARD and enter the FORWARD chain, okay. There is also A rule that can pass (iptables-a forward-d 10.1.1. 1-p tcp -- dport 80-j ACCEPT), enter the routebox route selection, find the appropriate path, and continue to enter the POSTROUTING chain, yeah? We also found a compliant rule (iptables-t nat-APOSTROUTING-d 10.1.1.1-p tcp -- dport 80-j SNAT -- to10.1.1.254). It turns out to be an SNAT. change your source address, the package is changed to 10.1.1.254: xxxx --> 10.1.1.1: 80. Why is xxxx used? the port here is random and I don't know what it will be. The two changes are recorded in ip_conntrack of linux. when webserver receives this package, it finds that it was originally a request from its own brother in the intranet, in another broadcast domain, the return packet is directly thrown to the switch without looking for a gateway. after receiving the return packet, linux performs two transformations based on the entries in ip_conntrack, returns the real internet user, and completes this access.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.