Home > other >  How to route TCP traffic through Tor proxy?
How to route TCP traffic through Tor proxy?

Time:08-19

I would like to create a TCP connection using python library socket. This traffic should be redirected through Tor network but socks.SOCKS5Error: 0x01: General SOCKS server failure is given.

The code below can connect to Tor proxy and gives a new Tor IP.

from stem.control import Controller
from stem import Signal
import socket
import socks

if __name__ == "__main__":
        with Controller.from_port(port=9051) as controller:
            # Creatting TOR connection
            controller.authenticate(password='password')
            controller.signal(Signal.NEWNYM)
    
            socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "192.168.1.148", 9050)
            socket.socket = socks.socksocket
            
            # Creatting socket connection
            new_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            host = '192.168.1.148'
            port = 50007
    
            new_socket.connect((host, port))
            new_socket.sendall('Hello world')
            print(new_socket.recv(1024))

This is the error given:

Traceback (most recent call last):
  File "/usr/lib/python3.10/site-packages/socks.py", line 809, in connect
    negotiate(self, dest_addr, dest_port)
  File "/usr/lib/python3.10/site-packages/socks.py", line 443, in _negotiate_SOCKS5
    self.proxy_peername, self.proxy_sockname = self._SOCKS5_request(
  File "/usr/lib/python3.10/site-packages/socks.py", line 533, in _SOCKS5_request
    raise SOCKS5Error("{:#04x}: {}".format(status, error))
socks.SOCKS5Error: 0x01: General SOCKS server failure

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pi/keylogger-project/./tor_proxy_connection.py", line 74, in <module>
    tor.__testing_socket_availability__()
  File "/home/pi/keylogger-project/./tor_proxy_connection.py", line 66, in __testing_socket_availability__
    self.socket.connect((host, port))
  File "/usr/lib/python3.10/site-packages/socks.py", line 47, in wrapper
    return function(*args, **kwargs)
  File "/usr/lib/python3.10/site-packages/socks.py", line 814, in connect
    raise GeneralProxyError("Socket error", error)
socks.GeneralProxyError: Socket error: 0x01: General SOCKS server failure

Server side is a simple nc -lvp 50007

Regards

CodePudding user response:

        socket.socket = socks.socksocket
        ...
        host = '192.168.1.148'
        ...
        new_socket.connect((host, port))

The target 192.168.1.148 is an IP address reserved for private networks and thus not reachable from the internet. But the nodes on the Tor network are on the internet and thus cannot reach the given target.

  • Related