mirror of
https://github.com/Orange-Cyberdefense/p3-loader
synced 2026-07-08 17:02:48 +00:00
72 lines
1.8 KiB
C++
72 lines
1.8 KiB
C++
#include "HTTPClient.h"
|
|
|
|
#pragma comment(lib, "wininet.lib")
|
|
|
|
HTTPClient::HTTPClient(const wchar_t* user_agent)
|
|
{
|
|
m_hInternet = InternetOpenW(user_agent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
|
|
if (!m_hInternet) {
|
|
printf("[-] InternetOpen failed with error: %d\n", GetLastError());
|
|
}
|
|
}
|
|
|
|
HTTPClient::~HTTPClient()
|
|
{
|
|
if (m_hInternet)
|
|
InternetCloseHandle(m_hInternet);
|
|
}
|
|
|
|
std::vector<uint8_t> HTTPClient::DownloadURL(const wchar_t* url)
|
|
{
|
|
std::vector<uint8_t> result;
|
|
|
|
if (!m_hInternet) {
|
|
return result;
|
|
}
|
|
|
|
HINTERNET hInternet_url;
|
|
hInternet_url = InternetOpenUrlW(m_hInternet, url, NULL, 0, 0, NULL);
|
|
if (!hInternet_url) {
|
|
printf("[-] InternetOpenUrlW failed with error: %d\n", GetLastError());
|
|
return result;
|
|
}
|
|
|
|
BOOL success;
|
|
DWORD status;
|
|
DWORD index = 0;
|
|
DWORD query_size = sizeof(status);
|
|
success = HttpQueryInfoW(hInternet_url, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &query_size, &index);
|
|
if (!success) {
|
|
printf("[-] HttpQueryInfoW status failed with error: %d\n", GetLastError());
|
|
InternetCloseHandle(hInternet_url);
|
|
return result;
|
|
}
|
|
if (query_size != sizeof(status)) {
|
|
printf("[-] HttpQueryInfoW status, retrieved too few bytes\n");
|
|
InternetCloseHandle(hInternet_url);
|
|
return result;
|
|
}
|
|
if (status != HTTP_STATUS_OK) {
|
|
printf("[-] Error HTTP Status: %d\n", status);
|
|
InternetCloseHandle(hInternet_url);
|
|
return result;
|
|
}
|
|
|
|
|
|
uint8_t tempbuf[1024];
|
|
DWORD nBytesRead = 0;
|
|
do {
|
|
nBytesRead = 0;
|
|
success = InternetReadFile(hInternet_url, tempbuf, 1024, &nBytesRead);
|
|
if (!success) {
|
|
printf("[-] InternetReadFile failed with error: %d\n", GetLastError());
|
|
InternetCloseHandle(hInternet_url);
|
|
return result;
|
|
}
|
|
result.insert(result.end(), tempbuf, tempbuf + nBytesRead);
|
|
} while (nBytesRead == 1024);
|
|
|
|
InternetCloseHandle(hInternet_url);
|
|
|
|
return result;
|
|
} |