Getting started with iptables

Source: Internet
Author: User
Tags ssh port

This section describes iptables concepts and usage.

Commands in iptables must be case sensitive.

The main syntax differences between ipchains and iptables are as follows:
1. In ipchains, for example, the input chain uses a lowercase name. In iptables, use uppercase input instead.
2. in iptables, you must specify the rule to be applied to that rule table (use-t to specify, such as-T Nat). If this parameter is not specified, the default value is used to filter the table.
3. In ipchains,-I refers to the interface, but in iptables,-I refers to the Inbound direction, and more-O represents the outbound direction.
4. In iptables, the source port should use the Keyword -- sport or -- source-port.
5. In iptables, the destination port uses the Keyword -- dport or -- destination-port.
6. In iptables, the disposal action of "discard" will no longer use the target deny, and use drop instead.
7. The ipchains record file function-L has been changed to the target-J log, and the title of the record file can be specified.
8.-y in ipchains can be used in iptables -- SYN or -- TCP-flag SYN, ack, Fin SYN
9. In iptables, add the Keyword -- ICMP-type to the imcp messages type, for example:

Iptables-A output-O eth0-p icmp-S $ fw_ip -- ICMP-type 8-D any/0-J accept

Iptables example
When setting iptables packet filtering rules, there are several examples. If you are familiar with them first, you can apply them on your own. So you can quickly enter the world.

######################################## ######

##--------##
Preparations
##--------##

First, check the settings of iptables on the machine.
The procedure is as follows:

Iptables-l-N
Iptablse-T nat-l-N

Define the IP address of the variable referenced in the following example
$ Fw_ip = "163.26.197.8"

To enable the core forward function, follow these steps:

###-----------------------------------------------------###
# Enable the forward Function
###-----------------------------------------------------###

Echo "1">/proc/sys/NET/IPv4/ip_forward
Or add
Forward_ipv4 = Yes
Enable forwarding

To clear all rules, follow these steps ::

In the first step, you must clear all rules and start again to prevent the old rules from affecting the new settings. The procedure is as follows:

###-----------------------------------------------------###
# Clear previous settings
###-----------------------------------------------------###

# Clear all rules in the Rule chain in the filter of the preset table
Iptables-F

# Clear the rules in the User-Defined chain in the filter of the preset table
Iptables-x

# Clear all rules in the Rule chain in the mangle table
Iptables-F-T mangle

# Clear the rules in the Custom link in the mangle table
Iptables-T mangle-x

# Clear all rules in the Rule chain in the NAT table
Iptables-F-T Nat

# Clear the rules in the user-defined link in the NAT table
Iptables-T nat-x

######################################## ###########

Select a preset Policy
Next, you need to select different rule chains and the predefined policies. The procedure is as follows:

Default discard all:

###-----------------------------------------------------###
# Set the filter table preset Policy
###-----------------------------------------------------###
Iptables-P input drop
Iptables-P output drop
Iptables-P forward drop

Or accept all by default:

###-----------------------------------------------------###
# Set the filter table preset Policy
###-----------------------------------------------------###
Iptables-P input accept
Iptables-P output accept
Iptables-P forward accept

** The preset policies of each rule chain can be set independently without being affected by other chains. **

In the following example, if the target is drop (discard), set policy to accept (accept). If the target is accept, set policy to drop to see the effect.

Open an Interface
The procedure is as follows:

Iptables-A input-I lo-J accept
Iptables-A output-O lo-J accept

Note: The Packet Flow of ipfw or netfilter does not pass through the forward chain,
Therefore, lo serves only the input and output chains.

Iptables-A input-I eth1-J accept
Iptables-A output-O eth1-J accept
Iptables-a forward-I eth1-J accept
Iptables-a forward-O eth1-J accept

IP camouflage (SNAT Application)
After the packets in the Intranet are disguised, the external eth0 Nic is used as the proxy number for external connections. The procedure is as follows:

###-----------------------------------------------------###
# Start internal external Address Transfer
###-----------------------------------------------------###

Iptables-T Nat-A postrouting-O eth0-s 172.16.0.0/16-j snat -- to-source $ fw_ip

The preceding command indicates that the network segment 172.16.0.0/16 is disguised as $ fw_ip.

