Trouble authenticating Tor with python

微笑、不失礼 提交于 2019-12-19 07:37:30

问题


Probably doing something very silly here, but I'm having some trouble authenticating automatically through Tor.

I'm using 32 bit ubuntu 12.04 with obfuscated bridges.

This should be all the relevant code, but let me know if there's something else that would be useful in debugging this issue:

import socket
import socks
import httplib

def connectTor():
    socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050, True)
    #9050 is the Tor proxy port
    socket.socket = socks.socksocket

def newIdentity():
    socks.setdefaultproxy() #Disconnect from Tor network

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.connect(("127.0.0.1", 46594))

s.send("AUTHENTICATE\r\n")

response = s.recv(128)
#128 bytes of data for now, just to see how Tor responds

print response
if response.startswith("250"): #250 is the code for a positive response from Tor
    s.send("SIGNAL NEWNYM\r\n") #Use a new identity
s.close()

connectTor() #Just to make sure we're still connected to Tor

Whenever I run this I get the following error:

515 Authentication failed: Password did not match HashedControlPassword value from configuration. Maybe you tried a plain text password

I tried using the --hash-password option and pasting that in the place of the AUTHENTICATE string, but that just caused the script to hang. Thoughts?


回答1:


That error means that you set the HashedControlPassword option in your torrc. I would suggest option for CookieAuthentication 1 instead then using a controller library rather than doing this from scratch.

What you're trying to do here (issue a NEWNYM) is a very, very common request (1, 2) so I just added a FAQ entry for it. Here's an example using stem...

from stem import Signal
from stem.control import Controller

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


来源:https://stackoverflow.com/questions/15459170/trouble-authenticating-tor-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!