TorをCentOS7に入れてみる

セキュリティー

■ tor のインストール

CentOSの場合

# sudo yum install epel-release
# sudo yum install tor
# tor --version
Tor version 0.2.9.17 (git-e057a19b74589fca).

■ tor 設定

/etc/tor/torrc を編集。下記をアンコメント&追記

RunAsDaemon 1
ORPort 443
DirPort 80
ExitPolicy reject *:* # no exits allowed

ORPort はTorのルーティングに使われるポートです。

検閲がある国のファイアウ ォールを突破するため、443にしておくのが良いでしょう。
DirPort はTorのディレクトリを通知するためのポートです。

同様の理由で80にします。
ExitPolicy は最も重要な項目です。

ExitPolicy reject *:*

と書くことで他人にIPを使わせるような Exitノード にならないようにします。

■ログふぃある

Log notice file /var/log/tor/notices.log

■ 設定ファイルのチェック

tor -f /etc/tor/torrc --verify-config

Dec 06 17:58:30.521 [notice] Tor 0.2.9.17 (git-e057a19b74589fca) running
on Linux with Libevent 2.0.21-stable, OpenSSL 1.0.2k-fips and Zlib 1.2.7.
Dec 06 17:58:30.521 [notice] Tor can’t help you if you use it wrong! Learn how to be safe at https://www.torproject.org/download/download#warning
Dec 06 17:58:30.521 [notice] Read configuration file “/etc/tor/torrc”.
Dec 06 17:58:30.525 [notice] Your ContactInfo config option is not set.
Please consider setting it, so we can contact you if your server is
misconfigured or something else goes wrong.
Dec 06 17:58:30.526 [notice] Based on detected system memory,
MaxMemInQueues is set to 2751 MB. You can override this by setting
MaxMemInQueues by hand.
Dec 06 17:58:30.527 [warn] You are running Tor as root. You don’t need
to, and you probably shouldn’t.

■ 起動してみる

# systemctl enable tor
Created symlink from /etc/systemd/system/multi-user.target.wants/tor.
service to /usr/lib/systemd/system/tor.service.

# systemctl start tor
# systemctl status tor

無事起動できました。

# ss -nlt
State Recv-Q Send-Q Local Address:Port Peer
Address:Port
LISTEN 0 128 *:80
*:*
LISTEN 0 128 127.0.0.1:9050
:
LISTEN 0 128 *:443
*:* 

ss -nlt

■ Firewallの許可

■ tor経由でwgetやcurlを使用する
torのインストール、起動ができたので、
tor経由でwgetやcurlを使ってWebサイトにアクセスしてみます

torはsocksプロキシとして動作しているので、
curlであれば以下のようにすることでtor経由でのアクセスが可能みたいです。

(例:curl -s –socks5-hostname 127.0.0.1:9050 URL)

下記のようにtorの再起動に応じてグローバルIPが変わっている事がわかります。

# curl -s --socks5-hostname 127.0.0.1:9050 http://inet-ip.info
27.124.124.126
# sudo systemctl restart tor
# curl -s --socks5-hostname 127.0.0.1:9050 http://inet-ip.info
185.220.101.12
# sudo systemctl restart tor
# curl -s --socks5-hostname 127.0.0.1:9050 http://inet-ip.info
197.231.221.211

curlの場合、上記のようにsocksを指定すればよいが、
既存のスクリプトなどでtor経由での接続をする場合、
「torsocks」コマンドもしくは「torify」コマンド経由で呼び出すことで、
簡単にtor経由での通信が行えるようです。

torsocks コマンド
torify コマンド

# vim test.py 

from urllib import urlopen
print(urlopen('http://httpbin.org/ip').read())

# cat ./test.py

from urllib import urlopen
print(urlopen('http://httpbin.org/ip').read())
# cat ./test.py
from urllib import urlopen
print(urlopen('http://httpbin.org/ip').read())


# torsocks python ./test.py
{
  "origin": "27.124.124.126"
}


# systemctl restart tor
# torsocks python ./test.py
{
  "origin": "185.220.101.12"
}

# systemctl restart tor
# torsocks curl http://httpbin.org/ip
{
  "origin": "197.231.221.211"
}



タイトルとURLをコピーしました