Iptables-1.1.9 Guide (Classic) (2)

Source: Internet
Author: User
Iptables-1.1.9 Guide (Super classic) (2) -- Linux Enterprise Application-Linux server application information, which is described below. CONFIG_IP_NF_MATCH_UNCLEAN-match the P, TCP, UDP, and ICMP data packets that do not conform to the type standard or are invalid, any package that is not in the correct mode is dirty. This is like the "dirty page" in the memory management of the operating system. It can be called "dirty package" here, and it is naturally UNCLEAN ). We usually discard such a package, but I don't know if this is correct. In addition, this matching function is still in the experimental stage and may cause some problems.

CONFIG_IP_NF_MATCH_OWNER-matches data packets based on the socket owner. For example, we only allow root users to access the Internet. In iptables, this module initially uses an example to describe its functions. This module is also in the experimental stage and cannot be used.

CONFIG_IP_NF_FILTER-this module adds basic filter tables for iptables, including the INPUT, FORWARD, and OUTPUT chains. The filter table can be used to completely filter IP addresses. This module is required to filter data packets, whether they are received or sent or not.

CONFIG_IP_NF_TARGET_REJECT-This operation allows us to use the ICMP error message to receive the packet instead of simply dropping it. In some cases, a response is required. For example, compared with ICMP and UDP, a tcp rst packet is always required to reset or reject TCP connections.

CONFIG_IP_NF_TARGET_MIRROR-This operation causes the data packet to be returned to the computer that sent it. For example, we set the MIRROR operation for the HTTP packet on the destination port in the INPUT chain. When someone accesses HTTP, the packet is sent back to the original computer. Finally, he may access his home page. (Note: It is difficult to understand why it is called MIRROR)

CONFIG_IP_NF_NAT-as the name suggests, this module provides the NAT Function. This option gives us the right to access the nat table. This module is required for port forwarding and disguise. Of course, if all the computers in your LAN have a unique valid IP address, this option is not required for firewall or disguise. Rc.firewall.txt is required :)

CONFIG_IP_NF_TARGET_MASQUERADE-Provides MASQUERADE (camouflage) operations. If we do not know the IP address used to connect to the Internet, the preferred method is to use MASQUERADE instead of DNAT or SNAT. In other words, it is better to use SNAT if we connect to the Internet using PPP or SLIP, and IP addresses are allocated by DHCP or other services. Because MASQUERADE does not need to know the IP address connected to the Internet in advance, although it is slightly higher than the NAT load for computers.

CONFIG_IP_NF_TARGET_REDIRECT-this operation is useful with the agent. It does not allow data packets to pass through directly, but remaps the packets to the local host, that is, transparent proxy.

CONFIG_IP_NF_TARGET_LOG-added the LOG operation for iptables. You can use the System Log service to record certain data packets, so that we can understand what happened on the package. This is invaluable for our security review and script debugging.

CONFIG_IP_NF_TARGET_TCPMSS-this option can be used by some ISPs (service providers) or services that block ICMP segment information. There is no ICMP segment information, and some webpages and large emails cannot pass. Although small emails are acceptable, there are also. After handshaking, ssh works, but scp does not work. We can use TCPMSS to solve this problem, that is, the MSS (Maximum Segment Size) is pinned on PMTU (Path Maximum Transmit Unit ). This method can be called "criminally brain-dead ISPs or servers" by Netfilter developers in Kernel configuration help.

CONFIG_IP_NF_COMPAT_IPCHAINS-ipchains, which is only used to convert the kernel from 2.2 to 2.4 and will be deleted in 2.6.

CONFIG_IP_NF_COMPAT_IPFWADM-the same as above. This is only the compatibility mode currently used by ipfwadm.

I briefly introduced many options above, but this is only in kernel 2.4.9. For more options, we recommend that you use Netfilter to check the patch-o-matic. There are some other options. POM may be added to the kernel. Of course, it is not yet available. There are many reasons for this. For example, Linus Torvalds does not plan or insist on putting these patches into the mainstream kernel because they are still being tested.

Compile the following statement into an internal statement or compile it into an example. rc.firewall.txt can be used.

*

CONFIG_PACKET
*

CONFIG_NETFILTER
*

CONFIG_IP_NF_CONNTRACK
*

CONFIG_IP_NF_FTP
*

CONFIG_IP_NF_IRC
*

CONFIG_IP_NF_IPTABLES
*

CONFIG_IP_NF_FILTER
*

