Skip to content

Instantly share code, notes, and snippets.

@DusanMadar
Last active April 17, 2024 18:59
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save DusanMadar/c1155329cf6a71e4346cae271a2eafd3 to your computer and use it in GitHub Desktop.
Save DusanMadar/c1155329cf6a71e4346cae271a2eafd3 to your computer and use it in GitHub Desktop.
A step-by-step guide how to use Tor without Authentication

A step-by-step guide how to use Tor without Authentication

Tested on Ubuntu 16.04 Docker container. The Dockerfile is a single line FROM ubuntu:16.04.

NOTE: stopping services didn't work for me for some reason. That's why there is pidof <service name> | xargs kill after each failed service <service name> stop to kill it.

References

Related

Steps

1. Install and check Tor status

root@75f6721089f2:/# apt update
root@75f6721089f2:/# apt install tor
root@75f6721089f2:/# service tor status
 * tor is not running

2. Start Tor and check it's running

root@75f6721089f2:/# service tor start 
 * Starting tor daemon...          [ OK ] 
root@75f6721089f2:/# service tor status
 * tor is running

3. Try to Authenticate with nc (Netcat)

It's not possible to connect as ControlPort is not set yet.

root@75f6721089f2:/# apt install netcat
root@75f6721089f2:/# echo -e 'AUTHENTICATE' | nc 127.0.0.1 9051
(UNKNOWN) [127.0.0.1] 9051 (?) : Connection refused

4. Stop/kill Tor, set ControlPort and start Tor again

root@75f6721089f2:/# service tor stop
 * Stopping tor daemon...          [fail]
root@75f6721089f2:/# pidof tor | xargs kill
root@75f6721089f2:/# service tor status
 * tor is not running
root@75f6721089f2:/# echo "ControlPort 9051" >> /etc/tor/torrc
root@75f6721089f2:/# service tor start 
 * Starting tor daemon...          [ OK ] 

5. Try to Authenticate with nc again

It's possible to connect but Authentication fails.

root@75f6721089f2:/# echo -e 'AUTHENTICATE' | nc 127.0.0.1 9051
515 Authentication failed: Wrong length on authentication cookie.

6. Stop/kill Tor, turn off CookieAuthentication and start Tor again

root@75f6721089f2:/# service tor stop
 * Stopping tor daemon...          [fail]
root@75f6721089f2:/# pidof tor | xargs kill
root@75f6721089f2:/# echo "CookieAuthentication 0" >> /etc/tor/torrc
root@75f6721089f2:/# service tor start
 * Starting tor daemon...
Jan 20 11:34:30.911 [warn] ControlPort is open, but no authentication method has been configured. 
                           This means that any program on your computer can reconfigure your Tor.
                           That's bad!
                           You should upgrade your Tor controller as soon as possible.
                    [ OK ] 

7. Try to Authenticate with nc again

Authentication passes.

# NOTE Use Ctrl+C to exit.
root@75f6721089f2:/# echo -e 'AUTHENTICATE' | nc 127.0.0.1 9051
250 OK

8. Check your public IP and currently used Tor ip

root@75f6721089f2:/# apt install curl
root@75f6721089f2:/# curl http://icanhazip.com/ 
89.196.159.79
root@75f6721089f2:/# torify curl http://icanhazip.com/   
185.220.101.17

9. Change and check Tor IP

root@75f6721089f2:/# echo -e 'AUTHENTICATE\r\nsignal NEWNYM\r\nQUIT' | nc 127.0.0.1 9051
250 OK
250 OK
250 closing connection
root@75f6721089f2:/# torify curl http://icanhazip.com/
185.220.101.6

10. Change (with Python3) and check Tor IP

root@75f6721089f2:/# apt install python3
root@75f6721089f2:/# apt install python3-pip
root@75f6721089f2:/# pip3 install stem
root@75f6721089f2:/# python3
>>> from stem import Signal
>>> from stem.control import Controller
>>> 
>>> with Controller.from_port(port=9051) as controller:
...     controller.authenticate()
...     controller.signal(Signal.NEWNYM)
... 
>>> 
root@75f6721089f2:/# torify curl http://icanhazip.com/
185.107.81.233
@stijnkliemesch
Copy link

stijnkliemesch commented Aug 16, 2019

If you know where the cookie file is, like let's say /run/tor/control.authcookie, then you can run this command:

echo -e 'AUTHENTICATE '`hexdump -ve '1/1 "%.2x"' /run/tor/control.authcookie`'\r\nsignal NEWNYM\r\nQUIT' | nc 127.0.0.1 9051

@Mikle-Bond
Copy link

As a reference, if you run tor in docker container, here's a useful command:

docker compose exec tor sh -c 'hexdump -ve '"'"'1/1 "%.2x"'"'"' /etc/tor/run/control.authcookie | xargs printf "AUTHENTICATE %s\r\n$*\r\nQUIT\r\n" | nc localhost 9051' _ signal NEWNYM

This applies to dperson/torproxy as your container's image. Lookup the placement of control.authcookie in your Dockerfile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment