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.
- How to request new TOR identity in terminal
- How to anonymize the programs from your terminal with torify
- How To: Using Tor From The Command Line
- How to change Tor identity in Python?
- Make requests using Python over Tor
- Crawling anonymously with Tor in Python
- Tor IP changing and web scraping
- A step-by-step guide how to use Tor without Authentication
- TorIpChanger - Python powered way to get a unique Tor IP
root@75f6721089f2:/# apt update
root@75f6721089f2:/# apt install tor
root@75f6721089f2:/# service tor status
* tor is not running
root@75f6721089f2:/# service tor start
* Starting tor daemon... [ OK ]
root@75f6721089f2:/# service tor status
* tor is running
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
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 ]
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.
Make sure that you have something like HashedControlPassword 16:ED2893D8EC97801C60DF4A72249CBCCD8B97B3B01A15C923DC49A0E500
(actual password hash can/will differ) in /etc/tor/torrc
.
root@75f6721089f2:/# service tor stop
* Stopping tor daemon... [fail]
root@75f6721089f2:/# pidof tor | xargs kill
root@75f6721089f2:/# echo HashedControlPassword $(tor --hash-password "my password" | tail -n 1) >> /etc/tor/torrc
root@75f6721089f2:/# tail -n 2 /etc/tor/torrc
ControlPort 9051
HashedControlPassword 16:ED2893D8EC97801C60DF4A72249CBCCD8B97B3B01A15C923DC49A0E500
root@75f6721089f2:/# service tor start
* Starting tor daemon... [ OK ]
Authentication passes with a correct password.
# NOTE Use Ctrl+C to exit.
root@75f6721089f2:/# echo -e 'AUTHENTICATE' | nc 127.0.0.1 9051
515 Authentication failed: Password did not match HashedControlPassword *or* authentication cookie.
root@75f6721089f2:/# echo -e 'AUTHENTICATE "my password"' | nc 127.0.0.1 9051
250 OK
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
root@75f6721089f2:/# echo -e 'AUTHENTICATE "my password"\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
root@75f6721089f2:/# apt install python3
root@75f6721089f2:/# apt install python3-pip
root@75f6721089f2:/# pip3 install stem
root@75f6721089f2:/# python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 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
Now that it's clear Tor is configured and works properly we can include privoxy
to the loop.
root@75f6721089f2:/# apt install privoxy
root@75f6721089f2:/# service privoxy status
* privoxy is not running
root@75f6721089f2:/# echo "forward-socks5t / 127.0.0.1:9050 ." >> /etc/privoxy/config
root@75f6721089f2:/# service privoxy start
* Starting filtering proxy server privoxy [ OK ]
root@75f6721089f2:/# torify curl http://icanhazip.com/
176.10.99.200
root@75f6721089f2:/# curl -x 127.0.0.1:8118 http://icanhazip.com/
176.10.99.200
root@75f6721089f2:/# pip3 install requests
root@75f6721089f2:/# python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>>
>>> from stem import Signal
>>> from stem.control import Controller
>>>
>>> response = requests.get('http://icanhazip.com/', proxies={'http': '127.0.0.1:8118'})
>>> response.text.strip()
'137.74.171.94'
>>>
>>> with Controller.from_port(port=9051) as controller:
... controller.authenticate(password='my password')
... controller.signal(Signal.NEWNYM)
...
>>> response = requests.get('http://icanhazip.com/', proxies={'http': '127.0.0.1:8118'})
>>> response.text.strip()
'87.118.92.43'
>>>