0

I was just working on a project, and I required a c++ code in which I could implement wininet functions to make an HTTPS request to a server. Here is the following code ...

void connect(LPCSTR hostname, LPCSTR endpoint) // hostname = "127.0.0.1", endpoint = "/"
{
    LPCWSTR userAgent = L"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:138.0) Gecko/20100101 Firefox/138.0";
    HINTERNET hInternet = InternetOpenW(userAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
    if (hInternet == NULL) {
        perror("InternetOpenW");
        exit(EXIT_FAILURE);
    }

    HINTERNET hConnect = InternetConnect(hInternet, hostname, 443, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);

    if (hConnect == NULL) {
        InternetCloseHandle(hInternet);
        perror("InternetConnect");
        exit(EXIT_FAILURE);
    }

    PCTSTR accept[] = {"application/json", NULL};

    HINTERNET hRequest = HttpOpenRequest(hConnect, "POST", endpoint, "HTTP/1.1", NULL, accept, INTERNET_FLAG_SECURE, 0);

    if (hRequest == NULL) {
        InternetCloseHandle(hInternet);
        InternetCloseHandle(hConnect);
        perror("HttpOpenRequest");
        exit(EXIT_FAILURE);
    }

    LPCSTR headers = "Content-Type: application/json\r\n";

    if (HttpAddRequestHeaders(hRequest, headers, -1L, HTTP_ADDREQ_FLAG_ADD) == false) {
        InternetCloseHandle(hInternet);
        InternetCloseHandle(hConnect);
        InternetCloseHandle(hRequest);
        perror("HttpAddRequestHeaders");
        exit(EXIT_FAILURE);
    }

    std::string data = "{\"sessionId\": \"importants.txt\", \"authToken\": \"Nothing Important Here.\"}";

    if (HttpSendRequest(hRequest, NULL, -1L, (LPVOID) data.c_str(), data.length()) == false) {
        InternetCloseHandle(hInternet);
        InternetCloseHandle(hConnect);
        InternetCloseHandle(hRequest);
        perror("HttpSendRequest");
        exit(EXIT_FAILURE);
    }

    std::string response = "";

    char buffer[4096];
    DWORD bytesRead;
    while (InternetReadFile(hRequest, buffer, sizeof(buffer) - 1, &bytesRead) && bytesRead > 0) {
        buffer[bytesRead] = '\0';
        response += buffer;
    }

    std::cout << response << std::endl;

    InternetCloseHandle(hInternet);
    InternetCloseHandle(hConnect);
    InternetCloseHandle(hRequest);
}

But when I try to compile it, it compiles well. Here's the command in case ...

x86_64-w64-mingw32-g++ harvester.cpp -o harvester.exe -l wininet -static-libstdc++ -static-libgcc

But when I try to run it using wine, it outputs something like this and exits ...

$ wine harvester.exe
MESA-INTEL: warning: Haswell Vulkan support is incomplete
MESA-INTEL: warning: Haswell Vulkan support is incomplete
HttpSendRequest: Success

My flask server at this point haven't even received a connection request, or else it would print a debug message. But if I try receiving the request using netcat, it shows something is received, but it's gibberish ...

$ nc -nvlp 443
listening on [any] 443 ...
connect to [127.0.0.1] from (UNKNOWN) [127.0.0.1] 54286
��A���3�fz�T�� J�M�_v��~�O�}�2�,̩���
�+���   �0̨��/����5���/�̪��9���3x

�@
"   127.0.0.1

#

^C

And then again the client exits with the previous message.

I can't understand where the bug is :(

2
  • What do expect netcat to print, if the data is binary rather than text? HTTPS is http over tls, so what netcat is trying to print is the TLS handshake protocol and then perhaps the the TLS record containing the encrypted HTTP request, which are byte streams with arbitrary values, not ASCII. Commented May 25 at 13:16
  • In addition to the above, Windows returns error codes via GetLastError() so perror() is not going to tell you anything. Given the TLS handshake seems to be occuring and you're connecting to localhost I'd venture the issue is that you're using a self-signed certificate. In that case, you can tell WinINet to ignore it by setting the SECURITY_FLAG_IGNORE_UNKNOWN_CA flag. Commented May 26 at 8:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.