iptables

Iptables名词和术语

四表:filter(INPUT,FORWARD,OUTPUT),NAT(OUTPUT,PREROUTING,POSTROUTING)MANGLE RAW
五链:(chains):INPUT,OUTPUT,FORWARD,PREROUTING,POSTROUTING 容器:   包含或被包含的关系 Policy(规则)
-->Chain(链)-->Table(表)--netfilter/iptables
 
  
采用的是数据包过滤的机制,会对请求的数据包的包头数据进行分析,按照规则从上到下匹配
iptables主要工作在OSI七层的二、三、四层,
iptables也可以工作在七层上(squid +iptables)                                                                                                                                            
 Filter表 默认表,主要和主机自身相关,真正负责防火墙功能的(过滤流入主机的数据包)
 INPUT 负责过滤所有进入主机的数据包(最主要)
 OUTPUT 处理所有源地址都是本机地址的数据包(过滤流出主机的数据包)
 FORWARD 负责流经主机的数据包 net.ipv4.ip_forward
 Nat表 主要负责网络地址之间的转换,即来源和目的ip和port的转换。可以做企业网关,DMZ,端口映射等
 OUTPUT 和从主机发出去的数据包有关,改变数据包的目的地址
 PREROUTING 在数据包到达防火墙时进行路由判断之前的的规则,作用是改变数据包的目的地址,目的端口等,用于企业路由(zebra)或网关(iptables),端口映射等
 POSTROUTING 离开防火墙时进行路由判断之后执行的规则,用来改变数据包的源地址和源端口,出网时,源地址改成了公网地址,即共享上网
  Mangle表 主要负责修改数据包中特殊的路由标记,如TTL,TOS,MARK等
  INPUT/ OUTPUT/ FORWARD/ PREROUTING POSTROUTING




 

 iptables --help

[root@node85 ~]# iptables --help
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  计数器清0
  --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:(centos5 非加在后面)
[!] --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)
  --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')
  --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.

 

 

iptables默认加载的是内核的模块
[root@node85 ~]# lsmod |egrep "nat|filter|ipt"
ipt_REJECT              2351  2 
iptable_filter          2793  1 
ip_tables              17831  1 iptable_filter

[root@node85 ~]# lsmod | grep ip
ipv6                  335589  266 
ipt_REJECT              2351  2 
nf_conntrack_ipv4       9154  2 
nf_defrag_ipv4          1483  1 nf_conntrack_ipv4
nf_conntrack           79206  2 nf_conntrack_ipv4,xt_state
iptable_filter          2793  1 
ip_tables              17831  1 iptable_filter

可加载模块
modprobe ip_tables modprobe iptable_filter modprobe iptable_nat modprobe ip_conntrack modprobe ip_conntrack_ftp modprobe ip_nat_ftp modprobe ipt_state

 

 

实践案例:禁止ssh 22端口

语法: iptables -t [table] -[AD]  chain rule-specification [options]
#iptables -t filter -A INPUT -p tcp --dport 22 -
j DROP 禁止ssh 22端口远程登录 #iptables -A INPUT -p tcp --dport 22 -j DROP (同上等价,默认filter表)
#iptables -L -n --line-number -t filter  显示规则序号

  -t       指定表
  -A       追加
  -p     指定协议
  --dport  指定目的端口
  -j     采取的方式
     ACCEPT     允许数据包通过
     DROP      直接丢弃数据包,不给任何回应信息.iptables -t filter -A INPUT -p tcp --dport 80 -j DROP 会造成找不到网页,不会造成404
     REJECT     拒绝数据包通过,必要时会给数据发送端一个响应的信息。
     SNAT       源地址转换。在进入路由层面的route之前,改写源地址,目标地址不变,并在本机建立NAT表项,当数据返回时,根据NAT表将目的地址数据改写为数据发送出去时候的源地址,并发送给主机。解决NAT上网问题
     MASQUERADE   是SNAT的一种特殊形式,适用于像adsl这种临时会变的ip上
     DNAT      目标地址转换。和SNAT相反,IP包经过route之后、出本地的网络栈之前,重新修改目标地址,源地址不变,在本机建立NAT表项,当数据返回时,根据NAT表将源地址修改为数据发送过来时的目标地址,并发给远程主机。可以隐藏后端服务器的真实地址。
     REDIRECT    是DNAT的一种特殊形式,将网络包转发到本地host上(不管IP头部指定的目标地址是啥),方便在本机做端口转发。
     LOG       在/var/log/messages文件中记录日志信息,然后将数据包传递给下一条规则

