Last active
May 3, 2024 22:13
-
-
Save aaaddress1/5831798e3e3c6c738db470eae6168377 to your computer and use it in GitHub Desktop.
using WinHTTP to obtain binary data (MSVC)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// using WinHTTP to obtain binary data (MSVC) | |
// by [email protected] | |
#include <vector> | |
#include <stdio.h> | |
#include <windows.h> | |
#include <Winhttp.h> | |
#pragma comment(lib, "winhttp") | |
using namespace std; | |
vector<char>* httpRecv(const wchar_t url[]) { | |
vector<char>* binaryData = new vector<char>(); | |
WCHAR sz_hostName[MAX_PATH], sz_reqPath[MAX_PATH]; int port = 0; | |
// begin with domain? | |
if (!wcsstr(url, L"//")) { | |
if (swscanf(url, L"%[^:]:%d%s", sz_hostName, &port, sz_reqPath) == 3); | |
else if (swscanf(url, L"%[^/]%s", sz_hostName, sz_reqPath) == 2) | |
port = INTERNET_DEFAULT_HTTP_PORT; | |
} | |
// begin with http:// or https:// ? | |
else if (swscanf(wcsstr(url, L"//") + 2, L"%[^:]:%d%s", sz_hostName, &port, sz_reqPath) == 3); | |
else if (swscanf(wcsstr(url, L"//") + 2, L"%[^/]%s", sz_hostName, sz_reqPath) == 2) | |
port = wcsstr(url, L"https") ? INTERNET_DEFAULT_HTTPS_PORT : INTERNET_DEFAULT_HTTP_PORT; | |
else return binaryData; | |
// check the parse result :) | |
wprintf(L"[v] send request -> %s:%i [Path = %s]\n", sz_hostName, port, sz_reqPath); | |
// launch a http request. | |
HINTERNET hSession = NULL, hConnect = NULL, hRequest = NULL; | |
hSession = WinHttpOpen(L"WinHTTP Example/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0); | |
hConnect = WinHttpConnect(hSession, sz_hostName, port, 0); | |
hRequest = WinHttpOpenRequest(hConnect, L"GET", sz_reqPath, NULL, WINHTTP_NO_REFERER, NULL, NULL); | |
if (!hRequest) return binaryData; | |
if (!WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0) or !WinHttpReceiveResponse(hRequest, NULL)) | |
return binaryData; | |
// recv binary data. | |
char byteCache[4096] = { 0 }; | |
for (DWORD dwRead(sizeof(byteCache)); dwRead == sizeof(byteCache); ) { | |
if (!WinHttpReadData(hRequest, byteCache, sizeof(byteCache), &dwRead)) return binaryData; | |
for (size_t x = 0; x < dwRead; x++) binaryData->push_back(byteCache[x]); | |
} | |
// clean up. | |
if (hRequest) WinHttpCloseHandle(hRequest); | |
if (hConnect) WinHttpCloseHandle(hConnect); | |
if (hSession) WinHttpCloseHandle(hSession); | |
wprintf(L"[v] recv payload [size = %i] done.\n", binaryData->size()); | |
return binaryData; | |
} | |
int main(void) { | |
printf("[+] here's the output...\n%s\n", &(*httpRecv(L"www.example.com/robots.txt"))[0]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment