Linux中的一个重要的命令——iptables,由于对其的掌握不多,因此将学到的东西都整理起来总结一下,不足之处很多望指教~
iptables 是与最新的 3.5 版本 Linux 内核集成的 IP 信息包过滤系统,位于/sbin/iptables,是用来管理防火墙规则的工具。
四表五链规则(规则写在链中,链包含在表中):
规则表(按序排列):raw(确定是否对数据包进行状态跟踪)、mangle(为数据包设置标记)、nat(修改数据包中的源、目标IP或端口)、filter(过滤)
规则链:INPUT(处理入站数据包)、OUTPUT(处理出站数据包)、FORWARD(处理转发数据包)、POSTOUTING(在路由选择后处理数据包)、PREROUTING(在路由选择前处理数据包)
语法:
iptables [-t 表名] 选项 [链名] [条件] [-j 控制类型]
默认情况下,表为filter,链为表内所有的链。
控制类型:ACCEPT、DROP、REJECT、LOG
查看规则列表:
-L:列出所有的规则条目
-n:以数字形式显示地址、端口等信息
-v:以更详细的方式显示规则信息
--line-numbers:查看规则时,显示规则的序号
添加新规则:
-A:在链的末尾追加一条规则
-I:在链的开头(或指定序号)插入一条规则

示例中可以看到,最后一条添加的规则通过-I参数指定插入的位置为2,因而结果显示icmp协议的规则在udp和tcp中间。
清空、删除规则:
-F:清空所有规则
-D:删除链内指定序号(或内容)的一条规则

将上述第三条规则即tcp的删除掉了。
设置默认规则:
-P:为指定的链设置默认规则(只允许ACCEPT或DROP)

示例中将INPUT链的默认规则设置为DROP。
规则的匹配条件:
1、通用匹配:可直接使用,不依赖于其他条件或扩展。
协议匹配:-p 协议名
地址匹配:-s 源地址、-d 目标地址
接口匹配:-i 入站网卡、-o 出站网卡
2、隐含匹配:以特定的协议匹配为前提。
端口匹配:--sport 源端口、--dport 目标端口
TCP标记匹配:--tcp-flags 检查范围 被设置的标记
ICMP类型匹配:--icmp-type ICMP类型

注意点如图指出的,进行TCP标记时如设置ACK/SYN等包时中间使用空格来连接即可。
3、显示匹配:以“-m 扩展模块”的形式明确指出类型
多端口匹配:-m multiport –sports(dports), 源(目标)端口列表
IP范围匹配:-m iprange –src-range, IP范围
MAC地址匹配:-m mac –mac-source, MAC地址
状态匹配:-m state –state, 连接状态
iptables实现NAT功能:
SNAT:
源地址转换,即实现内网主机穿过防火墙访问互联网。
步骤如下:
1、查看路由信息:route –n

2、开启路由转发:echo 1 > /proc/sys/net/ipv4/ip_forward或sysctl -w net.ipv4.ip_forward=1
查看设置后的配置信息:sysctl –p
3、编写SNAT转换规则:
iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -o eth0 -j SNAT --to-source 10.10.10.160
POSTROUTING:路由选择后处理
-s:内网源地址
-o:外网接口
--to-source:外网接口地址,转换后的新的源地址
示例,先为Kali添加一张网卡:

在Kali输入ifconfig可看到多了一张eth1、IP为192.168.10.128的网卡:

以win7为例,对其进行配置:

查看配置结果:

没问题,接着到Kali中开启路由转发功能。
然后先在win7中尝试访问百度,返回404:

接着到Kali中输入SNAT的iptables规则并进行查看:

没问题,接着到win7上访问百度,发现OK了,而且搜索的页面都可以打开,即SNAT配置成功:

Masquerade地址伪装:
适用于外网IP地址非固定的情况,如ADSL拨号上网。
iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -o eth0 -j MASQUERADE
示例中,先将上述SNAT的规则删掉:

然后添加MASQUERADE的规则并查看:

最后到win7上访问新浪首页,没有问题:

DNAT:
修改数据包的目标地址或端口号,实现内网服务器通过防火墙发布到互联网。
1、开启路由转发功能
2、编写DNAT转发规则:
iptables -t nat -A PREROUTING -i eth0 -d 172.26.102.73 -p tcp --dport 80 -j DNAT --to-destination 192.168.10.144
在DNAT规则中以“IP:Port”形式指定目标地址与端口:
iptables -t nat -A PREROUTING -i eth0 -d 172.26.102.73 -p tcp --dport 80 -j DNAT --to-destination 192.168.10.144:22
PREROUTING:路由选择前处理
-i:指定外网接口
-d:公网目标地址,来自互联网的访问目标
--to-destination:内网IP地址,内网要发布的服务器
示例中,先将之前的规则删掉,然后输入相应的规则:

其中192.168.10.144为Windows 2k3 server的IP地址,其为内网DVWA的Web服务器,接着到物理机中访问该URL,访问成功:

iptables系统服务:
service iptables start|stop|restart
脚本位置:/etc/init.d/iptables
规则文件位置:/etc/sysconfig/iptables
使用脚本配置防火墙规则:
ipfw.sh:
#!/bin/bash
#定义基本变量
WAN_IF="eth0" #外网接口
WAN_IP="10.10.10.160" #外网IP
LAN_IF="eth1" #局域网接口
LAN_IP="192.168.10.128" #局域网IP
LAN_NET="192.168.10.0/24"
LAN_WWW_IP="192.168.10.144" #局域网内部的Web服务器IP
IPT="/sbin/iptables" #iptables命令
MOD="/sbin/modprobe" #modprobe命令
CTL="/sbin/sysctl" #sysctl命令
#加载必要的内核模块
$MOD ip_tables
$MOD ip_conntrack
$MOD ipt_REJECT
$MOD ipt_LOG
$MOD ipt_iprange
$MOD xt_tcpudp
$MOD xt_state
$MOD xt_multiport
$MOD xt_max
$MOD ip_nat_ftp
$MOD ip_conntrack_ftp
#调整/proc参数
$CTL -w net.ipv4.ip_forward=1
$CTL -w net.ipv4.ip_default_ttl=128
$CTL -w net.ipv4.icmp_echo_ignore_all=1
$CTL -w net.ipv4.icmp_echo_ignore_broadcasts=1
$CTL -w net.ipv4.tcp_syncookies=1
$CTL -w net.ipv4.tcp_syn_retries=3
$CTL -w net.ipv4.tcp_synack_retries=3
$CTL -w net.ipv4.tcp_fin_timeout=60
$CTL -w net.ipv4.tcp_max_syn_backlog=3200
#具体的防火墙规则
$IPT -t filter -X
$IPT -t nat -X
$IPT -t filter -F
$IPT -t nat -F
$IPT -P INPUT DROP
$IPT -P FORWARD DROP
$IPT -P OUTPUT ACCEPT
$IPT -t nat -A POSTROUTING -s $LAN_NET -o $WAN_IF -j SNAT --to-source $WAN_IP
$IPT -t nat -A PREROUTING -i $WAN_IF -d $WAN_IP -p tcp --dport 80 -j DNAT --to-destination $LAN_WWW_IP
#在脚本中调用黑白名单
BLST="/opt/ipfw.b"
for i in $(grep -v "^#" $BLST)
do
$IPT -I INPUT -s $i -j DROP
$IPT -I OUTPUT -d $i -j DROP
$IPT -I FORWARD -s $i -j DROP
$IPT -I FORWARD -d $i -j DROP
done
WLST="/opt/ipfw.w"
for i in $(grep -v "^#" $WLST)
do
$IPT -I INPUT -s $i -j ACCEPT
$IPT -I OUTPUT -d $i -j ACCEPT
$IPT -I FORWARD -s $i -j ACCEPT
$IPT -I FORWARD -d $i -j ACCEPT
done
注意点是变量赋值即=号左右不能留空格。
将该脚本保存到/opt目录中,并通过命令chmod +x ipfw.sh赋予相应的权限。
然后是编写黑白名单文件:

最后通过vim /etc/rc.local命令,在其中添加/opt/ipfw.sh即可:

接着重启Kali,在重启的过程中可以看到脚本被运行了:

重启完成后查看iptables规则,发现规则是一条条慢慢地列举出来的:

先看看Kali的配置吧:

接着到win7中访问新浪看看能不能通(win7使用的是内网网卡VMnet1仅主机模式,IP为192.168.10.7,网关设置为Kali的局域网网卡IP:192.168.10.128):

可以看到,能够正常访问,没问题。
上面的脚本只是举例,更多的可以自己根据情况去添加黑白名单或者进行其它规则的设置即可。
允许ping对方,但不允许对方ping本机:
iptables -A INPUT -p icmp --icmp-type 8 -j DROP
iptables -A INPUT -p icmp --icmp-type 0 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type 3 -j ACCEPT
iptables -A INPUT -p icmp -j DROP
(ICMP协议,类型0为回显、3为不可达、8为请求)


示例中,Kai(IP为10.10.10.160)ping得了IP为10.10.10.123的主机,但该主机却不能ping通Kali。
利用iptables防CC攻击:
connlimit模块:
用于限制每一个客户端IP的并发连接数。
参数:-connlimit-above n 限制并发个数
例:iptables -I INPUT -p tcp --syn --dport 80 -m connlimit --connlimit-above 100 -j REJECT
-m参数即module,加载模块。

例子中是将限制并发的个数设定为100。
Limit模块:
用于限速、控制流量。
例:iptables -A INPUT -p icmp -m limit --limit 3/m --limit-burst 10 -j ACCEPT
将限制个数设置为10,当超过10时就进行前面限制的规则即每分钟通过1个。
iptables -A INPUT -p icmp -j DROP3
没有满足前一条规则的就DROP掉。
--limit-burst默认值为5
示例中先进行iptables的规则设置:

然后到BT5中ping Kali,发现ping到第10个包时会停下来:

等了大概一分钟后,才出现第二个ping包,也就是说该规则进行了限速、控制流量:

利用iptables防DDoS攻击:
iptables -A INPUT -p tcp --syn -m limit --limit 12/s --limit-burst 24 -j ACCEPT
iptables -A FORWARD -p tcp --syn -m limit --limit 1/s -j ACCEPT
其实也是运用了limit模块,这里就不多演示了。
防DDoS的脚本ipddos.sh:
#防止SYN攻击 轻量级预防
iptables -N syn-flood
iptables -A INPUT -p tcp --syn -j syn-flood
iptables -I syn-flood -p tcp -m limit --limit 3/s --limit-burst 6 -j RETURN
iptables -A syn-flood -j REJECT
#防止DOS太多连接进来,可以允许外网网卡每个IP最多15个初始连接,超过的丢弃
iptables -A INPUT -i eth0 -p tcp --syn -m connlimit --connlimit-above 15 -j DROP
iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
原理也差不多,这里就不多演示了。
|