LoginSignup
6
10

More than 5 years have passed since last update.

VPSサーバー借りた後のiptables設定

Last updated at Posted at 2016-06-13

前提環境

・サーバーにcentOS6がインストールされている。

サーバーにログイン

ssh hoge@your_ip_address

現時点のiptablesの設定を確認

iptalbes -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

何も設定されていない状態。

サーバーへの攻撃対策

コマンドラインに以下を打ち込んでいきます。

データを持っていないパケットの接続を破棄

iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP

SYNflood攻撃と思われる接続を破棄する。

iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP
SYNflood攻撃とは。。。

Dos攻撃の一種で、大量のSYNパケットをターゲットに対して送りつけることでサービス不能状態にする攻撃

ステルススキャンと思われる接続を破棄

iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
ステルススキャン攻撃とは。。。

ログを残すことなく相手のサーバーのポートの開放状態を確認する行為

localhostとpingの通信を許可する。

iptables -A INPUT -i lo -j ACCEPT 
iptables -A INPUT -p icmp -j ACCEPT

サーバーの用途に合わせてポートを許可する。

iptables -A INPUT -p tcp -m tcp --dport ポート番号 -j ACCEPT
プロトコル ポート番号
HTTP 80
HTTP(SSL) 443
SMTP 25
SMTP(SSL) 465
POP3 110
POP3(SSL) 995
IMAP 143
IMAP(SSL) 993
SSH 22

確立済みの通信を許可

iptables -I INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

許可した通信以外のサーバーが受診するパケットを拒否しつつ、サーバーから送信するパケットは許可する。

iptables -P INPUT DROP 
iptables -P OUTPUT ACCEPT

今まで設定してきたものを反映

service iptables save

iptablesの設定ファイルを確認

cat /etc/sysconfig/iptables
# Generated by iptables-save v1.4.7 on Mon Jun 13 22:03:24 2016
*filter
:INPUT DROP [1:32]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [16:1640]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -j DROP
-A INPUT -p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -m state --state NEW -j DROP
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,SYN,RST,PSH,ACK,URG -j DROP
-A INPUT -i lo -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
COMMIT
# Completed on Mon Jun 13 22:03:24 2016

iptalbesの設定を確認

iptables -L
Chain INPUT (policy DROP)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
DROP       tcp  --  anywhere             anywhere            tcp flags:FIN,SYN,RST,PSH,ACK,URG/NONE
DROP       tcp  --  anywhere             anywhere            tcp flags:!FIN,SYN,RST,ACK/SYN state NEW
DROP       tcp  --  anywhere             anywhere            tcp flags:FIN,SYN,RST,PSH,ACK,URG/FIN,SYN,RST,PSH,ACK,URG
ACCEPT     all  --  anywhere             anywhere
ACCEPT     icmp --  anywhere             anywhere
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:http
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:https
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ssh

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

DONE

6
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
10