I found a code which should work, but it doesn't for me on Visual Studio 2017. It returns me "Error" (return(-1);) and some weird symbol after. Maybe for somebody else this code worked? https://gist.github.com/anhldbk/f62f...6959e3e0907c81
Printable View
I found a code which should work, but it doesn't for me on Visual Studio 2017. It returns me "Error" (return(-1);) and some weird symbol after. Maybe for somebody else this code worked? https://gist.github.com/anhldbk/f62f...6959e3e0907c81
I suspect your issue is here:
You can verify this by changing the error message here. If this is the issue, then you are not receiving the expected data.Code:char Resp2[10];
recv(Socket, Resp2, 10, 0);
if(Resp2[1] != 0x00)
{
std::cout << "[*] Error : " << Resp2[1] << std::endl;
return(-1); // ERROR
}
I know that issue is here, but how to fix it?
I see some problems in the code: https://imgur.com/a/t1L5B72
Well, for a start the code is not checking the return values from any function calls. For recv(), a bad return value is SOCKET_ERROR. If the return value is this, then call WSAGetLastError() to determine the error. See https://docs.microsoft.com/en-us/win...f-winsock-recv
When you call an API function, you should check the return value everytime. If this isn't an OK value, then you need to determine the error and deal with it. The documentation for each API states the return value, what is a good return and what possible errors could occur.
All the other API calls in the TOR code should also be checked as per their documentation.
PS Just because you found some code on githib, this does not mean that the code is well written - or even correct!
I'm getting https://imgur.com/a/2ftdaGy Do you know how to enable sdl warnings? Mine is enabled in C++/General/SDL checks. But it overrides anyway. As I remember I did something with command line to override SDL but I don't know how to revert it back. I'm getting warnings: Command line warning D9025: overriding '/sdl' with '/sdl-'
Error 10093 is WSANOTINITIALISED, which means WSAStartup() has not been called yet. See https://docs.microsoft.com/en-us/win...-error-codes-2
I don't see any reference to WSAStartup() in the github code? See https://docs.microsoft.com/en-us/win...ock-wsastartup
I did this: https://imgur.com/a/pQpa6pr from https://gist.github.com/anhldbk/f62f...6959e3e0907c81 and now I'm getting 10057
and if you look up that error code you get
From which API are you getting 10057? Are you now checking the return value from every API?Quote:
Socket is not connected.
A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using sendto) no address was supplied.
I suspect
is failing. What's the return value? If it's not 0, there is a problem here.Code:connect(Socket, (SOCKADDR*)&SocketAddr, sizeof(SOCKADDR_IN));
Return value is -1. I use console application. I get 10057 from console application.
Right, then you know the issue is with connect() and WSAGetLastError() will give you the reason for the error - as per post #5.
It looks like the code is using port 9050. Is this port enabled on your firewall?
VictorN WSAGetLastError(). 2kaud Is this problem has a solution?
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.
> 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
salem_c I tried Port Listener and now I'm getting an error:
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.HTML Code:[*] C++ Tor[*] Connecting[*] Error Authenticating
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.
Do you mean this? std::cout << "[*] Error Authenticating " << WSAGetLastError() << std::endl;? It's[*] Error Authenticating 0, it exited with code -1.
WSAGetLastError does NOT return code -1. It can return only the codes mentioned here.
VictorN, sorry, I mean: WSAGetLastError() returned 0, there are no such code in the list, my application exited with code -1.
Post your latest code here so that we can have a look at it. Don't forget to use code tags.
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);
}
This is what I'm talking about regarding checking return values from every API:
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.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;
}
Error Authenticating Resp1. It seems like Resp1 is failing.
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?