You are on page 1of 7

#!

/bin/bash

###########################################################
#
#
#
#
#
###########################################################
###########################################################
#
#
# ACCEPT :
# DROP :
# REJECT :
###########################################################

###########################################################
#
#
# -A, --append
1
# -D, --delete
1
# -P, --policy

# -N, --new-chain

# -X, --delete-chain
# -F

#
# -p, --protocol

(tcpudpicmpall)
# -s, --source
IP[/mask] IPor
# -d, --destination IP[/mask] IPor
# -i, --in-interface

# -o, --out-interface

# -j, --jump

# -t, --table

# -m state --state

#
state NEWESTABLISHEDRELATEDINVALID
# !

###########################################################
#
PATH=/sbin:/usr/sbin:/bin:/usr/bin
###########################################################
# IP
#
###########################################################
#
# LOCAL_NET="xxx.xxx.xxx.xxx/xx"
#
# LIMITED_LOCAL_NET="xxx.xxx.xxx.xxx/xx"
# ZabbixIP
# ZABBIX_IP="xxx.xxx.xxx.xxx"
# IP
# ANY="0.0.0.0/0"

# ()
# ALLOW_HOSTS=(
#
"xxx.xxx.xxx.xxx"
#
"xxx.xxx.xxx.xxx"
#
"xxx.xxx.xxx.xxx"
# )
# ()
# DENY_HOSTS=(
#
"xxx.xxx.xxx.xxx"
#
"xxx.xxx.xxx.xxx"
#
"xxx.xxx.xxx.xxx"
# )
###########################################################
#
###########################################################
SSH=22
FTP=20,21
DNS=53
SMTP=25,465,587
POP3=110,995
IMAP=143,993
HTTP=80,443
IDENT=113
NTP=123
MYSQL=3306
NET_BIOS=135,137,138,139,445
DHCP=67,68
###########################################################
#
###########################################################
# iptables,
initialize()
{
iptables -F
iptables -X
iptables -Z
iptables -P
iptables -P
iptables -P
}

#
#
#
INPUT ACCEPT
OUTPUT ACCEPT
FORWARD ACCEPT

#
finailize()
{
/etc/init.d/iptables save && #
/etc/init.d/iptables restart && #
return 0
return 1
}
#
if [ "$1" == "dev" ]
then
iptables() { echo "iptables $@"; }
finailize() { echo "finailize"; }

fi
###########################################################
# iptables
###########################################################
initialize

###########################################################
#
###########################################################
iptables -P INPUT DROP # DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
###########################################################
#
###########################################################
#
# lo
iptables -A INPUT -i lo -j ACCEPT # SELF -> SELF
#
# $LOCAL_NET LAN
if [ "$LOCAL_NET" ]
then
iptables -A INPUT -p tcp -s $LOCAL_NET -j ACCEPT # LOCAL_NET -> SELF
fi
#
# $ALLOW_HOSTS
if [ "${ALLOW_HOSTS}" ]
then
for allow_host in ${ALLOW_HOSTS[@]}
do
iptables -A INPUT -p tcp -s $allow_host -j ACCEPT # allow_host > SELF
done
fi
###########################################################
# $DENY_HOSTS
###########################################################
if [ "${DENY_HOSTS}" ]
then
for deny_host in ${DENY_HOSTS[@]}
do
iptables -A INPUT -s $deny_host -m limit --limit 1/s -j LOG --lo
g-prefix "deny_host: "
iptables -A INPUT -s $deny_host -j DROP
done
fi
###########################################################
#
###########################################################
iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
###########################################################
# : Stealth Scan

###########################################################
iptables -N STEALTH_SCAN # "STEALTH_SCAN"
iptables -A STEALTH_SCAN -j LOG --log-prefix "stealth_scan_attack: "
iptables -A STEALTH_SCAN -j DROP
# "STEALTH_SCAN"
iptables -A INPUT -p tcp --tcp-flags SYN,ACK SYN,ACK -m state --state NEW -j STE
ALTH_SCAN
iptables -A INPUT -p tcp --tcp-flags ALL NONE -j STEALTH_SCAN
iptables -A INPUT -p tcp --tcp-flags SYN,FIN SYN,FIN
-j STEALTH_SCAN
iptables -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST
-j STEALTH_SCAN
iptables -A INPUT -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j STEALTH_SCAN
iptables
iptables
iptables
iptables

