2008年09月07日

OpenSimサーバ構築記録8 (手作りファイアウォール編)

● OpenSimサーバがポートスキャンされてました。 わ~い。 ^^/

と、喜んでる場合ではないのですが、手作りのファイアウォールがしっかり働いている所を見るのは、
やっぱりはうれしいものです。
普通はルータでファイアウォールを構築するのが当然ですけど、
Linuxならファイアウォールのルールを一つずつ作り上げていく、という手作りの楽しみがあります。
ネットワークやTCP/IPの勉強にもなりますし。



● ポートスキャンされた記録

試験的に、スタンドアロンモードのOpenSimサーバを2ヶ月ほど公開してましたが、
8月10日の不正アクセスログの一部です。8000~10000のポート番号がスキャンされてました。

SRC=59.188.XXX.XXX   : アクセス元IPアドレス、APNICで調べたところ、59.188は香港です。
DPT=9000                    : アクセス先ポート番号、OpenSim Standaloneモードのポート番号です。

Aug 10 20:49:40 ns kernel: ## UNDEFINED_INPUT ## IN=eth0 OUT= SRC=59.188.XXX.XXX DST=221.999.999.999 LEN=48 TOS=0x00 PREC=0x00 TTL=128 ID=5039 DF PROTO=TCP SPT=3698 DPT=9000 WINDOW=65535 RES=0x00 SYN URGP=0

Aug 10 20:49:43 ns kernel: ## UNDEFINED_INPUT ## IN=eth0 OUT= SRC=59.188.XXX.XXX DST=221.999.999.999 LEN=48 TOS=0x00 PREC=0x00 TTL=128 ID=5041 DF PROTO=TCP SPT=3698 DPT=9000 WINDOW=65535 RES=0x00 SYN URGP=0

Aug 10 20:49:47 ns kernel: ## UNDEFINED_INPUT ## IN=eth0 OUT= SRC=59.188.XXX.XXX DST=221.999.999.999 LEN=48 TOS=0x00 PREC=0x00 TTL=128 ID=5044 DF PROTO=TCP SPT=3700 DPT=9000 WINDOW=65535 RES=0x00 SYN URGP=0

もちろんOpenSimを直接狙ったスキャンではないでしょうけれど、インターネットは危険がいっぱいです。
OpenSim自体にも必ず脆弱性・セキュリティホールはあるはずですから、セキュリティ対策は必要ですね~。



● Linuxカーネルのファイアウォール(Netfilter)の設定を確認

iptablesコマンドを使って、ファイアウォールの設定/確認ができます。
デフォルトで、3つのチェイン(ルールが繋がったもの)があります。

root@opensim1:~/bin# iptables -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

INPUTチェイン       :カーネル空間に入ってくるパケットのルール
OUTPUTチェイン    :カーネル空間から出て行くパケットのルール
FORWARDチェイン :LANカード複数ざしの場合、パケットルーティングのルール

デフォルトでは、すべて許可(policy ACCEPT)になってます。
最近のLinuxはインストール時にGUIで設定できますけど、きめ細かい設定は、やっぱり
手作りが必要になってきます。





● 手作りファイアウォール(/etc/init.d/myfirewall.sh) B-Shell スクリプト

#!/bin/sh

#--------------------------------------------------------------------------------------------------
# シェル変数の設定:接続元(自分のパソコン)のIPアドレスを指定してます。
# スクリプト内では、$MYPCで参照できます。
#--------------------------------------------------------------------------------------------------
MYPC=999.999.999.999


#--------------------------------------------------------------------------------------------------
# チェインのクリア:ファイアウォールの設定を全て初期化します。
#--------------------------------------------------------------------------------------------------
iptables -F             # INPUT,OUTPUT,FORWARDチェインのクリア
iptables -F -t nat   # NATテーブルのクリア
iptables -X            # ユーザ定義チェインの削除
iptables -Z            # パケットカウントの0クリア


#--------------------------------------------------------------------------------------------------
# デフォルトポリシーの設定 : 全てのパケットの送受信を拒否(DROP)します。
#--------------------------------------------------------------------------------------------------
iptables -P INPUT       DROP
iptables -P OUTPUT    DROP
iptables -P FORWARD DROP


