トップ > 興味のあること > コンピュータ > ソフトウェア

更新情報

2024年4月13日 スピードマスタープロフェッショナルを調整に
2024年4月13日 コンクエストV.H.P.回収
2024年4月13日 ランドマスター到着
2024年4月6日 ノート給油
2024年3月31日 ATH-AD900X購入
2024年3月19日 分針
2024年3月17日 グランドセイコー
2024年2月12日 Bluetoothマウス切断
2024年2月11日 ANKER SoundCore
2024年2月9日 防水温湿度計

block

2004年9月21日

自宅のサーバーに、けっこうアタックがある。 昔は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

2004年10月1日

  • 以前作ったプログラムから骨格を流用したので、 必要のないバッファリングを行っていた部分を削除した。
  • 61.48/13などという書き方がされていると 61.48/255.248.0.0と出力されていたので、 61.48.0.0/255.248.0.0と出力するようにした。
 #!/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