zoukankan      html  css  js  c++  java
  • 比较好的iptable实例

    转载 xxxxx

    以前公司用的一个例子,写成一个脚本,放在rc.local开机运行即可: https://files.cnblogs.com/derekchen/iptables.rar

    service iptables stop   

    service iptables stop    //停止先停止防火墙

    INET_IP=`ifconfig | grep 'inet ' | grep -v '127.0.0.1' | awk -F ' ' '{print $2}'| awk -F':' '{print $2}'`    //获取本机IP

    LO_IFACE="lo"
    LO_IP="127.0.0.1"

    IPTABLES="/sbin/iptables"     //定义iptables路径

    /sbin/depmod -a     //以下几项为开启iptables的各种模块
    /sbin/modprobe ip_tables
    /sbin/modprobe ip_conntrack
    /sbin/modprobe iptable_filter
    /sbin/modprobe iptable_mangle
    /sbin/modprobe ipt_state

    $IPTABLES -P INPUT ACCEPT      //定于INPUT为开放状态
    $IPTABLES -P OUTPUT ACCEPT     //定于OUTPUT为开放状态
    $IPTABLES -P FORWARD ACCEPT    //定于FORWARD为开放状态

    $IPTABLES -N bad_tcp_packets   //自定义一个 bad_tcp_packets组    以下同
    $IPTABLES -N allowed
    $IPTABLES -N tcp_packets
    $IPTABLES -N udp_packets
    $IPTABLES -N icmp_packets

    $IPTABLES -A bad_tcp_packets -p tcp --tcp-flags SYN,ACK SYN,ACK -m state --state NEW -j REJECT --reject-with tcp-reset    //丢弃坏包
    $IPTABLES -A bad_tcp_packets -p tcp ! --syn -m state --state NEW -j DROP     //丢弃坏包

    $IPTABLES -A allowed -p TCP --syn -j ACCEPT      //这3条顺序很重要      允许初始连接通过
    $IPTABLES -A allowed -p TCP -m state --state ESTABLISHED,RELATED -j ACCEPT   //允许ESTABLISHED,RELATED状态的包通过
    $IPTABLES -A allowed -p TCP -j DROP   拒绝所有包

    $IPTABLES -A tcp_packets -p TCP -s 0/0 --dport 80 -j allowed   //允许TCP 80 通过,下同,根据自己环境添加相关端口
    $IPTABLES -A tcp_packets -p TCP -s 0/0 --dport 5166 -j allowed
    $IPTABLES -A tcp_packets -p TCP -s 0/0 --dport 3301 -j allowed
    $IPTABLES -A tcp_packets -p TCP -s 0/0 --dport 813 -j allowed
    $IPTABLES -A tcp_packets -p TCP -s 0/0 --dport 2213 -j allowed
    $IPTABLES -A tcp_packets -p TCP -s 0/0 --dport 33885 -j allowed
    $IPTABLES -A tcp_packets -p TCP -s 0/0  -j DROP


    $IPTABLES -A udp_packets -p UDP -s 0/0 --destination-port 161 -j ACCEPT   //允许UDP 161端口 通过,根据自己环境添加相关端口
    $IPTABLES -A udp_packets -p UDP -s 0/0 -j DROP

    $IPTABLES -A icmp_packets -p ICMP -s 0/0 --icmp-type 8 -j ACCEPT      //ICMP的相关项,各个数字代表什么可以google下
    $IPTABLES -A icmp_packets -p ICMP -s 0/0 --icmp-type 11 -j ACCEPT 
    $IPTABLES -A icmp_packets -p ICMP -s 0/0 -j DROP

    $IPTABLES -A INPUT -p TCP -j bad_tcp_packets    //一个连接包新进来先通过bad_tcp_packets检测判断是否为坏包
    $IPTABLES -A INPUT -p ALL -m state --state ESTABLISHED,RELATED -j ACCEPT    //状态为ESTABLISHED,RELATED 的包则通过
    $IPTABLES -A INPUT -p TCP -m tcp --dport 30000:50000 --syn -j ACCEPT   //3W-5W的端口可以通过,这个是为了配合FTP的,可以不要
    $IPTABLES -A INPUT -p TCP  -j tcp_packets    //最后用过 tcp_packets 这个组来判断是否让包通过

    $IPTABLES -A INPUT -p ALL -i $LO_IFACE  -j ACCEPT    //lo本地的全部通过
    $IPTABLES -A INPUT -p UDP  -j  tcp_packets      //UDP包发送到 tcp_packets 组检测
    $IPTABLES -A INPUT -p ICMP -j icmp_packets    //ICMP 发送到 icmp_packets  组检测

    $IPTABLES -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT   

    $IPTABLES -A OUTPUT -p tcp -j bad_tcp_packets    //OUTPUT出去的包先通过bad_tcp_packets判断是否为坏包
    $IPTABLES -A OUTPUT -p ALL -s $LO_IP -j ACCEPT   //LO出去的全部通过
    $IPTABLES -A OUTPUT -p ALL  -j ACCEPT    //不是坏包就允许出去

  • 相关阅读:
    linux下开启防火墙,允许通过的端口
    linux下限定连接ip和端口
    centos7关闭防火墙
    linux下清空文件内容的3个命令
    yum安装软件包提示Error Downloading Packages解决方法
    Zabbix 监控服务介绍
    Redis 应用
    分布式中间件MyCat 使用
    DevOps Gitlab环境部署
    MySQL Atlas 读写分离软件介绍
  • 原文地址:https://www.cnblogs.com/derekchen/p/2827289.html
Copyright © 2011-2022 走看看