Featured image of post 在Kali中搭建Tor网络代理

在Kali中搭建Tor网络代理

下载了Tor Browser,他本身提供了一个代理服务,地址是127.0.0.1:9150。 通过这个代理,访问 Httpbin,会发现自己的IP在不断变化。证明代理可用。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
"""
使用Tor代理服务器更换代理IP
"""
from stem import Signal
from stem.control import Controller
import socket
import socks
import requests

# 连接到Tor的ControlPort(默认端口9151)
controller = Controller.from_port(port=9151)
controller.authenticate()  # 认证Tor控制器

# 设置SOCKS5代理,使用Tor默认的9050端口
socks.set_default_proxy(socks.SOCKS5, '127.0.0.1', 9050)
socket.socket = socks.socksocket  # 将默认socket替换为socksocket

# 循环10次,每次请求IP并更换Tor线路
for i in range(1, 11):
    response = requests.get('https://httpbin.org/ip')  # 获取当前IP
    print(response.text)  # 打印返回的IP信息
    controller.signal(Signal.NEWNYM)  # 发送信号更换Tor线路(IP)

但是这种方式是使用了浏览器,想搭建一个Tor网络代理服务,这种方式不合适,必须使用后端进程运行的方式。所以最好的是安装Tor服务了。 这里直接在Kali上面安装,记录配置如下。

安装Tor

1
sudo -i #切换到root用户 apt install tor systemctl restart tor systemctl status tor

可以看到tor已经运行起来了,但是测试发现没有用。 开启tor的日志信息

1
vi /etc/tor/torrc #我安装的是Tor 0.4.5.9的版本

打开日志记录的注释。

然后重启tor,这是可以看到tor报错的信息,应该是建立连接的时候被屏蔽了。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Tor 日志记录(警告和通知)

- **Jul 22 21:07:01.000** [warn] 20 connections have failed:
- **Jul 22 21:07:01.000** [warn] 20 connections died in state handshaking (TLS) with SSL state SSLv3/TLS write client hello in HANDSHAKE
- **Jul 22 21:07:29.000** [warn] Problem bootstrapping. Stuck at 10% (conn_done): Connected to a relay. (TLS_ERROR; TLS_ERROR; count 21; recommendation warn; host 5889A54CCD68C319F6F8ACEFF81E063E6E84B9BE at 147.135.64.217:443)
- **Jul 22 21:07:29.000** [warn] 21 connections have failed:
- **Jul 22 21:07:29.000** [warn] 21 connections died in state handshaking (TLS) with SSL state SSLv3/TLS write client hello in HANDSHAKE
- **Jul 22 21:08:12.000** [warn] Rejecting SOCKS request for anonymous connection to private address [scrubbed].
- **Jul 22 21:08:12.000** [notice] Application request when we haven’t used client functionality lately. Optimistically trying directory fetches again.
- **Jul 22 21:08:15.000** [warn] Problem bootstrapping. Stuck at 10% (conn_done): Connected to a relay. (TLS_ERROR; TLS_ERROR; count 22; recommendation warn; host C9DF64AF926E2E584E345D13BCE4A97C231A36BE at 87.118.96.154:9001)
- **Jul 22 21:08:15.000** [warn] 22 connections have failed:
- **Jul 22 21:08:15.000** [warn] 22 connections died in state handshaking (TLS) with SSL state SSLv3/TLS write client hello in HANDSHAKE
- **Jul 22 21:08:16.000** [warn] Problem bootstrapping. Stuck at 10% (conn_done): Connected to a relay. (TLS_ERROR; TLS_ERROR; count 23; recommendation warn; host 81893D8444F0C9100CCD963BD0D62BBC50121D14 at 95.216.118.16:4223)

但是Tor浏览器又可以,只有一个地方了,那就是浏览器用了桥接(网桥)方式进行的连接。那么就需要配置网桥了。Tor浏览器用的是obfs4协议的网桥。必须先安装obfsproxy

安装obfsproxy

1
apt install obfs4proxy

修改Tor配置文件

1
vim /etc/tor/torrc

增加如下配置信息

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#/etc/tor/torrc

PublishServerDescriptor 0
UseBridges 1
ClientTransportPlugin obfs4 exec /usr/bin/obfs4proxy managed
Bridge obfs4 ************ iat-mode=0
Bridge obfs4 ************ iat-mode=0
Bridge obfs4 ************ iat-mode=2
#桥接地址可以去这里领取 https://bridges.torproject.org/ 
#安装了Tor浏览器的话可以直接从Tor浏览器中复制过来

