Iptables basics of Cainiao schools

Source: Internet
Author: User
Article title: Iptables basics of Cainiao School. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
   Preface
The firewall has always been regarded as a complex lab on campus, and few Department administrators have the courage to carry out planning experiments. Basically, this handout can be read as a test report, the author holds the spirit that I do not go to hell, who is going to hell, who is risking his life, and has made great achievements. he also hopes to inspire others who are better at home in China, together, we will conduct public welfare research that is conducive to campus networks!
  
   1. what is firewall?
A firewall is a combination of hardware and software devices that can be connected between two or more networks and clearly separate physical connections. The isolated network can communicate with each other through the packet forwarding technology. through the security management mechanism of the firewall, it can determine which data can be circulated and which data cannot be circulated, to achieve network security protection.
Firewall products can be roughly classified as hardware-based firewalls and software-based firewalls. However, both hardware-based and software-based firewalls require hardware for online intercommunication, software is also required to set security policies. Strictly speaking, there is not much difference between the two. We can only distinguish between the hardware used and the operating system. hardware firewalls use proprietary hardware, while software firewalls use general computer hardware, hardware Firewalls use proprietary operating systems, while software firewalls use general operating systems.
Firewalls are classified according to their operating methods. they can be classified into Packet Filter and Application-Level Gateway) circuit Layer Gateway (Circuit-Level Gateway ). The encapsulation filter firewall is widely used. The iptables firewall described in this article belongs to this type.
Packet filtering is the first firewall technology to be implemented. it operates at the IP layer under the TCP/IP layer-4 architecture. The function of the packet filter is to check each IP address data packet passed. if the data content contained in the header meets the filter conditions, further processing will be performed. the main processing methods include: allow, drop, or reject ). To filter packets, the firewall must be able to analyze the source IP addresses and destination IP addresses of packets, you must also be able to check data such as the package type, source port number and destination port number, packet flow direction, Nic interface for packets entering the firewall, and online status of TCP.
Firewall costs remain high for various reasons. it is impossible for poor primary and secondary schools to purchase a firewall. due to the popularity of Linux, Linux is used as a software-based firewall, it seems to be a good solution. This article will introduce the latest and most powerful iptables firewall software on Linux to build filtering rules suitable for schools, to enable schools with insufficient funds to have an easy-to-use firewall to guard the door of campus networks.
  
   II. Brief History of Linux firewall evolution
The earliest firewall software in Linux is called ipfw. ipfw can analyze IP packet headers, identify the source IP address and destination IP address of the packet, the type of the packet, the source port number and destination port number, the packet flow direction, and the interface of the packet entering the firewall ...... the analysis results are used to compare the rules for packet filtering, and the IP camouflage function is also supported. This function can solve the problem of insufficient IP addresses. Unfortunately, this program lacks elastic design, it is impossible to set up a rule combination (ruleset) on its own for more streamlined settings. at the same time, it also lacks the url translation function. it cannot cope with increasingly complex network environments and is gradually eliminated.
The ipchains are not only easier to understand the command syntax, but also more powerful than ipfw. ipchains allow custom rule combinations (ruleset), which are called user-define chains. through this design, we can combine rules related to each other and jump to this group of rules for filtering as needed, effectively reducing the number of rules. in the past, ipfw can only perform sequential filtering, as a result, the rules are smelly and long, and the problem becomes worse. In addition to this obvious benefit, ipchains can combine its own port functions with the redir program's packet forwarding mechanism to simulate the url translation capability and meet the complete needs of NAT, it is a mature set of firewall works.
The emergence of firewall software has indeed made the hackers sleep at night, because the firewall's barrier can effectively make the stand-alone hosts with no security protection on the internal network not exposed, but also effectively reduce the visibility of the server, reducing the chance of being attacked. The network detection technology used by hackers in the past has been severely challenged. more and more attack objects are hiding behind the firewall, making it difficult for hackers to approach. Therefore, they must address new situations, this article does not discuss the new detection technology to avoid firewall checks, find targets, and attack intrusions, please refer to the technical documents organized by CERT at www.cert.org. if you want to see Chinese, please connect to www.cert.org.tw.
As the new generation successor of ipchains, iptables, of course, also develops some countermeasures for the hacker's constantly new detection technology, that is, to make a more detailed analysis of the online status of packets, such: whether it is a new online or response packet, whether it is online, whether it has lost response, whether the online time is too long ...... and so on. This analysis can block some vulnerabilities that may be exploited by hackers (for details, refer to the description below). In addition, it also develops the true encapsulation rewrite capability, without the help of other programs, you do not need to simulate website translation. In addition, iptables is also directly supported by the system core, and you do not need to re-compile the core like ipchains.
The superior performance of iptables has replaced ipchains and become the mainstream of network firewalls, while ipchains have not been eliminated. Currently, ipchains has been transformed into a single-host firewall. when the new version of Linux is installed, it is automatically installed and enabled to protect unused communication ports on a single machine.
  
   III. Introduction to iptables firewall
