⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lss_bastion_iptables.sh

📁 linux驱动实例
💻 SH
字号:
#! /bin/sh# init.d/localfw## System startup script for local packet filters on a bastion server# in a DMZ (NOT for an actual firewall)## Functionally the same as Example 3-10, but with SuSE-isms restored and# with many more comments.## Structurally based on SuSE 7.1's /etc/init.d/skeleton, by Kurt Garloff## Please remember that this script is just a model to use for developing# your own firewall rules; such scripts should never be dropped blindly# onto a system.## The following 9 lines are SuSE-specific#### BEGIN INIT INFO# Provides: localfw# Required-Start: $network $syslog# Required-Stop:  $network $syslog# Default-Start:  2 3 5# Default-Stop:   0 1 2 6# Description:    Start localfw to protect local heinie### END INIT INFO# /End SuSE-specific stuff (for now)   # Let's save typing & confusion with a couple of variables.# These are NOT SuSE-specific in any way.   IP_LOCAL=208.13.201.2IPTABLES=/usr/sbin/iptablestest -x $IPTABLES || exit 5   # The following 42 lines are SuSE-specific   # Source SuSE config#  (file containing system configuration variables, though in SuSE 8.0 this#     has been split into a number of files in /etc/rc.config.d). /etc/rc.config   # Determine the base and follow a runlevel link name.base=${0##*/}link=${base#*[SK][0-9][0-9]}   # Force execution if not called by a runlevel directory.test $link = $base && START_LOCALFW=yestest "$START_LOCALFW" = yes || exit 0   # Shell functions sourced from /etc/rc.status:#      rc_check         check and set local and overall rc status#      rc_status        check and set local and overall rc status#      rc_status -v     ditto but be verbose in local rc status#      rc_status -v -r  ditto and clear the local rc status#      rc_failed        set local and overall rc status to failed#      rc_reset         clear local rc status (overall remains)#      rc_exit          exit appropriate to overall rc status. /etc/rc.status   # First reset status of this servicerc_reset   # Return values acc. to LSB for all commands but status:# 0 - success# 1 - misc error# 2 - invalid or excess args# 3 - unimplemented feature (e.g. reload)# 4 - insufficient privilege# 5 - program not installed# 6 - program not configured# 7 - program is not running# # Note that starting an already running service, stopping# or restarting a not-running service as well as the restart# with force-reload (in case signalling is not supported) are# considered a success.   # /End SuSE-specific stuff.#  The rest of this script is non-SuSE specific   case "$1" instart)echo -n "Loading Woofgang's Packet Filters"   # SETUP -- stuff necessary for any bastion host   # Load kernel modules first#   (We like modprobe because it automatically checks for and loads any other#   modules required by the specified module.)    modprobe ip_tablesmodprobe ip_conntrack_ftp   # Flush active rules and custom tables$IPTABLES --flush$IPTABLES --delete-chain   # Set default-deny policies for all three default chains$IPTABLES -P INPUT DROP$IPTABLES -P FORWARD DROP$IPTABLES -P OUTPUT DROP   # Give free reign to the loopback interfaces, i.e. local processes may connect# to other processes' listening-ports.$IPTABLES -A INPUT  -i lo -j ACCEPT$IPTABLES -A OUTPUT -o lo -j ACCEPT   # Do some rudimentary anti-IP-spoofing drops. The rule of thumb is "drop#  any source IP address which is impossible" (per RFC 1918)## NOTE: If you use RFC 1918 address-space, comment out or edit the appropriate #  lines below!#$IPTABLES -A INPUT -s 255.0.0.0/8 -j LOG --log-prefix "Spoofed source IP"$IPTABLES -A INPUT -s 255.0.0.0/8 -j DROP$IPTABLES -A INPUT -s 0.0.0.0/8 -j LOG --log-prefix "Spoofed source IP"$IPTABLES -A INPUT -s 0.0.0.0/8 -j DROP$IPTABLES -A INPUT -s 127.0.0.0/8 -j LOG --log-prefix "Spoofed source IP"$IPTABLES -A INPUT -s 127.0.0.0/8 -j DROP$IPTABLES -A INPUT -s 192.168.0.0/16 -j LOG --log-prefix "Spoofed source IP"$IPTABLES -A INPUT -s 192.168.0.0/16 -j DROP $IPTABLES -A INPUT -s 172.16.0.0/12 -j LOG --log-prefix "Spoofed source IP"$IPTABLES -A INPUT -s 172.16.0.0/12 -j DROP$IPTABLES -A INPUT -s 10.0.0.0/8 -j LOG --log-prefix " Spoofed source IP"$IPTABLES -A INPUT -s 10.0.0.0/8 -j DROP   # NOTE: If you use RFC 1918 address-space in your internal or DMZ networks,#  comment out or edit the appropriate lines below!## The following will NOT interfere with local inter-process traffic, whose#   packets have the source IP of the local loopback interface, e.g. 127.0.0.1   $IPTABLES -A INPUT -s $IP_LOCAL -j LOG --log-prefix "Spoofed source IP"$IPTABLES -A INPUT -s $IP_LOCAL -j DROP   # Tell netfilter that all TCP sessions do indeed begin with SYN#   (There may be some RFC-non-compliant application somewhere which #    begins its transactions otherwise, but if so I've never heard of it)   $IPTABLES -A INPUT -p tcp ! --syn -m state --state NEW -j LOG --log-prefix "Stealth scan attempt?"$IPTABLES -A INPUT -p tcp ! --syn -m state --state NEW -j DROP   # Finally, the meat of our packet-filtering policy:   # INBOUND POLICY#   (Applies to packets entering our network interface from the network, #   and addressed to this host)   # Accept inbound packets that are part of previously-OK'ed sessions$IPTABLES -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT   # Accept inbound packets which initiate SSH sessions$IPTABLES -A INPUT -p tcp -j ACCEPT --dport 22 -m state --state NEW   # Accept inbound packets which initiate FTP sessions$IPTABLES -A INPUT -p tcp -j ACCEPT --dport 21 -m state --state NEW   # Accept inbound packets which initiate HTTP sessions$IPTABLES -A INPUT -p tcp -j ACCEPT --dport 80 -m state --state NEW   # Log and drop anything not accepted above#   (Obviously we want to log any packet that doesn't match any ACCEPT rule, for#    both security and troubleshooting. Note that the final "DROP" rule is #    redundant if the default policy is already DROP, but redundant security is#    usually a good thing.)#$IPTABLES -A INPUT -j LOG --log-prefix "Dropped by default (INPUT):"$IPTABLES -A INPUT -j DROP   # OUTBOUND POLICY#   (Applies to packets sent to the network interface (NOT loopback)#   from local processes)   # If it's part of an approved connection, let it out$IPTABLES -I OUTPUT 1 -m state --state RELATED,ESTABLISHED -j ACCEPT   # Allow outbound ping #   (For testing only! If someone compromises your system they may attempt    to use ping to identify other active IP addresses on the DMZ. Comment    this rule out when you don't need to use it yourself!)## $IPTABLES -A OUTPUT -p icmp -j ACCEPT --icmp-type echo-request    # Allow outbound DNS queries, e.g. to resolve IPs in logs#   (Many network applications break or radically slow down if they#   can't use DNS. Although DNS queries usually use UDP 53, they may also use TCP #   53. Although TCP 53 is normally used for zone-transfers, DNS queries with #   replies greater than 512 bytes also use TCP 53, so we'll allow both TCP and UDP #   53 here# $IPTABLES -A OUTPUT -p udp --dport 53 -m state --state NEW -j ACCEPT$IPTABLES -A OUTPUT -p tcp --dport 53 -m state --state NEW -j ACCEPT   # Log & drop anything not accepted above; if for no other reason, for troubleshooting## NOTE: you might consider setting your log-checker (e.g. Swatch) to#   sound an alarm whenever this rule fires; unexpected outbound trans-#   actions are often a sign of intruders!#$IPTABLES -A OUTPUT -j LOG --log-prefix "Dropped by default (OUTPUT):"$IPTABLES -A OUTPUT -j DROP   # Log & drop ALL incoming packets destined anywhere but here.#   (We already set the default FORWARD policy to DROP. But this is #   yet another free, reassuring redundancy, so why not throw it in?)#$IPTABLES -A FORWARD -j LOG --log-prefix "Attempted FORWARD? Dropped by default:"$IPTABLES -A FORWARD -j DROP   ;;   # Unload filters and reset default policies to ACCEPT.# FOR LAB/SETUP/BENCH USE ONLY -- else use `stop'!!# Never run this script `wide_open' if the system is reachable from # the Internet!#wide_open)echo -n "DANGER!! Unloading Woofgang's Packet Filters!!"$IPTABLES --flush$IPTABLES -P INPUT ACCEPT$IPTABLES -P FORWARD ACCEPT$IPTABLES -P OUTPUT ACCEPT;;   stop)echo -n "Portcullis rope CUT..."# Unload all fw rules, leaving default-drop policies$IPTABLES --flush;;   status)echo "Querying iptables status (via iptables --list)..."$IPTABLES --line-numbers -v --list;;   *)echo "Usage: $0 {start|stop|wide_open|status}"exit 1;;esac

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -