r/learnprogramming • • 6d ago

Tutorial How do I setup a Socket Client, that sends Data through Tor?

So, I know how I get a Server to being accessible trough the Tor-Browser with an onion address, but how do I write a Client, which can for example access a .onion address, or any ip over the tor network? Some Information: I'm not new to Socket programming, I already did some http Server in C and some basic Server-Client projects, but I didn't really find an answer to that and I forbid myself to use AI this moth. I hope I got the right subreddit.

Edit: Maybe important, I'm using Linux.

TL;DR: If anyone got the same question in the future: I can't post a full solution, but when you start the tor service, it listens on port 9050 as a SOCKS(5) proxy by default, so you send some handshake bytes and after that you can use it as a normal socket to you destination! How the handshake works is explained by u/jjangg96 or here --> https://medium.com/@nimit95/socks-5-a-proxy-protocol-b741d3bec66c and https://www.rfc-editor.org/info/rfc1928/

1 Upvotes

12 comments sorted by

1

u/kabekew 6d ago

Just follow the TOR specification (document is on the tor project website)

1

u/Able-Substance-8310 6d ago

Oh, that's what I searched for I think, thank you.

1

u/lo9314 6d ago

You can also have Tor run as an independent service and access the tor network through a socks proxy. Just in case you weren’t aware…

1

u/Able-Substance-8310 6d ago

That's kind of what I thought, thanks.

1

u/jjangg96 6d ago • Heartwarming

to add to the socks proxy point: tor listens on 127.0.0.1:9050 by default as a socks5 proxy. in c you connect() to that, not to the destination, and do the socks5 handshake yourself. send 05 01 00, you get 05 00 back, then send the connect request 05 01 00 03 <len> <hostname> <port as 2 bytes>.

use address type 03 (domain name) and pass the .onion name as is. if you resolve the name yourself first it fails, tor has to do the lookup. same reason curl wants socks5h:// for this, plain socks5:// resolves locally.

1

u/Able-Substance-8310 2d ago edited 2d ago

This was the most helpful reply, thanks, I was now able to program a function, which creates a tcp socket connection over tor or any SOCKS5 proxy(but I think I'm not allowed to post the code here because of the rule "no complete solution"). And sorry for the late reply.

1

u/lisa_j_870 6d ago

For C, handle partial recv() calls while parsing the CONNECT reply; TCP can split it across reads. The reply also has a variable-length bound address, so consume the whole response and check REP == 0 before sending application data.

1

u/jjangg96 4d ago

yeah good catch, i skipped that. recv can hand you one byte at a time so loop until you have the 4 byte header (ver, rep, rsv, atyp) before you look at anything.

then read the rest based on atyp. 4 bytes for ipv4, 16 for ipv6, or a length byte plus that many bytes for a domain, and then 2 for the port. easy to hardcode the ipv4 size and have it work for a while until it doesn't.

1

u/bellsimon_20 3d ago

The same goes for send(): it can write fewer bytes than requested, so loop until the whole handshake request is sent before waiting for the reply.

1

u/jjangg96 2d ago

yep, same thing. in python sock.sendall() loops for you, in c you check the return of send() and keep going from where it stopped.

and it'll look fine on localhost because the short write basically never happens there.