Iptables firewall configuration process instance

Source: Internet
Author: User
1. iptables introduction iptables is complex and integrated into the Linux kernel. You can use iptables to filter data packets that come in and out of your computer. Use the iptables command to set your rules to keep your computer network secure-what data is allowed to pass, what cannot pass, and what data is recorded (log ). Next, I will, IptablesIntroduction
Iptables is complex and integrated into the Linux kernel. You can use iptables to filter data packets that come in and out of your computer. Use the iptables command to set your rules to keep your computer network secure-what data is allowed to pass, what cannot pass, and what data is recorded (log ). Next, I will show you how to set your own rules, from now on.

2. initialization
Enter
Iptables-F
Iptables-X
Iptables-t nat-F
Iptables-t nat-X
Each of the preceding commands has its exact meaning. Before setting your iptables, we need to clear all previously set rules and call them initialization. Although it does not do anything in many cases, be careful when you are out of warranty! If you are using redhat or fedora, you have a simpler method.
Service iptables stop

3. start setting rules:
Next, set your rules.
Iptables-P INPUT DROP
This command will build a very "secure" firewall for you. it is hard to imagine which hacker can break such a machine, because it drops all data from the network into your machine. This is of course too secure. at this time, your machine will be equivalent to no network. If you pinglocalhost, you will find that the screen is always there, because ping cannot receive any response.

4. add rules
Run the following command:
Iptables-a input-I! Ppp0-j ACCEPT
This rule means accepting all data from the network interface ppp0.
Assume that you have two network interfaces, eth0 is connected to the LAN, and loop is the back-to-loop network (localhost ). Ppp0 is an internet interface for internet access through adsl. if you are not using this method, it may be eth1. Assume that you are using adsl to access the internet and your internet interface is ppp0.
In this case, you are allowed to access the LAN, and you can also access the localhost
Then enter the command ping localhost. Will the result be the same as that in the previous step?
At this point, we cannot access www or mail. let's take a look.

5. I want to access www
Iptables-a input-I ppp0-p tcp-sport 80-j ACCEPT
Allow data from the network interface ppp0 (internet interface) and the source port is 80 to enter your computer.
Port 80 is the port used by the www service.
Now you can view the webpage. But can you see it?
If you enter www.baidu.com in the browser address, can you see the webpage?
The result is: the host www.baidu.com cannot be found.
However, if you enter 220.181.27.5, you can still access the baidu webpage.
Why? If you know dns, you must know the reason.
If you enter www.baidu.com, your computer cannot obtain the IP address 220.181.27.5 that can be used by the name www.baidu.com. If you do remember this ip address, you can still access www. of course, you can only access www by ip address. if you want to challenge your memory, ^ _ ^, of course, we want to open DNS.

6. open the dns Port
Open your dns port and enter the following command:
Iptables-a input-I ppp0-p udp-sport 53-j ACCEPT
This command accepts all data from Port 53 of the network interface ppp0 and upd. 53 is the famous dns port.
In this case, test whether you can access www by host name? Can you access www through an ip address?
Yes, of course!

7. view the firewall
Now you can view your firewall.
Iptables-L
If you only want to access www, you can only access www. But don't worry, just summarize the content above and write it as a script.
#! /Bin/bash
# This is a script
# Edit by liwei
# Establish static firewall
Iptables-F
Iptables-X
Iptables-t nat-F
Iptables-t nat-X
Iptables-P INPUT DROP
Iptables-a input-I! Ppp0-j ACCEPT
Iptables-a input-I ppp0-p tcp -- sport 80-j ACCEPT
Iptables-a input-I ppp0-p udp -- sport 53-j ACCEPT

8. is it complicated? By now, iptables can filter packets according to your requirements. You can set some ports to allow your machine to access these ports. In this way, you may not be able to access QQ, or play online games. it may be good or bad, or it depends on your own. By the way, QQ is really difficult to control. The connection between the user and the server seems to be Port 8888, while friends send messages to each other on QQ are using udp port 4444 (it is not clear whether it is 4444 ). QQ can also use port 80 of www to log on and send messages. it seems that there is no end to learning. do you really want to control this guy? Let's go to our topic.
What if your machine is a server?

9. if your machine is a server and you need to provide www service. Obviously, the above scripts cannot meet our requirements. But as long as you hold on to the rules and make some modifications, it can also work well. Add a sentence at the end
Iptables-a input-I ppp0-p tcp -- dport 80-j ACCEPT
This is to open port 80 on your machine to the outside, so that other people on the internet can access your www. Of course, you have to work on the www server. If your machine is a smtp and pop3 server at the same time, add two statements to change the 80 following -- dport to 25 and 110. If you still have an ftp server, what if you want to open port 100 ......
Our work seems to be repeating similar statements, and you may think of it yourself. I can use a loop statement to complete it. right, the shell script function can be effectively used here, it also allows you to experience the power of shell scripting. See the following:

10. use a script to simplify your work. Read the following script.
#! /Bin/bash
# This is a script
# Edit by liwei
# Establish a static firewall
# Define const here
Open_ports = "80 25 110 10" # open ports on your own machine
Allow_ports = "53 80 20 21" # internet data can enter the port of your machine
# Init
Iptables-F
Iptables-X
Iptables-t nat-F
Iptables-t nat-X
Iptables-p input drop # we can use another method to insteadit
Iptables-a input-I! Ppp0-j ACCEPT
# Define ruler so that some data can come in.
For Port in "Allow_ports"; do
Iptables-a input-I ppp0-p tcp-sport $ Port-j ACCEPT
Iptables-a input-I ppp0-p udp-sport $ Port-j ACCEPT
Done
For Port in "Open_ports"; do
Iptables-a input-I ppp0-p tcp-dport $ Port-j ACCEPT
Iptables-a input-I ppp0-p udp-dport $ Port-j ACCEPT
Done
This script has three parts (the first part is the comment, not included in the three parts)
The first part is to define some ports: access the data on the "Open_ports" port of your machine and allow access. the Source is the data on the "Allow_ports" port.
The second part is the initialization of iptables, and the third part is the specific operation on the defined port.
If our requirements change in the future, for example, if you add an ftp server to your machine, add ports 20 and 21 corresponding to ftp. Well, you must have realized the powerful scalability of the script function, but the script capability is far more than that!

11. improve your firewall
Look at the last sentence of the init part of the above script.
Iptables-P INPUT DROP
This is to set the default rules for the firewall. When the data entering our computer does not match any of our conditions, the default rule is used to process the data ---- drop, and no response is sent to the sender.
That is to say, if you ping your host from another computer on the internet, the ping will stop there without responding.
If a hacker uses the namp tool to scan the port of your computer, it will prompt the hacker that your computer is under firewall protection. I don't want hackers to know too much about my computer. what should I do? if we change drop to another action, we may be able to cheat this hacker.
How to change it? Remove the previous sentence (iptables-p input drop) and add it to the end of the script.
Iptables-a input-I ppp0-p tcp-j REJECT -- reject-withtcp-reset
Iptables-a input-I ppp0-p udp-j REJECT -- reject-withicmp-port-unreachable
This is much better. although hackers can scan open ports, it is hard to know that our machines are under firewall protection. If you only run ftp and only access the intranet, it is difficult for you to know whether ftp is running. Here we give data that should not enter our machine, a fraudulent answer, instead of dropping it and then ignore it. This function is particularly useful in designing stateful firewalls (I am talking about static firewalls.
You can perform this operation in person to see how the results of the modification are different from those obtained by using namp scan?

12. this tutorial is over. many of the things are not mentioned here, such as ip camouflage, port forwarding, and packet recording. Another important thing is the process of iptables processing data packets. here, I want to tell you that the order of the filtering rules you set is very important and should not be described in detail here, because in this way, this tutorial will stick to the details.
Iptables is complex. I have read a lot of tutorials on linuxsir. they are often numerous and complete, but they are daunting. I hope this tutorial will help you get started. Come on!
Finally, I write the complete script as follows. you only need to modify the definition of a constant to demonstrate great scalability. ^_^
#! /Bin/bash
# This is a script
# Edit by liwei
# Establish a static firewall
# Define const here
Open_ports = "80 25 110 10" # open ports on your own machine
Allow_ports = "53 80 20 21" # internet data can enter the port of your machine
# Init
Iptables-F
Iptables-X
Iptables-t nat-F
Iptables-t nat-X
# The follow is comment, for make it better
# Iptables-P INPUT DROP
Iptables-a input-I! Ppp0-j ACCEPT
# Define ruler so that some data can come in.
For Port in "Allow_ports"; do
Ptables-a input-I ppp0-p tcp-sport $ Port-j ACCEPT
Iptables-a input-I ppp0-p udp-sport $ Port-j ACCEPT
Done
For Port in "Open_ports"; do
Iptables-a input-I ppp0-p tcp-dport $ Port-j ACCEPT
Iptables-a input-I ppp0-p udp-dport $ Port-j ACCEPT
Done
# This is the last Rter, it can make you firewall better
Iptables-a input-I ppp0-p tcp-j REJECT -- reject-withtcp-reset
Iptables-a input-I ppp0-p udp-j REJECT -- reject-withicmp-port-unreachable
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.