Apacheのログを観察しているとプロキシ経由での不正なアクセスが目立つ。
なのでApacheのアクセス制限を利用してプロキシ経由でのアクセスを予めシャットアウト出来ないかと考えた。
まずは普通にIPアドレス制限の方法(以下sampleは任意のディレクトリ若しくは設定ファイル名)
/etc/apahce2/sites-available/sample.conf
に
<Directory /var/www/sample>
AllowOverride ALL
</Directory>
と追加して後の.htaccessの設定を有効にする必要がある。
これでapacheを再起動させると.htaccessが反映される。
sample.conf自体に制限を記述するという方法もあるが、そうすると追加変更があった際にApache自体を再起動させないといけないらしい。
.htaccessなら保存したら即反映されるらしい。
/var/www/sample/.htaccess
ディレクトリ内のアクセス制限を設定する
order allow,deny
allow from all
deny from 192.168.0.20
このように設定すると
192.168.0.20のアクセスを拒否、他は許可する
という状態になる
参考にさせてもらったページ
ApacheによるWebサーバ構築(9):IP認証によるアクセス制限のテクニック (2/2) - @IT
これで.htaccessファイル内のdeny fromにプロキシIPアドレスを追加すれば良いわけだ。
.htaccessはディレクトリ毎に指定可能だそうだが、サブディレクトリに.htaccessが存在しないと親ディレクトリの.htaccessの内容が反映されるようだ。
匿名プロキシ紹介サイトはここを利用させてもらうことにした。
ここに掲載されているIPアドレス一覧をPythonで取得して
.htaccessに記述するようにした
記述箇所は#--PLOXY_BLOCK--以下に記述するように指定する。
order allow,deny
allow from all#--PLOXY_BLOCK--
(この間にproxyを追加)
#--PLOXY_BLOCK_END--
まずはプロキシのリストを取得するコード(悪用厳禁)
--get_proxy.py--
#!/usr/bin/python
#coding:utf-8
import urllib2
url = 'http://www.cybersyndrome.net/pla.html'
def get_html(url = ''):
try:
fp = urllib2.urlopen(url)
html = fp.read()
fp.close()
except:
html = ''
return html
def get_proxylist(html = ''):
proxylist =
try:
ar = html.split('</a></li><li>')
for s in ar:
t = s.rsplit(">",1)
proxylist.append( t[1] )
except:
pass
return proxylist
def split_proxy(proxys = ):
proxy_port =
for p in proxys:
if 1 < len(p):
proxy_port.append(p.split(':'))
return proxy_port
def get():
html = get_html(url)
proxys = get_proxylist(html) #proxy:port
proxy_port = split_proxy(proxys)
return proxy_port
if __name__=='__main__':
lst = get()
for s in lst:
print s
これをimportしてリスト取得、.htaccessの指定行にdeny from 記述にして書き込むコードを生成、cronで指定時間毎に実行するようにする
設定ファイル /usr/local/etc/proxy_blocker
で書き込む.htaccessファイルを指定してそれらのファイルを書き換えるようにした。
--/usr/local/etc/proxy_blocker--
/var/www/sample/.htaccess
/var/www/hoo/.htaccess
/var/www/hoo/bar/.htaccess
このような感じ
file_mngは以前作ったもの
--proxy_blocker.py--
#!/usr/bin/python
#coding:utf-8
import get_proxy
import file_mng
etcfile = '/usr/local/etc/proxy_blocker'
block_start = "#--PLOXY_BLOCK--"
block_end = "#--PLOXY_BLOCK_END--"
def load_proxy():
proxy_list = get_proxy.get()
return proxy_list
def get_deny_text(proxy = ):
txt = []
for p in proxy:
txt.append('deny from ' + p[0])
txt.append('#update:' + file_mng.get_daytime())
return txt
def get_htlist():
lst = file_mng.load(etcfile)
return lst
def open_htaccess(htfile):
return file_mng.load(htfile)
def insert_proxys(htaccess,deny_text):
if block_start in htaccess and block_end in htaccess:
start = htaccess.index(block_start)
end = htaccess.index(block_end)
del htaccess[ start +1 : end ] #delete old list
for s in deny_text:
htaccess.insert(start +1,s)
return htaccess
def save(htaccess_file,htaccess_new):
text = ''
for s in htaccess_new:
text += s + '\n'
file_mng.save(htaccess_file, text)
def write_htaccess(htlist, deny_text):
for ht in htlist:
htaccess = open_htaccess(ht)
if 'not Load' != htaccess:
htaccess_new = insert_proxys(htaccess, deny_text)
save(ht,htaccess_new)
if __name__=='__main__':
proxy = load_proxy()
if 1 < len(proxy):
deny_text = get_deny_text(proxy)
htlist = get_htlist()
write_htaccess(htlist, deny_text)
これを実行すれば/usr/loca/etc/proxy_blockerで指定したファイルの内容が置き換わる
(略)
後はcronを設定する
sudo crontab -e
で
0 */1 * * * python /usr/local/lib/proxy_blocker.py &
と記述。毎時0分にバックグラウンドで実行する
参考にさせていただいたサイト