Simple setting of iptables firewall

Source: Internet
Author: User
Tags ftp connection to domain
The iptables-AINPUT-ptcp-s10.1.2.187-jACCEPT adds 10.1.2.187 to the permitted range iptables-tnat-APOSTROUTING-jSNAT -- to-source10.1.2.1 parameter --- A: add (with link)-I: insert-p: with protocol-s: source ip-d: Target IP-j: Operation behavior-t: Add table -- t

Iptables-A input-p tcp-s 10.1.2.187-j ACCEPT
Add 10.1.2.187 to the permitted range
Iptables-t nat-a postrouting-j SNAT -- to-source 10.1.2.1
Parameter --
-A: add (link)
-I: insert
-P: Protocol
-S: source IP address
-D: Target IP address
-J: Operation behavior
-T: Add table
-- To-source: used for SNAT to indicate the SNAT source address.
-- To-destination: used for DNAT to indicate the destination address of the changed DANT.

IPTABLE: input output forword prerouting postrouting (chain)
For more information, see online.
View the current iptable list:
Iptables-L
Iptables-t nat-L (content in the nat table)
Iptables-L-n: IP address displayed, not automatically resolved to domain name
Iptables-L -- line-numbers: Display number

Iptables-F delete the filter table content

Iptables-F-t nat delete nat table content
SNAT is generally used to access the Internet.
DNAT is used to come in from outside
SNAT eg:
Iptables-t nat-I POSTROUTING-s 10.1.0.0/24-j SNAT -- to-source192.168.0.5
Map intranet 10.1 fields to 192.168.0.5
You can also do this:
Iptables-t nat-I POSTROUTING-s 10.1.0.0/24-j SNAT -- to-source192.168.0.5-192.168.0.245
Map a local IP address to an IP address (attack can be performed ^)
The same function in the preceding example: iptables-t nat-I POSTROUTING-s 10.1.0.0/24-j NETMAP -- to 192.168.0.0/24
DNAT eg:
Iptables-t nat-a prerouting-d ROUTEIP-p tcp -- dport 80-j DNAT -- to-destination WEBIP
ROUTEIP indicates the public IP address of the firewall (router ).
WEBIP indicates the IP address of the intranet WEB server
This rule indicates that when the internet accesses the local port HTTP80, it is automatically forwarded to the intranet WEB server. As a result, the web server is mapped to the Internet.

This is sufficient when you only need to access the intranet from the Internet, but if you need to access the WEB server from the intranet machine through the Internet IP address of the WEB server, you also need to add an SNAT rule:

Iptables-t nat-a postrouting-p tcp-d WEBIP -- dport 80-j SNAT -- to ROUTEIP

Change the source IP address of the data packet accessing the WEB server to the Gateway IP address. Otherwise, the access will fail.

Reasons for simple analysis:

Assume that the intranet 192.168.0.10-> hosts that need to access the WEB from the external IP address

192.168.0.254-> internal IP address of the WEB server

192.168.0.1-> Gateway (external IP address: 202.96.22.22)

When 192.168.0.10 accesses the WEB service of 202.96.22.22, according to the DNAT on the Gateway, the destination IP address of the data packet is changed from 202.96.22.22 to 192.168.0.254. 254 after receiving the packet, it is found that 10 is sent, then it will directly return the packet to 192.168.0.10, but 10 after receiving the packet, it is found that the packet source is not your desired 202.96.22.22, the package will be discarded directly. The solution is to send packets to 10 instead of the Gateway at 254, so that the Gateway returns the original route to 10 machines. In this way, you can solve this problem by changing the source IP address of the packet sent to the WEB service 254 request to the Gateway IP address 192.168.0.1.

Iptables-t nat-a postrouting-p tcp-d192.168.0.254 -- dport 80-j SNAT -- to 192.168.0.1

There is also a better solution, with an independent network segment, that is, the DMZ area, which is often referred to as DMZ, where each SERVER is placed.

LinuxDetailed description of IPTABLES configuration:

If you do not know the basic knowledge about IPTABLES, we recommend that you first check it out.