#--------------------------------------------------------------------------------------------------
# ユーザチェイン(SYNFLOOD)の作成 : SYNFlood/DoS攻撃チェック
# 「-N」でルールのサブルーチンを作ることができます。
# 短時間に大量のSYNパケット(一番目のパケット)が送られてきたら
# ログに記録して拒否します。
#--------------------------------------------------------------------------------------------------
iptables -N SYNFLOOD
iptables -A SYNFLOOD -m limit --limit 10/s --limit-burst 20 -j RETURN
iptables -A SYNFLOOD -m limit --limit 1/s --limit-burst 10 -j LOG --log-level=1 --log-prefix '## SYNFLOOD ## '
iptables -A SYNFLOOD -j DROP


#--------------------------------------------------------------------------------------------------
# INPUT/OUTPUTで許可(ACCEPT)する共通ルール(ステートフルパケットフィルタ)
# 許可したSYNパケット(一番目のパケット)に関係するパケットは、
# RELATED,ESTABLISHEDでまとめて表現して許可します。
#--------------------------------------------------------------------------------------------------
iptables -A INPUT    -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT


#--------------------------------------------------------------------------------------------------
# INPUTチェイン1
# 自分自身(内部)からの受信「-i lo」は無条件で許可します。
# プロセス間の通信は無条件で許可します。
#--------------------------------------------------------------------------------------------------
iptables -A INPUT -i lo -j ACCEPT


#--------------------------------------------------------------------------------------------------
# INPUTチェイン2
# SYNフラグの立った第1パケット(--syn)は、SYNFLOODルールでチェックします。
# DoS(Deny of Service)攻撃と判断できたら拒否されます。
#--------------------------------------------------------------------------------------------------
iptables -A INPUT -p tcp --syn -j SYNFLOOD


#--------------------------------------------------------------------------------------------------
# INPUTチェイン3
# OpenSimサーバのメンテナンス/ファイル転送用にOpenSSH(TCP/22)を起動してます。
# 自分のパソコン($MYPC)から、(TCP/22)への接続(第一パケット --state NEW)を許可
# します。TelnetやFtpは暗号化しないのでパスワードや通信内容が丸見えです。
# なるべく使わないようにしましょう。
#--------------------------------------------------------------------------------------------------
iptables -A INPUT -i eth0 -p tcp -m state --state NEW -s $MYPC --dport 22 -j ACCEPT


#--------------------------------------------------------------------------------------------------
# INPUTチェイン (OpenSim用ポートの開放)
# スタンドアロンモード(9000)/グリッドモード(8002)どちらかを使い分けます。
# 一般開放していないので、接続元は自分のパソコン($MYPC)のみに限定。
# ちなみに、OpenSSHでポートフォーワードしているなら、上記TCP/22のみ
# 開放すればOK
#--------------------------------------------------------------------------------------------------
iptables -A INPUT -i eth0 -p tcp -m state --state NEW -s $MYPC --dport 8002 -j ACCEPT
iptables -A INPUT -i eth0 -p tcp -m state --state NEW -s $MYPC --dport 9000 -j ACCEPT
iptables -A INPUT -i eth0 -p udp                                  -s $MYPC --dport 9000 -j ACCEPT


#--------------------------------------------------------------------------------------------------
# INPUTチェイン (Ping, Traceroute用)
# ネットワークの疎通確認するときは有効にします。(普段はコメントにして拒否)
#--------------------------------------------------------------------------------------------------
#iptables -A INPUT -i eth0 -p icmp --icmp-type echo-request                  -j ACCEPT
#iptables -A INPUT -i eth0 -p icmp --icmp-type echo-reply                       -j ACCEPT
#iptables -A INPUT -i eth0 -p icmp --icmp-type destination-unreachable -j ACCEPT
#iptables -A INPUT -i eth0 -p icmp --icmp-type source-quench                -j ACCEPT
#iptables -A INPUT -i eth0 -p icmp --icmp-type time-exceeded                -j ACCEPT
#iptables -A INPUT -i eth0 -p icmp --icmp-type parameter-problem         -j ACCEPT


#--------------------------------------------------------------------------------------------------
# OUTPUTチェイン1
# 自分自身(内部)からの送信「-o lo」は無条件で許可します。
# プロセス間の通信は無条件で許可します。
#--------------------------------------------------------------------------------------------------
iptables -A OUTPUT -o lo -j ACCEPT


#--------------------------------------------------------------------------------------------------
# OUTPUTチェイン2
# インターネット上の、以下のサービスへのアクセスを許可します。
# HTTP(TCP/80)、HTTPS(TCP/443)、DNS(UDP/53)、TIME(UDP/123)
#--------------------------------------------------------------------------------------------------
iptables -A OUTPUT -o eth0 -p tcp -m state --state NEW --dport 80   -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp -m state --state NEW --dport 443 -j ACCEPT
iptables -A OUTPUT -o eth0 -p udp                                  --dport 53   -j ACCEPT
iptables -A OUTPUT -o eth0 -p udp                                  --dport 123 -j ACCEPT


#--------------------------------------------------------------------------------------------------
# Logに記録
# 上記ルールにマッチしない接続はカーネルログ(/var/log/kernel.log)に記録します。
#--------------------------------------------------------------------------------------------------
iptables -A INPUT    -j LOG --log-prefix '## UNDEFINED_INPUT ## '
iptables -A OUTPUT -j LOG --log-prefix '## UNDEFINED_OUTPUT ## '




● おしまい

まだまだ、鍛えるところはいっぱいありますけど、基本はこんなところでしょうか?
詳しい方がおられましたら、どんどん突っ込んでくださいませ~。 _O_

あ~、もうすぐ情報処理試験だ。TOEICも受けろと言われてるし、、
こんなことしてていいのか~。。><




【資料8.1】
OpenSim.iniファイル V0.5.8 -> V0.5.9差分


# diff OpenSim.ini.0.5.8 OpenSim.ini.0.5.9

