pythontorstem

TOR Stem not finding the cookie control authentication file


I'm using Windows 10. I have Tor Win32 Service actively running.

Here is my torrc file from C:...\Tor Browser\Browser\TorBrowser\Data\Tor\torrc

ControlPort 9051
CookieAuthentication 1
CacheDirectoryGroupReadable 1

Here is my python 3.9 code:

from stem import Signal
from stem.control import Controller

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

Running the script generates this stack trace:

Traceback (most recent call last):
  File "<input>", line 2, in <module>
  File "C:\Users\Shaun\PycharmProjects\scm-hunter-analysis\venv\lib\site-packages\stem\control.py", line 1112, in authenticate
    stem.connection.authenticate(self, *args, **kwargs)
  File "C:\Users\Shaun\PycharmProjects\scm-hunter-analysis\venv\lib\site-packages\stem\connection.py", line 629, in authenticate
    raise auth_exc
  File "C:\Users\Shaun\PycharmProjects\scm-hunter-analysis\venv\lib\site-packages\stem\connection.py", line 591, in authenticate
    authenticate_safecookie(controller, cookie_path, False)
  File "C:\Users\Shaun\PycharmProjects\scm-hunter-analysis\venv\lib\site-packages\stem\connection.py", line 904, in authenticate_safecookie
    cookie_data = _read_cookie(cookie_path, True)
  File "C:\Users\Shaun\PycharmProjects\scm-hunter-analysis\venv\lib\site-packages\stem\connection.py", line 1085, in _read_cookie
    raise UnreadableCookieFile(exc_msg, cookie_path, is_safecookie)
stem.connection.UnreadableCookieFile: Authentication failed: '/run/tor/control.authcookie' doesn't exist

Why can't I authenticate using stem? The error message confuses me because I have no idea where the run directory is.

I tried setting CookieAuthentication 0, restarting the service, then resetting CookieAuthentication 1 and restarting. It generates the same error.


Solution

  • This worked for me, first generate a password (for instance welcome) and hash:

    $ tor --hash-password welcome
    Feb 14 11:31:48.321 [warn] Tor was compiled with zstd 1.4.5, but is running with zstd 1.4.4. For safety, we'll avoid using advanced zstd functionality.
    16:05B7B9E8F3D0AB3160E030928F9517EDA5348ECD1CDCE2D95F0D230016
    

    Then go to /etc/tor/torrc, uncomment HashedControlPassword setting and copy hash generated on the previous step:

    HashedControlPassword 16:05B7B9E8F3D0AB3160E030928F9517EDA5348ECD1CDCE2D95F0D230016
    

    Then restart service:

    $ sudo service tor restart
    

    Last step, pass your new password welcome in the controller authenticate method:

    from stem import Signal
    from stem.control import Controller
    
    with Controller.from_port(port = 9051) as controller:
        controller.authenticate("welcome")
        controller.signal(Signal.NEWNYM)
    

    then make sure it works:

    import requests
    
    proxies = {'http':  'socks5://127.0.0.1:9050',
               'https': 'socks5://127.0.0.1:9050'}
    
    print("first IP:", requests.get('https://ident.me', proxies=proxies).text)
    
    with Controller.from_port(port = 9051) as controller:
        controller.authenticate("welcome")
        controller.signal(Signal.NEWNYM)
        
    print("second IP:", requests.get('https://ident.me', proxies=proxies).text)