UJP - 技術情報1

Life is fun and easy!

不正IP報告数

Okan Sensor
 
メイン
ログイン
ブログ カテゴリ一覧

sshdへコンピュータ指定でアクセス制限 〜/etc/hosts.allow /etc/hosts.deny〜

sshdへコンピュータ指定でアクセス制限

〜 /etc/hosts.allow /etc/hosts.deny 〜


0.改訂履歴

  • 2005.08.31 新規作成
  • 2006.03.07 拒否リストの抽出方法の追加
  • 2006.03.15 拒否リストの追加
  • 2006.05.15 ホワイトリスト方式の追加.

1.はじめに

 このドキュメントでは,とあるサーバのセキュリティを強化する為の物語?である. 今回は,ブルートフォースアタック(力づく,DoSに近い)を仕掛けてくるサーバ達を見つけたら,どんどん登録してそれを排除していく方法である. なので,大量のサーバを管理している場合には,この手法は最適ではないのですが,無料でできるので自宅サーバ等でとりあえず設定してみるにはちょうど良いと考えられる. また,追加で信頼のあるサーバからしかアクセスできないように設定するホワイトリスト方式についてもドキュメントを追加する.

2.不正アクセスサーバを調べる(ブラックリスト方式)

  • 不正アクセスをしてこようとしているサーバを調べる.
  • secureログファイルからログイン失敗となっているマシンを調べればよい.
[root@jupiter shinnai]# grep Failed /var/log/secure.* | cut -f 13 -d' '
 | sort | uniq -c | sort --reverse |head -n20
   2042 202.61.97.nnn
   1655 218.66.59.nnn
   1490 220.150.18.nnn
    619 211.21.59.nnn
    391 210.143.128.nnn
    267 62.175.231.nnn
    128 222.106.127.nnn
    112 210.17.20.nnn
    108 207.36.180.nnn
    105 193.146.191.nnn
     98 129.81.216.nnn
     68 219.198.120.nnn
     43 203.116.189.nnn
     43 200.60.113.nnn
     19 203.101.41.nnn
     14 211.21.195.nnn
     12 202.141.157.nnn
      6 218.89.185.nnn
      5 61.145.222.nnn
      5 221.6.4.nnn
[root@jupiter shinnai]#
  • ここでは,失敗アクセス数の多いサーバのトップ20をランキングで取り出している.
  • きつめのパスワードで運用している場合は,このリストの中に自分達が接続に使っているコンピュータが入っていないか気をつける. 間違ってリストに入ってしまってこのまま手順通りに設定してしまうとアクセスできなくなるからである.
  • 来てほしくないサーバのIPアドレスがこれで分かるので,このIPアドレスからの接続を拒否する設定にする.
  • まずは,/etc/hosts.allowで許可されているコンピュータを確認する.
変更前
[root@jupiter shinnai]# cat /etc/hosts.allow
#
# hosts.allow   This file describes the names of the hosts which are
#               allowed to use the local INET services, as decided
#               by the '/usr/sbin/tcpd' server.
#
ALL:ALL

[root@jupiter shinnai]#
変更後
[root@jupiter shinnai]# cat /etc/hosts.allow
#
# hosts.allow   This file describes the names of the hosts which are
#               allowed to use the local INET services, as decided
#               by the '/usr/sbin/tcpd' server.
#
#ALL:ALL

[root@jupiter shinnai]#
  • ALL:ALLとなっており,全部許可となっている.
  • これが設定されていると,denyの内容が上書きされてしまいALL:ALLになっているので,それを閉じるようにコメントアウトにした.
  • 次に,/etc/hosts.denyファイルにアクセスしてきてほしくないコンピュータを登録していく.
変更前
[root@jupiter shinnai]# cat /etc/hosts.deny 
#
# hosts.deny    This file describes the names of the hosts which are
#               *not* allowed to use the local INET services, as decided
#               by the '/usr/sbin/tcpd' server.
#
# The portmap line is redundant, but it is left to remind you that
# the new secure portmap uses hosts.deny and hosts.allow.  In particular
# you should know that NFS uses portmap!

[root@jupiter shinnai]#  
変更後
[root@jupiter shinnai]# cat /etc/hosts.deny 
#
# hosts.deny    This file describes the names of the hosts which are
#               *not* allowed to use the local INET services, as decided
#               by the '/usr/sbin/tcpd' server.
#
# The portmap line is redundant, but it is left to remind you that
# the new secure portmap uses hosts.deny and hosts.allow.  In particular
# you should know that NFS uses portmap!

sshd: 202.61.97.nnn,218.66.59.nnn,220.150.18.nnn 211.21.59.nnn
 210.143.128.nnn 62.175.231.nn 222.106.127.nnn 112 210.17.20.nnn 
