防火墙样本

简介:

 

 单机防火墙需要考虑的只是本机对外的一块网卡,因此入站要限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,如需转载请自行联系原作者
目录
相关文章
|
安全 Linux 应用服务中间件
在Linux中,包过滤防火墙与代理应用防火墙有什么区别?有哪些相应的产品?
在Linux中,包过滤防火墙与代理应用防火墙有什么区别?有哪些相应的产品?
|
机器学习/深度学习 安全 网络协议
Linux防火墙iptables命令管理入门
本文介绍了关于Linux防火墙iptables命令管理入门的教程,涵盖了iptables的基本概念、语法格式、常用参数、基础查询操作以及链和规则管理等内容。
505 73
|
监控 安全 Linux
启用Linux防火墙日志记录和分析功能
为iptables启用日志记录对于监控进出流量至关重要
445 1
|
网络协议 Linux 网络安全
入职必会-开发环境搭建39-Linux常用操作-Linux防火墙操作
在CentOS 7中,新引入了firewalld服务(防火墙),取代了CentOS 6之前的iptables服务(防火墙)。
225 5
入职必会-开发环境搭建39-Linux常用操作-Linux防火墙操作
|
存储 运维 Linux
Linux防火墙firewall的使用
CentOS 7 中的 firewalld 是基于 Netfilter 的防火墙服务,支持动态配置,无需重启服务即可生效。它通过区域管理网络流量,每个区域可以设置不同的防火墙规则。默认区域为 public,可以通过命令行工具 firewall-cmd 进行管理和配置。firewalld 提供了丰富的预定义服务和区域,方便用户根据需求进行灵活配置。
277 0
|
Linux 网络安全
linux关闭方防火墙的命令
linux关闭方防火墙的命令
294 2
|
Linux 网络安全
在Linux中,如何设置防火墙规则?
在Linux中,如何设置防火墙规则?
|
Linux 网络安全
在Linux中,iptables和firewalld两种防火墙如何使用?
在Linux中,iptables和firewalld两种防火墙如何使用?
|
安全 Linux 测试技术
在Linux中,如何配置防火墙和安全规则?
在Linux中,如何配置防火墙和安全规则?
|
监控 安全 Linux
在Linux中,什么是入侵检测系统(IDS)和入侵防御系统(IPS)?
在Linux中,什么是入侵检测系统(IDS)和入侵防御系统(IPS)?