[an error occurred while processing this directive]
[an error occurred while processing this directive]自宅のサーバーに、けっこうアタックがある。 昔はftpdが対象だったが、 最近ではssh2でログインを試みて失敗している。 で、なぜかは知らないが中国と韓国に割り振られている IPアドレスからのことが多い。 なので、/etc/hosts.allowにこれをはじく記述を追加した。
http://cgi.apnic.net/apnic-bin/ipv4-by-country.plを使えば、 各国に割り振られたIPアドレスがすぐにわかる。 ただし、ここで得られるのはx.x.x.x/xというフォーマットのデータ。 ところが/etc/hosts.allowには x.x.x.x/x.x.x.xという形式で記述する必要がある。 これを人手でやっていたのではいやになるので、 Rubyでスクリプトを組んだ。
まず、 webブラウザで表示させたデータをコピー&ペーストでテキストファイルにする。 できたテキストファイルをこのスクリプトに喰わせると、 /etc/hosts.allowにコピーするためのテキストを標準出力に出す。
#!/usr/local/bin/ruby
# http://cgi.apnic.net/apnic-bin/ipv4-by-country.plから取得したデータで
# /etc/hosts.allow用のデータを作る
# usage: chkwd.rb src_file
#$Id: block.rb,v 1.1 2004/09/21 13:08:54 tom-a Exp tom-a $
dic = []
i = 0
begin
ARGF.each do |line|
if !(line =~ /^$/) then
dic[i] = line.scan(/[^\t\n]+/)
i += 1
end
end
dic.each do |element|
network = element[1][/[0-9.]+/]
netmask = element[1][/[0-9]+$/]
netmask2 = 0xffffffff00000000 >> netmask.to_i
d1 = (netmask2 & 0xffffffff) >> 24
d2 = (netmask2 & 0xffffff) >> 16
d3 = (netmask2 & 0xffff) >> 8
d4 = netmask2 & 0xff
printf("all : %s/%d.%d.%d.%d : deny\n",network,d1,d2,d3,d4)
end
end
#!/usr/local/bin/ruby
# http://cgi.apnic.net/apnic-bin/ipv4-by-country.plから取得したデータで
# /etc/hosts.allow用のデータを作る
# usage: chkwd.rb src_file
#$Id: block.rb,v 1.4 2004/10/01 02:43:01 tom-a Exp tom-a $
begin
ARGF.each do |line|
if !(line =~ /^$/) then
words = line.scan(/[^ \t\n]+/)
network = words[1][/[0-9.]+/]
network2 = network.scan(/[^\.]+/)
network = String.new
i = 0
while network2[i] != nil
network = network + network2[i] + "."
i += 1
end
case i
when 1
network += "0.0.0"
when 2
network += "0.0"
when 3
network += "0"
end
netmask = words[1][/[0-9]+$/]
netmask2 = 0xffffffff00000000 >> netmask.to_i
d1 = (netmask2 & 0xffffffff) >> 24
d2 = (netmask2 & 0xffffff) >> 16
d3 = (netmask2 & 0xffff) >> 8
d4 = netmask2 & 0xff
printf("all : %s/%d.%d.%d.%d : deny\n",network,d1,d2,d3,d4)
end
end
end