207.36.180.nnn 193.146.191.nnn 129.81.216.nnn 219.198.120.nnn 
203.116.189.nnn 200.60.113.nnn 203.101.41.nnn 211.21.195.nnn 
202.141.157.nnn 218.89.185.nnn 61.145.222.nnn 221.6.4.nnn
[root@jupiter shinnai]#  
  • これで,sshdに接続するサーバで,ここで指定されているIPアドレスのマシンからは接続できなくなる.
  • 複数のIPアドレスやホスト名を登録する場合は,区切り文字はブランクを入れる.
  • denyで設定したマシンから接続を行ってみる.
[root@jupiter shinnai]# ssh shinnai@202.61.97.nnn
ssh_exchange_identification: Connection closed by remote host
[root@jupiter shinnai]#
  • ちゃんとエラーになっている.
  • アクセスログを確認してみる.
[root@jupiter shinnai]# tail -n 1 /var/log/secure
Aug 31 20:42:33 jupiter sshd[6328]: refused connect from 202.61.97.nnn 
(202.61.97.nnn)
[root@jupiter shinnai]#
  • refused(拒絶した) connectとしてエラーログが表示されているので,登録したホストからの接続ということは分かる.
  • このようなメッセージも出た.
[root@jupiter shinnai]# tail -n 1 /var/log/secure
Aug 31 22:21:29 jupiter sshd[6993]: Did not receive identification string from 221.6.4.nnn
[root@jupiter shinnai]#
  • いろいろと調べていると見つかるもんだ.

3.LogWatchメールから抽出

  • rootユーザ向けにLogWatchから次のようなメールが来る.
 ################### LogWatch 4.3.2 (02/18/03) #################### 
       Processing Initiated: Tue Mar  7 04:02:04 2006
       Date Range Processed: yesterday
     Detail Level of Output: 0
          Logfiles for Host: artemis
 ################################################################ 

 --------------------- pam_unix Begin ------------------------ 

sshd:
   Invalid Users:
      Unknown Account: 2457 Time(s)
   Authentication Failures:
      nobody (203.234.238.206 ): 22 Time(s)
      lp (220.117.241.46 ): 1 Time(s)
      unknown (161.246.59.161 ): 92 Time(s)
      apache (203.234.238.206 ): 16 Time(s)
      mysql (203.234.238.206 ): 16 Time(s)
      unknown (203.234.238.206 ): 2289 Time(s)
      smmsp (203.234.238.206 ): 24 Time(s)
      postgres (220.117.241.46 ): 2 Time(s)
      nobody (61.185.50.54 ): 1 Time(s)
  • このアドレスを抽出して集計することで,sshの不正なアクセスを試みるサーバからの除外リストを作成することが可能になる.
  • まずは,LogWatchのメールを,LogWatchMail.txtというファイルに保存して,次のようなコマンドで集計する.
iMacG5:~/Documents/mail shinnai$ grep "Illegal user" ¥
> LogWatchMail.txt | cut -f 5 -d' ' | sort | uniq -c | ¥
> sort -r | head -n 50
6050 207.36.180.62
5332 220.202.78.138
4907 203.234.238.206
4537 220.231.20.214
3549 211.110.15.178
3508 216.98.154.106
3374 210.113.211.251
3230 202.180.175.138
2570 218.153.252.125
2517 218.53.86.99
2490 61.235.76.190
2490 218.12.214.51
2484 210.51.8.130
2425 87.233.128.164
2400 69.93.23.10
2249 83.222.30.178
2127 159.90.161.47
2066 211.97.183.6
1994 210.127.244.158
1873 158.227.68.247
1814 211.233.15.92
1679 211.96.97.37
1673 210.248.159.157
1374 207.245.0.100
1355 210.196.191.68
1250 210.202.123.149
1196 202.141.140.248
1188 211.106.243.14
1145 59.163.176.20
1137 66.90.75.98
1074 61.219.134.90
 998 211.144.100.123
 986 202.108.45.13
 959 219.239.107.134
 828 58.159.46.219
 651 67.120.133.2
 621 210.123.94.33
 617 65.68.62.82
 616 210.240.60.2
 609 221.165.150.183
 589 218.248.33.225
 585 130.15.100.227
 496 61.71.72.164
 490 210.240.26.178
 479 68.120.97.218
 473 211.200.44.247
 450 200.25.183.58
 444 80.55.79.245
 433 216.32.73.50
 411 216.106.109.137
iMacG5:~/Documents/mail shinnai$ 
  • 先頭に出ているのは不正接続回数となり,最初の50件を表示させている.
  • 以下が,うちのサーバにくる上位100のブラックリスト(笑)
