设置单机防火墙的一个实例
2016-03-03 23:33
309 查看
这是从《鸟哥的Linux私房菜(服务器)》第三版中摘出来的一个防火墙设置实例。具体整理如下:
一、规则草拟
网络接口有底下这些:
外部网络使用 eth0 (如果是拨接,有可能是 ppp0,请针对你的环境来设定);
内部网络使用 eth1 ,且内部使用 192.168.100.0/24 这个 Class ;
主机默认开放的服务有 WWW, SSH, https 等等;
具体的架构示意图如下:
整个防火墙流程如下:
实际防火墙规则的脚步如下,整个script拆成3部分:
- iptables.rule:设定最基本的规则,包括清除防火墙规则、加载模块、设定服务可接受等;
- iptables.deny:设定抵挡某些恶意主机的进入;
- iptables.allow:设定允许某些自定义的后门来源主机!
具体脚步如下:
[root@www ~]# mkdir -p /usr/local/virus/iptables
[root@www ~]# cd /usr/local/virus/iptables
[root@www iptables]# vim iptables.rule
#!bin/bash # 请先输入您的相关参数,不要输入错误了! EXTIF="eth0" # 这个是可以连上 Public IP 的网络接口 INIF="eth1" # 内部 LAN 的连接接口;若无则写成 INIF="" INNET="192.168.100.0/24" # 若无内部网域接口,请填写成 INNET="" export EXTIF INIF INNET # 第一部份,针对本机的防火墙设定!########################################## # 1. 先设定好核心的网络功能: echo "1" > /proc/sys/net/ipv4/tcp_syncookies echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts for i in /proc/sys/net/ipv4/conf/*/{rp_filter,log_martians}; do echo "1" > $i done for i in /proc/sys/net/ipv4/conf/*/{accept_source_route,accept_redirects,\ send_redirects}; do echo "0" > $i done # 2. 清除规则、设定默认政策及开放 lo 与相关的设定值 PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin; export PATH iptables -F iptables -X iptables -Z iptables -P INPUT DROP iptables -P OUTPUT ACCEPT iptables -P FORWARD ACCEPT iptables -A INPUT -i lo -j ACCEPT iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT # 3. 启动额外的防火墙 script 模块 if [ -f /usr/local/virus/iptables/iptables.deny ]; then sh /usr/local/virus/iptables/iptables.deny fi if [ -f /usr/local/virus/iptables/iptables.allow ]; then sh /usr/local/virus/iptables/iptables.allow fi if [ -f /usr/local/virus/httpd-err/iptables.http ]; then sh /usr/local/virus/httpd-err/iptables.http fi # 4. 允许某些类型的 ICMP 封包进入 AICMP="0 3 3/4 4 11 12 14 16 18" for tyicmp in $AICMP do iptables -A INPUT -i $EXTIF -p icmp --icmp-type $tyicmp -j ACCEPT done # 5. 允许某些服务的进入,请依照你自己的环境开启 # iptables -A INPUT -p TCP -i $EXTIF --dport 21 --sport 1024:65534 -j ACCEPT # FTP # iptables -A INPUT -p TCP -i $EXTIF --dport 22 --sport 1024:65534 -j ACCEPT # SSH # iptables -A INPUT -p TCP -i $EXTIF --dport 25 --sport 1024:65534 -j ACCEPT # SMTP # iptables -A INPUT -p UDP -i $EXTIF --dport 53 --sport 1024:65534 -j ACCEPT # DNS # iptables -A INPUT -p TCP -i $EXTIF --dport 53 --sport 1024:65534 -j ACCEPT # DNS # iptables -A INPUT -p TCP -i $EXTIF --dport 80 --sport 1024:65534 -j ACCEPT # WWW # iptables -A INPUT -p TCP -i $EXTIF --dport 110 --sport 1024:65534 -j ACCEPT # POP3 # iptables -A INPUT -p TCP -i $EXTIF --dport 443 --sport 1024:65534 -j ACCEPT # HTTPS # 第二部份,针对后端主机的防火墙设定!############################### # 1. 先加载一些有用的模块 modules="ip_tables iptable_nat ip_nat_ftp ip_nat_irc ip_conntrack ip_conntrack_ftp ip_conntrack_irc" for mod in $modules do testmod=`lsmod | grep "^${mod} " | awk '{print $1}'` if [ "$testmod" == "" ]; then modprobe $mod fi done # 2. 清除 NAT table 的规则吧! iptables -F -t nat iptables -X -t nat iptables -Z -t nat iptables -t nat -P PREROUTING ACCEPT iptables -t nat -P POSTROUTING ACCEPT iptables -t nat -P OUTPUT ACCEPT # 3. 若有内部接口的存在 (双网卡) 开放成为路由器,且为 IP 分享器! if [ "$INIF" != "" ]; then iptables -A INPUT -i $INIF -j ACCEPT echo "1" > /proc/sys/net/ipv4/ip_forward if [ "$INNET" != "" ]; then for innet in $INNET do iptables -t nat -A POSTROUTING -s $innet -o $EXTIF -j MASQUERADE done fi fi # 如果你的 MSN 一直无法联机,或者是某些网站 OK 某些网站不 OK, # 可能是 MTU 的问题,那你可以将底下这一行给他取消批注来启动 MTU 限制范围 # iptables -A FORWARD -p tcp -m tcp --tcp-flags SYN,RST SYN -m tcpmss \ # --mss 1400:1536 -j TCPMSS --clamp-mss-to-pmtu # 4. NAT 服务器后端的 LAN 内对外之服务器设定 # iptables -t nat -A PREROUTING -p tcp -i $EXTIF --dport 80 \ # -j DNAT --to-destination 192.168.1.210:80 # WWW # 5. 特殊的功能,包括 Windows 远程桌面所产生的规则,假设桌面主机为 1.2.3.4 # iptables -t nat -A PREROUTING -p tcp -s 1.2.3.4 --dport 6000 \ # -j DNAT --to-destination 192.168.100.10 # iptables -t nat -A PREROUTING -p tcp -s 1.2.3.4 --sport 3389 \ # -j DNAT --to-destination 192.168.100.20 # 6. 最终将这些功能储存下来吧! /etc/init.d/iptables save
假如我要让一个 140.116.44.0/24 这个网域的所有主机来源可以进入我的主机的话,那么这个档案的内容可以写成这样:
[root@www iptables]# vim iptables.allow #!/bin/bash # 底下则填写你允许进入本机的其他网域或主机啊! iptables -A INPUT -i $EXTIF -s 140.116.44.0/24 -j ACCEPT # 底下则是关于抵挡的档案设定法! [root@www iptables]# vim iptables.deny #!/bin/bash # 底下填写的是『你要抵挡的那个咚咚!』 iptables -A INPUT -i $EXTIF -s 140.116.44.254 -j DROP [root@www iptables]# chmod 700 iptables.*
相关文章推荐
- hive建表以及测试
- Android中的ImageView的设置
- redis和memcached的区别
- 自己实现字符串转数字
- Celery+rabbitmq+mysql+flower
- gdb常用调试命令以及多线程堆栈的查看
- 密码爆破工具:Medusa(美杜莎)-操作说明
- php计算串/文件的md5值
- iOS UITabBar 详解
- 一次dialog导致的内存泄漏
- 如何在 Ubuntu 15.04 上安装带 JSON 支持的 SQLite 3.9
- CentOS(5.8/6.4)linux生产环境若干优化实战
- POJ 3414 Pots【BFS】【图搜】
- 筛法求素数 (一般的线性筛法)
- linker command failed with exit code 1 (use -v to see 错误总结
- fis-receiver:一行命令将项目部署到远程服务器
- 30分钟带你快速入门MySQL教程
- Redis杂记
- jQuery修改操作css属性实现方法
- 需要思考的一些问题列表