c - MessagePack 解包失败,并通过 Libcurl 收到 C 语言中的大型 Metasploit Framework 消息

标签 c libcurl msgpack metasploit

首先,我使用了 How do I unpack and extract data properly using msgpack-c?找出如何正确解包数据,因为 MessagePack 自己的 C API 文档不是很好。不过,http://wiki.msgpack.org/display/MSGPACK/QuickStart+for+C+Language也还可以。

我想指出的是,对于大多数对 Metasploit 的 API 调用,该代码都可以正常工作,只有较大的一个(返回的 module.exploits 大约 16KB)会失败。

就像我的上一个问题一样,我正在工作,所以无法发布实际的代码。我将尝试在下面的代码片段中准确地展示我的代码正在做什么。

// Function declaration from msgpack headers
bool msgpack_unpack_next(msgpack_unpacked* result, const char* data, size_t len, size_t* off);

// Called by curl after request
size_t unpack_result(char *ptr, size_t size, size_t nmemb, void *userdata){
    size_t real_size = size * nmemb;

    msgpack_unpacked msg;
    msgpack_unpacked_init(&msg);
    // Fails [returns false] when used on "module.exploits" return, not when
    // "module.payloads" is called nor "auth.login".
    msgpack_unpack_next(&msg, ptr, real_size, 0); 


}

msgpack_sbuffer* pack_payload(char **data, size_t size){
    // Code that actually packs payload, pretty standard with nothing special
    // Definitely works, have not had any problems with my payloads
}


int main(void)
{
    CURL *curl;
    CURLcode res;

    curl = curl_easy_init();

    char *auth_payload_array[3] = {"auth.login", "<username>", "<password>"};

    msgpack_sbuffer *auth_payload = pack_payload(auth_payload_array, 3);
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "https://<IP Address>/api/");
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, auth_payload->data);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, auth_payload->size);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);

        // Add custom content type header
        struct curl_slist *headers = NULL;
        headers = curl_slist_append(headers, "Content-Type: binary/message-pack");
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, unpack_result);

        res = curl_easy_perform(curl);
        if (res != CURLE_OK){
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                        curl_easy_strerror(res));
            return 1;
        }

        char *exploit_payload_array[2] = {"module.exploits", AUTH_TOKEN};
        msgpack_sbuffer *exploit_payload = pack_payload(exploit_payload_array,2);

        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, exploit_payload->data);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, exploit_payload->size);

        res = curl_easy_perform(curl);
        if (res != CURLE_OK){
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                        curl_easy_strerror(res));
            return 1;
        }
    }
}

我知道上面有内存泄漏,我不想输入所有正确的销毁/释放,因为这只是一个示例。我省略了如何实际抓取 AUTH_TOKEN 的部分,因为它不应该是错误,因为我在代码中此时没有问题。我还知道一些curl请求代码可以优化为函数。

重要说明是,ruby gem 在相同的 API 调用中工作得很好,但它是一个 C 扩展,不使用标准 msgpack 库,而是自行进行二进制解析。

当我将返回的原始数据写入文件而不是字符串缓冲区时,手动检查它表明它似乎是正确的 msgpack 格式。

你知道这里出了什么问题吗?我将开始使用 gdb 来尝试追踪问题,同时研究将 ruby​​ c 扩展转换为我可以使用的库,因为它似乎可以工作。但希望这是一件简单的事情,你们中的一个人会明白!

最佳答案

写入回调未返回正确的返回代码。

关于c - MessagePack 解包失败,并通过 Libcurl 收到 C 语言中的大型 Metasploit Framework 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18363474/

相关文章:

c - 如何为C语言的每个函数插入打印进行调试?

C - 无法使用 while 和逻辑运算符停止程序

c - 如何在 ListView 控件中查找项目?

c - 如何在 C 的 curl 中启用 scp?

c - libcurl 中的宏函数,它返回 libcurl 版本

c++ - 消息包,C++ : How to use MSGPACK_DEFINE with c++11 enum classes

计算谐波和

c++ - 使用 libcurl 进度数据

python - 将 Msgpack 与 Flask 和 AngularJS 一起使用时出现无效类型错误

c - 为什么 MsgPack 键是 msgpack_object 类型?