Debian系统基本的iptables防火墙文件 iptables 添加规则

2017年3月3日

防火墙配置是基本的服务器防护措施
创建一个基本的防火墙文件(开放部分端口80-http、443-https、20/21-ftp、22-ssh、ping等)

cat /etc/iptables.basic.rule

  1. *filter
  2. # Allows all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
  3. -A INPUT -i lo -j ACCEPT
  4. -A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT
  5. # Accepts all established inbound connections
  6. -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  7. # Allows all outbound traffic
  8. # You could modify this to only allow certain traffic
  9. -A OUTPUT -j ACCEPT
  10. # Allows HTTP and HTTPS connections from anywhere (the normal ports for websites)
  11. -A INPUT -p tcp --dport 80 -j ACCEPT
  12. -A INPUT -p tcp --dport 443 -j ACCEPT
  13. -A INPUT -p tcp -s 0/0 --sport 1024:65535 --dport 21 -m state --state NEW,ESTABLISHED -j ACCEPT
  14. -A INPUT -p tcp -s 0/0 --sport 1024:65535 --dport 20 -m state --state NEW,ESTABLISHED -j ACCEPT
  15. # Allows SSH connections
  16. # THE -dport NUMBER IS THE SAME ONE YOU SET UP IN THE SSHD_CONFIG FILE
  17. -A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT
  18. # Now you should read up on iptables rules and consider whether ssh access
  19. # for everyone is really desired. Most likely you will only allow access from certain IPs.
  20. # Allow ping
  21. -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
  22. # log iptables denied calls (access via 'dmesg' command)
  23. -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
  24. # Reject all other inbound - default deny unless explicitly allowed policy:
  25. -A INPUT -j REJECT
  26. -A FORWARD -j REJECT
  27. COMMIT

Or (示例)

  1. # Generated by iptables-save v1.4.14 on Wed Nov 11 17:22:03 2015
  2. *filter
  3. :INPUT DROP [0:0]
  4. :FORWARD ACCEPT [0:0]
  5. :OUTPUT ACCEPT [0:0]
  6. :syn-flood - [0:0]
  7. -A INPUT -i lo -j ACCEPT
  8. -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  9. -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
  10. -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
  11. -A INPUT -p tcp -m state --state NEW -m tcp --dport 21 -j ACCEPT
  12. -A INPUT -s 221.226.186.102 -p tcp -m state --state NEW -m tcp --dport 873 -j ACCEPT
  13. -A INPUT -p tcp -m state --state NEW -m tcp --dport 10050 -j ACCEPT
  14. -A INPUT -p tcp -m state --state NEW -m tcp --dport 20000:30000 -j ACCEPT
  15. -A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
  16. -A INPUT -p icmp -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
  17. -A INPUT -p icmp -m limit --limit 1/sec --limit-burst 10 -j ACCEPT
  18. -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood
  19. -A INPUT -j REJECT --reject-with icmp-host-prohibited
  20. -A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN
  21. -A syn-flood -j REJECT --reject-with icmp-port-unreachable
  22. COMMIT
  23. # Completed on Wed Nov 11 17:22:03 2015

2、使配置文件生效

root@Debain:~# iptables-restore < /etc/iptables.basic.rule

3、查看生效的配置文件

root@Debain:~# iptables -L

  1. Chain INPUT (policy ACCEPT)
  2. target prot opt source destination
  3. ACCEPT all -- anywhere anywhere
  4. REJECT all -- anywhere loopback/8 reject-with icmp-port-unreachable
  5. ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
  6. ACCEPT tcp -- anywhere anywhere tcp dpt:www
  7. ACCEPT tcp -- anywhere anywhere tcp dpt:https
  8. ACCEPT tcp -- anywhere anywhere tcp spts:1024:65535 dpt:ftp state NEW,ESTABLISHED
  9. ACCEPT tcp -- anywhere anywhere tcp spts:1024:65535 dpt:ftp-data state NEW,ESTABLISHED
  10. ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
  11. ACCEPT icmp -- anywhere anywhere icmp echo-request
  12. LOG all -- anywhere anywhere limit: avg 5/min burst 5 LOG level debug prefix `iptables denied: '
  13. REJECT all -- anywhere anywhere reject-with icmp-port-unreachable
  14. Chain FORWARD (policy ACCEPT)
  15. target prot opt source destination
  16. REJECT all -- anywhere anywhere reject-with icmp-port-unreachable
  17. Chain OUTPUT (policy ACCEPT)
  18. target prot opt source destination
  19. ACCEPT all -- anywhere anywhere

没有评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注