解决办法(误设禁止ssh规则)
    ①进到虚拟机 iptables -F 清除所有的规则
    ②自己有远程管理卡清理
    ③机房人员帮忙清理
    ④定时任务 ,定时清理配置防火墙的时候每5分钟执行一次
    ⑤登录tty清除
    ⑥iptables -t filter -D INPUT -p tcp --dport 22 -j DROP
    ⑦/etc/init.d/iptabales stop

删除规则方法
  1、service iptables restart
  2、iptables -F
  3、iptables -D INPUT -p tcp --dport 80 -j drop
  4、iptables -D INPUT rulenumber (--line-number)

 

实践案例:封IP

# iptables -I INPUT -i eth0 -s 192.168.0.11 -p tcp --dport 80 -j DROP 
  -i  指定接口
  -s  指定源地址 (也可以指定网段 -s 192.168.0.0/24)
  -p tcp  指定协议
    --dport  端口
# iptables -L -n --line-number -
t filter Chain INPUT (policy ACCEPT) num target prot opt source destination 1 DROP tcp -- 192.168.0.11 0.0.0.0/0 tcp dpt:80

 

实践案例:禁ping

# iptables -t filter  -I INPUT -p icmp --icmp-type 8(或any) -i eth0  -s 192.168.0.103 -j DROP  #不允许192.168.0.103 ping

 

案例学习

匹配协议 
  #iptables -A INPUT -p  tcp   -s 192.168.0.103 -j DROP #指定地址
  #iptables -A INPUT  ! -p  tcp  -s 192.168.0.103 -j DROP #指定地址

匹配主机源IP   #iptables -A INPUT -s 192.168.0.103 
  #iptables -A INPUT ! -s  192.168.0.103

匹配网段   #iptables -A INPUT -s 10.0.0.0/24  
  #iptables -A INPUT -s ! 10.0.0.0/24

匹配3306端口
  #iptables -A INPUT -p tcp --dport 3306

匹配单一端口封源端口DNS封堵   #iptables
-A INPUT -p tcp --sport 53   #iptables -A INPUT -p udp --dport 53 冒号隔开封范围   #iptables -A INPUT -p tcp --sport 22:80
  #iptables -I INPUT -p tcp         --dport 21,22,23,24  ===>错误语法
  #iptables -I INPUT -p tcp -m multiport --dport 21,22,23,24
  #iptables -I INPUT -p tcp --dport 3306:8809


匹配指定的网络接口
  #iptables -A INPUT -i eth0
  #iptables -A FORWARD -o eth0

常用服务的iptables设置
##nagios监控
  iptables -A INPUT -s 192.168.0.0/24 -p tcp --dport 5666 -j ACCEPT
##mysql
  iptables -A INPUT -s 192.168.0.0/24 -p tcp --dport 3306 -j ACCEPT
##snmp
  iptables -A INPUT -s 192.168.0.0/24 -p UDP --dport 161 -j ACCEPT
##rsync
  iptables -A INPUT -s 192.168.0.0/24 -p tcp -m tcp --dport 873 -j ACCEPT
##nfs2049,portmap 111
  iptables -A INPUT -s 192.168.0.0/24 -p udp -m multiport --dport 111,892,2049 -j ACCEPT
  iptables -A INPUT -s 192.168.0.0/24 -p tcp -m multiport --dport 111,892,2049 -j ACCEPT
##icmp
  iptables -A INPUT -s 192.168.0.0/24 -p icmp -m icmp --icmp-type any -j ACCEPT

 

匹配网络状态

匹配网络状态  (FTP服务是特殊的,需要配状态连接,21端口连接,20端口传数据)
-m state --state
    NEW:         已经或将启动新的连接
    ESTABLISHED: 已建立的连接
    RELATED:     正在启动新连接
    INVALID:     非法或无法识别的

允许关联的状态包通过(web服务不要使用)
#允许关联的状态包
iptables -A INPUT  -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
    比喻:看电影出去WC或者接个电话,回来也得允许进去


控制包的速率设置时间段和并发
-m limit  限制指定时间包的允许通过数量及并发数
  --limit n/{second/minute/hour}:指定时间内的请求速率"n"为速率,后面为时间分别为:秒、分、时
  --limit-burst [n]:在同一时间内允许通过的请求"n"为数字,不指定默认为5
本机地址:
172.16.14.1,允许172.16.0.0/16网络ping本机,但限制每分钟请求不能超过20,每次并发不能超过6个 #iptables -A INPUT -s 172.16.0.0/16 -d 172.16.14.1 -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

 

完整定义一个防火墙案例

