iptables 规则整理

一、iptables命令帮助信息

1.1 实际测试iptables规则
1.1.1启动和查看iptables状态
  /etc/init.d/iptables start
  iptables -L -n或iptables -L -n -v -x
实例演示1:

[root@xiaorui ~]# iptables -V
iptables v1.4.7
[root@xiaorui ~]# 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)
  --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 -F  //清除所有规则,不会处理默认的规则。
  iptables -X  //删除用户自定义的链。
  iptables -Z  //链的记数器清零。
实例演示2:

[root@xiaorui ~]# iptables -F
[root@xiaorui ~]# iptables --flush
提示:以上两条命令等价
[root@xiaorui ~]# iptables -X
[root@xiaorui ~]# iptables --delete-chain
提示:以上两条命令等价
[root@xiaorui ~]# iptables -Z
[root@xiaorui ~]# iptables --zero
提示:以上两条命令等价
[root@xiaorui ~]# 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

1.1.3禁止规则
       #禁止ssh端口

(1)找出当前机器的SSH端口

[root@xiaorui ~]# netstat -lntup|grep ssh
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      2073/sshd           
tcp        0      0 :::22                       :::*                        LISTEN      2073/sshd 

(2)禁止当前的SSH端口,这里是22

语法:
Usage: iptables -t [table] -[AD] chain rule-specification [options]
具体命令:
[root@xiaorui ~]# iptables -A INPUT -p tcp --dport 22 -j DROP
[root@xiaorui ~]# iptables -t filter -A INPUT -p tcp --dport 22 -j DROP
注:
1、iptables默认用的就是filter表,因此,以上两条命令等价
2、其中的INPUT DROP要大写
3、--jump  -j target
   target for rule(may load target extension)
   基本的处理行为:ACCEPT(接受)、DROP(丢弃)、REJECT(拒绝)
   比较:DROP好于REJECT
4、命令行执行的规则,仅仅在内存里临时生效

(3)恢复刚才断掉的SSH连接
      1)去机房重启系统或者登录删除刚才的禁止规则
      2)让机房人员重启服务器或者让机房人员拿用户名密码登录进去
      3)通过服务器的远程管理卡管理
      4)先写一个定时任务,每5分钟就停止防火墙
      5)测试环境测试好,写成脚本,批量执行

(4)使用-I和-A的顺序,防火墙的过滤根据规则顺序的。
  -A是添加规则到指定链的结尾,最后一条。
  -I是添加规则到指定链的开头,第一条。也可以指定插入位置。

  插入到第二行:
  [root@xiaorui ~]# iptables -I INPUT 2 -p tcp --dport 8080 -j DROP

(5)总结下删除规则的方法:
  1)iptables -D INPUT -p tcp --dport 8080 -j DROP
  2)iptables -F 删除所有规则
  3)iptables -D INPUT 规则序号
  4)/etc/init.d/iptables restart (用iptables命令行配置的命令都是临时生效)

二、知识点整理
1、禁止10.0.0.0网段连入:
  iptables -t filter -A INPUT -i eth0 -s 10.0.0.0/24 -j DROP

2、源地址不是10.0.0.101单个ip的禁止连接
  iptables -t filter -I INPUT -i eth0 ! -s 10.0.0.101 -j DROP

3、源地址不是10.0.0.0/24的网段禁止连接
  iptables -t filter -I INPUT -i eth0 ! -s 10.0.0.0/24 -j DROP

4、源地址不是10.0.0.0/24禁ping
  iptables -t filter  -I INPUT -p icmp --icmp-type 8 -i eth0 ! -s 10.0.0.0/24 -j DROP

5、封掉3306端口
  iptables -A INPUT -p tcp  --dport 3306 -j DROP

6、匹配规则
  匹配指定协议外的所有协议
  iptables -A INPUT -p ! tcp

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

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

  匹配单一端口
  iptables -A INPUT -p tcp --sport 53
  iptables -A INPUT -p udp --dport 53

  匹配指定端口之外的端口
  iptables -A INPUT -p tcp --dport ! 22
  iptables -I INPUT -p tcp ! --dport 22  -s 10.0.0.123 -j DROP

  匹配端口范围
  iptables -A INPUT -p tcp --sport 22:80
  iptables -I INPUT -p tcp -m multiport --dport 21,22,23,24 -j ACCEPT
  iptables -I INPUT -p tcp --dport 3306:8809 -j ACCEPT
  iptables -I INPUT -p tcp --dport 18:80 -j DROP

  匹配ICMP类型
  iptables -A INPUT -p icmp --icmp-type 8
  iptables -A INPUT -p icmp --icmp-type 8 -j DROP
  iptables -A INPUT -p icmp -m icmp --icmp-type any -j ACCEPT

  匹配指定的网络接口
  iptables -A INPUT -i eth0
  iptables -A FORWARD -o eth0
  记忆方法:
  --in-interface -i [!] input name[+]
                                network interface name ([+] for wildcard)
  --out-interface -o [!] output name[+]
                                network interface name ([+] for wildcard)
  匹配网络状态
  -m state --state
      NEW:已经或将启动新的连接
      ESTABLISHED:已建立的连接
      RELATED:正在启动新连接
      INVALID:非法或无法识别的

7、配置一个企业防火墙

[root@ipt ~]# iptables -F
[root@ipt ~]# iptables -X
[root@ipt ~]# iptables -Z
[root@ipt ~]# iptables -A INPUT -p tcp  --dport 22 -s 10.0.0.0/24  -j ACCEPT
[root@ipt ~]# iptables -A INPUT -i lo -j ACCEPT
[root@ipt ~]# iptables -A INPUT -o lo -j ACCEPT  
[root@ipt ~]# iptables -A OUTPUT -o lo -j ACCEPT  

  允许合法的进入:

iptables -A INPUT -s 124.43.62.96/27 -p all -j ACCEPT
iptables -A INPUT -s 192.168.1.0/24 -p all -j ACCEPT
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
iptables -A INPUT -s 201.82.34.0/24 -p all -j ACCEPT

iptables -A INPUT -p icmp --icmp-type 8  -j ACCEPT

#others RELATED ftp协议
#允许关联的状态包
iptables -A INPUT  -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

8、企业iptables面试题:自定义链处理syn攻击
  iptables -N syn-flood
  iptables -A INPUT -i eth0 -syn -j syn-flood
  iptables -A syn-flood -m limit -limit 5000/s -limit-burst 200 -j RETURN
  iptables -A syn-flood -j DROP

9、局域网共享的两条命令方法:
  方法1:适合于有固定外网地址的:
    iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j SNAT --to-source 10.0.0.7
    (1)-s 192.168.1.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 伪装。

10、映射多个外网IP上网
  iptables -t nat -A POSTROUTING -s 10.0.0.0/255.255.240.0 -o eth0 -j SNAT --to-source 124.42.60.11-124.42.60.16
  iptables -t nat -A POSTROUTING -s 172.16.1.0/255.255.255.0 -o eth0 -j SNAT --to-source 124.42.60.103-124.42.60.106

11、将访问10.0.0.7的80端口转到192.168.1.8的9000端口
  iptables -t nat -A PREROUTING -d 10.0.0.7 -p tcp --dport 80 -j DNAT --to-destination 192.168.1.8:9000

12、保存iptables规则
  iptables-save >/etc/sysconfig/iptables
  
13、允许关联的状态包通过(FTP服务是特殊的,需要配状态连接。)

  #允许关联的状态包
  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

  fg:本机地址: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

14、Linux上配置zebra路由:

client(config)#int eth0
client(config-if)#ip add 10.1.34.81 255.255.255.0
client(config-if)#int eth1
client(config-if)#ip add 110.233.24.96 255.255.255.224
client(config)#ip route 0.0.0.0 0.0.0.0 10.1.32.1
client(config)#ip route 110.233.24.96/27 eth1

15、iptables的生产常用场景:
  1)实现服务器本身防火墙功能,使用filter表。
  2)实现局域网上网网关,使用nat表,网关上也可以同时用filter表做防火墙。
  3)实现NAT功能,如:由外部IP映射到内部服务器IP(包括端口),使用nat表。
  4)其他。。。略。

16、相关知识参看
  (1)生产环境大于254台机器网段划分及路由解决方案详解01
  http://v.youku.com/v_show/id_XNTAyMjAwMzI0.html
  (2) linux route命令深入浅出与实战案例精讲
  http://oldboy.blog.51cto.com/2561410/1119453

17、查看是否加载相应的模块

lsmod |egrep "nat|filter"
modprobe ip_tables
modprobe iptable_filter
modprobe iptable_nat
modprobe ip_conntrack
modprobe ip_conntrack_ftp
modprobe ip_nat_ftp
modprobe ipt_state

 



转载于:https://www.cnblogs.com/migongci0412/p/5204520.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值