I'm trying to download an .exe file from my webserver for licensing purpose. For that I need to :
- Download executable file from webserver
- Receive that file in binary format ( 0x00/x00 )
- I need to verify that the download succeeded using IMAGE_DOS_HEADER->e_magic
This is not very difficult in theory and was easily done with high level language but I struggle doing it in C
I already know how to download a file ( .hmtl, .txt, ... ) from my webserver using the WinHttp library ( WinHttpOpen, WinHttpConnect, WinHttpOpenRequest, WinHttpSetOption, WinHttpSendRequest, WinHttpReceiveResponse, WinHttpQueryDataAvailable, WinHttpReadData ) without any issue.
The code I use look like that :
DWORD outBufSize = 0;
LPSTR lpOutBuffer;
DWORD bytesDownloaded = 0;
do
{
bQueryData = WinHttpQueryDataAvailable(hOpenRequest, &outBufSize);
if (bQueryData)
printf("WinHttpQueryDataAvailable Success | Bytes available : %d\n", outBufSize);
else
printf("Error using WinHttpQueryDataAvailable : %d\n", GetLastError());
lpOutBuffer = malloc(outBufSize + 1);
if (!lpOutBuffer)
{
printf("out of memory for lpOutBuffer\n");
outBufSize = 0;
free(lpOutBuffer);
}
else
{
bReadData = WinHttpReadData(hOpenRequest, (LPVOID)lpOutBuffer, outBufSize, &bytesDownloaded);
if (bReadData)
{
printf("WinHttpReadData Success | Bytes downloaded : %d\n", bytesDownloaded);
printf("0x%2x", lpOutBuffer);
}
else
printf("Error using WinHttpReadData : %d\n", GetLastError());
}
} while (outBufSize > 0);
free(lpOutBuffer);
When I try it with an executable file i get this response
WinHttpQueryDataAvailable Success | Bytes available : 3742
WinHttpReadData Success | Bytes downloaded : 3742
0x85c070
WinHttpQueryDataAvailable Success | Bytes available : 866
WinHttpReadData Success | Bytes downloaded : 866
0x8e4288
WinHttpQueryDataAvailable Success | Bytes available : 0
WinHttpReadData Success | Bytes downloaded : 0
0x8b3820
The output I would expect is something like :
0x45, 0x50, 0x00, 0x00, 0x00, ...