) Iptables Firewall

Source: Internet
Author: User
Tags ftp commands ftp protocol

From: http://blog.csdn.net/jixiuffff/article/details/5879547

[C-sharp] View plaincopy
  1. # Five checkpoints prerouting, forward postrouting Input Output
  2. # 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
  3. # 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.
  4. #
  5. # Prerouting ----------------> forward ---------------------> postrouting
  6. # | ^
  7. # |
  8. # Input Output
  9. # |
  10. # V |
  11. # ---> Application on my machine ---------->
  12. #
  13. # 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.
  14. # 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.
  15. #
  16. # The filter has three built-in rule chains by default. The input forward Output
  17. # Nat ........... Postrouting, output prerouting
  18. # Mangle ...... two .......... output prerouting
  19. #
  20. # The general format of the iptables command iptables [-T table] operation [Chain] [Options]
  21. # An example of checking complete Parameters
  22. # Iptables-T filter-I input 2-I eth0-s 10.2.1.111 -- Sport 1234-D 10.2.1.123 -- dport 22-J accept
  23. # Iptables-T filter-I output 2-I eth0-D 10.2.1.123 -- dport 22-s 10.2.1.123 -- Sport 1234-J accept
  24. # 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.
  25. # Then, from my IP address 10.2.1.123: 22 to 10.2.1.111: 1234 through the eth0 Nic package release
  26. # Iptables-F: clears all rule chains of the filter.
  27. # Iptables-T nat-F clear all rule chains in the NAT table
  28. ######################################## ######################################## ###################
  29. # 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.
  30. #-S-d -- Sport -- dport indicates the Source IP (source), destination IP (destination), Source IP port, and destination IP port respectively.
  31. # When processing input,-S refers to the opposite machine, and-D refers to my machine, because data flows from the other machine to me,
  32. # When processing the output,-S refers to me, and-D refers to the opposite machine.
  33. # 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
  34. # First start iptables service/etc/init. d/iptables start
  35. # 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,
  36. /Etc/init. d/iptables save
  37. /Etc/init. d/iptables start
  38. # Check the default access rules after startup
  39. Iptables-l or iptables-l -- line-number: displays the row number and-V details.
  40. Chain input (Policy accept)
  41. Target prot opt source destination
  42. Chain forward (Policy accept)
  43. Target prot opt source destination
  44. Chain output (Policy accept)
  45. Target prot opt source destination
  46. # By default, the policy is accept, which means no firewall exists. Now, modify the Default policy.
  47. # Do not use a remote SSH connection for this operation, because it will also disable port 22 used by SSH,
  48. # Use SSH to connect. First open port 22 and then run the following three commands:
  49. # Sshd
  50. # Allow any machine to send requests to port 22
  51. #-T is not used here. The default value is-T filter.
  52. Iptables-A input-p tcp -- dport 22-J accept
  53. # Equivalent to iptables-T filter-A input-p tcp -- dport 22-J accept
  54. # Allow data output from port 22
  55. Iptables-A output-p tcp -- Sport 22-J accept
  56. # If only machines with certain IP addresses can access me, replace the above two
  57. Iptables-A input-p tcp -- dport 22-s 10.2.1.110-J accept
  58. Iptables-A output-p tcp -- Sport 22-D 10.2.1.110-J accept
  59. # Currently, only IP address 10.2.1.110 can access me.
  60. #
  61. Iptables-P input drop
  62. Iptables-P output drop
  63. Iptables-P forward drop
  64. # The Default policy can only be accept, drop, or reject.
  65. Chain input (Policy drop)
  66. Target prot opt source destination
  67. Chain forward (Policy drop)
  68. Target prot opt source destination
  69. Chain output (Policy drop)
  70. Target prot opt source destination
  71. # At present, no matter whether input, output, or forward are both packet loss (drop rejection) by default, rather than accept acceptance
  72. # 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.
  73. # Now I want to access the Internet
  74. # 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.
  75. # Allow me to send a request to the other party's port 80
  76. Iptables-A output-p tcp -- dport 80-J accept
  77. # Allow the other party's port 80 to return data to me
  78. Iptables-A input-p tcp -- Sport 80-J accept
  79. # 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
  80. Iptables-A output-p udp -- dport 53-J accept
  81. Iptables-A input-p udp -- Sport 53-J accept
  82. # The IP address of the DNS server is not specified here. If you want to limit the IP address of the DNS server
  83. # Write like this
  84. Iptables-A output-p udp-D 211.64.208.1 -- dport 53-J accept
  85. Iptables-A input-p udp-s 211.64.208.1 -- Sport 53-J accept
  86. #
  87. # I need to open port 61440 of UPD for traffic billing using drcom on the campus network.
  88. # Drcom
  89. # Allow 211.64.208.160 to connect to the 61440 (dport) of my machine from its 61440 (sport) Port)
  90. #-S indicates the source, which machine sends data to me
  91. Iptables-A input-p udp -- Sport 61440 -- dport 61440-s 211.64.208.160-J accept
  92. # Allow my machine to send data from Port 61440 (sport) to port 61440 (dport) of 211.64.208.160
  93. #-D specify the target machine)
  94. Iptables-A output-p udp -- Sport 61440 -- dport 61440-D 211.64.208.160-J accept
  95. # 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?
  96. # Web server, open port 80
  97. Iptables-A input-p tcp -- dport 80-J accept
  98. Iptables-A output-p tcp -- Sport 80-J accept
  99.  
  100. # Open FTP service
  101. # Iptables-A input-M State-State established, related-J accept
  102. # Allow passive access maintained by connections.
  103. # 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.
  104. # 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.
  105. #
  106. #
  107. #1 Add the following statement to the/etc/CONF. d/iptables configuration file (different distributions may have different file locations)
  108. # Iptables_modules = "ip_conntrack_ftp"
  109. #
  110. Iptables-A input-M state -- State established, related-J accept
  111. Iptables-A output-M state -- State established, related-J accept
  112. Iptables-A input-p tcp -- dport 21-J accept
  113. Iptables-A output-p tcp -- Sport 21-J accept
  114. # Use port 20 in Active Mode
  115. Iptables-A output-p tcp -- Sport 20-J accept
  116. Iptables-A input-p tcp -- dport 20-J accept
  117.  
  118. # All data packets of the LO device are allowed, that is, local data-I indicates input, and-O indicates output
  119. # Indicates all data from Lo's accept
  120. Iptables-T filter-I input l-I lo-J accept
  121. # Accept
  122. Iptables-T filter-I output 1-O lo-J accept
  123. #
  124. #
  125. #
  126. #
  127. Complete script:
  128. Sudo/etc/init. d/iptables save
  129. Sudo/etc/init. d/iptable restart
  130. # Clearing the rule chain in a table
  131. Iptables-F
  132. Iptables-x
  133. Iptables-T nat-F
  134. Iptables-T nat-x
  135. # Opening the sshd service
  136. Iptables-A input-p tcp -- dport 22-J accept
  137. Iptables-A output-p tcp -- Sport 22-J accept
  138. # Drop all packages by default
  139. Iptables-P input drop
  140. Iptables-P output drop
  141. Iptables-P forward drop
  142. # Allow local devices
  143. Iptables-T filter-I input 1-I lo-J accept
  144. Iptables-T filter-I output 1-O lo-J accept
  145. # DNS
  146. Iptables-A output-p udp -- dport 53-J accept
  147. Iptables-A input-p udp -- Sport 53-J accept
  148. # Surfing the internet
  149. Iptables-A output-p tcp -- dport 80-J accept
  150. Iptables-A input-p tcp -- Sport 80-J accept
  151. # Drcom
  152. Iptables-A input-p udp -- Sport 61440 -- dport 61440-s 211.64.208.160-J accept
  153. Iptables-A output-p udp -- Sport 61440 -- dport 61440-D 211.64.208.160-J accept
  154. # Ftp
  155. # Add iptables_modules = "ip_conntrack_ftp" to the configuration file"
  156. Iptables-I input 2-M state -- State established, related-J accept
  157. Iptables-I output 2-M state -- State established, related-J accept
  158. Iptables-A input-p tcp -- dport 21-J accept
  159. Iptables-A output-p tcp -- Sport 21-J accept
  160. Iptables-A output-p tcp -- Sport 20-J accept
  161. Iptables-A input-p tcp -- dport 20-J accept
  162. # Web Services
  163. Iptables-A input-p tcp -- dport 80-J accept
  164. Iptables-A output-p tcp -- Sport 80-J accept
  165. # DHCP: use DHCP to obtain the IP address,
  166. # DHCP
  167. Iptables-A input-p udp -- Sport 67 -- dport 68-J accept

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.