1、清空之前的部署。
    iptables -F
    iptables -X
    iptables -Z

2、让自己的SSH 的端口通过(源地址)连接CRT的ifconfig的地址即可、 iptables -A INPUT -p tcp --dport 22 -s 192.168.0.0/24 -j ACCEPT ADSL拨号的网操作以下命令 iptables -A INPUT -p tcp --dport 22 -j ACCEPT 3、设置本机lo的通讯规则   iptables -A INPUT -i lo -j ACCEPT   iptables -A OUTPUT -o lo -j ACCEPT 4、设置默认的防火墙禁止和允许   iptables -P INPUT DROP   iptables -P OUTPUT ACCEPT   iptables -P FORWARD DROP 5、开启信任的IP网段(内部的网段) #允许IDC LAN /WAN和办公网IP的访问,及对外合作机构的访问   iptables -A INPUT -s 124.43.62.96/27 -p all -j ACCEP ←办公室固定的IP段   iptables -A INPUT -s 192.168.1.0/24 -p all -j ACCEPT ←IDC机房的内网网段   iptables -A INPUT -s 10.0.0.0/24 -p all -j ACCEPT ←其他机房的内网网段   iptables -A INPUT -s 203.83.24.0/24 -p all -j ACCEPT ←IDC机房的外网网段   iptables -A INPUT -s 201.82.34.0/24 -p all -j ACCEPT ←其他IDC机房的外网网段
6、WEB服务允许业务服务端口对外访问(允许http服务无条件通过) #web server   iptables -A INPUT -p tcp --dport 80 -j ACCEPT

7、允许icmp类型协议通过   iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT #对外允许ping   iptables -A INPUT -p icmp -m icmp --icmp-type any -j ACCEPT #看需求   如果对内开启,对外不开就用下面的形式。   iptables -A INPUT -p icmp -s 10.0.0.0/24 -m icmp --icmp-type any -j ACCEPT #对内用户能ping通   iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT #允许外面的人ping通可以不设定 8、允许关联的状态包通过 (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 允许关联的包出去

保存配置
  #/etc/init.d/iptables save
或 #iptables-save >/etc/sysconfig/iptables


 

防止DoS攻击(http://seanlook.com/2014/02/26/iptables-example/)

SYN洪水是攻击者发送海量的SYN请求到目标服务器上的一种DoS攻击方法,下面的脚本用于预防轻量级的DoS攻击:

iptables -N syn-flood   (如果您的防火墙默认配置有“ :syn-flood - [0:0] ”则不许要该项,因为重复了)
iptables -A INPUT -p tcp --syn -j syn-flood   
iptables -I syn-flood -p tcp -m limit --limit 2/s --limit-burst 5 -j RETURN   
iptables -A syn-flood -j REJECT   
# 防止DOS太多连接进来,可以允许外网网卡每个IP最多15个初始连接,超过的丢弃
# 需要iptables v1.4.19以上版本:iptables -V 
iptables -A INPUT -p tcp --syn -i eth0 --dport 80 -m connlimit --connlimit-above 20 --connlimit-mask 24 -j DROP   

#用Iptables抵御DDOS (参数与上相同) iptables
-A INPUT -p tcp --syn -m limit --limit 5/s --limit-burst 10 -j ACCEPT iptables -A FORWARD -p tcp --syn -m limit --limit 1/s -j ACCEPT iptables -A FORWARD -p icmp -m limit --limit 2/s --limit-burst 10 -j ACCEPT iptables -A INPUT -p icmp --icmp-type 0 -s ! 172.29.73.0/24 -j DROP

 

 

部署企业及IDC机房上网配置网关
局域网共享的两条命令方法:
  方法1:适合于有固定外网地址的:
  iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o eth0 -j SNAT --to-source 10.0.0.7
  (1)-s 192.168.0.0/24 办公室或IDC内网网段。
  (2)-o eth0 为网关的外网卡接口。
  (3)-j SNAT --to-source 10.0.0.19 是网关外网卡IP地址。
  方法2:适合变化外网地址(ADSL):
  iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j MASQUERADE 伪装。
  
实战企业上网的网关服务器
1)网关服务器A,首先是能上网,然后setup添加一块网卡,设置内网10.0.0.7
2)客户服务器B,setup配置IP为10.0.0.8 网关为10.0.0.7,还需要配置DNS3)
3)关服务器上编辑文件/etc/sysctl.conf将设置成:net.ipv4.ip_forward = 1
4)将默认的防火墙转发规则开启:iptables -P FORWARD ACCEPT
5)加载内核模块并查看:lsmod |egrep ^ip
    modprobe  ip_tables   
    modprobe  iptable_filter   
    modprobe  iptable_nat   
    modprobe  ip_conntrack   
    modprobe  ip_conntrack_ftp   
    modprobe  ip_nat_ftp   
    modprobe  ipt_state
    查看:lsmod | grep ^ip
6)在网关服务器上配置: 
iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -o eth0 -j SNAT --to-source 192.168.1.129
【iptables -t nat  -A POSTROUTING  -s 10.0.0.0/24 -o eth0 -j MASQUERADE伪装(ADSL)】
7)/etc/init.d/iptables save
8)访问外网映射到内网的服务器上
iptables -t nat -A PREROUTING -d 内IP -p tcp --dport 80 -j DNAT --to-destination 内IP:端口  



iptables的生产常用场景:
1)实现服务器本身防火墙功能,使用filter表。
  iptables -A INPUT -s 192.168.0.0/24 -p tcp -m tcp --dport 22 -j ACCEPT
2)实现局域网上网网关,使用nat表,(POSTROUTING)网关上也可以同时用filter表做防火墙。
  iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j SNAT --to-source 10.0.0.7
3)实现NAT功能,如:由外部IP映射到内部服务器IP(包括端口),使用nat表,PREROUTING的链。
  iptables -t nat -A PREROUTING -d 10.0.0.7 -p tcp --dport 80 -j DNAT --to-destination 192.168.0.8:9000
4)其他。。。略。
  
企业应用场景:
1) 把访问外网IP及端口的请求映射到内网某个服务器及端口(企业内部)。
2) 硬件防火墙,把访问LVS/nginx外网VIP及80端口的请求映射到IDC 负载均衡服务器内部IP及端口上(IDC机房的操作)
  
映射多个外网ip上网:
iptables -t nat -A POSTROUTING -s 192.168.0.0/255.255.255.0 -o eth0 -j SNAT 124.42.60.11 -124.42.60.16

 

iptables 的内核优化
dmesg里面显示 ip_conntrack: table full, dropping packet.的错误提示.如何解决。 
C64:
net.nf_conntrack_max = 25000000
net.netfilter.nf_conntrack_max = 25000000
net.netfilter.nf_conntrack_tcp_timeout_established = 180
net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60
net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120
  
优化常常出现以下的错误:
http://oldboy.blog.51cto.com/2561410/1336488
  
15.8版本上
error: "net.ipv4.ip_conntrack_max"is an unknown key 
error: "net.ipv4.netfilter.ip_conntrack_max"is an unknown key 
error: "net.ipv4.netfilter.ip_conntrack_tcp_timeout_established"is an unknown key 
error: "net.ipv4.netfilter.ip_conntrack_tcp_timeout_time_wait"is an unknown key 
error: "net.ipv4.netfilter.ip_conntrack_tcp_timeout_close_wait"is an unknown key 
error: "net.ipv4.netfilter.ip_conntrack_tcp_timeout_fin_wait"is an unknown key
这个错误可能是你的防火墙没有开启或者自动处理可载入的模块ip_conntrack没有自动载入,解决办法有二,一是开启防火墙,二是自动处理开载入的模块ip_conntrack
解决办法:
modprobe ip_conntrack 
echo "modprobe ip_conntrack">> /etc/rc.local

26.4版本上 error: "net.nf_conntrack_max"isan unknown key error: "net.netfilter.nf_conntrack_max"isan unknown key error: "net.netfilter.nf_conntrack_tcp_timeout_established"isan unknown key error: "net.netfilter.nf_conntrack_tcp_timeout_time_wait"isan unknown key error: "net.netfilter.nf_conntrack_tcp_timeout_close_wait"isan unknown key error: "net.netfilter.nf_conntrack_tcp_timeout_fin_wait"isan unknown key 这个错误可能是你的防火墙没有开启或者自动处理可载入的模块ip_conntrack没有自动载入,解决办法有二,一是开启防火墙,二是自动处理开载入的模块ip_conntrack 解决办法: modprobe nf_conntrack echo "modprobe nf_conntrack">> /etc/rc.local

6.4版本上 error: "net.bridge.bridge-nf-call-ip6tables"isan unknown key error: "net.bridge.bridge-nf-call-iptables"isan unknown key error: "net.bridge.bridge-nf-call-arptables"isan unknown key 这个错误是由于自动处理可载入的模块bridge没有自动载入,解决办法是自动处理开载入的模块ip_conntrack 解决办法: modprobe bridge echo "modprobe bridge">> /etc/rc.local

转载于:https://www.cnblogs.com/gtms/p/6731903.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值