Tor,Stem和Sockets - 用TOR改变身份 [英] Tor, Stem, and Sockets - Changing Identity with TOR

查看:1105
本文介绍了Tor,Stem和Sockets - 用TOR改变身份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过python运行Tor。我的目标是能够在我选择的时间切换退出或以其他方式改变我的IP。我已经遵循了几个教程并遇到了几个不同的错误。

I'm trying to run Tor through python. My goal is to be able to switch exits or otherwise alter my IP from time-to-time at a time of my choosing. I've followed several tutorials and encountered several different errors.

此代码打印我的IP地址

This code prints my IP address

import requests
r = requests.get('http://icanhazip.com/')
r.content
# returns my regular IP

我已经下载并安装了Tor浏览器(虽然它似乎是从.exe运行的)。当它运行时,我可以使用以下代码返回我的Tor IP,它似乎每10分钟左右更改一次,所以到目前为止一切似乎都在工作

I've downloaded and 'installed' the Tor browser (although it seems to run from an .exe). When it is running, I can use the following code to return my Tor IP, which seems to change every 10 minutes or so, so everything seems to be working so far

import socks
import socket
socks.set_default_proxy(socks.SOCKS5, "127.0.0.1", 9150)
socket.socket = socks.socksocket

r = requests.get('http://icanhazip.com/')
print r.content
# Returns a different IP

现在,我已找到说明这里关于如何以编程方式请求新身份,但这是事情开始出错的地方。我运行以下代码

Now, I've found instructions here on how to request a new identity programmatically, but this is where things begin to go wrong. I run the following code

from stem import Signal
from stem.control import Controller

with Controller.from_port(port = 9151) as controller:
    controller.authenticate()
    controller.signal(Signal.NEWNYM)

我在创建控制器的行上收到错误SOCKS5Error:0x01:General SOCKS server failure。我认为这可能是一个配置问题 - 说明说应该有一个配置文件称为'torrc',包含端口号,除了其他东西。

and I get the error "SOCKS5Error: 0x01: General SOCKS server failure" at the line that creates the controller. I thought this might be a configuration problem - the instructions say that there should be a config file called 'torrc' that contains the port numbers, apart from other things.

最后,在目录C:\ Users \..myname..\Desktop中\Tor Browser\Browser \TorBrowser \ Data \我找到了三个文件,一个'torrc',一个'torrc-defaults'和一个'torrc.orig.1'。我的'torrc-defaults'看起来类似于文档中显示的'torrc',我的'torrc.orig.1'是空的,而我的'torrc'有两行注释,看起来如下,之后有更多设置,但没有端口设置

Eventually, in the directory C:\Users\..myname..\Desktop\Tor Browser\Browser\TorBrowser\Data\Tor I found three files, a 'torrc', a 'torrc-defaults', and a 'torrc.orig.1'. My 'torrc-defaults' looks similar to the 'torrc' shown in the documentation, my 'torrc.orig.1' is empty, and my 'torrc' has two lines of comments that look as follows with some more settings afterwards but no port settings

# This file was generated by Tor; if you edit it, comments will not be preserved
# The old torrc file was renamed to torrc.orig.1 or similar, and Tor will ignore it
...

我试图确保'torrc-defaults'中列出的两个端口与socks语句中的端口匹配和控制器的声明进一步。当它们分别是9150和9151时,我得到上面列出的一般SOCKS服务器故障。

I've tried to make sure that the two ports listed in the 'torrc-defaults' match the ports in the socks statement at the beginning and the controller statment further on. When these are 9150 and 9151 respectively I get the general SOCKS server failure listed above.

当我尝试用错误的端口运行Controller(在这种情况下,9051,在其他帖子中被推荐但是对我来说当我调整'torrc-defaults'时导致Tor浏览器无法加载)然后我得到错误消息[Errno 10061]无法建立连接,因为目标机器主动拒绝它

When I try and run the Controller with the wrong port (in this case, 9051 which was recommended in other posts but for me leads to Tor browser failing to load when I adjust the 'torrc-defaults') then I instead get the error message "[Errno 10061] No connection could be made because the target machine actively refused it"

最后,当我在后台运行Tor浏览器但没有先运行SOCKS语句的情况下运行Controller时,我得到了很多警告文本,最后出现错误,如下部分所示:

Finally, when I run the Controller with Tor browser running in the background but without first running the SOCKS statement, I instead get a lot of warning text and finally an error, as shown in part below:

...
...
250 __OwningControllerProcess
DEBUG:stem:GETCONF __owningcontrollerprocess (runtime: 0.0010)
TRACE:stem:Sent to tor:
SIGNAL NEWNYM
TRACE:stem:Received from tor:
250 OK
TRACE:stem:Received from tor:
650 SIGNAL NEWNYM
TRACE:stem:Sent to tor:
QUIT
TRACE:stem:Received from tor:
250 closing connection
INFO:stem:Error while receiving a control message (SocketClosed): empty socket content

起初我认为这看起来非常有前途 - 我在新用户请求之前和之后都设置了这样的快速检查线

At first I thought this looked quite promising - I put in a quick check line like this both before and after the new user request

print requests.get('http://icanhazip.com/').content

打印IP,但不幸的是之前和之后都是一样的。

It prints the IP, but unfortunately it's the same both before and afterwards.

我很漂亮迷失在这里 - 任何支持将不胜感激!

I'm pretty lost here - any support would be appreciated!

谢谢。

推荐答案

获取您的哈希密码:

tor --hash-password my_password

修改配置文件:

sudo gedit /etc/tor/torrc

添加以下行

ControlPort 9051 
HashedControlPassword 16:E600ADC1B52C80BB6022A0E999A7734571A451EB6AE50FED489B72E3DF
CookieAuthentication 1

注意: - HashedControlPassword是从第一个命令生成的

Note :- The HashedControlPassword is generated from first command

请参阅此链接

我没有自己试过,因为我使用的是mac,但我希望这对配置有帮助

I haven't tried it on my own because I am using mac, but I hope this helps with configuration

这篇关于Tor,Stem和Sockets - 用TOR改变身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