iptables防火墙


======================================
http://blog.51cto.com/search/user?uid=2561410&q=iptables

iptables及tcpdump实战应用案例
企业优秀运维人员20道必会iptables面试题

http://www.cnblogs.com/clsn/p/8308678.html
企业防火墙之iptables
======================================
1.iptables前言
关闭两项功能:
(1)selinux(生产中是关闭的),ids入侵检测,md5指纹。
(2)iptables(生产中看情况,内网关闭,外网打开)
大并发的情况下不能开iptables,影响性能,硬件防火墙。
安全优化:
(1)尽可能不给服务器配置外网IP。可以通过代理转发或者通过防火墙映射。
(2)并发不是特别大的情况下在外网IP的环境,要开启iptables防火墙。

企业网站安全案例与全面防护讲座
http://edu.51cto.com/course/772.html

学习iptables基础:
(1)OSI7层模型及其对应的协议。
(2)TCP/IP三次握手,四次挥手的过程,TCP HEADER.
(3)常用的服务端口要记住。

2.iptables简介
  Netfilter/ tables(以下简称 Iptables)是unix/inux自带的一款优秀且开放源代码的完全自由的基于包过滤的防火墙工具,它的功能十分强大,使用非常灵活,可以对流入和流出服务器的数据包进行很精细的控制。特别是它可以在一台非常低的硬件配置下跑的非常好(本人曾经在赛扬500Hz cpu64M内存的情况部署网关防火墙)提供近400人的上网服务丝毫不逊色企业级专业路由器防火墙)。 iptables+ zebra+ squid
  Iptables是linux2.4及2.6内核中集成的服务。其功能与安全性比其老一辈ipfwadm, ipchains强大的多(长江后浪推前浪啊),Iptables主要工作在OSI七层的二、三、四层,如果重新编译内核,Iptables也可以支持7层控制(squid代理+ iptables)。

[root@oldboyedu-01 ~]# man iptables
容器:包含或者说属于的关系。

Netfilter/iptables(楼):是表的的容器,iptables包含各个表。
(filter,NAT,MANGLE,RAW)

iptables的表(房子):是链的容器,即所有的链都属于其对应的表。

链(chains)(家居比如柜子):是规则的容器,
链:INPUT,OUTPUT,FORWARD,PREROUTING,POSTROUTING
注意:所有链名要大写。

规则(policys)(柜子如何增加摆放柜子东西等):就是iptables一系列过滤信息的规范和具体方法条款,即一条条过滤的语句。

  

  

帮助文件:
[root@oldboyedu-01 ~]# man iptables
[root@oldboyedu-01 ~]# iptables -h
iptables v1.4.7
Usage: iptables -[ACD] chain rule-specification [options]
       iptables -I chain [rulenum] rule-specification [options]
       iptables -R chain rulenum rule-specification [options]
       iptables -D chain rulenum [options]
       iptables -[LS] [chain [rulenum]] [options]
       iptables -[FZ] [chain] [options]
       iptables -[NX] chain
       iptables -E old-chain-name new-chain-name
       iptables -P chain target [options]
       iptables -h (print this help information)

Commands:
Either long or short options are allowed.
  --append  -A chain        Append to chain //增加规则
  --check   -C chain        Check for the existence of a rule
  --delete  -D chain        Delete matching rule from chain //删除规则
  --delete  -D chain rulenum
                Delete rule rulenum (1 = first) from chain
  --insert  -I chain [rulenum]
                Insert in chain as rulenum (default 1=first) //插入规则
  --replace -R chain rulenum
                Replace rule rulenum (1 = first) in chain
  --list    -L [chain [rulenum]]
                List the rules in a chain or all chains //列表一个或所有链
  --list-rules -S [chain [rulenum]]
                Print the rules in a chain or all chains
  --flush   -F [chain]        Delete all rules in  chain or all chains //清空所有规则,但不会处理默认规则
  --zero    -Z [chain [rulenum]]
                Zero counters in chain or all chains //链的计数器清零
  --new     -N chain        Create a new user-defined chain //定义自定义链
  --delete-chain
            -X [chain]        Delete a user-defined chain //删除自定义链
  --policy  -P chain target //指定改变目标链的规则
                Change policy on chain to target
  --rename-chain
            -E old-chain new-chain
                Change chain name, (moving any references)
