Chinaunix首页 | 论坛 | 博客
  • 博客访问: 555375
  • 博文数量: 114
  • 博客积分: 5611
  • 博客等级: 大校
  • 技术积分: 1027
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-19 08:55
文章分类

全部博文(114)

文章存档

2011年(29)

2010年(20)

2009年(1)

2008年(11)

2007年(53)

分类: LINUX

2007-12-12 16:22:39

自动定制iptables脚本的网站,非常的不错,值得借鉴。
特此把我自己生成的脚本放在这里做个留存!
#!/bin/sh
###############################################################################
# Local Settings
SYSCTL="/sbin/sysctl -w" 
IPT="/sbin/iptables"
IPTS="/sbin/iptables-save"
IPTR="/sbin/iptables-restore"
 
# Internet Interface
INET_IFACE="eth0"
INET_ADDRESS="*.*.*.*"
 
# Local Interface Information
LOCAL_IFACE="eth1"
LOCAL_IP="172.21.41.15"
LOCAL_NET="172.21.41.0/24"
LOCAL_BCAST="172.21.41.255"
 
# Localhost Interface
LO_IFACE="lo"
LO_IP="127.0.0.1"
 
# Save and Restore arguments handled here
if [ "$1" = "save" ]
then
         echo -n "Saving firewall to /etc/sysconfig/iptables ... "
         $IPTS > /etc/sysconfig/iptables
         echo "done"
         exit 0
elif [ "$1" = "restore" ]
then
         echo -n "Restoring firewall from /etc/sysconfig/iptables ... "
         $IPTR < /etc/sysconfig/iptables
         echo "done"
         exit 0
fi
 
###############################################################################
# Load Modules
echo "Loading kernel modules ..."
 
# core netfilter module
/sbin/modprobe ip_tables
/sbin/modprobe ip_conntrack
/sbin/modprobe iptable_nat
/sbin/modprobe ipt_MASQUERADE
/sbin/modprobe ipt_mark
/sbin/modprobe multiport
/sbin/modprobe ip_nat_ftp
/sbin/modprobe ip_conntrack_ftp
/sbin/modprobe ip_conntrack_irc
 
# /sbin/modprobe iptable_filter
# /sbin/modprobe iptable_mangle
# /sbin/modprobe ipt_LOG
# /sbin/modprobe ipt_limit
# /sbin/modprobe ipt_unclean
# /sbin/modprobe ipt_state
# /sbin/modprobe ipt_tcpmss
# /sbin/modprobe ipt_REJECT
 
############################################################################
# Kernel Parameter Configuration
 
if [ "$SYSCTL" = "" ]
then
    echo "1" > /proc/sys/net/ipv4/ip_forward
else
    $SYSCTL net.ipv4.ip_forward="1"
fi
 
if [ "$SYSCTL" = "" ]
then
    echo "1" > /proc/sys/net/ipv4/tcp_syncookies
else
    $SYSCTL net.ipv4.tcp_syncookies="1"
fi
 
if [ "$SYSCTL" = "" ]
then
    echo "1" > /proc/sys/net/ipv4/conf/all/rp_filter
else
    $SYSCTL net.ipv4.conf.all.rp_filter="1"
fi
 
 
if [ "$SYSCTL" = "" ]
then
    echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
else
    $SYSCTL net.ipv4.icmp_echo_ignore_broadcasts="1"
fi
 
if [ "$SYSCTL" = "" ]
then
    echo "0" > /proc/sys/net/ipv4/conf/all/accept_source_route
else
    $SYSCTL net.ipv4.conf.all.accept_source_route="0"
fi
 
if [ "$SYSCTL" = "" ]
then
    echo "1" > /proc/sys/net/ipv4/conf/all/secure_redirects
else
    $SYSCTL net.ipv4.conf.all.secure_redirects="1"
fi
 
# This option logs packets from impossible addresses.
if [ "$SYSCTL" = "" ]
then
    echo "1" > /proc/sys/net/ipv4/conf/all/log_martians
else
    $SYSCTL net.ipv4.conf.all.log_martians="1"
fi
 
###############################################################################
# Flush Any Existing Rules or Chains
echo "Flushing Tables ..."
 
# Reset Default Policies
$IPT -P INPUT ACCEPT
$IPT -P FORWARD ACCEPT
$IPT -P OUTPUT ACCEPT
$IPT -t nat -P PREROUTING ACCEPT
$IPT -t nat -P POSTROUTING ACCEPT
$IPT -t nat -P OUTPUT ACCEPT
$IPT -t mangle -P PREROUTING ACCEPT
$IPT -t mangle -P OUTPUT ACCEPT
 
# Flush all rules
$IPT -F
$IPT -t nat -F
$IPT -t mangle -F
 
# Erase all non-default chains
$IPT -X
$IPT -t nat -X
$IPT -t mangle -X
 
if [ "$1" = "stop" ]
then
         echo "Firewall completely flushed!  Now running with no firewall."
         exit 0
fi
 
