From: http://blog.csdn.net/jixiuffff/article/details/5879547
[C-sharp] View plaincopy
-
- # Five checkpoints prerouting, forward postrouting Input Output
-
- # A data packet enters my machine from prerouting. It has two destinations. One is to access the applications on my machine through input.ProgramAfter output, postrouting
-
- # The other direction is to direct the forward postrouting to another machine. That is to say, my machine only acts as a route and data packets are routed to other machines through my machine.
-
- #
- # Prerouting ----------------> forward ---------------------> postrouting
-
- # | ^
- # |
-
- # Input Output
- # |
-
- # V |
- # ---> Application on my machine ---------->
-
- #
-
- # The structure of iptables is from top to bottom: Table, rule chain, and rule. A table is composed of Rule chains, and rule chains are composed of rules.
-
- # Iptables has three filters, Nat, and mangle by default. The-t parameter is used to specify which table to operate on. If this parameter is not specified, the filter table is operated by default.
-
- #
- # The filter has three built-in rule chains by default. The input forward Output
-
- # Nat ........... Postrouting, output prerouting
-
- # Mangle ...... two .......... output prerouting
-
- #
-
- # The general format of the iptables command iptables [-T table] operation [Chain] [Options]
- # An example of checking complete Parameters
-
- # Iptables-T filter-I input 2-I eth0-s 10.2.1.111 -- Sport 1234-D 10.2.1.123 -- dport 22-J accept
-
- # Iptables-T filter-I output 2-I eth0-D 10.2.1.123 -- dport 22-s 10.2.1.123 -- Sport 1234-J accept
- # This command inserts a rule at 2 in the input chain of the filter table (this rule is placed in the second position). The rule details are: connecting me from my eth0 Nic, the IP address of the host on the other side is 10.2.1.111 and the port on the other side is 1234. It is accepted only when the IP address is 10.2.1.123 and my port on port 22 is accessed.
-
- # Then, from my IP address 10.2.1.123: 22 to 10.2.1.111: 1234 through the eth0 Nic package release
-
- # Iptables-F: clears all rule chains of the filter.
-
- # Iptables-T nat-F clear all rule chains in the NAT table
-
- ######################################## ######################################## ###################
- # For input, output is relative to the "I" machine, that is, input: indicates that data is input to me, and output indicates "I" to output data.
-
- #-S-d -- Sport -- dport indicates the Source IP (source), destination IP (destination), Source IP port, and destination IP port respectively.
-
- # When processing input,-S refers to the opposite machine, and-D refers to my machine, because data flows from the other machine to me,
-
- # When processing the output,-S refers to me, and-D refers to the opposite machine.
-
- # Correct use of the firewall, which is usually set to deny all, and then only open to allow, rather than allow all, only refuse to reject
- # First start iptables service/etc/init. d/iptables start
-
- # After iptables is installed on the Gentoo system, the first time you run the kernel command, it prompts you to run/etc/init. d/iptables save first, as if you were doing some initialization or saving some files,
-
- /Etc/init. d/iptables save
-
- /Etc/init. d/iptables start
-
- # Check the default access rules after startup
-
- Iptables-l or iptables-l -- line-number: displays the row number and-V details.
-
- 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
- # By default, the policy is accept, which means no firewall exists. Now, modify the Default policy.
-
- # Do not use a remote SSH connection for this operation, because it will also disable port 22 used by SSH,
-
- # Use SSH to connect. First open port 22 and then run the following three commands:
-
- # Sshd
-
- # Allow any machine to send requests to port 22
-
- #-T is not used here. The default value is-T filter.
- Iptables-A input-p tcp -- dport 22-J accept
-
- # Equivalent to iptables-T filter-A input-p tcp -- dport 22-J accept
-
- # Allow data output from port 22
- Iptables-A output-p tcp -- Sport 22-J accept
-
- # If only machines with certain IP addresses can access me, replace the above two
-
- Iptables-A input-p tcp -- dport 22-s 10.2.1.110-J accept
- Iptables-A output-p tcp -- Sport 22-D 10.2.1.110-J accept
-
- # Currently, only IP address 10.2.1.110 can access me.
-
- #
-
- Iptables-P input drop
-
- Iptables-P output drop
-
- Iptables-P forward drop
-
- # The Default policy can only be accept, drop, or reject.
- Chain input (Policy drop)
-
- Target prot opt source destination
-
- Chain forward (Policy drop)
-
- Target prot opt source destination
-
- Chain output (Policy drop)
- Target prot opt source destination
-
- # At present, no matter whether input, output, or forward are both packet loss (drop rejection) by default, rather than accept acceptance
-
- # At this time, I am extremely secure. I am not connected to the Internet. I am not allowed to access others. Others are not allowed to access me.
-
- # Now I want to access the Internet
-
- # If I want to access port 80 of the other party, there are actually two aspects. First, I have the permission to send a request to port 80 of the other party, second, you have the permission to obtain data from port 80 of the other party. Here, only port 80 of the other party is specified, but port 80 of the other party is not specified, this means that I can access the port 80 of the other party from any port, where the port is of the TCP type.
- # Allow me to send a request to the other party's port 80
-
- Iptables-A output-p tcp -- dport 80-J accept
-
- # Allow the other party's port 80 to return data to me
-
- Iptables-A input-p tcp -- Sport 80-J accept
-
- # Although we can access port 80 of the other party at this time, entering www.baidu.com in the browser does not display the webpage of the other party, but http: // 202.108.22.142/does. Because in this process, we need to perform DNS domain name resolution and have another permission, that is, to allow me to request to the UDP port 53 of the DNS server and to return data from it
-
- Iptables-A output-p udp -- dport 53-J accept
- Iptables-A input-p udp -- Sport 53-J accept
-
- # The IP address of the DNS server is not specified here. If you want to limit the IP address of the DNS server
-
- # Write like this
-
- Iptables-A output-p udp-D 211.64.208.1 -- dport 53-J accept
-
- Iptables-A input-p udp-s 211.64.208.1 -- Sport 53-J accept
-
- #
- # I need to open port 61440 of UPD for traffic billing using drcom on the campus network.
-
- # Drcom
-
- # Allow 211.64.208.160 to connect to the 61440 (dport) of my machine from its 61440 (sport) Port)
-
- #-S indicates the source, which machine sends data to me
-
- Iptables-A input-p udp -- Sport 61440 -- dport 61440-s 211.64.208.160-J accept
-
- # Allow my machine to send data from Port 61440 (sport) to port 61440 (dport) of 211.64.208.160
- #-D specify the target machine)
-
- Iptables-A output-p udp -- Sport 61440 -- dport 61440-D 211.64.208.160-J accept
-
- # So far, I have been visiting other people as a customer. What if I want to set up a server on my computer, such as setting up sshd and web servers?
-
- # Web server, open port 80
-
- Iptables-A input-p tcp -- dport 80-J accept
-
- Iptables-A output-p tcp -- Sport 80-J accept
-
-
- # Open FTP service
-
- # Iptables-A input-M State-State established, related-J accept
-
- # Allow passive access maintained by connections.
-
- # The FTP protocol is a simple TCP protocol with poor confidentiality (plaintext). Its working principle is that the client first connects to port 21 on the server, A connection is established after three steps of handshake. It should be noted that this connection can only be used to transmit FTP commands. Nothing can be passed through this connection, even if you use the "ls" command to view files.
-
- # After a command connection is established, the server needs to establish a data connection. Data connections are divided into active and passive modes ). By default, FTP is in passive mode. The "pass" command is used between the active and passive modes. The active mode is connected to the client through Port 20, while the passive mode is connected to the client through the port after Port 1024. Because ports later than 1024 are randomly allocated, in passive mode, we do not know what ports the server uses to connect to the client. That is to say, we do not know what port iptables should open.
- #
-
- #
-
- #1 Add the following statement to the/etc/CONF. d/iptables configuration file (different distributions may have different file locations)
-
- # Iptables_modules = "ip_conntrack_ftp"
-
- #
-
- Iptables-A input-M state -- State established, related-J accept
-
- Iptables-A output-M state -- State established, related-J accept
- Iptables-A input-p tcp -- dport 21-J accept
-
- Iptables-A output-p tcp -- Sport 21-J accept
-
- # Use port 20 in Active Mode
-
- Iptables-A output-p tcp -- Sport 20-J accept
-
- Iptables-A input-p tcp -- dport 20-J accept
-
-
-
- # All data packets of the LO device are allowed, that is, local data-I indicates input, and-O indicates output
- # Indicates all data from Lo's accept
-
- Iptables-T filter-I input l-I lo-J accept
-
- # Accept
-
- Iptables-T filter-I output 1-O lo-J accept
-
- #
-
- #
-
- #
- #
-
-
-
-
-
- Complete script:
-
- Sudo/etc/init. d/iptables save
-
- Sudo/etc/init. d/iptable restart
-
- # Clearing the rule chain in a table
-
- Iptables-F
-
- Iptables-x
-
- Iptables-T nat-F
-
- Iptables-T nat-x
- # Opening the sshd service
-
- Iptables-A input-p tcp -- dport 22-J accept
-
- Iptables-A output-p tcp -- Sport 22-J accept
-
- # Drop all packages by default
-
- Iptables-P input drop
-
- Iptables-P output drop
-
- Iptables-P forward drop
-
- # Allow local devices
- Iptables-T filter-I input 1-I lo-J accept
-
- Iptables-T filter-I output 1-O lo-J accept
-
- # DNS
-
- Iptables-A output-p udp -- dport 53-J accept
-
- Iptables-A input-p udp -- Sport 53-J accept
-
- # Surfing the internet
- Iptables-A output-p tcp -- dport 80-J accept
-
- Iptables-A input-p tcp -- Sport 80-J accept
-
- # Drcom
-
- Iptables-A input-p udp -- Sport 61440 -- dport 61440-s 211.64.208.160-J accept
-
- Iptables-A output-p udp -- Sport 61440 -- dport 61440-D 211.64.208.160-J accept
-
- # Ftp
- # Add iptables_modules = "ip_conntrack_ftp" to the configuration file"
-
- Iptables-I input 2-M state -- State established, related-J accept
-
- Iptables-I output 2-M state -- State established, related-J accept
-
- Iptables-A input-p tcp -- dport 21-J accept
-
- Iptables-A output-p tcp -- Sport 21-J accept
-
- Iptables-A output-p tcp -- Sport 20-J accept
- Iptables-A input-p tcp -- dport 20-J accept
-
- # Web Services
-
- Iptables-A input-p tcp -- dport 80-J accept
-
- Iptables-A output-p tcp -- Sport 80-J accept
-
- # DHCP: use DHCP to obtain the IP address,
-
- # DHCP
-
- Iptables-A input-p udp -- Sport 67 -- dport 68-J accept