Options:
[!] --proto    -p proto    protocol: by number or name, eg. `tcp' //指定协议参数
[!] --source    -s address[/mask][...] //指定源地址
                source specification
[!] --destination -d address[/mask][...] //指定目的地址
                destination specification
[!] --in-interface -i input name[+] //指定进入的接口
                network interface name ([+] for wildcard)
 --jump    -j target
                target for rule (may load target extension) //指定行为目标
                基本的处理行为:ACCEPT(接受)、DROP(丢弃)、REJECT(拒绝)
  --goto      -g chain
                              jump to chain with no return
  --match    -m match    //匹配网络状态
                extended match (may load extension)
  --numeric    -n        numeric output of addresses and ports //地址和端口的数字显示
[!] --out-interface -o output name[+]
                network interface name ([+] for wildcard)
  --table    -t table    table to manipulate (default: `filter') //指定表,默认filter
  --verbose    -v        verbose mode //显示
  --line-numbers        print line numbers when listing //显示规则序号
  --exact    -x        expand numbers (display exact values) //显示
[!] --fragment    -f        match second or further fragments only
  --modprobe=<command>        try to insert modules using this command
  --set-counters PKTS BYTES    set the counter during insert/append
[!] --version    -V        print package version.

3.iptables防火墙filter表及对应链(重要,着重对主机防护)
[root@oldboyedu-01 ~]# iptables -L -n
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

[root@oldboyedu-01 ~]# iptables -L -n -t nat
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination

filter表:主要和主机自身有关,真正负责主机防火墙功能的(过滤流入流出的数据包)。filter表是iptables默认使用的表。
这个表定义了三个链:
(1)INPUT:负责过滤所有目标地址是本机地址的数据包。即过滤进入主机的数据包。
(2)OUTPUT:处理所有源地址是本机地址的数据包。即处理从主机发出去的数据包。
(3)FORWARD:负责转发流经主机的数据包。起转发作用,和NAT关系很大,LVS NAT模式,net.ipv4.ip_forward=0。
强调:对于filter表的控制是我们实现本机防火墙功能的重要手段,特别是对INPUT链的控制。

4.iptables防火墙NAT表及对应链
NAT表:负责网络地址转换,即来源与目的IP地址和port的转换。应用:和主机本身无关。一般用于局域网共享上网或者特殊的端口转换服务相关。
NAT功能一般企业工作场景:
(1)用于做企业路由(zebra)或者网关(iptables),共享上网(POSTROUTING)。
(2)做内部外部IP地址一对一映射(dmz),硬件防火墙映射IP到内部服务器,ftp服务(PREROUTING)。
(3)web,单个端口的映射,直接映射80端口(PREROUTING)。

这个表定义了三个链,nat功能就相当于网络的ACL控制,和网络交换机ACL类似。
(1)OUTPUT:和主机发出去的数据包有关,改变主机发出去的数据包的目标地址。
(2)PREROUTING:在数据包到达防火墙时进行路由判断之前执行的规则,作用是改变数据包的目的地址、目的端口等(eg:收信时根据规则重写收件人的地址,不地道,哈哈!)例如:将公网IP为124.42.60.113映射局域网的10.0.0.19服务器上。如果是web服务器可以把80转换为局域网的服务器上的9000端口。
(3)POSTROUTING:在数据包离开防火墙时进行路由判断之后执行的规则,作用是改变数据包的源地址、源端口等(eg:寄信时写好发件人的地址,要让人家回信时能够有地址可回。)例如:我们现在的带笔记本和虚拟机都是10.0.0.0/24,就是出网时被我们企业路由器把源地址改成了公网地址。
生产应用:局域网共享上网。
注意:私网地址在互联网没有注册,路由器不转发!

5.iptables工作流程
是人发明的就要符合人生活中的东西。
iptables采用数据包过滤机制工作的,它会对请求的数据包的包头数据进行分析,并根据我们预先设定的规则进行匹配来决定是否可以进入主机(有系统管理员控制)。
  
