0

I have a question, regularly we see the use of tor in socks5 for http requests, concretely if we wanted to make a client-server communication (socket) by asking the client to go by tor to send his request to the server all In C ++, what would be the approach to adapt?

Thank you for your time and your answers!

Ps: google translation, sorry.

edit : Add source code #include "h/main.h"

using namespace std;

bool TcpSockC::_Init()
{
    // SOCKET c for tor
    c = socket(AF_INET,SOCK_STREAM,0);
    if (c == INVALID_SOCKET) 
    {
        perror("Erreur main::sock()");
        CLOSE(c);
        return 0;
    }
    // sockaddr_in cs for tor
    cs.sin_family = AF_INET;
    cs.sin_port = htons(9050);
    inet_pton(AF_INET,"127.0.0.1", &(cs.sin_addr.s_addr));
    return 1;
}
bool TcpSockC::_Co()
{

    if(connect(c, (sockaddr*)&cs, sizeof(cs)) == SOCKET_ERROR)
    {
        perror("Erreur main::connect()");
        CLOSE(c);
        return 0;
    }
    return 1;
}
bool TcpSockC::Send()
{
    struct sockaddr_in destaddr; // for socket server
    destaddr.sin_addr.s_addr = inet_addr("x.x.x.x");
    int dest_port = 4148;
    char buf[256], *ptr;
    ptr = buf;
    PUT_BYTE( ptr++,5);
    PUT_BYTE( ptr++,1);
    PUT_BYTE(ptr++,0x00);
    if (send(c,buf,ptr-buf,0) != -1)
        if (recv(c,buf,2,0) != -1)
            if ( buf[0] != 5 || buf[1] == 0xFF )
            {
                cout << "error" END;
                return 0;
            }
            ptr = buf;
            PUT_BYTE(ptr++,5);
            PUT_BYTE(ptr++,1);
            PUT_BYTE(ptr++,0);
            PUT_BYTE(ptr++,1);
            memcpy( ptr, &destaddr.sin_addr.s_addr,sizeof(destaddr.sin_addr));
            ptr += sizeof(destaddr.sin_addr);
            PUT_BYTE(ptr++,dest_port>>8);
            PUT_BYTE(ptr++,dest_port&0xFF);
            send(c,buf,ptr-buf,0);
            recv(c,buf,4,0);
            cout << buf[1];
            if(buf[1] != 0x00)
            {

                cout << "error" END;
                return 0;
            }
            ptr = buf + 4;
            switch ( buf[3] ) {                         
            case 1:                                     
                recv( c, ptr, 4+2,0 );              
                break;
            case 3:                                     
                recv( c, ptr, 1 ,0);                 
                recv( c, ptr+1, *(unsigned char*)ptr + 2,0);
                break;
            case 4:                                    
                recv( c, ptr, 16+2,0 );              
                break;
            }
            printf("Succes!");

            // There we are connected to the server x.x.x.x through the proxy
            // Sample Send
            char req[] = "Hello World";
            send(c, req, sizeof(req), 0);
            CLOSE(c);
            return 1;
}
int main()
{
    TcpSockC c;
    c._Init();
    c._Co();
    c.Send();
    return 0;
}

After authentication how to send a command to the server through tor? Thanks.

Edit : Solution found updated source code, thanks Alexey Vesnin for your help!

0

Just use tor as a socks proxy with dns resolution via proxy, and it will be fine: Tor works on OSI layers 3 and 4(TCP) and it basically tunnels TCP pipes, it does not care if it's a http or other protocol inside

  • Hello, thank you for replying. Would you have code examples? – DrFrench Feb 3 '17 at 7:35
  • Just use cURL it has a very detailed tutorial and examples :) – Alexey Vesnin Feb 3 '17 at 7:56
  • Hello, thank you for replying, i prefer not to use dependency. – DrFrench Feb 3 '17 at 8:14
  • you mean the native C code? – Alexey Vesnin Feb 3 '17 at 8:14
  • Thank you replying, No, cURL asks to link libcurl.lib and I would like to avoid linking this lib. – DrFrench Feb 3 '17 at 8:29
0

Ye Gods! That is some ugly looking C++ code!

But to address the question, the SOCKS negotiation is the "command".

You connect to Tor's SOCKS port and say "Tor, connect to this destination!" and Tor replies "Yay" or "Nay". If it's "Nay", figure out what went wrong or try again, if it's "Yay" then the socket that you negotiated the SOCKS protocol with now sends to, and receives from, your requested destination.

So, where // Send cmd to server through tor? is, currently is where you'd send whatever data you wanted to send to the remote server in the first place through your existing connection.

e.g.

char cmd[] = "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n";
send(c, cmd, sizeof(cmd), 0); // send HTTP reqest
recv(c, buf, sizeof(buf), 0); // buf now contains the http response, or some of it at least

Once you've successfully negotiated the SOCKS protocol with Tor's SOCKS port you should consider the socket that you negotiated the SOCKS connection with as being like a normal socket to your requested destination.

P.S. I wrote a simple SOCKS5 implementation intended for use with Tor from C, available here. See socks.h for usage.

  • Thank you for your reply, I have been seeing your code, it is interesting, it is very well presented. Unfortunately I do not fully understand it, and I do not like to use what I do not understand. But I think using two three things your code thank you for sharing and your time! – DrFrench Feb 3 '17 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.