Analysis and Application of iptables and layer7 protocols

Source: Internet
Author: User


This introduction to iptabbles + l7 is divided into two parts: the first part is the l7 patch for the system upgrade, the second part is the application of iptables + l7: patch the system. In order to use the l7 protocol for control, we need to patch the file in the kernel and re-compile the kernel file. We need to be careful in each of the following steps. Otherwise, we have to start over again ~ If you need software, Q395061796
Software requirements:
1. disassemble the Kernel File and layer7 file: [root @ zhangc ~] # Tar jxvf linux-2.6.25.19.tar.bz2-C/usr/src/[root @ zhangc ~] # Tar zxvf netfilter-layer7-v2.20.tar.gz-C/usr/src/2, put the patch file into the kernel folder [root @ zhangc ~] # Cd/usr/src/linux-2.6.25.19/[root @ zhangc linux-2.6.25.19] # patch-p1 & lt;/usr/src/netfilter-layer7-v2.20/kernel-2.6.25-layer7-2.20.patch
3. Use the Kernel File to configure the new kernel [root @ zhangc linux-2.6.25.19] # cp/boot/config-2.6.18-164.el5. config [root @ zhangc linux-2.6.25.19] # make menuconfig selects some of the required service options as M and compiles them into modules. Option 1:
 
Compile the following three modules
 
Option 2:
 
Compile the following four modules
4. Compile the kernel, install the module, install the kernel [root @ zhangc linux-2.6.25.19] # make [root @ zhangc linux-2.6.25.19] # make modules_install [root @ zhangc linux-2.6.25.19] # make install
5. Uninstall older versions of iptables [root @ zhangc ~] # Rpm-e iptables-nodeps6. disassemble and merge iptables + layer7 Patch [root @ zhangc ~] # Tar jxvf iptables-1.4.2.tar.bz2-C/usr/src/[root @ zhangc ~] # Cd/usr/src/netfilter-layer7-v2.20/iptables-1.4.1.1-for-kernel-2.6.20forward/[root @ zhangc iptables-1.4.1.1-for-kernel-2.6.20forward] # cp */usr/src/iptables-1.4.2/extensions/
7. compile and install the [root @ zhangc iptables-1.4.1.1-for-kernel-2.6.20forward] # cd/usr/src/iptables-1.4.2/[root @ zhangc iptables-1.4.2] #. /configure -- prefix =/-- with-ksource =/usr/src/linux-2.6.25.19/[root @ zhangc iptables-1.4.2] # make [root @ zhangc iptables-1.4.2] # make install
8. Install the l7-prorocols mode package [root @ zhangc ~] # Tar zxvf Co., l7-protocols-2008-10-04.tar.gz-C/etc/[root @ zhangc ~] # Mv/etc/l7-protocols-2008-10-04 // etc/l7-protocols9. Modify the file ip6tables, can enable service normally [root @ zhangc ~] # Vim/etc/init. d/ip6tables/etc/init. d/iptables [root @ zhangc ~] # Service iptables save [test this command] [root @ zhangc ~] # Service iptables restart
 
10. The system has been repaired. You can use iptables + l7.
Part 2: iptables + l7 application l7 rule format: by providing the firewall with instructions on what to do for information packets from a source address, to a destination, or with a specific protocol type, filter rule control information packages. Use the Special Command iptables provided by the iptables system to create these rules and add them to the chain in the kernel space-specific information package filtering table. The general syntax for adding, removing, and editing rules is as follows:
Iptables [-t table] command [match] [target] 1. The table [-t table] Option allows any table outside the standard table. A table is an information packet filtering table that contains rules and links that only process specific types of information packets. There are three available table options: filter, nat, and mangle. This option is not required. If not specified, the filter is used as the default table. Filters are used to Filter general information packets, including INPUT, OUTPUT, and FORWARD chains. Nat is used to FORWARD information packets, including PREROUTING, OUTPUT, and POSTROUTING chains.
2. command is the most important part of the iptables command. It tells the iptables command what to do, such as inserting rules, adding rules to the end of the chain, or deleting rules. -A or append: append A rule to the end of the chain-D or delete: delete A rule-P or policy: Set the default target of the chain-R or replace: replace the corresponding rule-L or list of the specified chain: list all rules in the specified chain-F or flush: by default, rules in all chains are deleted. If you specify a chain name, this command deletes all rules-N or new-chain in the chain: Creates a new chain (the name is the name specified in the Command)
-X: delete a user's empty chain-I refers to the data inbound entry NIC-o refers to the egress NIC-t refers to the table-p, and the protocol-m Refers to the call module -j indicates the action executed. sport indicates the source port. dport indicates the destination port. syn indicates the TCP packet with the syn flag. SYN is used to initialize a TCP connection. If no server is running on your machine, no SYN packet will be sent to you.
3. match (match) the optional match part of the iptables command specifies the characteristics (such as source address, Destination Address, protocol, etc.) that the information package and rule match should have ). Matching can be classified into general matching and protocol-specific matching. -P or-protocol: This generic protocol match is used to check certain protocols. ALL is the default value. The default value is that all ip addresses match. Available! Symbol indicates that it does not match this item. -S or source: match the source address of the data packet. Available! Indicates that it does not match this option. -D or destination: match the destination address of the data packet. Available! Indicates the inverse.
4. the target object is an operation specified by a rule that performs these operations on information packages that match those rules. In addition to allowing users to define targets, there are also many available target options. Many other goals used to establish advanced rules, such as LOG, REDIRECT, MARK, MIRROR, and MASQUERADE. Here we will give a brief introduction to the three commonly used ones.
ACCEPT: when the data packet exactly matches the rule, the data packet passes through. DROP: when the data packet exactly matches the rule, the data packet is discarded. MASQUERADE: when a data packet exactly matches the rule, the source IP address of the data packet is disguised as the IP address of the current interface.
5. usage: add rules: block data packets from A specific IP address range # Iptables-t filter-a input-s 192.168.100.0/24-j DROP block all data packets flowing to the attacker's IP address # Iptables-t filter- OUTPUT-d 192.168.100.0/24-j DROP
Delete rule: # Iptabels-t filter-d output-d 192.168.100.0/24-j DROP default policy # iptabels-p input drop address disguise # iptabels-t nat-a postrouting-s 192.168.100.0/24- o eth1-j MASQUERADE
6. common iptables modules (1) string (string matching, which can be used for content filtering) # iptables-I FORWARD-d 192.168.3.0/24-p tcp -- sport 80-m string -- string "ad"-j DROP (2) comment (Remarks match, supports up to 256 characters) # iptables-I FORWARD-s 192.168.3.159-m string -- string "qq.com"-j DROP-m comment -- comment "denny go to qq.com" (3) connlimit (the maximum number of connections at the same time) -- maximum number of connlimit-abve n. -- The mask of the connlimit-mask n group of hosts. The default value is connlimit-mask 32, that is, each ip address.
# Iptables-p tcp -- syn -- dport 23-m connlimit -- connlimit-above 2-j REJECT (4) time (time matching) timestart start time. -- Timestop end time. # Iptables-t filter-a input-m time -- timestart 8:00 -- timestop 18:00 -- days Mon, Tue, Wed, Thu, Fri (5) iprange (ip Range matching) # iptables-a forward-m iprange -- src-range 192.168.1.5-192.168.1.124-j ACCEPT (6) state module -- three states of state: NEW, ESTABLISHED, RELATED # iptables-t filter-A-m state -- state ESTABLISHED, RELATED-j ACCEPT
7. Case study: Objective: To access the FTP server during work hours of the 100.21-30 network segment, prohibit access to the sina website during work hours, and prohibit the use of thunder and QQ Software.
1. Enable the forwarding function to reject all requests through [root @ zhangc ~] # Vim/etc/sysctl. conf [root @ zhangc ~] # Sysctl-p 2. Access to the FTP server is allowed through [root @ zhangc ~] # Iptables-t filter-a forward-m iprange -- src-range 192.168.100.21-192.168.100.30-m time -- timestart 08:00 -- timestop 20:00 -- weekdays Mon, Tun, Wed, Thu, fri-p tcp -- dport 80-j ACCEPT
Allow DNS to pass [root @ zhangc ~] # Iptables-t filter-a forward-m iprange -- src-range 192.168.100.21-192.168.100.30-m time -- timestart 08:00 -- timestop 20:00 -- weekdays Mon, Tun, Wed, Thu, fri-p udp -- dport 53-j ACCEPT
Standard packet filtering: each time a service is disabled, the outgoing and incoming packets must be filtered out, which is a little complicated. [Root @ zhangc ~] # Iptables-t filter-a forward-m iprange -- src-range 192.168.100.10-192.168.100.20-m time -- timestart 08:00 -- timestop 20:00 -- weekdays Mon, Tue, Wed, Thu, fri-p tcp -- dport 21-j ACCEPT [root @ zhangc ~] # Iptbales-t filter-a forward-p tcp -- sport 21-j ACCEPT [port 21 for receiving server requests] [root @ zhangc ~] # Iptables-t filter-a forward-p tcp -- sport 20-j ACCEPT [Server Response] [root @ zhangc ~] # Iptables-t filter-a forward-p tcp -- dport 20-j ACCEPT [the client receives Port 20]
Status package filtering: records the status of a session. data packets sent out of the firewall are allowed to pass through by default. [Root @ zhangc ~] # Iptables-t filter-a forward-m iprange -- src-range 192.168.100.10-192.168.100.20-m time -- timestart 08:00 -- timestop 20:00 -- weekdays Mon, Tue, Wed, Thu, fri-p tcp -- dport 21-j ACCEPT [root @ zhangc ~] # Iptables-t filter-A-m state -- state ESTABLISHED, RELATED-j ACCEPT
3. Prohibit Access to sina, thunder, and qq during work hours [root @ zhangc ~] # Iptables-t filter-a forward-m iprange -- src-range 192.168.145.21-192.168.145.30-m time -- timestart 08:00 -- timestop 20:00 -- weekdays Mon, Tue, Wed, Thu, fri-p tcp -- dport 80-j ACCEPT [root @ zhangc ~] # Iptables-t filter-a forward-m iprange -- src-range 192.168.220.21-192.168.220.30-m time -- timestart 08:00 -- timestop 20:00-weekdays Mon, Tun, Wed, Thu, fri-m string -- string "sina" -- algo bm-j DROP
[Root @ zhangc ~] # Iptables-a forward-m iprange -- src-range 192.168.145.21-192.168.145.30-m time -- timestart 08:00 -- timestop 20:00 -- weekdays Mon, Tue, fri-m layer7 -- l7proto qq-j DROP [root @ zhangc ~] # Iptables-a forward-m iprange -- src-range 192.168.145.21-192.168.145.30-m time -- timestart 08:00 -- timestop 20:00 -- weekdays Mon, Tue, Fri-m layer7 -- l7proto xunlei-j DROP
 

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.