207.36.180.62
220.202.78.138
203.234.238.206
220.231.20.214
211.110.15.178
216.98.154.106
210.113.211.251
202.180.175.138
218.153.252.125
218.53.86.99
61.235.76.190
218.12.214.51
210.51.8.130
87.233.128.164
69.93.23.10
83.222.30.178
159.90.161.47
211.97.183.6
210.127.244.158
158.227.68.247
211.233.15.92
211.96.97.37
210.248.159.157
207.245.0.100
210.196.191.68
210.202.123.149
202.141.140.248
211.106.243.14
59.163.176.20
66.90.75.98
61.219.134.90
211.144.100.123
202.108.45.13
219.239.107.134
58.159.46.219
67.120.133.2
210.123.94.33
65.68.62.82
210.240.60.2
221.165.150.183
218.248.33.225
130.15.100.227
61.71.72.164
210.240.26.178
68.120.97.218
211.200.44.247
200.25.183.58
80.55.79.245
216.32.73.50
216.106.109.137
210.14.28.57
202.57.191.201
161.246.59.161
210.172.165.69
221.251.37.140
210.113.10.146
60.248.204.54
203.198.161.212
149.156.13.120
211.115.81.91
221.12.111.242
211.22.4.234
217.110.207.210
62.193.241.85
220.117.241.46
221.218.198.220
221.218.195.18
219.84.168.2
157.120.144.1
218.27.100.204
222.191.243.113
220.165.4.70
59.124.127.103
210.196.66.252
203.177.108.33
210.68.188.134
209.66.220.213
203.196.159.180
82.224.162.170
216.194.14.198
202.53.73.149
61.143.38.56
61.218.113.90
61.129.57.105
218.90.165.178
212.249.33.58
203.145.174.7
84.10.170.134
213.134.208.58
211.137.77.62
202.222.28.22
218.146.114.221
202.110.131.27
211.198.234.59
211.43.222.82
81.1.244.21
66.221.32.128
202.115.131.206
218.24.139.109
61.222.4.53
  • 定期的にウォッチしてあげる必要があると思われる.
  • 2006年3月15日版の追加リスト.
210.246.161.225,72.232.17.122, 125.240.14.131,213.136.185.227,202.171.156.75,2
13.159.122.206,210.14.91.57,211.75.249.123,202.82.207.154,193.198.20.3,211.144.
44.6,210.0.217.214,202.108.158.248,125.240.170.195,82.100.17.161

4.ホワイトリスト方式

  • 前出の方法は,不正アクセスリストを元にしてアクセス拒否を行う,いわゆる「ブラックリスト方式」であるが,このリストは整備しているときりがないので,逆のアクセスを許可するサーバを登録していく「ホワイトリスト方式」について説明を行う.
  • ホワイトリストを設定するには,まずhosts.denyファイルを編集する.
[root@mars shinnai]# cat /etc/hosts.deny 

192.168.102.#
# hosts.deny    This file describes the names of the hosts which are
#               *not* allowed to use the local INET services, as decided
#               by the '/usr/sbin/tcpd' server.
#
# The portmap line is redundant, but it is left to remind you that
# the new secure portmap uses hosts.deny and hosts.allow.  In particular
# you should know that NFS uses portmap!
ALL:ALL

[root@mars shinnai]#
  • ここでは,全サーバからのアクセス拒否を行っている.
  • 次に,アクセス許可サーバをhosts.allowファイルに設定する.
[root@mars shinnai]# cat /etc/hosts.allow
#
# hosts.allow   This file describes the names of the hosts which are
#               allowed to use the local INET services  as decided
#               by the '/usr/sbin/tcpd' server.
#
sshd:192.168.102.241,192.168.102.242,192.168.102.243,192.168.102.244,210.
196.107.245,192.168.102.246,192.168.102.248,192.168.102.248,192.168.102.2
49,192.168.102.250,192.168.102.251,192.168.102.252,192.168.102.253,210.19
6.107.254
[root@mars shinnai]# 
  • まずは,sshで許可リストに入っていないマシンから接続してみる.
shinnai@jupiter:~ # ssh shinnai@192.168.102.247
ssh_exchange_identification: Connection closed by remote host
shinnai@jupiter:~ # 
  • このメッセージが表示されるまで5秒かかるので,連続アタックなどがしづらくなっている.
  • サーバ側でどのように記録されているか確認する.
[root@mars shinnai]# tail -n 1 /var/log/secure
May 15 12:06:16 mars sshd[20443]: refused connect from 192.168.102.247
 (192.168.102.247)
[root@mars shinnai]# 
  • コネクションされていないことがわかる.
  • また,このメッセージが表示されるまで数秒かかるので


広告スペース
Google