重启tor

1
systemctl restart tor tail -f /var/log/tor/notices.log

查看启动日志

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Tor 日志记录

- **Jul 22 22:07:28.000** [notice] Opened Socks listener connection (ready) on `/run/tor/socks`
- **Jul 22 22:07:28.000** [notice] Opening Control listener on `/run/tor/control`
- **Jul 22 22:07:28.000** [notice] Opened Control listener connection (ready) on `/run/tor/control`
- **Jul 22 22:07:28.000** [notice] Bootstrapped 1% (conn_pt): Connecting to pluggable transport
- **Jul 22 22:07:28.000** [notice] Bootstrapped 2% (conn_done_pt): Connected to pluggable transport
- **Jul 22 22:07:29.000** [notice] Bootstrapped 10% (conn_done): Connected to a relay
- **Jul 22 22:07:29.000** [notice] Bootstrapped 14% (handshake): Handshaking with a relay
- **Jul 22 22:07:30.000** [notice] Bootstrapped 15% (handshake_done): Handshake with a relay done
- **Jul 22 22:07:30.000** [notice] Bootstrapped 20% (onehop_create): Establishing an encrypted directory connection  
  *(我隐藏了三个网桥地址)*
- **Jul 22 22:07:32.000** [notice] Bootstrapped 30% (loading_status): Loading networkstatus consensus
- **Jul 22 22:07:36.000** [notice] I learned some more directory information, but not enough to build a circuit: We have no usable consensus.
- **Jul 22 22:07:37.000** [notice] Bootstrapped 40% (loading_keys): Loading authority key certs
- **Jul 22 22:07:37.000** [notice] The current consensus has no exit nodes. Tor can only build internal paths, such as paths to onion services.
- **Jul 22 22:07:37.000** [notice] Bootstrapped 45% (requesting_descriptors): Asking for relay descriptors
- **Jul 22 22:07:37.000** [notice] I learned some more directory information, but not enough to build a circuit: We need more microdescriptors: we have 0/6649, and can only build 0% of likely paths. (We have 100% of guards bw, 0% of midpoint bw, and 0% of end bw (no exits in consensus, using mid) = 0% of path bw.)
- **Jul 22 22:07:38.000** [notice] Bootstrapped 50% (loading_descriptors): Loading relay descriptors
- **Jul 22 22:07:38.000** [notice] The current consensus contains exit nodes. Tor can build exit and internal paths.
- **Jul 22 22:07:39.000** [notice] Bootstrapped 56% (loading_descriptors): Loading relay descriptors
- **Jul 22 22:07:40.000** [notice] Bootstrapped 62% (loading_descriptors): Loading relay descriptors
- **Jul 22 22:07:46.000** [notice] Bootstrapped 69% (loading_descriptors): Loading relay descriptors
- **Jul 22 22:07:49.000** [notice] Bootstrapped 75% (enough_dirinfo): Loaded enough directory info to build circuits
- **Jul 22 22:07:50.000** [notice] Bootstrapped 76% (ap_conn_pt): Connecting to pluggable transport to build circuits
- **Jul 22 22:07:50.000** [notice] Bootstrapped 77% (ap_conn_done_pt): Connected to pluggable transport to build circuits
- **Jul 22 22:07:50.000** [notice] Bootstrapped 85% (ap_conn_done): Connected to a relay to build circuits
- **Jul 22 22:07:51.000** [notice] Bootstrapped 89% (ap_handshake): Finishing handshake with a relay to build circuits
- **Jul 22 22:07:51.000** [notice] Bootstrapped 90% (ap_handshake_done): Handshake finished with a relay to build circuits
- **Jul 22 22:07:51.000** [notice] Bootstrapped 95% (circuit_create): Establishing a Tor circuit
- **Jul 22 22:07:53.000** [notice] Bootstrapped 100% (done): Done

可以看到没有之前那种错误信息了,这次是100% done。 测试连通性 这里直接用ProxyChain来测试了。

可以看到,没有问题,获取到了正常的IP地址。

Licensed under CC0-1.0
comments powered by Disqus
宇宙备案号:SOL-EARTH-20070001
使用 Hugo 构建
主题 StackJimmy 设计