Trouble authenticating Tor with python

2019-02-17 11:59发布

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条回答
Melony?
2楼-- · 2019-02-17 12:15

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)
查看更多
登录 后发表回答