Iptables firewall commands are very similar to ipchains. people who have used ipchains should be very easy to use. However, the iptables mechanism is very different from ipchains. the ipchains concept is used to set rules, this will make the firewall unable to operate normally. The biggest difference between ipchains and iptables lies in the different definitions of the INPUT, FORWARD, and OUTPUT network types. these three network types are part of the TCP/IP driver. The structure is shown in, it is between the NIC driver and application. INPUT, OUTPUT, and LOOPBACK are enabled in the Linux core preset, and the FORWARD function must be enabled on its own. you can use the following command, or directly modify the/etc/sysconfig/network configuration file:
Echo "1">/proc/sys/net/ipv4/ip_forward
The figure on the left shows the operation of ipchains.
The following table describes how ipchains process the flow of packets:
• Ip input: all packets are processed by the ip input function. Therefore, when filtering rules are set, they are almost all set in the INPUT rule refining.
• Ip forward: the destination IP address is not the local IP address. these packets need to be transferred further. This function is used to handle IP spoofing and Port forwarding.
• Ip output: all outbound packets are processed by this function. generally, no rules are required.
In addition to the preceding three functions, iptables also uses two new functions: Prerouting and Postrouting. Now let's compare the operation modes of iptables (the loopback interface is the same, so it is omitted without painting ):
  
The following table describes how iptables processes the flow of packets:
• Ip input: only packets sent to the local machine are processed by the INPUT function. Therefore, packets from the internal network are allowed to be opened unconditionally. packets from the external network are filtered out to determine whether the packets are response packets, if yes, allow access.
• PREROUTING: This function is used to process the packets to be transferred. This function is used to translate the destination IP address (DNAT ).
• Ip forward: all forwarded packets are processed here, and the filtering rules are the most complex.
• POSTROUTING: This function is used to translate the source IP address (SNAT) before the transfer packet is sent ).
• Ip output: This function processes the packets sent from the local machine, and usually releases all packets.
Both iptables and ipchains can define their own rule groups (rule-set). rule groups are called rule groups, there are also corresponding rule refining (INPUT, FORWARD, OUTPUT, Prerouting, and Postrouting). to be different from custom rule refining, these rules are called built-in rule refining, its operation process simulation is as follows:
  
  
From the above two hypothetical figures, it is not difficult for students to understand why ipchains is called chains, because it concatenates all rules into a sequence and checks and filters them one by one, just like a chain, one ring, and one ring, in the filtering process, if one rule is met, it will be processed immediately. if the processing action is to jump to a rule Group, the system will continue to check the rule settings in the group, however, if the processing action is ACCEPT, REJECT, DROP, REDIRECT, or MASQUERADE, the filter program will be interrupted and the subsequent rule settings will not be checked. under such a structure, sometimes the reverse results are generated when the rule order is reversed, so you must be cautious when setting the firewall.
Iptables uses the rule stack for filtering. when a packet enters the NIC, it first checks Prerouting and then checks the destination IP to determine whether the packet needs to be transferred out, then, it will jump to INPUT or Forward for filtering. if the packet needs to be transferred for processing, it will check Postrouting. if it is from the local packet, it will check OUTPUT and Postrouting. If a rule is met during the process, the processing will be performed in addition to ACCEPT, REJECT, DROP, REDIRECT, and MASQUERADE, there are also more LOG, ULOG, DNAT, SNAT, MIRROR, QUEUE, RETURN, TOS, TTL, MARK, and so on, some of the processing actions will not interrupt the filtering program, some processing actions will interrupt the filtering of the same rule and continue filtering the next rule according to the preceding process (note: This is different from ipchains ), until the rule check in the stack is complete. The advantage of this mechanism is that we can perform complex and multi-packet filtering. In short, iptables can be used to filter packets (tables) instead of chins ).
Although iptables must adopt a complicated filtering process to expand the firewall function, the rule setting under the same rule refining still has a sequential relationship in actual applications, therefore, you must pay attention to the logic when setting rules.
  
   IV. set campus network security policies
Before setting a firewall, we must formulate a security policy based on the security requirements of the campus network. before formulating a security policy, we must collect the following information:
1. find the server to be filtered and protected
2. list the network services that the protected server will provide
3. what kind of protection is required for normal workstations?
4. understand network architecture and server placement
Based on the data, we can decide the security policy. The following example uses Shi Pai as an example:
1. NAT virtual network is used in the school. the number of IP addresses requires two groups of C. all IP addresses must be disguised as IP addresses.
2. campus security

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.