28a29,32
> ; Maximum total size, and maximum size where a prim can be physical
> NonPhysicalPrimMax = 256
> PhysicalPrimMax = 10
>
45,54c49,51
<
< ; turn on experimental storage of prim inventories in the region database
<
< ; Prim inventory storage is now turned on by default
< ; However, this option remains in case it needs to be disabled
< ; More details of the current state of this can be found at
< ;
< ; http://opensimulator.org/wiki/OpenSim:Prim_Inventory_Persistence
< ;
< storage_prim_inventories = true
---
> ; If you want to use a different database/server for estate data, then
> ; uncomment and change this connect string. Defaults to the above if not set
> ; estate_connection_string="Data Source=localhost;Database=opensim;User ID=opensim;Password=*****;";
94a92,96
> allow_grid_gods = false
> ; This allows somne control over permissions
> ; please note that this still doesn't duplicate SL, and is not intended to
> ;region_owner_is_god = true
> ;parcel_owner_is_god = true
113,114c115,119
< ; This is the current and most stable ScriptEngine:
< script_engine = OpenSim.Region.ScriptEngine.DotNetEngine.dll
---
> ; This is the current ScriptEngine:
> script_engine = "OpenSim.Region.ScriptEngine.DotNetEngine.dll"
>
> ; This is the new XEngine
> ;script_engine = "OpenSim.Region.ScriptEngine.XEngine.dll"
117c122
< ;script_engine = OpenSim.Region.ScriptEngine.RemoteServer.dll
---
> ;script_engine = "OpenSim.Region.ScriptEngine.RemoteServer.dll"
129,130c134,135
< ; the Asset DB source. This only works for sqlite and nhibernate for now
< ; Asset Source SQLite Exampe
---
> ; the Asset DB source. This only works for sqlite, mysql, and nhibernate for now
> ; Asset Source SQLite example
132c137
< ; Asset Source NHibernate Example (DIALECT;DRIVER;CONNECTSTRING)
---
> ; Asset Source NHibernate example (DIALECT;DRIVER;CONNECTSTRING)
133a139,140
> ; Asset Source MySQL example
> ;asset_source = "Data Source=localhost;Database=opensim;User ID=opensim;Password=****;"
139c146
< ; Inventory Source NHibernate Example (DIALECT;DRIVER;CONNECTSTRING)
---
> ; Inventory Source NHibernate example (DIALECT;DRIVER;CONNECTSTRING)
140a148,149
> ; Inventory Source MySQL example
> ;inventory_source = "Data Source=localhost;Database=opensim;User ID=opensim;Password=****;"
148a158,159
> ; User Source MySQL example
> ;user_source = "Data Source=localhost;Database=opensim;User ID=opensim;Password=****;"
311a323,332
> ; ##
> ; ## Physics logging settings - logfiles are saved to *.DIF files
> ; ##
>
> ;physics_logging = true
> ;; every n simulation iterations, the physics snapshot file is updated
> ;physics_logging_interval = 50
> ;; append to existing physics logfile, or overwrite existing logfiles?
> ;physics_logging_append_existing_logfile = true
>
316a338,347
> ; the create_region XmlRpc call uses region_file_template to generate
> ; the file name of newly create regions (if they are created
> ; persistent). the parameter available are:
> ;   {0} - X location
> ;   {1} - Y location
> ;   {2} - region UUID
> ;   {3} - region port
> ;   {4} - region name with " ", ":", "/" mapped to "_"
>
> region_file_template = "{0}x{1}-{2}.xml"
326a358
>
330a363
> ;enabled = true ; you need to set this otherwise it won't connect
586a620,621
> ; Use the newer LSL to CS compiler (under test)
> UseNewCompiler = true
606c641
< AllowedCompilers = lsl
---
> AllowedCompilers = lsl,vb,js,cs
620c655,697
< AllowosConsoleCommand = false
\ No newline at end of file
---
> AllowosConsoleCommand = false
>
>
> [GridInfo]
>
> ; These settings are used to return information on a get_grid_info call.
> ; Client launcher scripts and third-party clients make use of this to
> ; autoconfigure the client and to provide a nice user experience. If you
> ; want to facilitate that, you should configure the settings here according
> ; to your grid or standalone setup.
> ;
> ; See http://opensimulator.org/wiki/GridInfo
>
> ; login uri: for grid this is the user server URI
> login = http://127.0.0.1:9000/
>
> ; long grid name: the long name of your grid
> gridname = "the lost continent of hippo"
>
> ; short grid name: the short name of your grid
> gridnick = "hippogrid"
>
>
> ; login page: optional: if it exists it will be used to tell the client to use
> ;            this as splash page
> welcome = http://127.0.0.1/welcome
>
> ; helper uri: optional: if it exists if will be used to tell the client to use
> ;            this for all economy related things
> economy = http://127.0.0.1:9000/
>
> ; web page of grid: optional: page providing further information about your grid
> about = http://127.0.0.1/about/
>
> ; account creation: optional: page providing further information about obtaining
> ;               a user account on your grid
> register = http://127.0.0.1/register
>
> ; help: optional: page providing further assistance for users of your grid
> help = http://127.0.0.1/help
>
> ; password help: optional: page providing password assistance for users of your grid
> password = http://127.0.0.1/password




同じカテゴリー(OpenSim)の記事
 再掲:OpenSim構築手順 (Linux版&OSGrid) (2009-04-18 19:33)
 OpenSimサーバ構築記録10/OSX版MySQLで動かす (2008-12-07 18:25)
 OpenSimサーバ構築記録9(MacOSXコンパイル編) (2008-11-30 16:10)
 OpenSimサーバ構築記録その7(暗号化しなくちゃ編) (2008-08-03 11:52)
 OpenSimサーバ構築記録その6(ブレンダーで土地作り編) (2008-07-19 00:07)
 OpenSimサーバ構築記録その5(外部Simサーバ接続編) (2008-07-08 19:43)

Posted by ぱすてる at 11:14│Comments(0)OpenSim
上の画像に書かれている文字を入力して下さい
 
<ご注意>
書き込まれた内容は公開され、ブログの持ち主だけが削除できます。