Virtual Host (DNAT Application)
Internet packets can be sent to server hosts in the Intranet by means of address and port forwarding, which is commonly known as virtual hosts. This method protects most of the ports on the server host from being exported.

Only open public service channels (such as Web server port 80), so the security is high.

The procedure is as follows:

###-----------------------------------------------------###
# Start external address translation
###-----------------------------------------------------###
# Anyone connected to $ fw_ip: 80 will be directed to 172.16.255.2: 80
Iptables-T Nat-A prerouting-I eth0-P TCP-d $ fw_ip -- dport 80-J DNAT -- to-destination 172.16.255.2: 80

Enable the internal host to telnet to the external host
Open the Intranet and telnet to the external host.

The procedure is as follows: (the Default policy is drop)

###-----------------------------------------------------###
# Open external host telnet port 23
###-----------------------------------------------------###

Iptables-A output-O eth0-p tcp-S $ fw_ip -- Sport 1024: 65535-D any/0 -- dport 23-J accept
Iptables-A input-I eth0-P TCP! -- Syn-s any/0 -- Sport 23-d $ fw_ip -- dport 1024: 65535-J accept

Mail Transfer Channel
Open any mail host to send a packet to your mail server, and your mail server can also send the packet.

The procedure is as follows: (the Default policy is drop)

###-----------------------------------------------------###
# Open SMTP port 25
###-----------------------------------------------------###

# The following is a message that someone else can send to you:
Iptables-A input-I eth0-P TCP-s any/0 -- Sport 1024: 65535-d $ fw_ip -- dport 25-J accept
Iptables-A output-O eth0-p tcp! -- Syn-S $ fw_ip -- Sport 25-D any/0 -- dport 1024: 65535-J accept

# You can send emails to others
Iptables-A output-O eth0-p tcp-S $ fw_ip -- Sport 1024: 65535-D any/0 -- dport 25-J accept
Iptables-A input-I eth0-P TCP! -- Syn-s any/0 -- Sport 25-d $ fw_ip -- dport 1024: 65525-J accept

Open Channels for offline mailing downloads
Enable Intranet for receiving mails from POP3 servers on the Internet.

The procedure is as follows: (the Default policy is drop)

###-----------------------------------------------------###
# Open POP3 port 110 for external hosts
###-----------------------------------------------------###

Iptables-A output-O eth0-p tcp-S $ fw_ip -- Sport 1024: 65535-D any/0 -- dport 110-J accept
Iptables-A input-I eth0-P TCP! -- Syn-s any/0 -- Sport 110-d $ fw_ip -- dport 1024: 65535-J accept

Open webpage viewing channel
You can open the Intranet to view Internet websites.

The procedure is as follows: (the Default policy is drop)

###-----------------------------------------------------###
# Open http port 80 for external hosts
###-----------------------------------------------------###

Iptables-A output-O eth0-p tcp-S $ fw_ip -- Sport 1024: 65535-D any/0 -- dport 80-J accept
Iptables-A input-I eth0-P TCP! -- Syn-s any/0 -- Sport 80-d $ fw_ip -- dport 1024: 65535-J accept

Open DNS host for querying the Internet
Open the Intranet to query any DNS host on the Internet.

The procedure is as follows: (the Default policy is drop)

###-----------------------------------------------------###
# Open DNS port 53
###-----------------------------------------------------###

# UDP packets will be used for the first query
Iptables-A output-O eth0-p udp-S $ fw_ip -- Sport 1024: 65535-D any/0 -- dport 53-J accept
Iptables-A input-I eth0-P UDP-s any/0 -- Sport 53-d $ fw_ip -- dport 1024: 65535-J accept

# If an error occurs, TCP packets are used for query.
Iptables-A output-O eth0-p tcp-S $ fw_ip -- Sport 1024: 65535-D any/0 -- dport 53-J accept
Iptables-A input-I eth0-P TCP! -- Syn-s any/0 -- Sport 53-d $ fw_ip -- dport 1024: 65535-J accept

# Enable interactive query between DNS on this host and external DNS host: Use UDP
Iptables-A output-O eth0-p udp-S $ fw_ip -- Sport 53-D any/0 -- dport 53-J accept
Iptables-A input-I eth0-P UDP-s any/0 -- Sport 53-d $ fw_ip -- dport 53-J accept
# Enable the interactive query between DNS on this host and external DNS host: use TCP
Iptables-A output-O eth0-p tcp-S $ fw_ip -- Sport 53-D any/0 -- dport 53-J accept
Iptables-A input-I eth0-P TCP! -Y-s any/0 -- Sport 53-d $ fw_ip -- dport 53-J accept

Open internal hosts to SSH to external hosts
Enable Intranet and SSH to external hosts.

The procedure is as follows: (the Default policy is drop)

###-----------------------------------------------------###
# Open external host ssh port 22
###-----------------------------------------------------###

Iptables-A output-O eth0-p tcp-S $ fw_ip -- Sport 1024: 65535-D any/0 -- dport 22-J accept
Iptables-A input-I eth0-P TCP! -- Syn-s any/0 -- Sport 22-d $ fw_ip -- dport 1024: 65535-J accept

# The following are differences between SSH protocols:
Iptables-A output-O eth0-p tcp-S $ fw_ip -- Sport 1020:1023-D any/0 -- dport 22-J accept
Iptables-A input-I eth0-P TCP! -- Syn-s any/0 -- Sport 22-d $ fw_ip -- dport 1020:1023-J accept

Enable internal hosts to FTP to external hosts
Enable Intranet and FTP to external hosts.

The procedure is as follows: (the Default policy is drop)

###-----------------------------------------------------###
# Open to external host FTP port 21
###-----------------------------------------------------###

# Open the command channel 21
Iptables-A output-O eth0-p tcp-S $ fw_ip -- Sport 1024: 65535-D any/0 -- dport 21-J accept
Iptables-A input-I eth0-P TCP! -- Syn-s any/0 -- Sport 21-d $ fw_ip -- dport 1024: 65535-J accept

# Open Channel 20
Iptables-A input-I eth0-P TCP-s any/0 -- Sport 20-d $ fw_ip -- dport 1024: 65535-J accept
Iptables-A output-O eth0-p tcp! -- Syn-S $ fw_ip -- Sport 1024: 65535-D any/0 -- dport 20-J accept

# Open the passive mode FTP data channel
Iptables-A output-O eth0-p tcp-S $ fw_ip -- Sport 1024: 65535-D any/0 -- dport 1024: 65535-J accept
Iptables-A input-I eth0-P TCP! -- Syn-s any/0 -- Sport 1024: 65535-d $ fw_ip -- dport 1024: 65535-J accept

Open Ping
You can ping any host externally.

The procedure is as follows: (the Default policy is drop)

Iptables-A output-O eth0-p icmp-S $ fw_ip -- ICMP-type 8-D any/0-J accept
Iptables-A input-I eth0-P ICM-s any/0 -- ICMP-type 0-d $ fw_ip-J accept

**************************************** **************************************** **************************************** *********************************
Append:

Question 1: Why does each function use two links of input and output? Does their function allow the output of the gateway machine and then allow the LAN to the gateway machine?
_____
//.
Incoming --> [routing] ---> | forward | -------> outgoing
[Demo-]/_____/^
|
V _____
____//
// | Output |
| Input | /______/
/____/^
|
----> Local process ----
Analyze the handling process of the package in the chain!
Three circles represent the preceding three chains. When a package arrives at one of the chains, the corresponding chains will be tested (examined) to determine how to deal with the package. If the chain thinks

The packet should be dropped. If the chain thinks it should accept the packet, it will continue to traverse in the figure.
(1) When a package enters, the kernel first looks at the destination of the package. If the destination address is the local machine, the package goes down to the input chain. If the destination address can pass the detection, the package is processed later.

Program.
(2) If the destination address is not the local host, but the kernel does not enable the forwarding function, or the kernel does not know how to forward the package, the package will be discarded.
(3) If the destination address is not the local machine and the forwarding function is enabled, the package will be directed to the forward link on the right. If the package is accepted, it will be sent out.
(4) A program running on the local machine sends a network package, and the package passes through the output chain directly. If it is accepted, the package will be sent to the network interface specified by the package.

Note: Incoming & outgoing can be Intranet or internet, and local process is a server that provides proxy or IP address filtering.

Now that you know the functions of each chain, you will be able to understand the meaning of the input and output Rule Statements at the same time.

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.