Start configuration

Let's configure a filter table firewall.

(1) view the settings of IPTABLES on the local machine

[Root @ tp ~] # Iptables-L-n
Chain INPUT (policy ACCEPT)
Target prot optsource destination

Chain FORWARD (policy ACCEPT)
Target prot optsource destination

Chain OUTPUT (policy ACCEPT)
Target prot optsource destination

Chain RH-Firewall-1-INPUT (0 Records)
Target prot optsource destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 icmp type 255
ACCEPT esp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT ah -- 0.0.0.0/0 0.0.0.0/0
ACCEPT udp -- 0.0.0.0/0 224.0.0.20.udp dpt: 5353
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 uddpt: 631
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED, ESTABLISHED
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt: 22
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt: 80
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt: 25
REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
We can see that when I installed linux, I chose to have a firewall and opened ports 22, 80, and 25.

If you do not choose to start the firewall when installing linux

[Root @ tp ~] # Iptables-L-n
Chain INPUT (policy ACCEPT)
Target prot optsource destination

Chain FORWARD (policy ACCEPT)
Target prot optsource destination

Chain OUTPUT (policy ACCEPT)
Target prot optsource destination

There are no rules.

(2) clear the original rules.

Whether or not you have enabled the firewall when installing linux, if you want to configure your own firewall, clear all the filter rules.

[Root @ tp ~] # Iptables-F clear the rules of all rule chains in the filter of the preset table
[Root @ tp ~] # Iptables-X clear the rules in the user-defined chain in the filter of the preset table

Let's take a look.

[Root @ tp ~] # Iptables-L-n
Chain INPUT (policy ACCEPT)
Target prot optsource destination

Chain FORWARD (policy ACCEPT)
Target prot optsource destination

Chain OUTPUT (policy ACCEPT)
Target prot optsource destination

Nothing, just like we didn't start the firewall when installing linux. (say in advance, these configurations are the same as configuring IP addresses with commands, so restarting them will lose effect.) how to save them.

[Root @ tp ~] #/Etc/rc. d/init. d/iptables save

In this way, you can write it to the/etc/sysconfig/iptables file. remember to repeat the firewall after writing it to make it take effect.

[Root @ tp ~] # Service iptables restart

Now there are no configurations in the IPTABLES configuration table. let's start with our configuration.

(3) set preset rules

[Root @ tp ~] # Iptables-p INPUT DROP

[Root @ tp ~] # Iptables-p OUTPUT ACCEPT

[Root @ tp ~] # Iptables-p FORWARD DROP
The above means that when two chain rules (INPUT and FORWARD) in the filter table in IPTABLES are exceeded, how can we process data packets not in these two rules, that is, DROP (discard ). it should be said that the configuration is safe. we want to control inbound data packets

For the OUTPUT chain, that is, the outgoing package, we do not need to impose too many restrictions, but adopt ACCEPT. that is to say, what should we do if the package is not in a rule.

We can see what packets are allowed to pass through the INPUT and FORWARD chains, and what packets are not allowed to pass through the OUTPUT chain.

This setting is quite reasonable. of course you can also DROP all three links, but I don't think it is necessary to do so, and the rules to be written will increase. but if you only want a limited number of rules, for example, only WEB servers. we recommend that all three links be DROP.

Note: If you log on remotely through SSH, you should drop it when you enter the first command and press enter because you have not set any rules.

What should I do? go to the local machine to operate it!

(4) add a rule.

First, add the INPUT chain. the default rule of the INPUT chain is DROP, so we will write the chain that requires ACCETP ().

To enable remote SSH login, we need to enable port 22.

[Root @ tp ~] # Iptables-a input-p tcp -- dport 22-jACCEPT

[Root @ tp ~] # Iptables-a output-p tcp -- sport 22-j ACCEPT, SSH is always unavailable. remotely, is it okay.

The same applies to other ports. if the web server is enabled and the OUTPUT is set to DROP, a chain should also be added:

[Root @ tp ~] # Iptables-a output-p tcp -- sport 80-j ACCEPT .)

If the WEB server is configured, enable port 80.

[Root @ tp ~] # Iptables-a input-p tcp -- dport 80-jACCEPT
If the email server is configured, enable Port 25,110.

[Root @ tp ~] # Iptables-a input-p tcp -- dport 110-j ACCEPT
[Root @ tp ~] # Iptables-a input-p tcp -- dport 25-j ACCEPT
If the FTP server is configured, enable port 21.

[Root @ tp ~] # Iptables-a input-p tcp -- dport 21-jACCEPT

[Root @ tp ~] # Iptables-a input-p tcp -- dport 20-jACCEPT

If the DNS server is configured, enable Port 53.

[Root @ tp ~] # Iptables-a input-p tcp -- dport 53-jACCEPT

If you have another server, you just need to open the port and write it.

The above mainly writes the INPUT chain, and all the rules that are not in the above DROP

Allow icmp packets to pass, that is, allow ping,

[Root @ tp ~] # Iptables-a output-p icmp-j ACCEPT (if OUTPUT is set to DROP)

[Root @ tp ~] # Iptables-a input-p icmp-jACCEPT (if INPUT is set to DROP)

Allow loopback! (Otherwise, DNS may fail to be shut down normally)

IPTABLES-a input-I lo-p all-j ACCEPT (if it is INPUTDROP)
IPTABLES-a output-o lo-p all-j ACCEPT (if it is output drop)