###############################################################################
# Rules Configuration
###############################################################################
# Filter Table
# Set Policies
$IPT -P INPUT DROP
$IPT -P OUTPUT DROP
$IPT -P FORWARD DROP
###############################################################################
# User-Specified Chains
echo "Create and populate custom rule chains ..."
$IPT -N bad_packets
$IPT -N bad_tcp_packets
$IPT -N icmp_packets
$IPT -N udp_inbound
$IPT -N udp_outbound
$IPT -N tcp_inbound
$IPT -N tcp_outbound
###############################################################################
# Populate User Chains
# bad_packets chain
$IPT -A bad_packets -p ALL -i $INET_IFACE -s $LOCAL_NET -j DROP
$IPT -A bad_packets -p ALL -m state --state INVALID -j DROP
$IPT -A bad_packets -p tcp -j bad_tcp_packets
$IPT -A bad_packets -p ALL -j RETURN
######################################################################
$IPT -A bad_tcp_packets -p tcp -i $LOCAL_IFACE -j RETURN
$IPT -A bad_tcp_packets -p tcp ! --syn -m state --state NEW -j DROP
$IPT -A bad_tcp_packets -p tcp --tcp-flags ALL NONE -j DROP
$IPT -A bad_tcp_packets -p tcp --tcp-flags ALL ALL -j DROP
$IPT -A bad_tcp_packets -p tcp --tcp-flags ALL FIN,URG,PSH -j DROP
$IPT -A bad_tcp_packets -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP
$IPT -A bad_tcp_packets -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
$IPT -A bad_tcp_packets -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
$IPT -A bad_tcp_packets -p tcp -j RETURN
 
# icmp_packets chain
$IPT -A icmp_packets --fragment -p ICMP -j DROP
# $IPT -A icmp_packets -p ICMP -s 0/0 --icmp-type 8 -j DROP
$IPT -A icmp_packets -p ICMP -s 0/0 --icmp-type 11 -j ACCEPT
$IPT -A icmp_packets -p ICMP -j RETURN
 
# TCP & UDP
# udp_inbound chain
$IPT -A udp_inbound -p UDP -s 0/0 --destination-port 137 -j DROP
$IPT -A udp_inbound -p UDP -s 0/0 --destination-port 138 -j DROP
$IPT -A udp_inbound -p UDP -s 0/0 --destination-port 113 -j REJECT
 
# Network Time Protocol (NTP) Server
$IPT -A udp_inbound -p UDP -s 0/0 --destination-port 123 -j ACCEPT
 
# DNS Server
$IPT -A udp_inbound -p UDP -s 0/0 --destination-port 53 -j ACCEPT
 
# Not matched, so return for logging
$IPT -A udp_inbound -p UDP -j RETURN
 
# udp_outbound chain
$IPT -A udp_outbound -p UDP -s 0/0 -j ACCEPT
 
# tcp_inbound chain
$IPT -A tcp_inbound -p TCP -s 0/0 --destination-port 113 -j REJECT
 
# Web Server
$IPT -A tcp_inbound -p TCP -s 0/0 --destination-port 80 -j ACCEPT
 
# FTP Server (Control)
$IPT -A tcp_inbound -p TCP -s 0/0 --destination-port 21 -j ACCEPT
 
# FTP Client (Data Port for non-PASV transfers)
$IPT -A tcp_inbound -p TCP -s 0/0 --source-port 20 -j ACCEPT
 
# Passive FTP
$IPT -A tcp_inbound -p TCP -s 0/0 --destination-port 30000:50000 -j ACCEPT
 
# Email Server (SMTP)
$IPT -A tcp_inbound -p TCP -s 0/0 --destination-port 25 -j ACCEPT
 
# Email Server (POP3)
$IPT -A tcp_inbound -p TCP -s 0/0 --destination-port 110 -j ACCEPT
 
# Email Server (IMAP4)
$IPT -A tcp_inbound -p TCP -s 0/0 --destination-port 143 -j ACCEPT
 
# sshd
$IPT -A tcp_inbound -p TCP -s 0/0 --destination-port 22 -j ACCEPT
 
# ICQ File Transfers & Other Advanced Features
$IPT -A tcp_inbound -p TCP -s 0/0 --destination-port 5000:5100 -j ACCEPT
 
# MSN Messenger File Transfers
$IPT -A tcp_inbound -p TCP -s 0/0 --destination-port 6891:6900 -j ACCEPT
 
# User specified allowed UDP protocol
$IPT -A tcp_inbound -p TCP -s 0/0 --destination-port 3333:3334 -j ACCEPT
 
# Not matched, so return so it will be logged
$IPT -A tcp_inbound -p TCP -j RETURN
 
# tcp_outbound chain
# Block Outbound Telnet
$IPT -A tcp_outbound -p TCP -s 0/0 --destination-port 23 -j REJECT
 
# No match, so ACCEPT
$IPT -A tcp_outbound -p TCP -s 0/0 -j ACCEPT
 
###############################################################################
# INPUT Chain
echo "Process INPUT chain ..."
 
# Allow all on localhost interface
$IPT -A INPUT -p ALL -i $LO_IFACE -j ACCEPT
 
# Drop bad packets
$IPT -A INPUT -p ALL -j bad_packets
 
# Drop them without logging.
$IPT -A INPUT -p ALL -d 224.0.0.1 -j DROP
 
# Rules for the private network (accessing gateway system itself)
$IPT -A INPUT -p ALL -i $LOCAL_IFACE -s $LOCAL_NET -j ACCEPT
$IPT -A INPUT -p ALL -i $LOCAL_IFACE -d $LOCAL_BCAST -j ACCEPT
 
# Allow DHCP client request packets inbound from internal network
$IPT -A INPUT -p UDP -i $LOCAL_IFACE --source-port 68 --destination-port 67 \
     -j ACCEPT
 
 
# Inbound Internet Packet Rules
# Accept Established Connections
$IPT -A INPUT -p ALL -i $INET_IFACE -m state --state ESTABLISHED,RELATED \
     -j ACCEPT
 
# Route the rest to the appropriate user chain
$IPT -A INPUT -p TCP -i $INET_IFACE -j tcp_inbound
$IPT -A INPUT -p UDP -i $INET_IFACE -j udp_inbound
$IPT -A INPUT -p ICMP -i $INET_IFACE -j icmp_packets
 
# broadcast protocols.
$IPT -A INPUT -m pkttype --pkt-type broadcast -j DROP
 
# Log packets that still don't match
# $IPT -A INPUT -m limit --limit 3/minute --limit-burst 3 -j LOG \
#   --log-prefix "INPUT packet died: "
###############################################################################
# FORWARD Chain
echo "Process FORWARD chain ..."
 
# Used if forwarding for a private network
# Drop bad packets
$IPT -A FORWARD -p ALL -j bad_packets
 
# Accept TCP packets we want to forward from internal sources
$IPT -A FORWARD -p tcp -i $LOCAL_IFACE -j tcp_outbound
 
# Accept UDP packets we want to forward from internal sources
$IPT -A FORWARD -p udp -i $LOCAL_IFACE -j udp_outbound
 
# If not blocked, accept any other packets from the internal interface
$IPT -A FORWARD -p ALL -i $LOCAL_IFACE -j ACCEPT
 
# Deal with responses from the internet
$IPT -A FORWARD -i $INET_IFACE -m state --state ESTABLISHED,RELATED \
     -j ACCEPT
 
# Port Forwarding is enabled, so accept forwarded traffic
$IPT -A FORWARD -p tcp -i $INET_IFACE --destination-port 80 \
     --destination 172.21.41.13 -j ACCEPT 
 
# Log packets that still don't match
# $IPT -A FORWARD -m limit --limit 3/minute --limit-burst 3 -j LOG \
#   --log-prefix "FORWARD packet died: "
###############################################################################
# OUTPUT Chain
echo "Process OUTPUT chain ..."
 
# Generally trust the firewall on output
# However, invalid icmp packets need to be dropped
# to prevent a possible exploit.
$IPT -A OUTPUT -m state -p icmp --state INVALID -j DROP
 
# Localhost
$IPT -A OUTPUT -p ALL -s $LO_IP -j ACCEPT
$IPT -A OUTPUT -p ALL -o $LO_IFACE -j ACCEPT
 
# To internal network
$IPT -A OUTPUT -p ALL -s $LOCAL_IP -j ACCEPT
$IPT -A OUTPUT -p ALL -o $LOCAL_IFACE -j ACCEPT
 
# To internet
$IPT -A OUTPUT -p ALL -o $INET_IFACE -j ACCEPT
 
# Log packets that still don't match
# $IPT -A OUTPUT -m limit --limit 3/minute --limit-burst 3 -j LOG \
#    --log-prefix "OUTPUT packet died: "
###############################################################################
# nat table
echo "Load rules for nat table ..."
 
###############################################################################
# PREROUTING chain
# Port Forwarding
$IPT -t nat -A PREROUTING -p tcp -i $INET_IFACE --destination-port 8080 \
     -j DNAT --to-destination 172.21.41.13:80
 
$IPT -t nat -A PREROUTING -p tcp -i $LOCAL_IFACE --destination-port 8080 \
     --destination $INET_ADDRESS -j DNAT --to-destination 172.21.41.13:80
 
# Redirect HTTP for a transparent proxy
$IPT -t nat -A PREROUTING -p tcp --destination-port 80 \
     -j REDIRECT --to-ports 3128
# Redirect HTTPS for a transparent proxy - commented by default
# $IPT -t nat -A PREROUTING -p tcp --destination-port 443 \
#     -j REDIRECT --to-ports 3128
###############################################################################
# POSTROUTING chain
$IPT -t nat -A POSTROUTING -o $INET_IFACE -j SNAT --to-source $INET_ADDRESS
$IPT -t nat -A POSTROUTING -o $LOCAL_IFACE -j SNAT --to-source $INET_ADDRESS
###############################################################################
# mangle table
echo "Load rules for mangle table ..."
 
 
阅读(950) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~