iptables工作流程小结:
<1>防火墙是一层层过滤的。实际是按照配置规则的顺序从上到下,从前到后进行过滤的。
<2>如果匹配上了规则,即明确表明是阻止还是通过,此时数据包就不在向下匹配新规则了。
<3>如果所有规则中没有明确表明是阻止还是通过这个数据包,也就是没有匹配上规则,向下进行匹配,直到匹配默认规则得到明确的阻止还是通过。
<4>防火墙的默认规则是对应链的所有的规则执行完以后才会执行的(最后执行的规则)。
注意:匹配上拒绝规则也是匹配!

6.iptables内部表和链的核心原理
iptables表和链工作的流程图:
  

  

  

类似可以用北京地铁1,2号线来描述:
1号线:主要是NAT功能
企业案例:
  1)局域网上网共享(路由和网关),使用NAT的POSTROUTING链。
  2)外部IP和端口映射为内部IP和端口(DMZ功能),使用NAT的PREROUTING链。

2号线:主要是FILTER功能,即防火墙功能FILTER INPUT FORWARD
企业案例:
  主要应用就是主机服务器防火墙,使用FILTER的INPUT链

7.iptables命令及参数实践
[root@oldboyedu-01 ~]# iptables -V
iptables v1.4.7

启动iptables功能:
[root@oldboyedu-01 ~]# /etc/init.d/iptables restart
iptables: Setting chains to policy ACCEPT: nat filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules: [ OK ]

查看默认表filter的规则:
[root@oldboyedu-01 ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target prot opt source destination
REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

查看iptables默认加载的内核模块:
[root@oldboyedu-01 ~]# lsmod |egrep "nat|filter"
iptable_filter 2793 1
ip_tables 17895 1 iptable_filter
加载如下模块到内核:可以加到/etc/rc.local文件中自启动!
[root@oldboyedu-01 ~]# modprobe ip_tables
[root@oldboyedu-01 ~]# modprobe iptable_filter
[root@oldboyedu-01 ~]# modprobe iptable_nat
[root@oldboyedu-01 ~]# modprobe ip_conntrack
[root@oldboyedu-01 ~]# modprobe ip_conntrack_ftp
[root@oldboyedu-01 ~]# modprobe ip_nat_ftp
[root@oldboyedu-01 ~]# modprobe ipt_state
[root@oldboyedu-01 ~]# lsmod |egrep "nat|filter"
nf_nat_ftp 3443 0
nf_conntrack_ftp 12049 1 nf_nat_ftp
iptable_nat 5923 0
nf_nat 22676 2 nf_nat_ftp,iptable_nat
nf_conntrack_ipv4 9186 5 iptable_nat,nf_nat
nf_conntrack 79537 6 nf_nat_ftp,nf_conntrack_ftp,iptable_nat,nf_nat,nf_conntrack_ipv4,xt_state
iptable_filter 2793 1
ip_tables 17895 2 iptable_nat,iptable_filter

(1)清除默认规则
清空所有规则,但不会处理默认规则:
[root@oldboyedu-01 ~]# iptables -F
[root@oldboyedu-01 ~]# iptables -L -n
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

删除自定义链:
[root@oldboyedu-01 ~]# iptables -X

链的计数器清零:
[root@oldboyedu-01 ~]# iptables -Z

(2)禁止规则
如何防止自己被关在门外?
法一:去机房重启系统或者登陆服务器删除刚才的禁止规则。
法二:让机房人员重启服芳器或者让机房人员拿用户密码登录进去。
法三:通过服务器的远程管理卡管理(推荐)。
法四:先写一个定时任务,每5分钟就停止防火墙。
法五:测试环境测试好,写成脚本,批量执行

例:配置禁止ssh端口:
语法:
Usage:
iptables -t [table] -[AD] chain rule-specification [options]
具体命令:
iptables -A INPUT -p tcp --dport 52113 -j DROP
iptables -t filter -A INPUT -p tcp --dport 52113 -j DROP
注:
1.iptables默认用的就是filter表,因此,以上两条命令等价。
2. 其中的INPUT DROP要大写。
3. --jump -j target
target for rule (may load target extension)
基本的处理行为:ACCEPT(接受)、DROP(丢弃)、REJECT(拒绝)
比较:DROP好于REJECT
[root@oldboyedu-01 ~]# ss -lntup|grep ssh
tcp LISTEN 0 128 :::22 :::* users:(("sshd",1145,4))
tcp LISTEN 0 128 *:22 *:* users:(("sshd",1145,3))
[root@oldboyedu-01 ~]# iptables -t filter -A INPUT -p tcp --dport 22 -j DROP //

自杀行为,恢复刚才的ssh连接:
法一:-F参数清空所有规则,不会处理默认规则。
[root@oldboyedu-01 ~]# iptables -F
[root@oldboyedu-01 ~]# iptables -t filter -A INPUT -p tcp --dport 80 -j DROP
[root@oldboyedu-01 ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80

Chain FORWARD (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination
法二:-D参数删除指定规则。
[root@oldboyedu-01 ~]# iptables -t filter -D INPUT -p tcp --dport 80 -j DROP

[root@oldboyedu-01 ~]# iptables -t filter -A INPUT -p tcp --dport 80 -j DROP
显示序号:
[root@oldboyedu-01 ~]# iptables -L -n --line-number
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80

Chain FORWARD (policy ACCEPT)
num target prot opt source destination

Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
法三:删除显示的序号1号规则。
[root@oldboyedu-01 ~]# iptables -t filter -D INPUT 1
法四:还原成默认规则。
[root@oldboyedu-01 ~]# /etc/init.d/iptables restart

 

[root@oldboyedu-01 ~]# iptables -t filter -A INPUT -p tcp --dport 80 -j DROP
[root@oldboyedu-01 ~]# iptables -t filter -A INPUT -p tcp --dport 81 -j DROP
[root@oldboyedu-01 ~]# iptables -t filter -I INPUT -p tcp --dport 82 -j DROP
[root@oldboyedu-01 ~]# iptables -L -n --line-number
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:82
2 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
3 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:81
说明:
-A是添加规则到指定链的结尾,最后一条。
-I是添加规则到指定链的开头,第一条。

例:指定位置插入规则:
[root@oldboyedu-01 ~]# iptables -t filter -I INPUT 2 -p tcp --dport 8080 -j DROP
[root@oldboyedu-01 ~]# iptables -L -n --line-number
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:82
2 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080
3 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
4 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:81

例:匹配单一端口。
[root@oldboyedu-01 ~]# iptables -t filter -A INPUT -p tcp --sport 53
[root@oldboyedu-01 ~]# iptables -t filter -A INPUT -p udp --dport 53
例:匹配指定端口之外的端口。
[root@oldboyedu-01 ~]# iptables -t filter -A INPUT -p tcp --sport ! 53
Using intrapositioned negation (`--option ! this`) is deprecated in favor of extrapositioned (`! --option this`).
[root@oldboyedu-01 ~]# iptables -t filter -A INPUT -p udp --dport ! 53
Using intrapositioned negation (`--option ! this`) is deprecated in favor of extrapositioned (`! --option this`).

例:匹配端口范围。
法一:
[root@oldboyedu-01 ~]# iptables -t filter -I INPUT -p tcp --dport 18:80 -j DROP
法二:
[root@oldboyedu-01 ~]# iptables -t filter -I INPUT -p tcp -m multiport --dport 18,19,22 -j DROP

例:封192.168.0.103不让其进入。
[root@oldboyedu-01 ~]# iptables -t filter -A INPUT -i eth0 -s 192.168.0.103/24 -j DROP
[root@oldboyedu-01 ~]# iptables -t filter -A INPUT -i eth0 ! -s 192.168.0.103 -j DROP //非的用法
本机无法被ping通。
解决:iptables -F或者iptables -t filter -D INPUT 1解封即可。

例:所有网段都不能ping通。匹配ICMP类型。匹配指定的网络接口:-i进入;-o出去。
[root@oldboyedu-01 ~]# iptables -t filter -A INPUT -p icmp --icmp-type 8 -i eth0 -s 10.0.0.0/24 -j DROP
全封:-p icmp --icmp-type any
[root@oldboyedu-01 ~]# iptables -t filter -A INPUT -p icmp --icmp-type any -i eth0 -s 10.0.0.0/24 -j DROP
例:除了0网段,别的网段都可以ping通。
[root@oldboyedu-01 ~]# iptables -t filter -A INPUT -p icmp --icmp-type 8 -i eth0 ! -s 10.0.0.0/24 -j DROP
说明:禁ping后别的服务是正常的。

例:匹配指定协议外的所有协议。
[root@oldboyedu-01 ~]# iptables -t filter -A INPUT -p ! tcp
Using intrapositioned negation (`--option ! this`) is deprecated in favor of extrapositioned (`! --option this`).
[root@oldboyedu-01 ~]# iptables -L -n --line-number
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 !tcp -- 0.0.0.0/0 0.0.0.0/0

例:匹配网络状态。
-m state --state
NEW:已经或将启动新的连接
ESTABLISHED:已建立的连接
RELATED:正在启动新连接
INVALID:非法或无法识别的

FTP服务是特殊的,需要配状态连接。
允许关联的状态包通过(web服务不要使用FTP服务)
#others RELATED ftp协议
#允许关联的状态包
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
#___________________________ ipconfig conf end _______________________________
比喻:看电影出去WC或者接个电话,回来也得允许进去。

例:-m limit
--limit n/{second/minute/hour}:指定时间内的请求速率"n"为速率,后面为时间分别为:秒、分、时
--limit-burst [n]:在同一时间内允许通过的请求"n"为数字,不指定默认为5
fg:本机地址:172.16.14.1,允许172.16.0.0/16网络ping本机,但限制每分钟请求不能超过20,每次并发不能超过6个

[root@oldboyedu-01 ~]# iptables -A INPUT -s 192.168.0.103 -d 10.0.0.200 -p icmp --icmp-type 8 -m limit --limit 20/min --limit-burst 6 -j ACCEPT
iptables -A OUTPUT -s 172.16.14.1 -d 172.16.0.0/16 -p icmp --icmp-type 0 -j ACCEPT

 

8.生产案例1:企业服务器防火墙功能部署
手动执行iptables命令配置企业生产环境下的防火墙
生产环境配置主机防火墙的两种模式:
a.逛公园:默认随便进出,对非法分子进行拒绝。 企业应用:企业配置上网网关路由。
b.看电影:默认没票进不去,花钱买票才能看电影。企业应用:服务器主机防火墙。

配置一个企业防火墙:
[root@oldboyedu-01 ~]# iptables -L -n --line-numbers
<1>清除防火墙规则
[root@oldboyedu-01 ~]# iptables -F
[root@oldboyedu-01 ~]# iptables -X
[root@oldboyedu-01 ~]# iptables -Z
<2>配置允许的ssh登录端口和自己的网段进入(管理员的管理入口)
[root@oldboyedu-01 ~]# iptables -A INPUT -p tcp --dport 22 -s 10.0.0.0/24 -j ACCEPT
<3>设置允许本机回环lo接口通信规则(服务器本地的lo网卡)
[root@oldboyedu-01 ~]# iptables -A INPUT -i lo -j ACCEPT
[root@oldboyedu-01 ~]# iptables -A OUTPUT -o lo -j ACCEPT

<4>设置默认的防火墙禁止和允许规则,重要!
[root@oldboyedu-01 ~]# iptables -P INPUT DROP
[root@oldboyedu-01 ~]# iptables -P FORWARD DROP
[root@oldboyedu-01 ~]# iptables -P OUTPUT ACCEPT

<5>开启信任的IP网段,允许合法的进入
[root@oldboyedu-01 ~]# iptables -A INPUT -s 124.43.62.96/24 -p all -j ACCEPT
还可以很多......


<6>[root@oldboyedu-01 ~]# iptables -A INPUT -p tcp --dport 80 -j ACCEPT
以80端口为例,进来的源就可以将你的服务器搞挂了,放置个木马一提权,挂了。所以还得配合系统的权限。

<7>允许外人ping通,若不想被ping通则删除该规则。
[root@oldboyedu-01 ~]# iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT

<8>其他关联的ftp协议,允许关联的状态包通过
[root@oldboyedu-01 ~]# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
[root@oldboyedu-01 ~]# iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
再需要啥就加入!

这是自己扫自己的结果,若是外人(黑客)则只能扫到80端口。
[root@oldboyedu-01 ~]# nmap 10.0.0.200 -p 1-65535
Starting Nmap 5.51 ( http://nmap.org ) at 2018-01-15 09:58 CST
Stats: 0:00:12 elapsed; 0 hosts completed (0 up), 0 undergoing Host Discovery
Parallel DNS resolution of 1 host. Timing: About 0.00% done
Nmap scan report for 10.0.0.200
Host is up (0.000011s latency).
Not shown: 65529 closed ports
PORT STATE SERVICE
21/tcp open ftp
22/tcp open ssh
139/tcp open netbios-ssn
445/tcp open microsoft-ds
3306/tcp open mysql
3307/tcp open opsession-prxy
Nmap done: 1 IP address (1 host up) scanned in 15.06 seconds
要控制默认规则就要重启iptables!

设置后,
[root@oldboyedu-01 ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT
[root@oldboyedu-01 ~]# iptables -P INPUT DROP
[root@oldboyedu-01 ~]# iptables -P FORWARD DROP
[root@oldboyedu-01 ~]# iptables -P OUTPUT ACCEPT
黑客扫不到!
[root@web01 ~]# nmap 10.0.0.200 -p 1-65535
Starting Nmap 5.51 ( http://nmap.org ) at 2018-01-31 22:17 CST
Stats: 0:00:12 elapsed; 0 hosts completed (0 up), 1 undergoing ARP Ping Scan
Parallel DNS resolution of 1 host. Timing: About 0.00% done

上面的配置全部在内存里,重启失效!

iptables -F
iptables -X
iptables -Z
iptables -A INPUT -p tcp --dport 22 -s 10.0.0.0/24 -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
iptables -A INPUT -s 124.43.62.96/24 -p all -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

iptables -F
iptables -X
iptables -Z
iptables -A INPUT -p tcp --dport 22 -s 10.0.0.0/24 -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
iptables -A INPUT -s 124.43.62.96/24 -p all -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

[root@oldboyedu-01 ~]# iptables -L -n --line-numbers
法一:生成默认配置文件,可以写成脚本。
[root@oldboyedu-01 ~]# /etc/init.d/iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
[root@oldboyedu-01 ~]# cat /etc/sysconfig/iptables
# Generated by iptables-save v1.4.7 on Mon Jan 15 10:14:51 2018
*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [1:40]
-A INPUT -s 10.0.0.0/24 -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -s 124.43.62.0/24 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A OUTPUT -o lo -j ACCEPT
-A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
COMMIT
# Completed on Mon Jan 15 10:14:51 2018

法二:
[root@oldboyedu-01 ~]# iptables-save >/etc/sysconfig/iptables

企业面试题:自定义链处理syn攻击
[root@oldboyedu-01 ~]# iptables -N syn-flood
[root@oldboyedu-01 ~]# iptables -A INPUT -i eth0 -syn -j syn-flood
[root@oldboyedu-01 ~]# iptables -A syn-flood -m limit -limit 5000/s -limit-burst 200 -j RETURN
[root@oldboyedu-01 ~]# iptables -A syn-flood -j DROP

10.企业场景如何维护iptables防火墙
法一:采用添加规则命令行或者脚本加入然后一次性保存成文件,然后以后可以改配置文件管理:
[root@oldboyedu-01 ~]# vim /etc/sysconfig/iptables //编辑加入的规则
[root@oldboyedu-01 ~]# /etc/init.d/iptables reload

法二:自动封IP:分析web或者应用日志或者网络连接状态封掉垃圾IP
[root@oldboyedu-01 ~]# netstat -na|grep ESTABLISHED
tcp 0 0 10.0.0.200:3306 10.0.0.200:40888 ESTABLISHED
tcp 0 0 10.0.0.200:22 10.0.0.253:1502 ESTABLISHED
tcp 0 0 10.0.0.200:40888 10.0.0.200:3306 ESTABLISHED
tcp 0 0 10.0.0.200:22 10.0.0.253:1790 ESTABLISHED
[root@oldboyedu-01 ~]# netstat -na|grep ESTABLISHED|awk '{print $5}'
10.0.0.200:40888
10.0.0.253:1502
10.0.0.200:3306
10.0.0.253:1790
[root@oldboyedu-01 ~]# netstat -na|grep ESTABLISHED|awk '{print $5}'|awk -F ":" '{print $1}'
10.0.0.200
10.0.0.253
10.0.0.200
10.0.0.253
[root@oldboyedu-01 ~]# netstat -na|grep ESTABLISHED|awk '{print $5}'|awk -F ":" '{print $1}'|sort
10.0.0.200
10.0.0.200
10.0.0.253
10.0.0.253
[root@oldboyedu-01 ~]# netstat -na|grep ESTABLISHED|awk '{print $5}'|awk -F ":" '{print $1}'|sort|uniq -c
2 10.0.0.200
2 10.0.0.253
[root@oldboyedu-01 ~]# netstat -na|grep ESTABLISHED|awk '{print $5}'|awk -F ":" '{print $1}'|sort|uniq -c|sort
2 10.0.0.200
2 10.0.0.253
[root@oldboyedu-01 ~]# netstat -na|grep ESTABLISHED|awk '{print $5}'|awk -F ":" '{print $1}'|sort|uniq -c|sort -rn
2 10.0.0.253
2 10.0.0.200
[root@oldboyedu-01 ~]# netstat -na|grep ESTABLISHED|awk '{print $5}'|awk -F ":" '{print $1}'|sort|uniq -c|sort -rn|head -10
2 10.0.0.253
2 10.0.0.200
[root@oldboyedu-01 ~]# netstat -na|grep ESTABLISHED|awk '{print $5}'|awk -F ":" '{print $1}'|sort|uniq -c|sort -rn|head -10|grep -v -E '192.168|172. 0'
2 10.0.0.253
2 10.0.0.200
[root@oldboyedu-01 ~]# netstat -na|grep ESTABLISHED|awk '{print $5}'|awk -F ":" '{print $1}'|sort|uniq -c|sort -rn|head -10|grep -v -E '192.168|172. 0'|awk '{if($2!=null && $1>4){print $2}}'
[root@oldboyedu-01 ~]# netstat -na|grep ESTABLISHED|awk '{print $5}'|awk -F ":" '{print $1}'|sort|uniq -c|sort -rn|head -10|grep -v -E '192.168|172. 0'|awk '{if($2!=null && $1>4){print $2}}' >/opt/dropip.txt

[root@oldboyedu-01 ~]# vim /service/script/feng_ip.sh
[root@oldboyedu-01 ~]# cat /service/script/feng_ip.sh
#!/bin/sh

netstat -na|grep ESTABLISHED|awk '{print $5}'|awk -F ":" '{print $1}'|sort|uniq -c|sort -rn|head -10|grep -v -E '192.168|172. 0'|awk '{if($2!=null && $1>4){print $2}}' >/opt/dropip.txt
for i in $(cat /opt/dropip.txt)
do
/sbin/iptables -I INPUT -s $i -j DROP
echo "$i kill at `date`" >> /var/log/ddos
done
说明:然后放在定时任务里;也可以定时封。

生产场景iptables脚本讲解
技巧:具备外网IP的服务器上不对外的服务最好要做源地址限制。
对外提供的服务,不能做源地址限制,例如:80端口。
企业硬件防火墙和iptables防火墙可以同时用。例如:企业防火墙一般放在网关位置,相当于大厦保安;但是楼里的每个屋子还是需要有人或者锁门的。
IDC机房部署了硬件防火墙我们的服务器要开防火墙。

12.部署企业及IDC机房上网网关。使用POSTROUTING。

  

  

 

B的网关是10.0.0.254(路由器A)
C的网关就是B的eth1的网关

服务器网关需要具备如下条件:
(1)物理条件是具备双网卡,建议:
eth0外网地址10.0.0.17,gw 10.0.0.254;eth1内网地址192.168.1.17,内网不配网关,否则会冲突!

//class-02 setup
  
[root@ipt ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:24:38:76
inet addr:10.0.0.17 Bcast:10.0.0.255 Mask:255.255.255.0
eth1 Link encap:Ethernet HWaddr 00:0C:29:24:38:80
inet addr:192.168.1.17 Bcast:192.168.1.255 Mask:255.255.255.0
[root@ipt ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 eth1
0.0.0.0 10.0.0.254 0.0.0.0 UG 0 0 0 eth0

(2)确保服务器网关B要可以上网(B上网才能代理别的机器上网)。可以通过ping baidu.com或ping 203.81.17.1测试。
[root@ipt ~]# ping baidu.com
PING baidu.com (220.181.57.216) 56(84) bytes of data.
64 bytes from 220.181.57.216: icmp_seq=1 ttl=128 time=42.6 ms
64 bytes from 220.181.57.216: icmp_seq=2 ttl=128 time=31.7 ms

另一台机器:

        
-----------------图i3 --》图i4

  
-----------------图i5

(3)内核文件/etc/sysctl.conf里开启转发功能:
a.在服务器端/linux网关B:
linux网关B 192.168.1.17机器上开启路由转发功能,编辑etc/sysctl.conf 修改net.ipv4.ip_forward = 1,然后执行sysctl -p使修改生效。
[root@ipt ~]# vim /etc/sysctl.conf
[root@ipt ~]# sysctl -p
net.ipv4.ip_forward = 1

(4)iptables的filter表我的FORWARD链允许转发。
[root@ipt ~]# iptables -F
[root@ipt ~]# iptables -P INPUT ACCEPT
[root@ipt ~]# iptables -P OUTPUT ACCEPT
[root@ipt ~]# iptables -P FORWARD ACCEPT
[root@ipt ~]# iptables -L -n
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

(5)不要filter防火墙功能,共享上网,因此最好暂停掉防火墙测试
[root@ipt ~]# /etc/init.d/iptables stop

检查和载入模块:
[root@ipt ~]# lsmod |egrep ^ip
ipv6 336368 264
[root@ipt ~]# modprobe ip_tables
[root@ipt ~]# modprobe iptable_filter
[root@ipt ~]# modprobe iptable_nat
[root@ipt ~]# modprobe ip_conntrack
[root@ipt ~]# modprobe ip_conntrack_ftp
[root@ipt ~]# modprobe ip_nat_ftp
[root@ipt ~]# modprobe ipt_state
[root@ipt ~]#
[root@ipt ~]# lsmod |egrep ^ip
iptable_nat 5923 0 //有了!
iptable_filter 2793 0
ip_tables 17895 2 iptable_nat,iptable_filter
ipv6 336368 264

b.在局域网机器端:
(1)局域网的机器有一块网卡即可,确保局域网的机器C,默认网关设置了网关服务器B的eth1内部网卡IP(192.168.1.18)。把主机C的网关设置为B的内网卡192网段的网卡IP,即192.168.1.17。
(2)检查手段:
关闭防火墙后,分别ping网关服务器B的内外网卡IP,都应该是通的就对了。
但是现在还不能上网。

  
//-----------------图i6 理应是通的!
(3)出公网检查除了ping网站域名外,也要ping下外网IP,排除DNS故障。不通。
(4)ping 10.0.0.254不通。


c.局域网共享上网的两条命令方法:
在服务器端/linux网关B:
法一:适合于有固定外网地址的:
[root@ipt ~]# iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j SNAT --to-source 10.0.0.17
a.-s 192.168.1.0/24 办公室或IDC内网网段;
b.-o eth0 为网关的外网卡接口;
c.-j SNAT --to-source 10.0.0.17是网关外网卡IP地址。

法二:适合变化的外网地址(ADSL)
[root@ipt ~]# iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE

d.局域网机器端测试:
ping baidu.com、ping 10.0.0.254都通了!

13.企业案例3:外网地址及端口映射到内网。使用PREROUTING。
把外网IP地址及端口映射到内网服务器地址及端口(和共享上网环境一样)
在10段主机可以通过访问的B 10.0.0.17:22,即可访问到192.168.1.17:9000提供的web服务。

待定。。。

posted @ 2018-02-01 12:19  bkycrmn  阅读(297)  评论(0)    收藏  举报