防火墙样本

简介:

 

 单机防火墙需要考虑的只是本机对外的一块网卡,因此入站要限IP,限端口

 

样本

 

 
 
  1. #!/usr/bin/env bash 
  2.  
  3. if [ "$(id -u)" != "0" ]; then 
  4.    echo "This script is designed to run as root" 1>&2 
  5.    exit 1 
  6. fi 
  7.  
  8. modprobe ip_tables 
  9. modprobe iptable_filter 
  10. modprobe ipt_REJECT 
  11. modprobe ip_conntrack 
  12.  
  13. iptables -P INPUT ACCEPT 
  14. iptables -F 
  15. iptables -A INPUT -i lo -j ACCEPT 
  16. iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT 
  17.  
  18. iptables -A INPUT -p tcp -s 10.0.0.0/8 --dport 22 -j ACCEPT 
  19. iptables -A INPUT -p tcp -s 10.0.0.0/8 --dport 80 -j ACCEPT 
  20. iptables -A INPUT -p tcp -s 10.0.0.0/8 --dport 5666 -j ACCEPT 
  21.  
  22. iptables -A INPUT -p udp -s 10.0.0.0/8 --dport 123 -j ACCEPT 
  23. iptables -A INPUT -p udp -s 10.0.0.0/8 --dport 161 -j ACCEPT 
  24.  
  25.  
  26. iptables -A INPUT -p icmp -m icmp --icmp-type 8 -m limit --limit 1/sec -j ACCEPT 
  27.  
  28. iptables -A OUTPUT -s 224.0.0.0/8 -j DROP 
  29. iptables -A OUTPUT -d 224.0.0.0/8 -j DROP 
  30. iptables -A OUTPUT -s 255.255.255.255/32 -j DROP 
  31. iptables -A OUTPUT -m state --state INVALID -j DROP 
  32.  
  33. iptables -P INPUT DROP 
  34. iptables -P FORWARD DROP 
  35. iptables -P OUTPUT ACCEPT 
  36. service iptables save 
  37. iptables -L -v -n  

 开放了内网的端口,ping包每秒一个,可以根据需要修改。

 

双网卡做网关(SNAT),既要保护自己,又要保护内网机器

 

样本 

 

 
 
  1. #!/usr/bin/env bash 
  2.  
  3. if [ "$(id -u)" != "0" ]; then 
  4.    echo "This script is designed to run as root" 1>&2 
  5.    exit 1 
  6. fi 
  7.  
  8. PATH=/usr/sbin:/sbin:/bin:/usr/bin 
  9.  
  10. wan=eth0 
  11. lan=eth1 
  12. # delete all existing rules. 
  13. iptables -F 
  14. iptables -t nat -F 
  15. iptables -t mangle -F 
  16. iptables -X 
  17. iptables -Z 
  18.  
  19. # Set the INPUT policy to ALLOW for the moment 
  20. iptables -P INPUT ACCEPT 
  21.  
  22. # Always accept loopback traffic 
  23. iptables -A INPUT -i lo -j ACCEPT 
  24.  
  25. # Allow established connections, and those not coming from the outside 
  26. iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT 
  27.  
  28. # Limit ping 
  29. iptables -A INPUT -p icmp -m icmp --icmp-type 8 -m limit --limit 1/sec -j ACCEPT 
  30.  
  31. # Open some ports to the public 
  32. iptables -A INPUT -i $wan -p tcp --dport 80 -j ACCEPT 
  33.  
  34. iptables -A INPUT -s 123.234.345.456 -p tcp --dport 22 -j ACCEPT 
  35.  
  36. # Open some ports to local netwrk 
  37. iptables -A INPUT -i $lan -p tcp --dport 22 -m recent --set --name ssh --rsource 
  38. iptables -A INPUT -i $lan -p tcp --dport 22 -m recent ! --rcheck --seconds 60 --hitcount 10 --name ssh --rsource -j ACCEPT 
  39.  
  40. iptables -A INPUT -i $lan -p udp --dport 123 -j ACCEPT 
  41. iptables -A INPUT -i $lan -p udp --dport 161 -j ACCEPT 
  42. iptables -A INPUT -i $lan -p tcp --dport 5666 -j ACCEPT 
  43. iptables -A INPUT -i $lan -p tcp --dport 9102 -j ACCEPT 
  44. iptables -A INPUT -i $lan -p tcp --dport 10000 -j ACCEPT 
  45. iptables -A INPUT -i $lan -p tcp --dport 10050 -j ACCEPT 
  46.  
  47. # Apply the default policy 
  48. iptables -P INPUT DROP 
  49. iptables -P FORWARD DROP 
  50. iptables -P OUTPUT ACCEPT 
  51.  
  52. # Allow outgoing connections from the LAN side. 
  53. iptables -A FORWARD -i $lan -o $wan -j ACCEPT 
  54.  
  55. iptables -A FORWARD -i $wan -o $lan -m state --state ESTABLISHED,RELATED -j ACCEPT 
  56.  
  57. iptables -A OUTPUT -s 224.0.0.0/8 -j DROP 
  58. iptables -A OUTPUT -d 224.0.0.0/8 -j DROP 
  59. iptables -A OUTPUT -s 255.255.255.255/32 -j DROP 
  60. iptables -A OUTPUT -m state --state INVALID -j DROP 
  61.  
  62. # Masquerade. 
  63. iptables -t nat -A POSTROUTING -o $wan -j MASQUERADE 
  64.  
  65. # Enable routing. 
  66. echo 1 > /proc/sys/net/ipv4/ip_forward 
  67.  
  68. # Save and restart iptables  
  69. service iptables save 
  70. service iptables restart 
  71.  
  72. # Show the final rules 
  73. iptables -n -v -L 
  74. iptables -n -v -L -t nat 








本文转自 紫色葡萄 51CTO博客,原文链接:http://blog.51cto.com/purplegrape/1051478,如需转载请自行联系原作者
目录
相关文章
|
1月前
|
机器学习/深度学习 人工智能 数据可视化
什么是条件生成对抗性网络?
什么是条件生成对抗性网络?
|
7月前
|
缓存 运维 搜索推荐
快速提高网站权重,这六个技巧一定要掌握!
SEO权重,简单来说就是网页在搜索引擎中的权值,常常被称为PR值(PageRank),它是衡量一个网站在搜索引擎中的重要性度量,它对于一个网站意义至关重要,一方面它不但有利于网站在搜索引擎排名靠前,另一方面还能提高整站的流量,提高网站信任度。
|
11月前
|
机器学习/深度学习 数据采集 运维
基于支持向量机的网络⼊侵检测系统的全面调查和分类
基于支持向量机的网络⼊侵检测系统的全面调查和分类
|
10月前
|
运维 网络协议 算法
基于Ryu 防火墙的检测和解决异常入侵的流量--实验
基于Ryu 防火墙的检测和解决异常入侵的流量--实验
|
10月前
|
机器学习/深度学习 数据采集 安全
使用支持向量机的基于异常的入侵检测系统
使用支持向量机的基于异常的入侵检测系统
|
11月前
|
网络协议 Ubuntu Linux
nmap流量特征修改
nmap流量特征修改
nmap流量特征修改
|
安全 网络协议 Shell
防火墙豁免实验
防火墙豁免实验
防火墙豁免实验
|
安全 网络协议 Shell
防火墙练习实验
防火墙练习实验
防火墙练习实验
|
Shell 网络安全
防火墙nat实验
防火墙nat实验
|
网络协议 Shell 网络安全
防火墙原理讲解——练习实验
防火墙原理讲解——练习实验
防火墙原理讲解——练习实验