`

iptables

阅读更多

#!/bin/bash
 
# The outside interface, use ADSL
EXTIF="eth0"

# the inside interface
INIF="eth1"

# the inside network, This is for NAT's network
INNET="192.168.1.0/24"   

#this shell is only for kernel 2.4 2.5 2.6
kver=`uname -r | cut -c 1-3`
if [ "$kver" != "2.4" ] && [ "$kver" != "2.5" ] && [ "$kver" != "2.6" ]; then
    echo "Your Linux Kernel Version may not be suported by this script!"
    echo "This scripts will not be runing"
    exit
fi

#remove ipchain mode
ipchains=`lsmod | grep ipchains`
if [ "$ipchains" != "" ]; then
    echo "unload ipchains in your system"
    rmmod ipchains  2>/dev/null
fi

# load some modes
PATH=/sbin:/bin:/usr/sbin:/usr/bin
export PATH EXTIF INIF INNET
modprobe ip_tables         2>/dev/null
modprobe iptable_nat       2>/dev/null
modprobe ip_nat_ftp        2>/dev/null
modprobe ip_nat_irc        2>/dev/null
modprobe ipt_mark          2>/dev/null
modprobe ip_conntrack      2>/dev/null
modprobe ip_conntrack_ftp  2>/dev/null
modprobe ip_conntrack_irc  2>/dev/null
modprobe ipt_MASQUERADE    2>/dev/null
modprobe ip_nat_h323       2>/dev/null
modprobe ip_conntrack_h323 2>/dev/null
#modprobe ip_nat_mms        2>/dev/null
#modprobe ip_conntrack_mms  2>/dev/null
modprobe ip_nat_pptp       2>/dev/null
modprobe ip_conntrack_pptp 2>/dev/null
#modprobe ip_nat_proto_gre        2>/dev/null
#modprobe ip_conntrack_proto_gre  2>/dev/null
#modprobe ip_nat_quake3         2>/dev/null
#modprobe ip_conntrack_quake3   2>/dev/null

# clean the iptables rule
/sbin/iptables -F
/sbin/iptables -X
/sbin/iptables -Z
/sbin/iptables -F -t nat
/sbin/iptables -X -t nat
/sbin/iptables -Z -t nat
/sbin/iptables -P INPUT   DROP
/sbin/iptables -P OUTPUT  ACCEPT
/sbin/iptables -P FORWARD DROP
/sbin/iptables -t nat -P PREROUTING  ACCEPT
/sbin/iptables -t nat -P POSTROUTING ACCEPT
/sbin/iptables -t nat -P OUTPUT      ACCEPT

#allow samba,smtp,pop3 
/sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
/sbin/iptables -A INPUT -p tcp -m multiport --dports 1863,443,110,80,25 -j ACCEPT
/sbin/iptables -A INPUT -p tcp -s $INNET --dport 139 -j ACCEPT

#allow dns
/sbin/iptables -A INPUT -i $INIF -p udp -m multiport --dports 53 -j ACCEPT

#limit dos connect 15,eles deny
/sbin/iptables -A INPUT -s $INNET -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
/sbin/iptables -A INPUT -i $EXTIF -p tcp --syn -m connlimit --connlimit-above 15 -j DROP
/sbin/iptables -A INPUT -s $INNET -p tcp --syn -m connlimit --connlimit-above 15 -j DROP

#setup icmp limit,and recode
/sbin/iptables -A INPUT -p icmp -m limit --limit 3/s -j LOG --log-level INFO --log-prefix "ICMP packet IN: "
/sbin/iptables -A INPUT -p icmp -m limit --limit 6/m -j ACCEPT
/sbin/iptables -A INPUT -p icmp -j DROP

#L7 layer patch filter
#/sbin/iptables -t mangle -I POSTROUTING -m layer7 --l7proto msnmessenger -j DROP
#/sbin/iptables -t mangle -I POSTROUTING -m layer7 --l7proto skypeout -j DROP
#/sbin/iptables -t mangle -I POSTROUTING -m layer7 --l7proto skypetoskype -j DROP
#/sbin/iptables -t mangle -I POSTROUTING -m layer7 --l7proto bittorrent -j DROP
#/sbin/iptables -t mangle -I POSTROUTING -m layer7 --l7proto fasttrack -j DROP
#/sbin/iptables -t mangle -I POSTROUTING -m layer7 --l7proto edonkey -j DROP
#/sbin/iptables -t mangle -I POSTROUTING -m layer7 --l7proto kugoo -j DROP
#/sbin/iptables -t mangle -I POSTROUTING -m layer7 --l7proto xunlei -j DROP
#/sbin/iptables -t mangle -I POSTROUTING -m layer7 --l7proto code_red -j DROP
#/sbin/iptables -t mangle -I POSTROUTING -m layer7 --l7proto kameng -j DROP
#/sbin/iptables -t mangle -I POSTROUTING -m layer7 --l7proto poco -j DROP
#/sbin/iptables -t mangle -I POSTROUTING -m layer7 --l7proto baiduxiaba -j DROP
#/sbin/iptables -t mangle -I POSTROUTING -m layer7 --l7proto 100bao -j DROP
#/sbin/iptables -t mangle -I POSTROUTING -m layer7 --l7proto socks -j DROP
#/sbin/iptables -t mangle -I POSTROUTING -m layer7 --l7proto nimda -j DROP

#To prevent light weight SYN attacks
/sbin/iptables -N syn-flood
/sbin/iptables -A INPUT -p tcp --syn -j syn-flood
/sbin/iptables -I syn-flood -p tcp -m limit --limit 3/s --limit-burst 6 -j RETURN
/sbin/iptables -A syn-flood -j REJECT


#FORWARD chain
#/sbin/iptables -A FORWARD -m layer7 --l7proto qq -m time --timestart 8:00 --timestop 12:00 --days Sun,Mon,Tue,Wed,Thu,Fri,Sat -j DROP
#/sbin/iptables -A FORWARD -m layer7 --l7proto qq -m time --timestart 13:30 --timestop 21:00 --days Sun,Mon,Tue,Wed,Thu,Fri,Sat -j DROP
/sbin/iptables -A FORWARD -p tcp  -s $INNET -m multiport --dports 25,110,443,1863 -j ACCEPT
/sbin/iptables -A FORWARD -p udp  -s $INNET --dport 53 -j ACCEPT
/sbin/iptables -A FORWARD -p gre  -s $INNET -j ACCEPT
/sbin/iptables -A FORWARD -p icmp -s $INNET -j ACCEPT

#deny BT connect
#/sbin/iptables -I FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
#/sbin/iptables -A FORWARD -m ipp2p --edk --kazaa --bit -j DROP
#/sbin/iptables -A FORWARD -p tcp -m ipp2p --ares -j DROP
#/sbin/iptables -A FORWARD -p udp -m ipp2p --kazaa -j DROP

#only allow 15 ipaddress connect to 80 port to forward at the same
/sbin/iptables -A FORWARD -p tcp --syn --dport 80 -m connlimit --connlimit-above 15 --connlimit-mask 24 -j DROP

#open syncookie To prevent light weight dos attacks
sysctl -w net.ipv4.tcp_syncookies=1 2>/dev/null
 
#setup defaults tcp connect for 3800s to do nothing
sysctl -w net.ipv4.netfilter.ip_conntrack_tcp_timeout_established=3800 2>/dev/null

#setup the max_number 300000 about your server'memory and the version of iptables 
sysctl -w net.ipv4.ip_conntrack_max=300000 2>/dev/null

#innernet ip forward
/sbin/iptables -A INPUT -i lo -j ACCEPT
if [ "$INIF" != "" ]; then
    /sbin/iptables -A INPUT -i $INIF -j ACCEPT
    echo "1" > /proc/sys/net/ipv4/ip_forward
    /sbin/iptables -t nat -A POSTROUTING -s $INNET -o $EXTIF -j MASQUERADE
    #/sbin/iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-ports 3128
    /sbin/iptables -t nat -A PREROUTING -p tcp -m iprange --src-range 192.168.1.3-192.168.1.253 --dport 80 -j REDIRECT --to-ports 3128
fi
 
#load the deny file and allow file
if [ -f /opt/iptables/iptables.deny ]; then
    sh /opt/iptables/iptables.deny
fi
if [ -f /opt/iptables/iptables.allow ]; then
    sh /opt/iptables/iptables.allow
fi
 
#limit the BW
if [ -f /opt/iptables/QoS.sh ]; then
    sh /opt/iptables/QoS.sh
fi
 
#to prevent attacks
#if [ -f /opt/httpd-err/http-netstat.sh ]; then
#    sh /opt/httpd-err/http-netstat.sh
#fi
 
#allow icmp and the package having connected
/sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
AICMP="0 3 3/4 4 11 12 14 16 18"
for tyicmp in $AICMP
do
    /sbin/iptables -A INPUT -i $EXTIF -p icmp --icmp-type $tyicmp -j ACCEPT
done
 
#open the port
/sbin/iptables -A INPUT -p TCP -i $EXTIF --dport 21  -j ACCEPT     # FTP
/sbin/iptables -A INPUT -p TCP -i $EXTIF --dport 22  -j ACCEPT     # SSH
/sbin/iptables -A INPUT -p TCP -i $EXTIF --dport 25  -j ACCEPT     # SMTP
/sbin/iptables -A INPUT -p UDP -i $EXTIF --dport 53  -j ACCEPT     # DNS
/sbin/iptables -A INPUT -p TCP -i $EXTIF --dport 53  -j ACCEPT
/sbin/iptables -A INPUT -p TCP -i $EXTIF --dport 80  -j ACCEPT     # WWW
/sbin/iptables -A INPUT -p TCP -i $EXTIF --dport 110 -j ACCEPT     # POP3
/sbin/iptables -A INPUT -p TCP -i $EXTIF --dport 113 -j ACCEPT     # auth

分享到:
评论

相关推荐

    Centos离线安装iptables.docx

    ### Centos离线安装iptables详解 #### 一、前言 在CentOS系统中,iptables是用于设置网络规则的重要工具之一。然而,在某些情况下,由于网络环境限制或安全考虑,我们可能无法通过在线方式安装iptables。本文将...

    iptables源码iptables源码

    iptables源码 iptables工具

    android流量防火墙iptables原理详解

    Android 流量防火墙 Iptables 原理详解 Android 流量防火墙是一种基于 Iptables 的防火墙解决方案,旨在限制单个应用的联网状态。Iptables 是一个功能强大的 IP 信息包过滤系统,可以用于添加、编辑和删除规则,...

    Linux使用iptables限制多个IP访问你的服务器

    iptables是一个管理netfilter的工具。 多个连续IP操作 1、拆分成多条命令运行 iptables -A INPUT 192.168.122.2 -j ACCEPT iptables -A INPUT 192.168.122.3 -j ACCEPT iptables -A INPUT 192.168.122.4 -j ACCEPT ...

    网络安全课程设计之D防火墙——Iptables.docx

    考查内容:iptables 的规则管理操作;iptables 常用的通用匹配条件和扩展匹配条件;添加、修 改、删除自定义链的方法。 实验内容:1)使用 iptables 制定规则,包括添加、修改、保存和删除规则等。 2)使用通用匹配...

    iptables详解:图文并茂理解iptables.pdf

    iptables 防火墙 linux

    iptables-1.4.7-19.el6.x86_64.rpm(for Centos6)

    CentOS6 64位(ipv4 only,如需ipv6请私信我)iptables防火墙安装rpm文件,安装方法: rpm -ivh iptables-1.4.7-19.el6.x86_64.rpm or yum localinstall -y iptables-1.4.7-19.el6.x86_64.rpm 启动: service ...

    iptables基本命令规则简介

    iptables 是基于内核的防火墙,功能非常强大,iptables 内置了 filter,nat 和 mangle 三张表。filter 负责过滤数据包,包括的规则链有,input,output 和 forward;nat 则涉及到网络地址转换,包括的规则链有,...

    Linux iptables Pocket Refrence

    - **The iptables-restore and iptables-save(iptables-restore和iptables-save)**:分别用于从文件恢复iptables配置和保存当前iptables配置到文件。 通过以上介绍,我们不仅了解了iptables的基本概念和架构,还...

    iptables 编译内核 iptables-1.4.6.tar.bz2

    iptables 源码包安装,编译linux内核

    Linux防火墙iptables入门教程

    一、关于iptables Iptables是一个基于命令行的防火墙工具,它使用规则链来允许/阻止网络流量。当一条网络连接试图在你的系统中建立时,iptables会查找其对应的匹配规则。如果找不到,iptables将对其采取默认操作。...

    iptables 语法 (经典)

    iptables是Linux系统中的一种强大的网络访问控制工具,用于在数据包进入、离开或通过系统时进行过滤和控制。本文将详细介绍iptables的基本语法及其在不同场景下的应用。 首先,iptables有多个表,包括filter、nat、...

    iptables配置(/etc/sysconfig/iptables)操作方法

    下面小编就为大家带来一篇iptables配置(/etc/sysconfig/iptables)操作方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    iptables应用手册详细介绍

    iptables 应用手册详细介绍 iptables 是 Linux 核心里的 Netfilter 子系统所提供的唯一工具程序,用于创建防火墙、网址转换、数据包记录、流量统计等功能。iptables 的接口很可能是 Linux 有史以来最精致的,使得 ...

    iptables防火墙应用指南

    ### iptables防火墙应用指南 #### 1. iptables简介 **1.1 iptables防火墙简介** iptables是一款广泛应用于Unix/Linux系统的免费包过滤防火墙工具。它具有强大的功能和高度灵活性,能够精确控制流入、流出以及通过...

    iptables指南1.1.19电子书

    iptables指南1.1.19电子书 译者序 关于作者 如何阅读 必备知识 本文约定 1. 序言 1.1. 为什么要写这个指南 1.2. 指南是如何写的 1.3. 文中出现的术语 2. 准备阶段 2.1. 哪里能取得iptables 2.2. 内核配置 ...

    iptables 手册 chm 格式

    iptables 手册 chm 格式 <br> 本文介绍了iptables,以便你可以领会iptables的精彩,文中不包含iptables或Netfilter在安全方面的 bug。如果你发现iptables(或其组成部分)任何bug或特殊的行为,请联系...

    Iptables中文使用指南

    第二章对想要亲自编译iptables的兄弟们是有些帮助的。 第三、第四两章可以使我们理解、掌握iptables工作方式和流程。 第五章和第六章是iptables命令使用方法的详细介绍。 第七章与第八章是实例讲解,对我们编写自己...

    Iptables速查手册

    ### Iptables速查手册知识点解析 #### 一、Iptables简介与基本概念 **Iptables** 是一个在Linux系统中管理网络数据包过滤规则的工具,它基于Netfilter框架实现。Netfilter是Linux内核的一个子系统,用于处理网络...

    为mini2440开发板移植iptables工具

    ### 为mini2440开发板移植iptables工具 #### 概述 本文旨在详细介绍如何为mini2440开发板移植iptables工具的过程。iptables是一款功能强大的网络管理工具,能够帮助用户实现数据包过滤、地址转换(NAT)等功能。对于...

Global site tag (gtag.js) - Google Analytics