雑記 - Linuxでのアクセス制御 Diff
- Added parts are displayed like this.
- Deleted parts are displayed
like this.
!iptables
外から見えるサーバともなると、外部からのアクセスを iptables とかで制御したくなります。以下はその一例。(いろんな server の設定をチャンポンにしてるので書式が統一されていません。)
/etc/init.d/iptables stop [Ret]
# clear all
iptables -F
iptables -X
iptables -Z
# initialization: all DROP
iptables -P INPUT DROP
# local loopback
iptables -A INPUT -i lo -j ACCEPT
# trusted network
iptables -A INPUT -i eth0 -j ACCEPT -s hogehoge.example.jp
# for syn-flooding attack
iptables -N syn-flood
iptables -A INPUT -i eth0 -p tcp --syn -j syn-flood
iptables -A syn-flood -m limit --limit 1/s --limit-burst 4 -j RETURN
iptables -A syn-flood -j LOG --log-prefix "IPTABLES SYN-FLOOD:"
iptables -A syn-flood -j DROP
echo 1 > /proc/sys/net/ipv4/tcp_syncookies
# Make sure new tcp connections are syn packets
iptables -A INPUT -i eth0 -p tcp ! --syn -m state --state NEW -j LOG --log-prefix "IPTABLES SYN-FLOOD:"
iptables -A INPUT -i eth0 -p tcp ! --syn -m state --state NEW -j DROP
# icmp
iptables -A INPUT -p icmp -m state --state ESTABLISHED,RELATED -j ACCEPT
# 80 Web
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --sport 80 -j ACCEPT
# 53 DNS
iptables -A INPUT -p tcp --dport 53 -j ACCEPT
iptables -A INPUT -p tcp --sport 53 -j ACCEPT
iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A INPUT -p udp --sport 53 -j ACCEPT
# 20,21 FTP
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp --sport 21 -j ACCEPT
iptables -A INPUT -p tcp --dport 4000:4029 -j ACCEPT
iptables -A INPUT -p tcp --sport 4000:4029 -j ACCEPT
iptables -A INPUT -p tcp --sport 20 -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --dport 20 -m state --state ESTABLISHED -j ACCEPT
# 123 NTP
iptables -A INPUT -i eth0 -p udp --sport 123 -j ACCEPT
# 67 DHCP
#iptables -A INPUT -i eth0 -p udp --dport 67 -j ACCEPT
iptables -A INPUT -i eth0 -p udp --sport 67 -j ACCEPT
# 110 POP
#iptables -A INPUT -p tcp --dport 110 -j ACCEPT
iptables -A INPUT -p tcp --sport 110 -j ACCEPT
# 143 IMAP4
#iptables -A INPUT -p tcp --dport 143 -j ACCEPT
iptables -A INPUT -p tcp --sport 143 -j ACCEPT
# 25 SMTP
iptables -A INPUT -p tcp --dport 25 -j ACCEPT
iptables -A INPUT -p tcp --sport 25 -j ACCEPT
# SSH-evil chains for blocking brute force attack
iptables -N SSH
iptables -N SSH-evil
iptables -A SSH-evil -m recent --name badSSH --set -j LOG --log-prefix "IPTABLES evil SSH user: "
iptables -A SSH-evil -j REJECT
iptables -A SSH -p TCP ! --syn -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A SSH -p TCP --syn -m recent --name badSSH --rcheck --seconds 600 -j REJECT
iptables -A SSH -p TCP --syn -m recent --name sshconn --rcheck --seconds 60 --hitcount 5 -j SSH-evil
iptables -A SSH -p TCP --syn -m recent --name sshconn --set
iptables -A SSH -p TCP --syn -j ACCEPT
# 22 SSH with SSH-evil chains
iptables -A INPUT -p tcp --dport 22 -j SSH
iptables -A INPUT -p tcp --sport 22 -j ACCEPT
# 115 sftp
iptables -A INPUT -p tcp --dport 115 -j ACCEPT
iptables -A INPUT -p tcp --sport 115 -j ACCEPT
# 5680 canna
iptables -A INPUT -p tcp --sport 5680 -j ACCEPT
# drop otherwise
iptables -P INPUT DROP
/etc/init.d/iptables save [Ret]
/etc/init.d/iptables start [Ret]
!! memo
ここの記事が役立ちそう
http://www.atmarkit.co.jp/flinux/rensai/iptables207/iptables207a.html
外から見えるサーバともなると、外部からのアクセスを iptables とかで制御したくなります。以下はその一例。(いろんな server の設定をチャンポンにしてるので書式が統一されていません。)
/etc/init.d/iptables stop [Ret]
# clear all
iptables -F
iptables -X
iptables -Z
# initialization: all DROP
iptables -P INPUT DROP
# local loopback
iptables -A INPUT -i lo -j ACCEPT
# trusted network
iptables -A INPUT -i eth0 -j ACCEPT -s hogehoge.example.jp
# for syn-flooding attack
iptables -N syn-flood
iptables -A INPUT -i eth0 -p tcp --syn -j syn-flood
iptables -A syn-flood -m limit --limit 1/s --limit-burst 4 -j RETURN
iptables -A syn-flood -j LOG --log-prefix "IPTABLES SYN-FLOOD:"
iptables -A syn-flood -j DROP
echo 1 > /proc/sys/net/ipv4/tcp_syncookies
# Make sure new tcp connections are syn packets
iptables -A INPUT -i eth0 -p tcp ! --syn -m state --state NEW -j LOG --log-prefix "IPTABLES SYN-FLOOD:"
iptables -A INPUT -i eth0 -p tcp ! --syn -m state --state NEW -j DROP
# icmp
iptables -A INPUT -p icmp -m state --state ESTABLISHED,RELATED -j ACCEPT
# 80 Web
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --sport 80 -j ACCEPT
# 53 DNS
iptables -A INPUT -p tcp --dport 53 -j ACCEPT
iptables -A INPUT -p tcp --sport 53 -j ACCEPT
iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A INPUT -p udp --sport 53 -j ACCEPT
# 20,21 FTP
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp --sport 21 -j ACCEPT
iptables -A INPUT -p tcp --dport 4000:4029 -j ACCEPT
iptables -A INPUT -p tcp --sport 4000:4029 -j ACCEPT
iptables -A INPUT -p tcp --sport 20 -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --dport 20 -m state --state ESTABLISHED -j ACCEPT
# 123 NTP
iptables -A INPUT -i eth0 -p udp --sport 123 -j ACCEPT
# 67 DHCP
#iptables -A INPUT -i eth0 -p udp --dport 67 -j ACCEPT
iptables -A INPUT -i eth0 -p udp --sport 67 -j ACCEPT
# 110 POP
#iptables -A INPUT -p tcp --dport 110 -j ACCEPT
iptables -A INPUT -p tcp --sport 110 -j ACCEPT
# 143 IMAP4
#iptables -A INPUT -p tcp --dport 143 -j ACCEPT
iptables -A INPUT -p tcp --sport 143 -j ACCEPT
# 25 SMTP
iptables -A INPUT -p tcp --dport 25 -j ACCEPT
iptables -A INPUT -p tcp --sport 25 -j ACCEPT
# SSH-evil chains for blocking brute force attack
iptables -N SSH
iptables -N SSH-evil
iptables -A SSH-evil -m recent --name badSSH --set -j LOG --log-prefix "IPTABLES evil SSH user: "
iptables -A SSH-evil -j REJECT
iptables -A SSH -p TCP ! --syn -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A SSH -p TCP --syn -m recent --name badSSH --rcheck --seconds 600 -j REJECT
iptables -A SSH -p TCP --syn -m recent --name sshconn --rcheck --seconds 60 --hitcount 5 -j SSH-evil
iptables -A SSH -p TCP --syn -m recent --name sshconn --set
iptables -A SSH -p TCP --syn -j ACCEPT
# 22 SSH with SSH-evil chains
iptables -A INPUT -p tcp --dport 22 -j SSH
iptables -A INPUT -p tcp --sport 22 -j ACCEPT
# 115 sftp
iptables -A INPUT -p tcp --dport 115 -j ACCEPT
iptables -A INPUT -p tcp --sport 115 -j ACCEPT
# 5680 canna
iptables -A INPUT -p tcp --sport 5680 -j ACCEPT
# drop otherwise
iptables -P INPUT DROP
/etc/init.d/iptables save [Ret]
/etc/init.d/iptables start [Ret]
!! memo
ここの記事が役立ちそう
http://www.atmarkit.co.jp/flinux/rensai/iptables207/iptables207a.html