zoukankan      html  css  js  c++  java
  • iptables的用例

    iptables书写思路顺序

    1.协议 icmp
    2.哪个功能和目标:过滤,拒绝
    3.数据包流向:外到内
    4.哪个链适合:越早越好,INPUT
    5.源地址和目标地址

    练习1.禁止某些主机或网络访问本机


    禁止192.168.56.14网段的主机ping本主机
    iptables -t filter -A INPUT -p icmp -s 192.168.56.14 -d 192.168.56.11 -j DROP
    watch -n 1 'iptables -t filter -L -v -n' 实时查看包统计

    禁止192.168.56.1访问本机网页 
    iptables -t filter -A INPUT -p tcp -s 192.168.56.1 -d 192.168.56.14 --dport 80 -j DROP

    练习2.练习filter的各类匹配规则写法


    对filter的规则优先级按从低到高为:首先使用通策略,其次对外开放80端口访问,最后禁止破解方的IP,
    查看和导出规则,监控包统计
    iptables -L -n -v -x
    iptables-save > /etc/sysconfig/iptables
    cat /etc/sysconfig/iptables
    watch -n 1 'iptables -t filter -L -nxv --line-numbers'
    清空filter表
    iptables -F
    iptables -Z

    清空filter表前先设置默认策略,否则登录不了

    添加ssh远程访问规则
    iptables -t filter -A INPUT -p tcp -i eth0 -s 192.168.56.0/24 -d 192.168.56.14 --dport 22 -j ACCEPT
    iptables -t filter -A OUTPUT -p tcp -o eth0 -s 192.168.56.14 -d 192.168.56.0/24 --sport 22 -j ACCEPT

    添加filter表默认规则
    iptables -t filter -P INPUT DROP 
    iptables -t filter -P OUTPUT DROP
    iptables -t filter -P FORWARD DROP

    更正规则ssh访问规则
    iptables -t filter -R OUTPUT 1 -s 192.168.56.14 -d 192.168.56.0/24 -o eth0 -p tcp --sport 22 -j ACCEPT

    添加回环接口访问
    iptables -t filter -I INPUT 1 -i lo -j ACCEPT
    iptables -t filter -I OUTPUT 1 -o lo -j ACCEPT

    禁止基于非法TCP标志位的连接
    iptables -t filter -I INPUT -p tcp --tcp-flags all all -j DROP
    iptables -t filter -I INPUT -p tcp --tcp-flags all none -j DROP

    放行ssh第一次握手,如果启用连接追踪功能,新写法更简洁
    iptables -t filter -I INPUT 3 -p tcp --dport 22 --tcp-flags syn,ack,rst,fin syn -j ACCEPT
    iptables -t filter -R INPUT 3 -p tcp --dport 22 --syn  -j ACCEPT 简单写法

    放行DNS查询
    iptables -t filter -A OUTPUT -p udp -s 192.168.8.14 --dport 53 -j ACCEPT
    iptables -t filter -A INPUT -p udp -d 192.168.8.14 --dport 53 -j ACCEPT
    dig -t A www.baidu.com @192.168.8.1

    开放ping
    iptables -t filter -I INPUT 3 -p icmp -d 192.168.56.14 -j ACCEPT
    iptables -t filter -I OUTPUT -p icmp -s 192.168.56.14 -j ACCEPT

    只允许自己ping别人,不允许别人自己
    iptables -t filter -D INPUT 3
    iptables -t filter -D OUTPUT 1
    iptables -t filter -A OUTPUT -p icmp --icmp-type 8 -s 192.168.56.14 -j ACCEPT
    iptables -t filter -I INPUT 3 -p icmp --icmp-type 0 -d 192.168.56.14 -j ACCEPT 


    同时只开放ssh和web访问
    iptables -t filter -I INPUT -p tcp -m multiport --dports 22,80 -d 192.168.56.14 -j ACCEPT
    iptables -t filter -I OUTPUT -p tcp -m multiport --sports 22,80 -s 192.168.56.14 -j ACCEPT
    iptables -t filter -D INPUT 2 执行多次
    iptables -t filter -D OUTPUT 2 执行多次

    允许一批连续IP的主机对本机的telnet
    iptables -t filter -A INPUT -p tcp --dport 23 -m iprange --src-range 192.168.56.1-192.168.56.100 -j ACCEPT
    iptables -t filter -A OUTPUT -p tcp --sport 23 -m iprange --dst-range 192.168.56.1-192.168.56.100 -j ACCEPT

    禁止访问敏感网页,含sex字眼的
    [root@node1 ~]# cd /var/www/html
    [root@node1 html]# echo "this is a sex page" > test.html
    http://192.168.56.14/test.html
    iptables -t filter -I OUTPUT -p tcp --sport 80 -m string --algo kmp --string "sex" -j DROP
    http://192.168.56.14/test.html 注意缓存问题

    禁止周一、二、四、五的8:20到18:40访问web服务器
    iptables -t filter -I INPUT -p tcp --dport 80 -m time --timestart 08:20 --timestop 18:40 --weekdays Mon,Tue,Thu,Fri -j DROP
    iptables -t filter -D INPUT 1 测试完,删掉它

    访问本机ssh服务,每IP不能超过4个并发
    iptables -t filter -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 4 -j DROP

    限制外部每分钟发20个ping包(允许超出5个包)到本机
    iptables -t filter -A INPUT -p icmp --icmp-type 8 -m limit --limit 20/minute --limit-burst 5 -j ACCEPT
    iptables -t filter -A OUTPUT -p icmp --icmp-type 0 -j ACCEPT

    使用连接追踪功能改写只允许ssh和http连接进来
    iptables -t filter -P INPUT ACCEPT
    iptables -t filter -P OUTPUT ACCEPT
    iptables -t filter -F
    iptables -t filter -A INPUT -p tcp -m multiport --dports 22,80 -m state --state NEW -j ACCEPT
    iptables -t filter -I INPUT -m state --state ESTABLISHED -j ACCEPT
    iptables -t filter -I OUTPUT -m state --state ESTABLISHED -j ACCEPT 放行所有的响应,效率超高
    iptables -t filter -P INPUT DROP
    iptables -t filter -P OUTPUT DROP

    使用连接追踪功能放行ping
    iptables -t filter -A INPUT -p icmp --icmp-type 8 -j ACCEPT
    注意这里不用再增加响应的放行规则,因为上一步的规则已经做了

    使用连接追踪功能放行ftp服务

    安装vfstpd
    yum -y install vsftpd
    service vsftpd start
    装载内核支持模块:
    cd /lib/modules/2.6.32-358.el6.x86_64/kernel/net/netfilter/
    ls *ftp*
    nf_conntrack_ftp.ko  nf_conntrack_tftp.ko
    modprobe nf_conntrack_ftp
    添加规则:
    放行21端口的NEW状态连接、放行所有ESTABLISH和RELATED状态的请求报文、放行ESTABLISH和RELATED状态的响应报文
    iptables -t filter -R INPUT 2 -p tcp -m multiport --dports 22,80,21 -m state --state NEW -j ACCEPT
    iptables -t filter -R INPUT 1 -m state --state ESTABLISHED,RELATED -j ACCEPT
    iptables -t filter -R OUTPUT 1 -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
    测试:
    ftp://192.168.56.14/

    用自定义规则的方式,禁止一批连续IP的主机对本机的web访问

    设置默认规则放通
    iptables -t filter -P INPUT ACCEPT
    iptables -t filter -P OUTPUT ACCEPT
    iptables -t filter -F

    添加web自定义规则
    iptables -t filter -N http_in
    iptables -t filter -A http_in -p tcp -d 192.168.56.14 --dport 80 -m iprange --src-range 192.168.56.1-192.168.56.100 -j DROP
    iptables -t filter -A http_in -p tcp -d 192.168.56.14 --dport 80 -m state --state NEW -j ACCEPT
    iptables -t filter -A http_in -j RETURN 在自定义链中无法匹配时返回主链

    在INPUT链放行所有已建立连接
    iptables -t filter -A INPUT -m state --state established -j ACCEPT

    添加ssh自定义规则
    iptables -t filter -N ssh_in
    iptables -t filter -A ssh_in -p tcp -d 192.168.56.14 --dport 22 -m state --state NEW -j ACCEPT

    在OUTPUT链放行所有已建立连接
    iptables -t filter -A OUTPUT -m state --state established -j ACCEPT

    在INPUT上调用http_in链
    iptables -t filter -A INPUT -p tcp -d 192.168.56.14 --dport 80 -j http_in

    在INPUT上调用ssh_in链
    iptables -t filter -A INPUT -p tcp -d 192.168.56.14 --dport 22 -j ssh_in

    设置默认规则禁止
    iptables -t filter -P INPUT DROP
    iptables -t filter -P OUTPUT DROP

    测试:
    curl 
    -i 显示响应头部和实体
    -I 显示响应头部
    -dump 显示响应实体
    curl -I http://192.168.56.14/test.html

    删除http_in自定义链的顺序
    iptables -t filter -D INPUT 2
    iptables -t filter -F http_in
    iptables -t filter -X http_in

    重命名自定义链
    iptables -t filter -N http_in
    iptables -t filter -E http_in web_in

    练习3:安装telnet服务


    安装并启动服务
    yum -y install xinetd
    yum -y install telnet-server
    yum -y install telnet
    vim /etc/xinetd.d/telnet
    disable         = no
    service xinetd start

    更改telnet服务端口号:
    vim /etc/services
    telnet          23/tcp
    telnet          23/udp

    更多/etc/xinetd.d/telnet选项说明
    # 先针对对内的较为松散的限制来设定
    bind       = 210.45.160.17 只允许经由这个适配卡的封包进来
    only_from    = 210.45.160.0/24 只允许 210.45.160.0/24 这个网段的主机联机进来使用 telnet 的服务
    only_from    = .edu.cn <==重复设定,只有教育网才能联机!
    no_access    = 192.168.25.{10,26} <==不许这些 PC 登入
    access_times  = 1:00-9:00 20:00-23:59 <==每天只有这两个时段开放服务


    练习4:配置主机作为路由网关


    环境:
    网络A
    客户端c:192.168.60.20/24
    路由网关r的地址1: 192.168.60.11/24
    网络B
    服务端s:192.168.56.20/24
    路由网关r的地址2:192.168.56.11/24
    要求c能访问s提供的服务:http、ssh、ftp
    配置s:
    清空filter表,以及删除自定义链,然后设置网络属性
    ifconfig | less
    ifconfig eth1 down
    ifconfig eth2 down
    ifconfig eth0 192.168.56.20/24 up
    route -n 
    route del -net default
    route add -net default gw 192.168.56.11 dev eth0
    安装并启动服务
    yum -y install httpd
    yum -y install vsftpd
    service httpd start
    service vsftpd start

    配置c
    清空filter表,以及删除自定义链,然后设置网络属性
    ip addr show
    ip addr del 192.168.60.15/24 dev eth2
    ip addr add 192.168.60.20/24 dev eth2
    ip link set eth0 down
    ip link set eth1 down
    ip route show
    ip route del default
    ip route add default via 192.168.60.11 dev eth2
    如果无法访问远程ssh,用ssh -v user@ip来检查问题所在。
    如果是gss的问题,则去掉本ssh客户端中的GSS认证
    vi /etc/ssh/ssh_config
    设置GSSAPIAuthentication no  
    如果是dns反解析的问题,则取消ssh服务器端的dns解析配置
    vim /etc/ssh/sshd_config
    UseDNS no


    配置r
    清空filter表,以及删除自定义链,然后设置网络属性
    修改内核中关于转发的参数
    vim /etc/sysctl.conf
    net.ipv4.ip_forward = 1
    sysctl -p
    cat /proc/sys/net/ipv4/ip_forward
    添加转发规则
    iptables -t filter -P FORWARD DROP
    iptables -t filter -A FORWARD -m state --state established,related -j ACCEPT
    iptables -t filter -A FORWARD -p tcp --dport 80 -d 192.168.56.20 -m state --state new -j ACCEPT
    iptables -t filter -A FORWARD -p tcp --dport 22 -d 192.168.56.20 -m state --state new -j ACCEPT
    iptables -t filter -A FORWARD -p tcp --dport 21 -d 192.168.56.20 -m state --state new -j ACCEPT
    加载ftp连接追踪模块
    lsmod | grep nf_conntrack_ftp
    临时生效:
    cd /lib/modules/2.6.32-358.el6.x86_64/kernel/net/netfilter/
    ls *ftp*
    modprobe nf_conntrack_ftp
    永久生效:
    vim /etc/sysconfig/iptables-config
    IPTABLES_MODULES="nf_conntrack_ftp"
    在客户端c上验证:
    ssh fedora@192.168.56.20
    curl -i http://192.168.56.20
    lftp 192.168.56.20 (匿名账号)


    练习5:内网与公网的服务能互相访问


    内网主机 h1:192.168.56.20/24,网关192.168.56.11
    公网主机 h2:192.168.60.20/24,无网关,但与h1的网关主机在同一网络
    网关主机:
    地址1: 192.168.56.11
    地址2: 192.168.60.11
    1.内网访问公网的http服务
    配置h1:
    ifconfig eth0 192.168.56.20/24 up
    route add -net default gw 192.168.56.11 dev eth0
    配置h2:
    ip addr add 192.168.60.20/24 dev eth2
    ip route del default
    service httpd start
    配置网关r:
    iptables -t nat -A POSTROUTING -s 192.168.56.0/24 -j SNAT --to-source 192.168.60.11
    优化:如果网关的公网ip经常变化(家里通过ADSL拨号上网,每次拨号得到的公网IP都不同)则上面语句改写为
    iptables -t nat -A POSTROUTING -s 192.168.56.0/24 -j MASQUERADE
    测试:
    h1通过http访问h2上的web,在h2上的web虚拟主机访问日志看到来源ip为h1的映射公网地址192.168.60.11即是正确
    补充
    在原来"1.内网访问公网的http服务"的基础上做限制,只允许访问公网的http服务,且开放时间为周一、二、四、五的8:20到18:40
    增加filter规则
    iptables -t filter -A FORWARD -p tcp -s 192.168.56.0/24 --dport 80 -m time --timestart 08:20 --timestop 18:40 --weekdays Mon,Tue,Thu,Fri -j DROP
    iptables -t filter -P FORWARD ACCEPT
    2.公网访问内网的http服务
    配置h1:
    ifconfig eth0 192.168.56.20/24 up
    route add -net default gw 192.168.56.11 dev eth0
    service httpd start
    配置h2:
    ip addr add 192.168.60.20/24 dev eth2
    ip route del default
    配置网关r:
    iptables -t nat -A PREROUTING -p tcp -d 192.168.60.11 --dport 80 -j DNAT --to-destination 192.168.56.20:80
    测试:
    h2通过http访问h1上的web,在h1上的web虚拟主机访问日志看到来源ip为h2的公网地址192.168.60.20即是正确




  • 相关阅读:
    数据库使用时应该避开的坑
    Linux 命令 curl 的用法及参数解析
    分析 Redis 是否适合作为消息队列
    WEB框架对比——Django、Flask、FastAPI
    视频下载神器——you-get
    QtScrcpy——开源的电脑控制手机(投屏+控制)软件
    Python库大全
    Docker 清理数据卷 volumes
    报错解决——失败
    微信电脑端多开
  • 原文地址:https://www.cnblogs.com/tsw1107/p/63935d63d1cbccfb10036af37c110f1b.html
Copyright © 2011-2022 走看看