Set iptables to limit traffic

Source: Internet
Author: User
Tags ftp connection

1. view the settings of iptables on the local machine.

[[Email protected] ~] # Iptables-l-N

Chain input (Policy accept)

Target prot opt source destination

Chain forward (Policy accept)

Target prot opt source destination

Chain output (Policy accept)

Target prot opt source destination

Chain RH-Firewall-1-INPUT (0 Records)

Target prot opt source 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

[[Email protected] ~] # Iptables-l-N

Chain input (Policy accept)

Target prot opt source destination

Chain forward (Policy accept)

Target prot opt source destination

Chain output (Policy accept)

Target prot opt source 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.

[[Email protected] ~] # Iptables-F clear the rules of all rule chains in the filter of the preset table

[[Email protected] ~] # Iptables-x clear the rules in the User-Defined chain in the filter of the preset table

Let's take a look.

[[Email protected] ~] # Iptables-l-N

Chain input (Policy accept)

Target prot opt source destination

Chain forward (Policy accept)

Target prot opt source destination

Chain output (Policy accept)

Target prot opt source 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.

[[Email protected] ~] #/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.

[[Email protected] ~] # Service iptables restart

 

Now, no configuration is available in the iptables configuration table. Let's start with our configuration.

 

3. Set preset rules

[[Email protected] ~] # Iptables-P input drop

[[Email protected] ~] # Iptables-P output accept

[[Email protected] ~] # 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 Rules.

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.

[[Email protected] ~] # Iptables-A input-p tcp -- dport 22-J accept

[[Email protected] ~] # Iptables-A output-p tcp -- Sport 22-J accept (Note: If you set output to drop, write this rule, many people are eager to write this rule, and 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:

[[Email protected] ~] # Iptables-A output-p tcp -- Sport 80-J accept .)

If the Web server is configured, enable port 80.

[[Email protected] ~] # Iptables-A input-p tcp -- dport 80-J accept

If the email server is configured, enable port 25,110.

[[Email protected] ~] # Iptables-A input-p tcp -- dport 110-J accept

[[Email protected] ~] # Iptables-A input-p tcp -- dport 25-J accept

If the FTP server is configured, enable port 21.

[[Email protected] ~] # Iptables-A input-p tcp -- dport 21-J accept

[[Email protected] ~] # Iptables-A input-p tcp -- dport 20-J accept

If the DNS server is configured, enable port 53.

[[Email protected] ~] # Iptables-A input-p tcp -- dport 53-J accept

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,

[[Email protected] ~] # Iptables-A output-p icmp-J accept (if output is set to drop)

[[Email protected] ~] # Iptables-A input-p icmp-J accept (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 input drop)

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

[[Email protected] ~] # Iptables-A output-p tcp -- Sport 31337-J Drop

[[Email protected] ~] # Iptables-A output-p tcp -- dport 31337-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.

[[Email protected] ~] # Iptables-A input-s 192.168.0.3-p tcp -- 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:

[[Email protected] ~] # Iptables-D input-p tcp -- dport 22-J accept

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.

[[Email protected] ~] #/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)

[[Email protected] ~] # Iptables-a forward-I eth0-O eth1-M state -- state related, established-J accept

[[Email protected] ~] # Iptables-a forward-I eth1-O eh0-J accept

Discard bad TCP Packets

[[Email protected] ~] # Iptables-a forward-p tcp! -- Syn-M state -- state new-J Drop

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

[[Email protected] ~] # Iptables-a forward-F-m limit -- limit 100/s -- limit-burst 100-J accept

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

[[Email protected] ~] # 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.

 

Configure a NAT table

1. View local Nat settings

[[Email protected] rc. d] # iptables-T nat-l

Chain prerouting (Policy accept)

Target prot opt source destination

Chain postrouting (Policy accept)

Target prot opt source destination

SNAT all -- 192.168.0.0/24 anywhere to: 211.101.46.235

Chain output (Policy accept)

Target prot opt source 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

[[Email protected] ~] # Iptables-F-T Nat

[[Email protected] ~] # Iptables-X-T Nat

[[Email protected] ~] # 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

[[Email protected] sysconfig] # iptables-T Nat-A prerouting-I eth0-s 10.0.0.0/8-J Drop

[[Email protected] sysconfig] # iptables-T Nat-A prerouting-I eth0-s 172.16.0.0/12-J Drop

[[Email protected] sysconfig] # iptables-T Nat-A prerouting-I eth0-s 192.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

[[Email protected] ~] # Iptables-T Nat-A prerouting-D 211.101.46.253-J Drop

Disable ftp (21) Port

[[Email protected] ~] # 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.

[[Email protected] ~] # Iptables-T Nat-A prerouting-p tcp -- dport 21-D 211.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

[[Email protected] ~] # Iptables-A input-M state -- State invalid-J Drop

[[Email protected] ~] # Iptables-A output-M state -- State invalid-J Drop

[[Email protected] ~] # Iptables-a forward-M state -- State invalid-J Drop

Allow all established and related connections

[[Email protected] ~] # Iptables-A input-M state -- State established, related-J accept

[[Email protected] ~] # Iptables-A output-M state -- State established, related-J accept

 

[[Email protected] ~] #/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.

 

[[Email protected] ~] # Service iptables restart

 

Don't forget to save it. If it doesn't work, write a record and save it once. You can save and experiment to see if your requirements are met,

I have tried all the rules above and there is no problem.

From: http://tech.ccidnet.com/art/9513/20070601/1098119_2.html

Set iptables to limit traffic

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.