The OUTPUT chain is written below. the default rule of the OUTPUT chain is ACCEPT, so we will write the chain that needs to be dropped (abandoned.

Reduce insecure port connections

[Root @ tp ~] # Iptables-a output-p tcp -- sport31337-j DROP

[Root @ tp ~] # Iptables-a output-p tcp -- dport31337-j DROP

Some Trojans scan services from ports 31337 to 31340 (elite ports in hacking languages. Since legal services do not use these non-standard ports for communication, blocking these ports can effectively reduce the chances of independent communication between machines that may be infected on your network and their remote master servers.

The same applies to other ports, such as 31335, 27444, 27665, 20034, 9704, 137-139 (smb), and 2049 (NFS, I have not written all of them here. if you are interested, check the relevant information.

Of course, you can set the OUTPUT chain to DROP for more secure access, so you can add more rules, just like adding

Allow SSH login. just write it.

The more detailed rules are as follows:

For example, we only allow SSH connections to machines 192.168.0.3.

[Root @ tp ~] # Iptables-a input-s 192.168.0.3-ptcp -- dport 22-j ACCEPT

If you want to allow or restrict the availability of a certain IP address, 192.168.0.0/24 indicates all IP addresses of 192.168.0.1-255.

24 indicates the number of subnet masks. remember to delete this line in/etc/sysconfig/iptables.

-A input-p tcp-m tcp -- dport 22-j ACCEPT because it indicates that all addresses can log on.

Or use the following command:

[Root @ tp ~] # Iptables-d input-p tcp -- dport 22-jACCEPT

Save it. I'll talk about it again. Instead, it uses the command method and only takes effect at that time. if you want to restart it, save it. write to the/etc/sysconfig/iptables file.

[Root @ tp ~] #/Etc/rc. d/init. d/iptables save

Write it like this! 192.168.0.3 indicates IP address other than 192.168.0.3

The same is true for other rule connections.

The following is the FORWARD chain. the default rule of the FORWARD chain is DROP, so we will write the chain that requires ACCETP (via) to monitor the ongoing forwarding chain.

Enable the forwarding function (required when the default FORWARD rule is DROP when performing NAT)

[Root @ tp ~] # Iptables-a forward-I eth0-o eth1-mstate -- state RELATED, ESTABLISHED-j ACCEPT

[Root @ tp ~] # Iptables-a forward-I eth1-o eh0-jACCEPT

Discard bad TCP packets

[Root @ tp ~] # Iptables-a forward-p TCP! -- Syn-mstate -- state NEW-j DROP

Number of IP fragments processed to prevent attacks. up to 100 IP fragments are allowed per second.

[Root @ tp ~] # Iptables-a forward-f-m limit -- limit100/s -- limit-burst 100-j ACCEPT

Set ICMP packet filtering to allow 1 packet per second. the trigger condition is 10 packets.

[Root @ tp ~] # Iptables-a forward-p icmp-m limit -- limit 1/s -- limit-burst 10-j ACCEPT

I only allow ICMP packets to pass in the front, because I have restrictions here.

2. configure a NAT table

1. View local NAT settings

[Root @ tp rc. d] # iptables-t nat-L
Chain PREROUTING (policy ACCEPT)
Target prot optsource destination

Chain POSTROUTING (policy ACCEPT)
Target prot optsource destination
SNAT all -- 192.168.0.0/24 anywhere to: 211.101.46.235

Chain OUTPUT (policy ACCEPT)
Target prot optsource destination

My NAT has been configured (only the simplest proxy Internet access function is provided, and no firewall rules have been added). for how to configure NAT, refer to my other article.

Of course, if you have not configured NAT, you do not need to clear the rules, because NAT does not have anything by default.

If you want to clear, the command is

[Root @ tp ~] # Iptables-F-t nat

[Root @ tp ~] # Iptables-X-t nat

[Root @ tp ~] # Iptables-Z-t nat

2. add rules

Add basic NAT address translation (see my other article on how to configure NAT ),

To add rules, we only add DROP links. because the default links are all ACCEPT.

Prevent internet spoofing using intranet IP addresses

[Root @ tp sysconfig] # iptables-t nat-a prerouting-I eth0-s 10.0.0.0/8-j DROP
[Root @ tp sysconfig] # iptables-t nat-a prerouting-I eth0-s172.16.0.0/12-j DROP
[Root @ tp sysconfig] # iptables-t nat-a prerouting-I eth0-s192.168.0.0/16-j DROP
If we want to, for example, block MSN, QQ, BT, etc., we need to find the port or IP address they use (I think it is not necessary)

Example:

Disable all connections to 211.101.46.253

[Root @ tp ~] # Iptables-t nat-APREROUTING-d 211.101.46.253-j DROP

Disable FTP (21) Port

[Root @ tp ~] # Iptables-t nat-a prerouting-p tcp -- dport 21-j DROP

In this way, the write range is too large, so we can define it more accurately.

[Root @ tp ~] # Iptables-t nat-APREROUTING-p tcp -- dport 21-d211.101.46.253-j DROP

In this way, only the FTP connection of the 211.101.46.253 address is disabled. Other connections can also be. for example, web (port 80) connections.

According to what I wrote, you only need to find the IP addresses, ports, and protocols of other software such as QQ and MSN.

Finally:

Drop illegal connection
[Root @ tp ~] # Iptables-a input-mstate -- state INVALID-j DROP
[Root @ tp ~] # Iptables-a output-m state -- state INVALID-j DROP
[Root @ tp ~] # Iptables-a forward-m state -- state INVALID-jDROP
Allow all established and related connections
[Root @ tp ~] # [Root @ tp ~] # Iptables-a input-m state -- state ESTABLISHED, RELATED-j ACCEPT
[Root @ tp ~] # Iptables-a output-m state -- state ESTABLISHED, RELATED-j ACCEPT
[Root @ tp ~] #/Etc/rc. d/init. d/iptables save
In this way, you can write it to the/etc/sysconfig/iptables file. remember to repeat the firewall after writing it to make it take effect.
[Root @ tp ~] # Service iptables restart

Don't forget to save it. if you can't save it, write it once. you can save it while doing experiments to see if it meets your requirements,
I have tried all the rules above and there is no problem.
It took me nearly a month to write this article. I searched for the materials and did my experiments on my own. I hope it will be helpful to everyone. if there are incomplete and incomplete information, please submit it.
This article focuses on configuration. I will upload the basic knowledge about IPTABLES and instructions on commands and commands as soon as possible. of course, you can search for them online.

Related Article

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.