iptables学习笔记

2017-07-18  本文已影响66人  drfung

iptables防火墙简介

image.png

filter表

提供主机防火墙的主要功能,iptables的默认表。这个表定义了三个链:

# 查看filter表
iptables -L -n 
# 查看nat表需要指定
iptables -L -n -t nat

nat表

网络地址转换,即来源与目的ip地址和port的转换。

  • PREROUTING - 在数据到达防火墙时进行路由判断之前执行的规则,改变数据包的 目的地址端口
  • POSTROUTING - 在数据包离开防火墙是进行路由判断之后执行的规则,改变数据包的 源地址端口

iptables数据流程

image.png
image.png
image.png

iptabels使用

# 版本
iptables -V
# 帮助
iptables -h
# -n 数字显示 -L chain列表
iptables -L -n
# 基于内核参数
lsmod | grep -E "nat|filter|ipt"
# 如果没有加载输入下列命令让内核加载相关参数
modprobe ip_tables
modprobe iptable_filter
modprobe iptable_nat
modporbe ip_conntrack
modporbe ip_conntrack_ftp
modprobe ip_nat_ftp
modprobe ipt_state

# -F 清除所有规则,无法清除默认规则
# -X 删除用户自定义的链
# -Z 链计数器清零

# 禁用22端口
iptables -t filter -A INPUT -p tcp --dport 22 -j DROP
# 删除规则
iptables -t filter -D INPUT -p tcp --dport 22 -j DROP
# 根据序号来删除
iptables -L -n --line-numbers
iptables -t filter -D INPUT 1

# -A 在指定链的结尾插入
# -I 在指定链的开头插入
# -i 网卡接口
# -s 源地址

# 禁用ICMP
iptables -t filter -I INPUT -p icmp --icmp-type 8 -i eth0 ! -s 10.0.0.0/24 -j DROP
# 封端口范围
iptables -I INPUT -p tcp -dport 52000:53000 -j  DROP
iptables -I INPUT -p tcp -m multoport --dport 21,22,23,24 -j ACCEPT

# -m state(NEW DSTABLISHED RELATED INVALID)

实验

iptables做nat转发,使内网机器访问外网

  1. 实验环境
    ## 跳板机
    [root@jump ~]# ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
        inet6 ::1/128 scope host 
           valid_lft forever preferred_lft forever
    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 00:0c:29:b2:8c:36 brd ff:ff:ff:ff:ff:ff
        inet 192.168.88.133/24 brd 192.168.88.255 scope global eth0
        inet6 fe80::20c:29ff:feb2:8c36/64 scope link 
           valid_lft forever preferred_lft forever
    3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 00:0c:29:b2:8c:40 brd ff:ff:ff:ff:ff:ff
        inet 192.168.56.200/24 brd 192.168.56.255 scope global eth1
        inet6 fe80::20c:29ff:feb2:8c40/64 scope link 
           valid_lft forever preferred_lft forever
    ## 内网机器
    [root@inner ~]# ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
        inet6 ::1/128 scope host 
           valid_lft forever preferred_lft forever
    2: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
        link/ether 00:0c:29:28:22:a1 brd ff:ff:ff:ff:ff:ff
    3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 00:0c:29:28:22:ab brd ff:ff:ff:ff:ff:ff
        inet 192.168.56.222/24 brd 192.168.56.255 scope global eth1
        inet6 fe80::20c:29ff:fe28:22ab/64 scope link 
           valid_lft forever preferred_lft forever
    
  2. 开启ipv4转发
    [root@inner ~]# sed -i "s/^net.ipv4.ip_forward .*/net.ipv4.ip_forward = 1/g" /etc/sysctl.conf
    [root@inner ~]# sysctl -p
    net.ipv4.ip_forward = 1
    net.ipv4.conf.default.rp_filter = 1
    net.ipv4.conf.default.accept_source_route = 0
    kernel.sysrq = 0
    kernel.core_uses_pid = 1
    net.ipv4.tcp_syncookies = 1
    kernel.msgmnb = 65536
    kernel.msgmax = 65536
    kernel.shmmax = 68719476736
    kernel.shmall = 4294967296
    
  3. 默认允许转发
    [root@inner ~]# 
    [root@inner ~]# 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  
    
  4. 加载必要的内核模块:
    modprobe ip_tables
    modprobe iptable_filter
    modprobe iptable_nat
    modporbe ip_conntrack
    modporbe ip_conntrack_ftp
    modprobe ip_nat_ftp
    modprobe ipt_state
    
  5. 添加nat转发:
    方法1:适合于有固定外网地址的:
    iptables -t nat -A POSTROUTING -o eth0 -s 192.168.56.0/24 -j SNAT --to-source 192.168.56.200
    方法2:适合变化外网地址(ADSL):
    iptables -t nat -A POSTROUTING -o eth0 -s 192.168.56.0/24 -j MASQUERADE
    
上一篇 下一篇

猜你喜欢

热点阅读