zoukankan      html  css  js  c++  java
  • Ubuntu 14.04 配置iptables防火墙

    Ubuntu默认安装是没有开启任何防火墙的,为了服务器的安全,建议大家安装启用防火墙设置,这里推荐使用iptables防火墙.如果mysql启本地使用,可以不用打开3306端口.

    # whereis iptables #查看系统是否安装防火墙可以看到:

    iptables: /sbin/iptables /usr/share/iptables /usr/share/man/man8/iptables.8.gz #表示已经安装iptables
    apt-get install iptables #如果默认没有安装,请运行此命令安装防火墙

    # iptables -L #查看防火墙配置信息,显示如下:

    Chain INPUT (policy ACCEPT)
    target prot opt source destination
    Chain FORWARD (policy ACCEPT)
    target prot opt source destination
    Chain OUTPUT (policy ACCEPT)
    target prot opt source destination

    # vi /etc/iptables/rules.v4

    添加以下内容(备注:80是指web服务器端口,3306是指MySQL数据库链接端口,22是指SSH远程管理端口.)

    *filter
    :INPUT DROP [0:0]
    :FORWARD ACCEPT [0:0]
    :OUTPUT ACCEPT [0:0]
    :syn-flood - [0:0]
    -A INPUT -i lo -j ACCEPT
    -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
    -A INPUT -p icmp -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
    -A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
    -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood
    -A INPUT -j REJECT --reject-with icmp-host-prohibited
    -A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN
    -A syn-flood -j REJECT --reject-with icmp-port-unreachable
    COMMIT

    # iptables-restore < /etc/iptables/rules.v4 #使防火墙规则生效

    # vi /etc/network/if-pre-up.d/iptables #创建文件,添加以下内容,使防火墙开机启动

    #!/bin/bash
    iptables-restore < /etc/iptables/rules.v4

    # chmod +x /etc/network/if-pre-up.d/iptables #添加执行权限

    # iptables -L -n查看规则是否生效.

    详细的看下面: 

    关于iptables有价值的信息很多,但是大多都描述的很复杂。如果你想做些基本的配置,下面的 How To 很适合你。 
    ◆ 基本命令 
    键入: 

    # iptables -L 

    列出您当前iptables中在规则。如果您是刚刚建立您的服务器,那么可能此时还没有任何规则,而且您应该看到如下: 

    Chain INPUT (policy ACCEPT) 
    target prot opt source destination 

    Chain FORWARD (policy ACCEPT) 
    target prot opt source destination 

    Chain OUTPUT (policy ACCEPT) 
    target prot opt source destination 

    ◆ 允许建立会话 

    我们可以允许建立会话来接受流量: 

    # iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT 

    ◆ 在指定端口上允许入站流量 

    阻断所有流量您也可以启动系统,但是您可能正在通过SSH工作,所有在您阻断其他流量前有必要允许SSH流量。 

    为了在22端口号(默认的SSH端口)上的允许流量入站,您可以告诉iptables允许您的网卡接受所有的目的端口为22的TCP流量。 

    # iptables -A INPUT -p tcp -i eth0 --dport ssh -j ACCEPT 

    特别的,这将向表中追加(-A)INPUT规则,允许目的端口号为SSH的所有流量进入接口(-i) eth0,以便iptables完成跳转(-j)或动作:ACCEPT 

    让我们核对下这些规则:(这里仅显示了少数行,您应该看到更多) 

    # iptables -L 
    Chain INPUT (policy ACCEPT) 
    target prot opt source destination 
    ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED 
    ACCEPT tcp -- anywhere anywhere tcp dpt:ssh 

    现在,让我们允许所有的web流量 

    # iptables -A INPUT -p tcp -i eth0 --dport 80 -j ACCEPT 

    检查我们现有的规则 

    # iptables -L 
    Chain INPUT (policy ACCEPT) 
    target prot opt source destination 
    ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED 
    ACCEPT tcp -- anywhere anywhere tcp dpt:ssh 
    ACCEPT tcp -- anywhere anywhere tcp dpt:www 

    我们已经指定SSH和web端口为允许通过的TCP流量,但是因为我们还没阻断任何流量,所以到目前为止所有的流量仍然可以进入。 

    ◆ 阻断流量 

    一旦一条规则对一个包进行了匹配,其他规则不再对这个包有效。因为我们的规则首先允许SSH和WEB流量,所以只要我们阻断所有流量的规则紧跟其後,我们依然能接受我们感兴趣的流量。我们要做的仅仅是把阻断所有流量的规则放在最後,所以我们需要再次用到它。 

    # iptables -A INPUT -j DROP 
    # iptables -L 
    Chain INPUT (policy ACCEPT) 
    target prot opt source destination 
    ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED 
    ACCEPT tcp -- anywhere anywhere tcp dpt:ssh 
    ACCEPT tcp -- anywhere anywhere tcp dpt:www 
    DROP all -- anywhere anywhere 

    因为我们刚才没有指定一个接口或一个协议,所以除了web和ssh流量外其他任何流量都会被阻断。 

    ◆ 编辑 iptables 

    到目前为止我们设置过程中唯一的问题是回环端口(loopbakc)也被阻断了。我们本可以通过指定 -i eth0 来仅仅丢弃eth0上的数据包,但我们也可以为回环端口(loopback)添加一条规则。如果我们追加这条规则,这将太晚了----因为所有的流量已经 被丢弃。我们必须插入这条跪着到第4行。 

    # iptables -I INPUT 4 -i lo -j ACCEPT 
    # iptables -L 
    Chain INPUT (policy ACCEPT) 
    target prot opt source destination 
    ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED 
    ACCEPT tcp -- anywhere anywhere tcp dpt:ssh 
    ACCEPT tcp -- anywhere anywhere tcp dpt:www 
    ACCEPT all -- anywhere anywhere 
    DROP all -- anywhere anywhere 

    最後2行看起来几乎一样,因此我们可以让iptables列的更详细些。 

    # iptables -L -v 

    ◆ 日志记录 

    在上面的例子中,所有的流量都不会被记录。如果您愿意在syslog中记录被丢弃的包, 下面将是最快捷的方式: 

    # iptables -I INPUT 5 -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7 

    看 提示 段获得更多关于logging的ideas. 

    ◆ 保存 iptables 

    如果您现在要重新启动机器的话,您的iptables配置将会消失。为了不用每次重新启动时敲入这些命令,您可以保存你的配置,让它在系统启动时自动启动。你可以通过iptables-save 和iptables-restore命令来保存配置。 


    ◆ 配置启动时自动加载规则 

    保存您的防火墙股则到一个文件 

    # iptables-save > /etc/iptables.up.rules 

    接着修改 /etc/network/interfaces 脚本自动应用这些规则(末行是添加的) 

    auto eth0 
    iface eth0 inet dhcp 
    pre-up iptables-restore < /etc/iptables.up.rules 

    你也可以准备一组规则冰并自动应用它 

    auto eth0 
    iface eth0 inet dhcp 
    pre-up iptables-restore < /etc/iptables.up.rules 
    post-down iptables-restore < /etc/iptables.down.rules 

    ◆ 提示 
    ◆ 如果你要在一个规则基础上手动编辑iptables 

    下面的步骤复习了怎样建立你的防火墙规则,并假定它们相对固定(而且对于大多数人来说它们也应该是)。但是如果你要做许多研究工作,你也许想要你的 iptables在你每次重启时保存一次。你可以在 /etc/network/interfaces 里添加像下面的一行: 

    pre-up iptables-restore < /etc/iptables.up.rules 
    post-down iptables-save > /etc/iptables.up.rules 

    "post-down iptables-save > /etc/iptables.up.rules" 此行将保存规则用于下次启动时使用。 
    ◆ 用iptables-save/restore来测试规则 

    如果你超出了这个指南来编辑iptables,你可能想利用iptables-save和iptables-restore来编辑和测试你的规则。你可以通过使用你喜爱的文本编辑器(此处为gedit)来打开这些规则文件来完成编辑。 

    # iptables-save > /etc/iptables.test.rules 
    # gedit /etc/iptables.test.rules 

    你会得到一个如下类似的文件(下面是紧接上的例子文件): 

    # Generated by iptables-save v1.3.1 on Sun Apr 23 06:19:53 2006 
    *filter 
    :INPUT ACCEPT [368:102354] 
    :FORWARD ACCEPT [0:0] 
    :OUTPUT ACCEPT [92952:20764374] 
    -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT 
    -A INPUT -i eth0 -p tcp -m tcp --dport 22 -j ACCEPT 
    -A INPUT -i eth0 -p tcp -m tcp --dport 80 -j ACCEPT 
    -A INPUT -i lo -j ACCEPT 
    -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7 
    -A INPUT -j DROP 
    COMMIT 
    # Completed on Sun Apr 23 06:19:53 2006 

    注意到这些都是减去iptables命令的iptables语句。随意编辑这些命令、完成後保存它们。然後简单的测试下: 

    # iptables-restore < /etc/iptables.test.rules 

    测试完毕後,如果你还没添加iptables-save命令 到 /etc/network/interfaces 里面,记得不要丢失了你的更改: 

    # iptables-save > /etc/iptables.up.rules 

    ◆ 更详细的日志 
    为了在你的syslog中获得更多细节,你可能想创建一个额外的链。下面是个很简短的例子---我的 /etc/iptables.up.rules ,它将展示我是如何设置iptables记录到syslog中的: 

    # Generated by iptables-save v1.3.1 on Sun Apr 23 05:32:09 2006 
    *filter 
    :INPUT ACCEPT [273:55355] 
    :FORWARD ACCEPT [0:0] 
    :LOGNDROP - [0:0] 
    :OUTPUT ACCEPT [92376:20668252] 
    -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT 
    -A INPUT -i eth0 -p tcp -m tcp --dport 22 -j ACCEPT 
    -A INPUT -i eth0 -p tcp -m tcp --dport 80 -j ACCEPT 
    -A INPUT -i lo -j ACCEPT 
    -A INPUT -j LOGNDROP 
    -A LOGNDROP -p tcp -m limit --limit 5/min -j LOG --log-prefix "Denied TCP: " --log-level 7 
    -A LOGNDROP -p udp -m limit --limit 5/min -j LOG --log-prefix "Denied UDP: " --log-level 7 
    -A LOGNDROP -p icmp -m limit --limit 5/min -j LOG --log-prefix "Denied ICMP: " --log-level 7 
    -A LOGNDROP -j DROP 
    COMMIT 
    # Completed on Sun Apr 23 05:32:09 2006 

    请注意 一个名为 LOGNDROP的链在文件顶部。而且,INPUT链底部标准的DROP被替换成了LOGNDROP,同时添加了协议描述so it makes sense looking at the log。最後我们在LOGNDROP链尾部丢弃了这些流量。下面的行告诉我们发生了什么: 

    * --limit 设置记录相同规则到syslog中的次数 
    * --log-prefix "Denied..." 添加一个前缀使得在syslog中查找更easy 
    * --log-level 7 设置syslog的消息级别 (see man syslog for more detail, but you can probably leave this) 

    ◆ 禁用防火墙 

    如果您要临时禁用防火墙,您可以通过下面的命令清空所偶的规则: 

    # iptables -F 

    ◆ 轻松配置通过 GUI 

    新手可以利用 Firetarter(一个gui工具)---仓库中的可用软件(新立德或apt-get 获得)来配置她或他的iptables规则,而需要命令行知识。请查看指南,尽管...... 配置很简单,但是对于高级用户来说可能远远不能满足。然而它对于大多数的家庭用户来说是足够的...... 。(我)建议您使用firestarter在策略表中将出站配置为 “限制”,而将您需要的连接类型(如用于http的80、https的443,msn chat的1683等等)加入白名单。您也可以通过它查看进出您计算机的活动连接...... 。防火墙会一直保持下去一旦通过向导配置完毕。拨号用户必须在向导中指定它在拨号时自动启动。 

    firestarter主页: http://www.fs-security.com/ (再次, 仓库源中可用, 不需要编译) 指南: http://www.fs-security.com/docs/tutorial.php 

    个人笔记:不幸运的是,它没有阻断(或询问用户)特定应用/程序的选项......。因此,我的理解是一旦启用了80端口(例如,用于访问网页),那么任何程序都可以通过80端口连接任何服务器、做任何它想做的事....

  • 相关阅读:
    UVA 1386 Cellular Automaton
    ZOJ 3331 Process the Tasks
    CodeForces 650B Image Preview
    CodeForces 650A Watchmen
    CodeForces 651B Beautiful Paintings
    CodeForces 651A Joysticks
    HUST 1601 Shepherd
    HUST 1602 Substring
    HUST 1600 Lucky Numbers
    POJ 3991 Seinfeld
  • 原文地址:https://www.cnblogs.com/mafeng/p/6547106.html
Copyright © 2011-2022 走看看