先来一句:好记性不如烂笔头!
1、iptables简介
iptables是基于包过滤的防火墙,它主要工作在osi模型的2,,4层,也可以工作在7层(iptables + squid)
2、原理
防火墙是一层一层过滤的。按照配置的规则的顺序从上到下,从前到后。
如果匹配上规则既明确表明是阻止还是通过,此时数据包就不再向下匹配新规则了。
如果所有规则都没有匹配上,就会一直向下匹配,直到匹配上默认规则得到明确的阻止还是通过
注释:规则表的先后顺序:raw→mangle→nat→filter
3、表和链
iptables有4张表filter nat mangle raw ,常用的是filter表和nat表
表(tables) |
链(chains) |
||||
INPUT |
OUTPUT |
FORWARD |
PREROUTING |
POSTROUTING |
|
Filter* |
√ |
√ |
√ |
╳ |
╳ |
Nat* |
╳ |
╳ |
√ |
√ |
√ |
Mangle |
√ |
√ |
√ |
√ |
√ |
注:filter表 三个链,真正的防火墙功能;Nat表 三个链表 input和后边两个 负责数据包改写、端口映射 |
INPUT: 对进入主机的数据包进行过滤。
OUTPUT: 对流出的数据包进行过滤。
FORWARD: 对流经的数据包进行过滤。转发数据包的功能。
PREROUTING: 在数据包到达防火墙时,进行路由判断之前执行的规则。改变数据包的目的地址和端口等,映射端口或者是ip。
POSTROUTING: 在数据包离开防火墙时,进行路由判断之前的规则。改变数据包的源地址和源端口。局域网共享上网。
规则链的先后顺序
入站顺序:PREROUTING→INPUT
出站顺序:OUTPUT→POSTROUTING
转发顺序:PREROUTING→FORWARD→POSTROUTING
4、iptables的安装
Centos7开始已经没有iptables了,默认使用的是Firewalld
4.1、关闭firewalld
systemctl status firewalld.service #检测是否开启了firewall
systemctl stop firewalld.service #关闭firewall
sytsemctl disable firewalld.service #禁止firewall开机自启
4.2、安装iptables
yum install iptables-services -y
systemctl start iptables.service
systemctl enable iptables.service service iptables save #所有新加的规则要先保存后再重启,要不会丢配置
iptables-save
5、常用命令
5.1、清空已有的规则
iptables -F 清除用户自定义的规则
iptables -X 清除用户自定义的链
iptables -Z 清除链的计数器
5.2、查看所有的规则
iptables -nL
5.3、查看版本
iptables -V
5.4 基本操作
CentOS 配置防火墙操作实例(启、停、开、闭端口):
查询防火墙状态 : service iptables status
停止防火墙 : service iptables stop
启动防火墙 : service iptables start
重启防火墙 : service iptables restart
永久关闭防火墙 : chkconfig iptables off
永久关闭后启用 : chkconfig iptables on
6、filter配置示例
6.1、删除指定规则
[root@redis3 ~]# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
num target prot opt source destination
DROP tcp -- 0.0.0.0/ 0.0.0.0/ tcp dpt: [root@redis3 ~]# iptables -D INPUT
注释:最好选ACCEPT和DROP 因为拒绝会给客户信息 -t filter 一般不添加,默认就是filter
6.1、开放一个端口
iptables -A INPUT -p tcp -m tcp --dport -j ACCEPT
6.2、允许某个IP或段链接本机
iptables -A INPUT -s 10.100.0.158/ -p tcp -j ACCEPT
iptables -A INPUT -s 10.100.0.158/32 -p all -j ACCEPT(内网适合)
6.3、允许某个IP连接某个端口
iptables -A INPUT -s 10.88.89.11/ -p tcp -m tcp --dport -j ACCEPT
6.4、插入一个规则
iptables -I INPUT -p tcp --dport -j DROP # 在序号1后插入一条规则
6.5、匹配端口范围
iptables -I INPUT -p tcp -m multiport --dport ,, -j ACCEPT
iptables -I INPUT -p tcp --dport : -j DROP
6.6、取反
除网络是192.168.0./24的网络外 都允许访问。
[root@test ~]# iptables -t filter -I INPUT ! -s 192.168.0.0/ -j ACCEPT
只要主机不是10.0.0.1的都允许访问80端口
[root@test ~]# iptables -A INPUT ! -s 10.0.0.1 -p tcp --dport -j ACCEPT iptables -t filter -A INPUT -i etho ! -s 10.0.0.200 -j DROP
6.7、匹配网络状态(不是必须的)
-m state --state
NEW: 已经或将启动的新连接
ESTBILISHED: 已建立的连接
RELATED: 正在启动的新连接
INVALID: 非法或无法识别的连接
# 允许关联的包(一般的包不需要,ftp比较特殊)
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
6.8、允许ICMP类型协议通过
iptables -A INPUT -p icmp -m icmp --icmp-type any -j ACCEPT
或
ipttables -A INPUT -p icmp -s 10.0.0./24 -m icmp --icmp-type any -j ACCEPT
6.9、封外部IP
iptables -I INPUT -S 10.0.0.200 -j DROP
或
iptables -I INPUT -p tcp -s 10.0.0.200 --dport -j DROP (细化拒绝)
6.10、一个防DDOS工具脚本(来自于老男孩)
#!/bin/bash
# name wangfei
# mail wangfei1000@yeah.net
# version 2.1 function CelIp(){
CleTime=""
NowTime=`date +%H%M`
YDenyIpFile="/tmp/deny_ip_`date +%F -d "-1days"`.log"
if [ "$CleTime" == "$NowTime" ] ;then
if [ -f "$YDenyIpFile" ];then
while read line
do
iptables -D INPUT -s $line -j DROP
done<$YDenyIpFile
mv $YDenyIpFile $YDenyIpFile.bak
sleep
fi
fi
} function DenyIp(){
IpList=($(awk -F "[ :/]+" '{ip[$3]=ip[$3]+1}END{for(k in ip) print k,ip[k]}' access.log_06|awk '$2>30{print $1}'))
for ip in ${IpList[@]}
do
if [ `iptables -nL|grep $ip|wc -l` -lt ];then
echo $ip>>/tmp/deny_ip_$(date +%F).log
iptables -I INPUT -s $ip -j DROP && echo "deny $ip success."
fi
done
} while :
do
CelIp
DenyIp
sleep
done
7、net配置示例
一定要在安装防火墙模块后重启iptables后再配置
modprobe ip_tables
modprobe iptable_nat
modprobe iptable_filter
modprobe ip_conntrack
modprobe ip_conntrack_ftp
modprobe ip_nat_ftp
modprobe ipt_state
1、代理服务器设置
# 需要开启linux内核转发
[root@test ~]# sed -i 's@net.ipv4.ip_forward =.*@net.ipv4.ip_forward =1@g' /etc/sysctl.conf
[root@test ~]# sysctl -p
[root@test ~]# iptables -t nat -A POSTROUTING -s 172.16.1.0/ -o eth1 -j SNAT --to-source 10.0.0.55 #要代理的网段 指定外网IP地址
如果是映射到多个ip上,可这样
iptables -t nat -I POSTROUTING -s 172.16.1.0/ -j SNAT --to-source 10.0.0.99-10.0.0.155 # 适用于外网ip不固定的情况(ADSL)
[root@test ~]# iptables -t nat -A POSTROUTING -s 172.16.1.0/ -j MASQUERADE
多外网IP地址或没有固定的外网IP地址的时候可以自动选择合适的外网网卡使用
注意:
不仅需要nat设置,还需要将iptables的Forward设置为 ACCEPT
2、客户端
内网客户端需要将网关指向代理服务器的内网IP
1、映射端口或IP
注意:
1、 代理端需要打开系统内核转发。
2、 客户端的内网网关需要指向代理服务器的内网IP。
3、 客户端如果是测试环境需要关闭外网网卡。
iptables -t nat -A PREROUTING -d 10.0.0.5 -p tcp --dport -j DNAT --to-destination 172.16.1.52: 实现外部10.0.0. 对映射到172.16.1.5上,如下操作即可。
iptables -t nat -A PREROUTING -d 10.0.0.99 -j DNAT --to-destination 172.16.1.5
iptables -t nat -A POSTROUTING -s 172.16.1.5 -j SNAT --to-source 10.0.0.99
写的很好的博客: https://blog.****.net/chengxuyuanyonghu/article/details/64441374