dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 28 of 28

Thread: How to make TOR configured application?

  1. #16
    Join Date
    Aug 2019
    Posts
    56

    Re: How to make TOR configured application?

    It's currently turned off. I don't have any antiviruses. I have VPN which is currently turned off. I'm using Windows 10. Windows Defender real time protection is also turned off.
    Last edited by prako2; August 31st, 2019 at 12:22 PM.

  2. #17
    Join Date
    Nov 2018
    Posts
    21

    Re: How to make TOR configured application?

    > SocketAddr.sin_port = htons(9050);
    > SocketAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    So, is your local TOR service running?

    https://www.computerweekly.com/tip/H...tch-open-ports
    If you can't see something like this, you're not going anywhere.
    TCP 127.0.0.1:9050 0.0.0.0:0 LISTENING

    I assume you've read this
    https://2019.www.torproject.org/docs...n#TBBSocksPort

  3. #18
    Join Date
    Aug 2019
    Posts
    56

    Re: How to make TOR configured application?

    salem_c I tried Port Listener and now I'm getting an error:
    HTML Code:
    [*] C++ Tor[*] Connecting[*] Error Authenticating
    Port Listener showed that I was connected and then disconnected. I also tried Privoxy before but it didn't work for me. I also tried this code on Windows 7, and I got the same error as default by using this code.
    Last edited by prako2; September 1st, 2019 at 05:13 AM.

  4. #19
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,816

    Re: How to make TOR configured application?

    So what is the return value from the send() and recv() just prior to the error message? If these aren't 'good' return values, what is the error code?

    I suggest you change this code so that the return value from every API is checked for 'good' and if not good, then the error code is displayed along with a message indicating which API caused the error. You'll need to check the API documentation to determine for each API what is a 'good' return value.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.2.5)

  5. #20
    Join Date
    Aug 2019
    Posts
    56

    Re: How to make TOR configured application?

    Do you mean this? std::cout << "[*] Error Authenticating " << WSAGetLastError() << std::endl;? It's[*] Error Authenticating 0, it exited with code -1.

  6. #21
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,456

    Re: How to make TOR configured application?

    Quote Originally Posted by prako2 View Post
    Quote Originally Posted by VictorN View Post
    What function does return this (-1) value?
    VictorN WSAGetLastError()...
    WSAGetLastError does NOT return code -1. It can return only the codes mentioned here.
    Victor Nijegorodov

  7. #22
    Join Date
    Aug 2019
    Posts
    56

    Re: How to make TOR configured application?

    VictorN, sorry, I mean: WSAGetLastError() returned 0, there are no such code in the list, my application exited with code -1.

  8. #23
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,816

    Re: How to make TOR configured application?

    Post your latest code here so that we can have a look at it. Don't forget to use code tags.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.2.5)

  9. #24
    Join Date
    Aug 2019
    Posts
    56

    Re: How to make TOR configured application?

    HTML Code:
    #include "pch.h"
    // g++ -lstdc++ -Wno-write-strings fetch.cpp -o fetch
    #ifdef _WIN32           // Windows
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #define MSG_NOSIGNAL 0
    #else                   // Linux + Mac
    
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    #include <string.h>
    #include <netdb.h>
    #include <errno.h>
    #include <err.h>
    
    #define SOCKET_ERROR    -1
    #define INVALID_SOCKET  -1
    
    typedef int         SOCKET;
    typedef sockaddr    SOCKADDR;
    typedef sockaddr_in SOCKADDR_IN;
    
    #define closesocket close
    
    #ifdef __APPLE__
    
    #define MSG_NOSIGNAL 0
    
    #endif
    
    #endif
    
    #include <iostream>
    
    int main()
    {
    	WSADATA wsaData;
    	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
    		std::cout << "WSAStartup failed.\n";
    		//system("pause");
    		return 1;
    	}
    	std::cout << "[*] C++ Tor" << std::endl;
    	std::cout << "[*] Connecting " << std::endl;
    
    	SOCKET      Socket;
    	SOCKADDR_IN SocketAddr;
    
    	Socket = socket(AF_INET, SOCK_STREAM, 0);
    	SocketAddr.sin_family = AF_INET;
    	SocketAddr.sin_port = htons(9050);
    	SocketAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    
    	connect(Socket, (SOCKADDR*)&SocketAddr, sizeof(SOCKADDR_IN));
    
    	char Req1[3] =
    	{
    		0x05, // SOCKS 5
    		0x01, // One Authentication Method
    		0x00  // No AUthentication
    	};
    
    	send(Socket, Req1, 3, MSG_NOSIGNAL);
    
    	char Resp1[2];
    	recv(Socket, Resp1, 2, 0);
    	if (Resp1[1] != 0x00)
    	{
    		std::cout << "[*] Error Authenticating " << WSAGetLastError() << std::endl;
    		return(-1); // Error
    	}
    
    	std::cout << "[*] Fetching... " << std::endl;
    	char* Domain = "vnexpress.net";
    	char  DomainLen = (char)strlen(Domain);
    	short Port = htons(80);
    
    	char TmpReq[4] = {
    		  0x05, // SOCKS5
    		  0x01, // CONNECT
    		  0x00, // RESERVED
    		  0x03, // DOMAIN
    	};
    
    	char* Req2 = new char[4 + 1 + DomainLen + 2];
    
    	memcpy(Req2, TmpReq, 4);                // 5, 1, 0, 3
    	memcpy(Req2 + 4, &DomainLen, 1);        // Domain Length
    	memcpy(Req2 + 5, Domain, DomainLen);    // Domain
    	memcpy(Req2 + 5 + DomainLen, &Port, 2); // Port
    
    	send(Socket, (char*)Req2, 4 + 1 + DomainLen + 2, MSG_NOSIGNAL);
    
    	delete[] Req2;
    
    	char Resp2[10];
    	recv(Socket, Resp2, 10, 0);
    	if (Resp2[1] != 0x00)
    	{
    		std::cout << "[*] Error : " << Resp2[1] << std::endl;
    		return(-1); // ERROR
    	}
    
    	std::cout << "[*] Connected " << std::endl;
    
    	// Here you can normally use send and recv
    	// Testing With a HTTP GET Request
    	std::cout << "[*] Testing with GET Request \n" << std::endl;
    	char * request = "GET / HTTP/1.1\r\nHost: vnexpress.net\r\nCache-Control: no-cache\r\n\r\n\r\n";
    	send(Socket, request, strlen(request), MSG_NOSIGNAL);
    	char RecvBuffer[2048];
    	size_t Rcved = recv(Socket, RecvBuffer, 2048, 0);
    	std::cout.write(RecvBuffer, Rcved);
    
    	std::cout << std::endl;
    
    	return(0);
    }

  10. #25
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,456

    Re: How to make TOR configured application?

    Quote Originally Posted by prako2 View Post
    Code:
    ...
    int main()
    {
    	WSADATA wsaData;
    	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
    		std::cout << "WSAStartup failed.\n";
    		//system("pause");
    		return 1;
    	}
    	std::cout << "[*] C++ Tor" << std::endl;
    	std::cout << "[*] Connecting " << std::endl;
    
    	SOCKET      Socket;
    	SOCKADDR_IN SocketAddr;
    
    	Socket = socket(AF_INET, SOCK_STREAM, 0);
    	SocketAddr.sin_family = AF_INET;
    	SocketAddr.sin_port = htons(9050);
    	SocketAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    
    	connect(Socket, (SOCKADDR*)&SocketAddr, sizeof(SOCKADDR_IN));
    
    	char Req1[3] =
    	{
    		0x05, // SOCKS 5
    		0x01, // One Authentication Method
    		0x00  // No AUthentication
    	};
    
    	send(Socket, Req1, 3, MSG_NOSIGNAL);
    
    	char Resp1[2];
    	recv(Socket, Resp1, 2, 0);
    	if (Resp1[1] != 0x00)
    	{
    		std::cout << "[*] Error Authenticating " << WSAGetLastError() << std::endl;
    		return(-1); // Error
    	}
    
    	...
    	return(0);
    }
    What does socket function return?
    What does connect function return?
    What does send function return?
    What does recv function return?
    Victor Nijegorodov

  11. #26
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,816

    Re: How to make TOR configured application?

    This is what I'm talking about regarding checking return values from every API:

    Code:
    #define _WINSOCK_DEPRECATED_NO_WARNINGS
    #define MSG_NOSIGNAL 0
    
    #include <winsock2.h>
    #include <ws2tcpip.h>
    
    #include <iostream>
    using namespace std;
    
    #pragma comment(lib, "Ws2_32.lib")
    
    int main()
    {
    	WSADATA wsaData;
    
    	if (const auto ret = WSAStartup(MAKEWORD(2, 2), &wsaData); ret != 0) {
    		cout << "WSAStartup failed - Error: " << ret << "\n";
    		return 1;
    	}
    
    	cout << "[*] C++ Tor" << endl;
    	cout << "[*] Connecting " << endl;
    
    	const SOCKET Socket = socket(AF_INET, SOCK_STREAM, 0);
    
    	SOCKADDR_IN SocketAddr;
    
    	SocketAddr.sin_family = AF_INET;
    	SocketAddr.sin_port = htons(9050);
    	SocketAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    
    	if (connect(Socket, (SOCKADDR*)& SocketAddr, sizeof(SOCKADDR_IN))) {
    		cout << "Connect error: " << WSAGetLastError() << endl;
    		return 2;
    	}
    
    	const char Req1[3] =
    	{
    		0x05, // SOCKS 5
    		0x01, // One Authentication Method
    		0x00  // No AUthentication
    	};
    
    	if (send(Socket, Req1, 3, MSG_NOSIGNAL) == SOCKET_ERROR) {
    		cout << "Send error (1): " << WSAGetLastError() << endl;
    		return 3;
    	}
    
    	char Resp1[2] = {0};
    
    	if (recv(Socket, Resp1, 2, 0) == SOCKET_ERROR) {
    		cout << "Recv error (1): " << WSAGetLastError() << endl;
    		return 4;
    	}
    
    	if (Resp1[1] != 0x00)
    	{
    		cout << "[*] Error Authenticating Resp1" << endl;
    		return 5; // Error
    	}
    
    	cout << "[*] Fetching... " << endl;
    
    	const char* const Domain = "vnexpress.net";
    	const char  DomainLen = (char)strlen(Domain);
    	const short Port = htons(80);
    
    	char TmpReq[4] = {
    		  0x05, // SOCKS5
    		  0x01, // CONNECT
    		  0x00, // RESERVED
    		  0x03, // DOMAIN
    	};
    
    	char* const Req2 = new char[4 + 1 + DomainLen + 2];
    
    	memcpy(Req2, TmpReq, 4);                // 5, 1, 0, 3
    	memcpy(Req2 + 4, &DomainLen, 1);        // Domain Length
    	memcpy(Req2 + 5, Domain, DomainLen);    // Domain
    	memcpy(Req2 + 5 + DomainLen, &Port, 2); // Port
    
    	if (send(Socket, Req2, 4 + 1 + DomainLen + 2, MSG_NOSIGNAL) == SOCKET_ERROR) {
    		cout << "Send error (2): " << WSAGetLastError() << endl;
    		return 6;
    	}
    
    	delete[] Req2;
    
    	char Resp2[10] = {0};
    
    	if (recv(Socket, Resp2, 10, 0) == SOCKET_ERROR) {
    		cout << "Recv error (2): " << WSAGetLastError() << endl;
    		return 7;
    	}
    
    	if (Resp2[1] != 0x00)
    	{
    		cout << "[*] Error Resp2: " << Resp2[1] << endl;
    		return 8; // ERROR
    	}
    
    	cout << "[*] Connected " << endl;
    
    	// Here you can normally use send and recv
    	// Testing With a HTTP GET Request
    	cout << "[*] Testing with GET Request \n" << endl;
    
    	const char* const request = "GET / HTTP/1.1\r\nHost: vnexpress.net\r\nCache-Control: no-cache\r\n\r\n\r\n";
    
    	if (send(Socket, request, strlen(request), MSG_NOSIGNAL) == SOCKET_ERROR) {
    		cout << "Send error (3): " << WSAGetLastError() << endl;
    		return 9;
    	}
    
    	char RecvBuffer[2048] = {0};
    
    	if (const int Rcved = recv(Socket, RecvBuffer, 2048, 0); Rcved != SOCKET_ERROR)
    		cout.write(RecvBuffer, Rcved);
    	else {
    		cout << "Recv error (3): " << WSAGetLastError() << endl;
    		return 10;
    	}
    
    	cout << endl;
    	return 0;
    }
    This compiles for me for MS VS 2019. Compile and run this and see what error message it generates. You will know which api is causing the issue and why.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.2.5)

  12. #27
    Join Date
    Aug 2019
    Posts
    56

    Re: How to make TOR configured application?

    Error Authenticating Resp1. It seems like Resp1 is failing.

  13. #28
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,816

    Re: How to make TOR configured application?

    Well, you now know 2 things 1) The prior API calls are not failing and 2) that the preceding recv() is actually receiving data (as Resp1 is now initialised to 0 before and then tested for not 0 after).

    Have you tried commenting out the Resp1 test so that the code gets as far as fetching?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.2.5)

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)