-A
-A
-A
-A

INPUT
INPUT
INPUT
INPUT

-p
-p
-p
-p

tcp
tcp
tcp
tcp

--tcp-flags
--tcp-flags
--tcp-flags
--tcp-flags

FIN,RST
ACK,FIN
ACK,PSH
ACK,URG

FIN,RST
FIN
PSH
URG

-j
-j
-j
-j

STEALTH_SCAN
STEALTH_SCAN
STEALTH_SCAN
STEALTH_SCAN

###########################################################
# : ,DOS
# namap -v -sF
###########################################################
iptables -A INPUT -f -j LOG --log-prefix 'fragment_packet:'
iptables -A INPUT -f -j DROP
###########################################################
# : Ping of Death
###########################################################
# 1ping10
iptables -N PING_OF_DEATH # "PING_OF_DEATH"
iptables -A PING_OF_DEATH -p icmp --icmp-type echo-request \
-m hashlimit \
--hashlimit 1/s \
--hashlimit-burst 10 \
--hashlimit-htable-expire 300000 \
--hashlimit-mode srcip \
--hashlimit-name t_PING_OF_DEATH \
-j RETURN
# ICMP
iptables -A PING_OF_DEATH -j LOG --log-prefix "ping_of_death_attack: "
iptables -A PING_OF_DEATH -j DROP
# ICMP "PING_OF_DEATH"
iptables -A INPUT -p icmp --icmp-type echo-request -j PING_OF_DEATH
###########################################################
# : SYN Flood Attack
# Syn Cookie
###########################################################
iptables -N SYN_FLOOD # "SYN_FLOOD"
iptables -A SYN_FLOOD -p tcp --syn \
-m hashlimit \
--hashlimit 200/s \
--hashlimit-burst 3 \
--hashlimit-htable-expire 300000 \
--hashlimit-mode srcip \
--hashlimit-name t_SYN_FLOOD \
-j RETURN

#
#
#
#
#
#
#
#

-m hashlimit
--hashlimit 200/s
--hashlimit-burst 3
--hashlimit-htable-expire 300000
--hashlimit-mode srcip
--hashlimit-name t_SYN_FLOOD
-j RETURN

limit hashlimit
200
3
ms

/proc/net/ipt_hashlimit

# SYN
iptables -A SYN_FLOOD -j LOG --log-prefix "syn_flood_attack: "
iptables -A SYN_FLOOD -j DROP
# SYN "SYN_FLOOD"
iptables -A INPUT -p tcp --syn -j SYN_FLOOD
###########################################################
# : HTTP DoS/DDoS Attack
###########################################################
iptables -N HTTP_DOS # "HTTP_DOS"
iptables -A HTTP_DOS -p tcp -m multiport --dports $HTTP \
-m hashlimit \
--hashlimit 1/s \
--hashlimit-burst 100 \
--hashlimit-htable-expire 300000 \
--hashlimit-mode srcip \
--hashlimit-name t_HTTP_DOS \
-j RETURN
#
#
#
#
#
#
#
#

-m hashlimit
--hashlimit 1/s
--hashlimit-burst 100
--hashlimit-htable-expire 300000
--hashlimit-mode srcip
--hashlimit-name t_HTTP_DOS
-j RETURN

limit hashlimit
1
100
ms

/proc/net/ipt_hashlimit

#
iptables -A HTTP_DOS -j LOG --log-prefix "http_dos_attack: "
iptables -A HTTP_DOS -j DROP
# HTTP "HTTP_DOS"
iptables -A INPUT -p tcp -m multiport --dports $HTTP -j HTTP_DOS
###########################################################
# : IDENT port probe
# ident
#
#
# DROP REJECT
###########################################################
iptables -A INPUT -p tcp -m multiport --dports $IDENT -j REJECT --reject-with tc
p-reset
###########################################################
# : SSH Brute Force
# SSH
# 15

# SSHDROPREJECT
# SSHON
###########################################################
# iptables -A INPUT -p tcp --syn -m multiport --dports $SSH -m recent --name ssh
_attack --set
# iptables -A INPUT -p tcp --syn -m multiport --dports $SSH -m recent --name ssh
_attack --rcheck --seconds 60 --hitcount 5 -j LOG --log-prefix "ssh_brute_force:
"
# iptables -A INPUT -p tcp --syn -m multiport --dports $SSH -m recent --name ssh
_attack --rcheck --seconds 60 --hitcount 5 -j REJECT --reject-with tcp-reset
###########################################################
# : FTP Brute Force
# FTP
# 15
# FTPDROPREJECT
# FTP
###########################################################
# iptables -A INPUT -p tcp --syn -m multiport --dports $FTP -m recent --name ftp
_attack --set
# iptables -A INPUT -p tcp --syn -m multiport --dports $FTP -m recent --name ftp
_attack --rcheck --seconds 60 --hitcount 5 -j LOG --log-prefix "ftp_brute_force:
"
# iptables -A INPUT -p tcp --syn -m multiport --dports $FTP -m recent --name ftp
_attack --rcheck --seconds 60 --hitcount 5 -j REJECT --reject-with tcp-reset
###########################################################
# ()
###########################################################
iptables -A INPUT -d 192.168.1.255 -j LOG --log-prefix "drop_broadcast: "
iptables -A INPUT -d 192.168.1.255 -j DROP
iptables -A INPUT -d 255.255.255.255 -j LOG --log-prefix "drop_broadcast: "
iptables -A INPUT -d 255.255.255.255 -j DROP
iptables -A INPUT -d 224.0.0.1
-j LOG --log-prefix "drop_broadcast: "
iptables -A INPUT -d 224.0.0.1
-j DROP
###########################################################
# (ANY)
###########################################################
# ICMP: ping
iptables -A INPUT -p icmp -j ACCEPT # ANY -> SELF
# HTTP, HTTPS
iptables -A INPUT -p tcp -m multiport --dports $HTTP -j ACCEPT # ANY -> SELF
# SSH: TRUST_HOSTS
iptables -A INPUT -p tcp -m multiport --dports $SSH -j ACCEPT # ANY -> SEL
# FTP
# iptables -A INPUT -p tcp -m multiport --dports $FTP -j ACCEPT # ANY -> SELF
# DNS
# iptables -A INPUT -p tcp -m multiport --sports $DNS -j ACCEPT # ANY -> SELF
# iptables -A INPUT -p udp -m multiport --sports $DNS -j ACCEPT # ANY -> SELF
# SMTP
# iptables -A INPUT -p tcp -m multiport --sports $SMTP -j ACCEPT # ANY -> SELF
# POP3

# iptables -A INPUT -p tcp -m multiport --sports $POP3 -j ACCEPT # ANY -> SELF
# IMAP
# iptables -A INPUT -p tcp -m multiport --sports $IMAP -j ACCEPT # ANY -> SELF
###########################################################
# ()
###########################################################
if [ "$LIMITED_LOCAL_NET" ]
then
# SSH
iptables -A INPUT -p tcp -s $LIMITED_LOCAL_NET -m multiport --dports $SS
H -j ACCEPT # LIMITED_LOCAL_NET -> SELF
# FTP
iptables -A INPUT -p tcp -s $LIMITED_LOCAL_NET -m multiport --dports $FT
P -j ACCEPT # LIMITED_LOCAL_NET -> SELF
# MySQL
iptables -A INPUT -p tcp -s $LIMITED_LOCAL_NET -m multiport --dports $MY
SQL -j ACCEPT # LIMITED_LOCAL_NET -> SELF
fi
###########################################################
#
###########################################################
if [ "$ZABBIX_IP" ]
then
# Zabbix
iptables -A INPUT -p tcp -s $ZABBIX_IP --dport 10050 -j ACCEPT # Zabbix
-> SELF
fi
###########################################################
#
#
###########################################################
iptables -A INPUT -j LOG --log-prefix "drop: "
iptables -A INPUT -j DROP
###########################################################
# SSH
# 30 iptables
# SSH Ctrl-C
###########################################################
trap 'finailize && exit 0' 2 # Ctrl-C
echo "In 30 seconds iptables will be automatically reset."
echo "Don't forget to test new SSH connection!"
echo "If there is no problem then press Ctrl-C to finish."
sleep 30
echo "rollback..."
initialize

You might also like