How to set up a Tor-Server (Hidden Service) as a proxy?
Question:
The goal is, being able to access the proxy anonymously, such that the host (proxy) doesn’t know, where the request came from (of course with credentials).
The client should be able to acess www.example.com
over the hosts ip, without the host knowing the clients ip.
Here’s a example request route to www.example.com
:
- How would I hookup a browser to it?
- How would I connect using Python? (something proxy-chain like?)
Note: OS doesn’t depend, programming language preferably Python
EDIT:
- The Client in my case should be able to specify:
- Headers
- request-method
- site-url
- For the request which the hidden-service makes (so basically a proxy)
Answers:
To use a Tor hidden service as a proxy, you must install Tor on the server. https://www.torproject.org/download/ has the programme.
After installing Tor, add these lines to your torrc file:
import socks
import socket
socks.set_default_proxy(socks.SOCKS5, "127.0.0.1", 9050)
socket.socket = socks.socksocket
# Use the socket module normally, and all connections will go via the proxy.
To utilise the tor hidden service with a browser, set the browser to use the Tor SOCKS proxy on localhost:9050 and use the hidden service hostname for the target address, which can be found in the file titled "hostname" in HiddenServiceDir.
The python-stem library can automate starting and stopping the tor service, establishing and deleting hidden services, and getting the hidden service’s onion address.
first you need to create a hidden service on tor from host to be able to communicate over tor network
basic flask proxy example (for more advanced proxy you can follow this code):
from stem.control import Controller
from requests import get
from flask import Flask
SITE_NAME = 'https://google.com/'
if __name__ == "__main__":
app = Flask("example")
port = 5000
host = "127.0.0.1"
hidden_svc_dir = "c:/temp/"
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def proxy(path):
return get(f'{SITE_NAME}{path}').content
controller = Controller.from_port(address="127.0.0.1", port=9151)
try:
controller.authenticate(password="")
controller.set_options([
("HiddenServiceDir", hidden_svc_dir),
("HiddenServicePort", "80 %s:%s" % (host, str(port)))
])
svc_name = open(hidden_svc_dir + "/hostname", "r").read().strip()
print "onion link: %s" % svc_name
except Exception as e:
print e
app.run()
after runing this you will get a onion link like: "somelongstringwithnumber123.onion" with that onion link you can connect to host from client over tor network
Than you need to make request over tor network from host:
import requests
session = requests.session()
session.proxies = {}
session.proxies['http'] = 'socks5h://localhost:9050'
session.proxies['https'] = 'socks5h://localhost:9050'
r = session.get("http://somelongstringwithnumber123.onion")
print(r.text)
im not gonna test that codes but i hope you understand the main idea.
The goal is, being able to access the proxy anonymously, such that the host (proxy) doesn’t know, where the request came from (of course with credentials).
The client should be able to acess www.example.com
over the hosts ip, without the host knowing the clients ip.
Here’s a example request route to www.example.com
:
- How would I hookup a browser to it?
- How would I connect using Python? (something proxy-chain like?)
Note: OS doesn’t depend, programming language preferably Python
EDIT:
- The Client in my case should be able to specify:
- Headers
- request-method
- site-url
- For the request which the hidden-service makes (so basically a proxy)
To use a Tor hidden service as a proxy, you must install Tor on the server. https://www.torproject.org/download/ has the programme.
After installing Tor, add these lines to your torrc file:
import socks
import socket
socks.set_default_proxy(socks.SOCKS5, "127.0.0.1", 9050)
socket.socket = socks.socksocket
# Use the socket module normally, and all connections will go via the proxy.
To utilise the tor hidden service with a browser, set the browser to use the Tor SOCKS proxy on localhost:9050 and use the hidden service hostname for the target address, which can be found in the file titled "hostname" in HiddenServiceDir.
The python-stem library can automate starting and stopping the tor service, establishing and deleting hidden services, and getting the hidden service’s onion address.
first you need to create a hidden service on tor from host to be able to communicate over tor network
basic flask proxy example (for more advanced proxy you can follow this code):
from stem.control import Controller
from requests import get
from flask import Flask
SITE_NAME = 'https://google.com/'
if __name__ == "__main__":
app = Flask("example")
port = 5000
host = "127.0.0.1"
hidden_svc_dir = "c:/temp/"
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def proxy(path):
return get(f'{SITE_NAME}{path}').content
controller = Controller.from_port(address="127.0.0.1", port=9151)
try:
controller.authenticate(password="")
controller.set_options([
("HiddenServiceDir", hidden_svc_dir),
("HiddenServicePort", "80 %s:%s" % (host, str(port)))
])
svc_name = open(hidden_svc_dir + "/hostname", "r").read().strip()
print "onion link: %s" % svc_name
except Exception as e:
print e
app.run()
after runing this you will get a onion link like: "somelongstringwithnumber123.onion" with that onion link you can connect to host from client over tor network
Than you need to make request over tor network from host:
import requests
session = requests.session()
session.proxies = {}
session.proxies['http'] = 'socks5h://localhost:9050'
session.proxies['https'] = 'socks5h://localhost:9050'
r = session.get("http://somelongstringwithnumber123.onion")
print(r.text)
im not gonna test that codes but i hope you understand the main idea.