CONFIG_IP_NF_NAT
*

CONFIG_IP_NF_MATCH_STATE
*

CONFIG_IP_NF_TARGET_LOG
*

CONFIG_IP_NF_MATCH_LIMIT
*

CONFIG_IP_NF_TARGET_MASQUERADE

The above are the minimum options required to ensure the normal operation of rc.firewall.txt. The options required by other scripts are described in the relevant sections. Currently, we only need to pay attention to the script to be learned.
2.3. Compilation and Installation

Next, let's take a look at how to compile iptables. The configuration and compilation of many iptables components are associated with Kernel configuration and compilation. It is important to understand this. Some Linux products are pre-installed with iptables, such as Red Hat, but iptables is not enabled by default. Later, we will introduce how to enable it and the iptables of other Linux products.
2.3.1. Compile

First, extract the iptables package. Here, I use iptables 1.2.6a as an example. (Note: During my translation, the latest version is 1.2.9, and many improvements have been made and some bugs have been fixed, added match and target .). Command bzip2-cd iptables-1.2.6a.tar.bz2 | tar-xvf-(of course you can also use tar-xjvf iptables-1.2.6a.tar.bz2, but this command may not work for some older versions of tar) unzip the package to the directory iptables-1.2.6a, the INSTALL file contains a lot of useful information for compilation and running.

In this step, you can configure and install some additional modules, or add some options for the kernel. Here we only check and install some patches that are not included in the kernel. Of course, more patches in the experiment phase will only be used for some other operations.

Note

It is not a good idea to install some patches only in the experiment phase. In this step, you will encounter a lot of interesting matching and packet operations, but they are still experimenting.

To complete this step, we need to use the following commands in the iptables directory:

Make pending-patches KERNEL_DIR =/usr/src/linux/

The KERNEL_DIR variable points to the real path of the kernel source code. Generally, it is/usr/src/linux/, but it will be different. It depends on your Linux product.

Note

In short, only some patches will be asked whether to join the kernel, and Netfilter developers have a large number of patches or accessories to join the kernel, but it will take a while to experiment. If you want to install these things, run the following command:

Make most-of-pom KERNEL_DIR =/usr/src/linux/

This command will install some patch-o-matic (the name of the patch in the netfilter world) and ignore the extremely extreme part, which may cause serious damage to the kernel. You need to know the role of this command and the impact of these commands on the original kernel code. Fortunately, there will be a prompt before you choose. The following command can install all patches-o-matic ).

Make patch-o-matic KERNEL_DIR =/usr/src/linux/

Carefully read the help files for each patch, because some patch-o-matic will damage the kernel, while some may damage other patches.

Note

If you do not plan to use patch-o-matic to patch the kernel, the above commands are not needed and they are not required. However, you can use these commands to see what is interesting, without affecting anything.

After installing patch-o-matic, we should re-compile the kernel because some patches are added. But don't forget to re-configure the kernel. The existing configuration file does not contain the patch information you have added. Of course, you can also compile iptables and then the kernel.

Next we should compile iptables and use the following simple command:

Make KERNEL_DIR =/usr/src/linux/

Iptables should be compiled. If not, consider where the problem is, or subscribe to Netfilter mailing list, where someone may help you.

If everything goes well, we should install iptables, which is almost no problem. We use the following command to complete this step:

Make install KERNEL_DIR =/usr/src/linux/

Now we are done. If you haven't re-compiled or installed the kernel before, you have to do it now. Otherwise, you still cannot use the updated iptables. Let's take a look at INSTALL, which contains detailed installation information.
2.3.2. install on Red Hat 7.1

Red Hat 7.1 uses the 2.4.x kernel and supports Netfilter and iptables. Red Hat contains all the basic programs and required configuration files, but B class = COMMAND> ipchains are used by default. "Why iptables cannot be used" is the most common problem. Let's talk about how to disable ipchains and use iptables.

Note

Red Hat 7.1 pre-installed iptables versions are somewhat old. before using it, you may want to install a new version and compile the kernel by yourself.

We need to disable ipchains first, and do not want to run it any more. To do this, we need to change some file names under the/etc/rc. d/directory. Run the following command:

Chkconfig -- level 0123456 ipchains off

This command renames all soft connections pointing to/etc/rc. d/init. d/ipchains to K92ipchains. Starting with "S" indicates that the script will be run by the initialization script at startup. If it starts with K, it indicates that the service is terminated or will not run at startup. In this way, ipchains will not run after being started.

To terminate a running service, use the service command. The command to terminate the ipchains service is:

Service ipchains stop

Now we can start the iptables service. First, determine which runtime layer to run, generally 2, 3 and 5. These layers are of different use:

*

2. Multi-user environments without NFS are only different from Layer 3 in multi-user environments without network support.
*

3. Multi-user environment is the layer we generally use.
*

5. X11: graphic interface.

Run the following command to enable iptables to run on these layers:

Chkconfig -- level 235 iptables on

You can also use this command to enable iptables to run on other layers. This is not necessary, because Layer 1 is a single-user mode, which is generally used for maintenance; Layer 4 is reserved; layer 6 is used to shut down the computer.

Start iptables:

Service iptables start

No rules have been defined in the script iptables. There are two ways to add rules in Red Hat 7.1: the first method is to edit/etc/rc. d/init. d/iptables. Note that when you use RPM to upgrade iptables, existing rules may be deleted. Another method is to load the rule first, and then use the command iptables-save to save the rule to the file, then the directory rc. d script (/etc/rc. d/init. d/iptables.

Let's first explain how to set/etc/rc. d/init. d/iptables using the cut and paste method. To enable iptables to load rules on a computer, you can place the rules in the "start" section or function start. Note: If you place rules in the "start)" section, do not run start () in the "start)" section, and edit the "stop)" section, this allows the script to know how to deal with it when it is shut down or enters a layer that does not require iptables. Check the settings of the "restart" and "condrestart" sections. Be sure to note that the changes we made may be deleted when iptables is upgraded, regardless of whether it is automatically upgraded through Red Hat or by RPM.

The following describes the second method: first write a rule script, or directly use the iptables command to generate the rule. To meet your needs, do not forget to test whether there is a problem. After confirming that the rule is normal, run the command iptables-save to save the rule. Generally, iptables-save>/etc/sysconfig/iptables is used to generate the file/etc/sysconfig/iptables for saving the rules. You can also use service iptables save, it can automatically save rules in/etc/sysconfig/iptables. When the computer starts, the script under rc. d will use the command iptables-restore to call this file, so that the rules are automatically restored.

It is best not to mix the above two methods to avoid the mutual influence of rules defined by different methods, or even make the firewall settings invalid.

Now, you can delete the pre-installed ipchains and iptables to avoid conflicts between the new and old versions of iptables. In fact, this is only required when you install it from the original code. But in general, there will be no issue of mutual influence, because rpm-based packages do not use the default directory of the original code. Run the following command to delete a file:

Rpm-e iptables

Why should I keep it without ipchains? Delete it! The command is as follows:

Rpm-e ipchains

After all the hardships, the Victory finally arrived. You can install iptables from the source code. Delete the old ones.
Chapter 3. Tables and links

In this chapter, we will discuss the sequence of data packets and how to traverse different chains and tables. Later, you will know how important this order is when writing rules by yourself. Some components are shared by iptables and the kernel, for example, packet routing judgment. It is important to understand this, especially when you use iptables to change the route of data packets. This will help you understand how and why data packets are routed in that way. A good example is DNAT and SNAT. Don't forget the role of TOS.
3.1. Overview

When a packet arrives at the firewall, if the MAC address is correct, it will be received by the corresponding driver in the kernel, and then a series of operations will be performed to decide to send the packet to the local program, or forward it to another host or something else.

Let's first look at a local data packet. It takes the following steps to reach the program to receive it:

There is a word mangle in the following. I really didn't think of a proper word to express this meaning, just because my English is too bad! I can only write what I understand. This word indicates that some transmission features of data packets are modified. operations allowed in the mangle table are TOS, TTL, and MARK. That is to say, in the future, we only need to see this word to understand its role.

Table 3-1. A local package (that is, our own machine)
Step (Step) Table (Table) Chain (Chain) Comment (Comment)
1. Online transmission (for example, Internet)
2. Enter the interface (for example, eth0)
3. the mangle PREROUTING chain is used to mangle data packets, such as changing TOS.
4. nat PREROUTING is mainly used for DNAT. Do not worry about this chain, because in some cases the package will slide.
5. route determination. For example, if a packet is sent to a local device, it must be forwarded.
6. the mangle INPUT packet is sent to the local program after the route.
7. filter INPUT all local-targeted packages must pass through this chain. The filtering conditions for these packages are located here, no matter where they come from.
8. You have reached the local program (for example, the service program or customer program)

Note: compared with the previous (ipchain), data packets are transferred by the INPUT chain instead of the FORWARD chain. This is more logical. It may not seem easy to understand at first, but I will suddenly realize it after careful consideration.

Now let's take a look at the steps to take if the source address is a local package:

Table 3-2. Local packages
Step Table Chain Comment
1. Local programs (such as service programs or customer programs)
2. Determine the route. Use the source address, outbound interface, and other information.
3 The mangle OUTPUT can be a mangle package here. We recommend that you do not filter data here, which may cause side effects.
4. The nat OUTPUT chain performs the DNAT operation on the packets sent from the firewall.
5. filter OUTPUT filters the packets sent locally.
6. the mangle POSTROUTING chain is mainly after the package DNAT. Once a local package is generated, it must be processed by the routing code. However, the specific location of the package must be determined after the NAT code is processed. So this is called the actual route .), Before leaving the local device, mangle the package. There are two types of packages that will pass through here, the packages generated by the sub-firewall itself, and the packets to be forwarded.
7. nat POSTROUTING performs SNAT here. But do not filter here, because there are side effects, and some packages will slide over, even if you use the DROP policy.
8. Exit interface (for example, eth0)
9 online transmission (for example, Internet)

In this example, we assume that a package is intended for one machine in another network. Let's take a look at the journey of this package:

Table 3-3. forwarded packets
Step Table Chain Comment
1. Online transmission (for example, Internet)
2. Enter the interface (for example, eth0)
3. mangle PREROUTING mangle data packets, such as changing TOS.
4. nat PREROUTING is mainly used for DNAT. Do not worry about this chain, because in some cases the package will slide. SNAT will be performed later.
5. route determination. For example, if a packet is sent to a local device, it must be forwarded.
6. the mangle FORWARD package is sent to the FORWARD chain of the mangle table, which is used in special cases. Here, the package is mangle (remember what mangle means ). This mangle occurs after the original route judgment and before the goal of the last packet change (note: this is what the following FORWARD chain does, because of its filtering function, may change the destination of some packages, such as discarded packets ).
7. The filter FORWARD package is sent to this FORWARD chain. Only packets that need to be forwarded come here, and all Filtering for these packets is also carried out here. Note that all packets to be forwarded must go through this process, whether from the Internet to the Intranet or from the Intranet to the Internet. Consider this when writing rules by yourself.
8 mangle POSTROUTING is also applicable to some special types of packages, the two links of the mangle table are used in special applications ). In this step, mangle is done after all the operations to change the destination address of the package are completed, but the package is still local.
9 nat POSTROUTING is used for SNAT, and also includes Masquerade ). But do not filter here, because some packages pass even if they do not meet the conditions.
10 Exit interface (for example, eth0)
11. It was transferred online again (for example, LAN)

As you can see, packages take many steps, and they can be blocked on any chain, or any problematic place. Our main interest is the overview of iptables. Note that there are no special links or tables for different interfaces. All packets to be forwarded by the firewall/router must go through the FORWARD chain.

Caution

In the above case, do not filter on the INPUT chain. INPUT is specially used to operate packages with the target address of our machines. They are not routed to other places.

Now, let's look at the different chains used in the above three cases. The figure is as follows:

To figure out the above figure, you can consider it like this. In the first route judgment, instead of a local package, we will send it through the FORWARD chain. If the destination of the package is the IP address of the local monitor, we will send this package through the INPUT chain and finally reach the local.

It is worth noting that during the NAT process, the destination address of the packet sent to the local machine may be changed in the PREROUTING chain. This operation occurs before the first route, so the packet can be routed only after the address is changed. Note that all packages will go through a path. If you send a DNAT package back to its original network, the package will continue to finish the remaining links in the corresponding path until it is sent back to the original network.

Tip

For more information, see rc.test-iptables.txt. This script includes rules that show you how a package uses tables and links.
3.2. mangle table

This table is mainly used for mangle packages. You can use mangle matching to change the features of packages such as TOS.

Caution

We strongly recommend that you do not filter this table, whether it is DANT, SNAT or Masquerade.

The following are the only operations in the mangle table:

*

TOS
*

TTL
*

MARK

The TOS operation is used to set or change the service type domain of data packets. This is often used to set policies such as how packets on the network are routed. Note that this operation is not complete, and sometimes it is not desirable. It is not available on the Internet, and many routers do not notice this domain value. In other words, do not set packets sent to the Internet unless you plan to rely on TOS for routing, such as using iproute2.

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.