Ipchains application details

Source: Internet
Author: User
For more information about Ipchains, see Linux Enterprise Application-Linux server application. Basic settings of Ipchains

First, you must change the core of your Linux system to a version that supports data packet filtering. You can check whether the/proc/net/ip_fwchains file in the system exists. If yes, your system core already supports the data packet filtering function. If not, add the following in the core configuration file:
CONFIG_FIREWALL = y
CONFIG_IP_FIREWALL = y
After two options, recompile your system core.
For more information about Ipchains, see the following section. The following describes the basic operations. At the beginning, the system defaults to three built-in chains: input, output, and forward to handle inbound and outbound and transfer rules respectively.
The basic operations of Ipchains are as follows:
1. Create a new chain: ipchains-N chain
2. delete an unwanted chain: ipchains-X chain
3. Change the polich: ipchains-P chain policy of the chain.
4. List the rule of all chains: ipchains-L [chain]
5. delete all the rule of the chain: ipchains-F [chain]
6. Return the chain counter to zero: ipchains-Z [chain]
If the preceding [chain] is not specified, it indicates all the chains.
Change the chains rule:
1. Add A new rule: ipchains-A chain
2. Delete the first matching rule: ipchians-D chain
3. Rule for deleting a location number: ipchains-D chain rulenum
4. Insert a new rule for the number at a location: ipchains-I chain [rulenum]
5. Change the rules for a location number: ipchains-R chain [rulenum]
The above location numbers are the order of Rule comparison (priority), usually 1 is not added (priority)

Detailed instructions on Ipchains

