【问题标题】:Downloading an .exe file in binary format to a buffer in C将二进制格式的.exe文件下载到C中的缓冲区
【发布时间】:2022-12-18 03:50:54
【问题描述】:

我正在尝试从我的网络服务器下载一个 .exe 文件以用于许可目的。 为此,我需要:

  1. 从网络服务器下载可执行文件
  2. 以二进制格式接收该文件 (0x00/x00)
  3. 我需要使用 IMAGE_DOS_HEADER->e_magic 验证下载是否成功

    这在理论上并不难,用高级语言很容易做到,但我在 C 中很难做到

    我已经知道如何使用 WinHttp 库(WinHttpOpen、WinHttpConnect、WinHttpOpenRequest、WinHttpSetOption、WinHttpSendRequest、WinHttpReceiveResponse、WinHttpQueryDataAvailable、WinHttpReadData)从我的网络服务器下载文件(.hmtl、.txt、...),没有任何问题。

    我使用的代码如下所示:

            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);
    

    当我尝试使用可执行文件时,我得到了这个响应

    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
    

    我期望的输出是这样的:

    0x45, 0x50, 0x00, 0x00, 0x00, ...
    

【问题讨论】:

  • 看起来您只是在打印指针lpOutBuffer 的值。要打印十六进制字节,您需要从该指针开始并打印字节,直到到达空终止符或达到大小outBufSize,以相关者为准。
  • @AndreasWenzel 啊,好点。评论已编辑,谢谢。
  • printf("0x%2x", lpOutBuffer);这一行会打印出malloc返回的指针的值,也就是内存缓冲区的地址。您可能想要打印该地址的数据而不是地址本身。因此,我建议您改为使用以下循环:for ( DWORD i = 0; i < bytesDownloaded; i++ ) printf( "0x%02x ", lpOutBuffer[i] );
  • @AndreasWenzel,它运行良好,但由于我忽略的原因,我得到了很多“0xfffffff”值。例如:0x4d 0x5a 0xffffff90 0x00。
  • @RenardoSharp:啊,对不起,那是我的错。将 printf( "0x%02x ", lpOutBuffer[i] ); 更改为 printf( "0x%02x ", (unsigned char)lpOutBuffer[i] );。这样,您将不再将负值传递给printf

标签: c


【解决方案1】:

在评论的帮助下,我们设法找到了解决方案,工作代码:

    DWORD outBufSize = 0;
    LPSTR lpOutBuffer;
    DWORD bytesDownloaded = 0;
    do
    {
        bQueryData = WinHttpQueryDataAvailable(hOpenRequest, &outBufSize);
        if (bQueryData)
            printf("WinHttpQueryDataAvailable Success | Bytes available : %d
", outBufSize);
        else
            printf("Error using WinHttpQueryDataAvailable : %d
", GetLastError());


        lpOutBuffer = malloc(outBufSize + 1);
        if (!lpOutBuffer)
        {
            printf("out of memory for lpOutBuffer
");
            outBufSize = 0;
            free(lpOutBuffer);
        }
        else
        {
            bReadData = WinHttpReadData(hOpenRequest, (LPVOID)lpOutBuffer, outBufSize, &bytesDownloaded);
            if (bReadData)
            {
                printf("WinHttpReadData Success | Bytes downloaded : %d
", bytesDownloaded);
                printf("0x%02x ", (unsigned char)lpOutBuffer[i]);
                
            }
            else
                printf("Error using WinHttpReadData : %d
", GetLastError());
        }
    } while (outBufSize > 0);
    free(lpOutBuffer);

【讨论】:

    猜你喜欢
    • 2017-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    • 2015-08-10
    • 2012-10-03
    • 1970-01-01
    • 2010-12-05
    相关资源
    最近更新 更多