Use ipchains to add and process data packet comparison rules:
Ipchains-[ADC] chain rule-specification [options]
Ipchains-[RI] chain rulenum rule-specification [options]
-A, -- append
Add one or more rules at the end of the specified chain. When the source or destination name corresponds to several addresses, this rule also adds each possible address.
-D, -- delete
Deletes one or more rules from the specified chain. It can be the location number of the rule in the chain or a rule for comparison.
-C, -- check
It is used to test whether a data packet that meets the specified rule type passes the current system rule.
The following are the parameters available for setting a rule:
-P, protocol [!] Protocol
The Protocol to be checked for this rule can be tcp, udp, icmp or all, or the number or name of a protocol. Add "!" before the agreement A symbol is used to check for negation. 0 indicates all.
-S, -- source [!] Address [/mask] [!] [Port [: port]
This parameter is used to specify the data packet source. The Address can be the name or IP Address on the network. The mask is the mask of the source network. You can add the source port number (IP packet) or ICMP type (ICMP packet ). You can use a number or service name to indicate the port or ipchains-h icmp to view the ICMP type that can be expressed.
-- Source-port [!] [Port [: port]
Used to specify the source port separately
-D, destination [!] Address [/mask] [!] [Port [: port]
Target address (set similar source)
-- Destination-port [!] [Port [: port]
Used to specify the destination port separately
-- Icmp-type [!] Typename
Used to separately represent the ICMP Type
-J, -- jump target
Specifies the target of the rule. That is, what needs to be done after the rule is met. The target can be another chain (recompare) or a special target chain.
-I, interface [!] Name
Indicates the network interface name used to receive or send data packets. If this item is not set, all network interfaces are displayed. If the interface name is followed by a "+", it indicates all network interfaces starting with this name.
[!] -F, -- fragment
This indicates that this rule will only refer to the second or later part of the data packet. If the data packet is divided into several parts. Because this cannot identify the source or destination port of the data packet, the data packet will fail to be compared as long as the rule contains the specified port or type.
-B, -- bidirectional
In bidirectional mode, this rule performs bidirectional Comparison on IP packets. This is the same as directly setting two rules with the same source and target for each other.
-V, -- verbose
Detailed output. This parameter allows the list command to display the network interface address, rule options, options, and TOS masking. The contents of the data packet and the size counter are also displayed. "K", "M", and "G" after the numbers represent 1000000000, and respectively. Add others
, Insert, or delete commands also display detailed data.
-N, -- numeric
Digital output. The IP address or port number is displayed in numbers. The default system uses the associated name as much as possible.
-L, -- log
Enable the core data packet record function. If this option is added to a rule, the core will use the printk () function to list the data packet information as long as there is a successful data packet.
-O, -- output [maxsize]
Copy the successful data packets to the userspace device. This is used to use the firewall in userspace.
-M, mark markvalue
This is a 32-bit long positive integer used to compare successful data packets. This option can be used only when the core internal experts need to be modified.
-T, -- TOS andmask xormask
The masking value is used to modify the TOS field of the IP packet header. After a data packet is successfully compared to this rule, the TOS field of the data packet will perform the and operation with the value of andmask, and then perform the xor operation with the value of xormask. TOS affects the system's processing of data packets (such as the priority of data packet transmission ).
-X, exact
Extend the number. When this parameter is used, the counter numbers listed by the-L command are expressed in correct numbers (K, M, G, and other abbreviations are not used)
[!] -Y, -- syn
Only the SYN bit setting and the ACK and FIN bit are used to clear the TCP packet comparison rules. This type of data packet is usually used to start a TCP connection. For example, if a file accepts such data packets on the network interface, a connection line cannot be opened to the local machine, but the local machine can open an external connection line.
-- Line-numbers
Used to specify the position of this rule in the chain (priority)
-- No-warnings
Disable all warning messages
The following are some functions ipchains are used to handle rules and chain concerns:
Ipchains-D chain rulenum [options]
Ipchains-[LFZNX] [chain] [options]
-D, -- delete
Delete one or more rules from the selected chain. Here, rulenum can be the rule number or a rule to be compared.
-L, -- list
Lists All rules in the selected chain. If no chain is specified, the rules in all chains are listed.
-F, -- flush
Clear the selected chain. This serves the same purpose as deleting all rules one by one.
-Z, -- zero
Returns the data packet book and data packet size counters to zero.
-N, -- newchain
Add a user-defined chain.
-X, -- delete-chain
Deletes the specified user-defined chain. If this parameter is not specified, all user-defined chains are deleted.
The following describes how to use ipchains to set processing actions after successful comparison:
Ipchains-P chain target [options]
-P, -- policy
Set the next step for successful comparison in the chain. These operations can be ACCEPT, DENY, REJECT, MASQ, REDIRECT, or RETURN special values or another user-defined chain. ACCEPT indicates to allow data packets to pass through. DENY discards data packets. REJECT will discard data packets like DENY. However
An ICMP message is sent to the source to indicate that the data packet has been discarded. MASQ is only effective for sending this action and user-defined chain processing. You must add the CONFIG_IP_MASQUERADE option to the kernel before compiling. With this processing, the data packet will be disguised as the data packet sent by the local machine, and the data packet replied by the other party will be automatically reversed and sent back to the original machine. REDIRECT can only be used in input and user-defined chain. To use this option, you must first add the CONFIG_IP_TRANSPARENT_PROXY option in the kernel before compiling. With this processing rule, data packets are redirected to the local socket even if these data packets are sent to the remote end. If the port to be switched is 0 (default value), the destination port of the data packet is the port to be switched. If a user-defined chain is checked to the end or the returned result is RETURN, it will RETURN the next rule of the previous chain called to continue the comparison. If RETURN processing is obtained from the end of the built-in chain or the internal chain, the processing method of this preset chain is executed.
The usage of the following ipchains is related to IP Maspuerade:
Ipchains-M [-L |-S] [options]
-M, -- masquerading
This parameter, coupled with-L, can be used to view connections currently using the masquerading method. The-S parameter can be used to adjust the parameters of the core masquerading.
-S,-set tcp tcpfin udp
Change the timeout value used by masquerading.

Examples and precautions for using Ipchains to build a firewall

In ipchains, each rule indicates a set of conditions to be met by the packet and the processing to be performed when this set of conditions is met. Therefore, for a simple example, if you want to reject all ICMP data packets from the network address 127.0.0.1 (that is, the local address), you can do this:
$ Ipchains-A input-s 127.0.0.1-p icmp-j DENY
In this rule, a packet must meet the following conditions: (1) if the packet belongs to the ICMP type, (2) it must come from 127.0.0.1. DENY is the thing to do after the conditions are met ).
When you set the ipchains firewall, you usually only use rules such as ipchains-A in the above example, so that the rules will not be changed after they are set at startup. If you use the delete rule action for testing or other reasons. There are two methods to delete a rule. The first is to specify the rule number, as shown in the following example:
$ Ipchains-D input 1
Rules numbered 1 are deleted from the input group. You can use ipchains-L to view the rule number.
Another method to delete a rule is to enter a complete rule, as shown in the following example:
$ Ipchains-D input-s 127.0.0.1-p icmp-j DENY
In this way, the rules we added above can be deleted.
For TCP connections, we may often want to restrict connections in a single direction. For example, we want to allow our machines to connect to other WWW servers, but not other machines. In this case, it is necessary to restrict the SYN data packets of the other party (this type of data packet is used to require the connection to be enabled), as shown in the following example:
$ Ipchains-A input-p TCP-s 200.200.200.200-y-j DENY
The TCP connection request will be rejected from the machine 200.200.200.200.
Below are more examples:
$ Ipchains-A output-d 200.200.207.0/24-j REJECT
$ Ipchains-A output-d 200.200.208.0/24-j REJECT
Do not connect my machine to the networks 200.200.207. * And 200.200.208.
$ Ipchains-N ppp-out
$ Ipchains-A output-I ppp0-j ppp-out
Create a new set of chains, and compare all data packets that use ppp0 with the rules in this set of ppp-out.
$ Ipchains-A ppp-out-p TCP-d proxy.virtual.net 80-t 0x01 0x10
$ Ipchains-A ppp-out-p TCP-d 0.0.0.0 telnet-t 0x01 0x10
This allows you to set the minimum latency for connecting WWW to proxy.virtual.net and telnet.
$ Ipchains-N ppp-in
$ Ipchains-A input-I ppp0-j ppp-in
Create a new group of chains, and compare all data packets transmitted using ppp0 with the rules in this group of ppp-in.
$ Ipchains-A ppp-in-s 200.200.200.0/24-l-j DENY
All data packets from 200.200.200.0 are rejected and recorded.
$ Ipchains-A input-I lo-j ACCEPT
Allow all connections from your machine to your machine.
$ Ipchains-P input DENY
Finally, all connections from outside are rejected.
Note that the order of rules is very important, because data packets are compared from the first one, so we usually set the permitted connections first, then, deny all other connections in the last rule.
Source: Aesop

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.