suricata-輸出http-json body

默認(rèn)suricata的http body是單獨(dú)輸出的,現(xiàn)在我們想在eve.json輸出http請(qǐng)求的時(shí)候輸出請(qǐng)求和響應(yīng)體。

下載源碼

這兒下載的是5.0.3,好像最新源碼和這個(gè)版本的不太相同,這兒只是做個(gè)筆記。

輸出httpBody

  • 修改源碼
    打開 src目錄下 output-json-http.c 文件(以下源碼文件皆在src目錄下)。找到 JsonHttpLogJSON 方法。將此方法改為:
static void JsonHttpLogJSON(JsonHttpLogThread *aft, json_t *js, htp_tx_t *tx, uint64_t tx_id)
{
    LogHttpFileCtx *http_ctx = aft->httplog_ctx;
    json_t *hjs = json_object();
    if (hjs == NULL) {
        return;
    }

    JsonHttpLogJSONBasic(hjs, tx);
    /* log custom fields if configured */
    if (http_ctx->fields != 0)
        JsonHttpLogJSONCustom(http_ctx, hjs, tx);
    if (http_ctx->flags & LOG_HTTP_EXTENDED)
        JsonHttpLogJSONExtended(hjs, tx);
    if (http_ctx->flags & LOG_HTTP_REQ_HEADERS)
        JsonHttpLogJSONHeaders(hjs, LOG_HTTP_REQ_HEADERS, tx);
    if (http_ctx->flags & LOG_HTTP_RES_HEADERS)
        JsonHttpLogJSONHeaders(hjs, LOG_HTTP_RES_HEADERS, tx);
    //加入下面7行代碼
    if (tx) {
        HtpTxUserData *htud = (HtpTxUserData *)htp_tx_get_user_data(tx);
        if (htud != NULL) {
            BodyPrintableBuffer(hjs, &htud->request_body, "http_request_body");
            BodyPrintableBuffer(hjs, &htud->response_body, "http_response_body");
        }
    }

    json_object_set_new(js, "http", hjs);
}
  • 查看結(jié)果
    這塊我訪問了下百度,結(jié)果如下:
cat eve.json 
image.png

響應(yīng)體里面應(yīng)該是把漢語變成.................了

  • 解決中文無法顯示問題

上述可知,輸出的結(jié)果中把中文變成...了?,F(xiàn)在查看源碼文件 output-json-http.c
上述加入的函數(shù)為BodyPrintableBuffer(),查看此函數(shù)定義:

image.png

可知該函數(shù)調(diào)用了PrintStringsToBuffer() 方法。全局搜索下,發(fā)現(xiàn)在 util-print.c(util-print.h是對(duì)應(yīng)的頭文件)中找到該函數(shù)的定義。


image.png

點(diǎn)入查看該函數(shù),發(fā)現(xiàn)在此函數(shù)中對(duì)輸出進(jìn)行了判斷,如果字符串為可打印字符(isprint函數(shù))或者換行等字符則輸出,否則輸出'.'


image.png

漢字'中'的ASCII碼是4e2d,驗(yàn)證下是否為可打印字符

image.png

可見中文為不可打印字符。替換上述代碼為:


void PrintStringsToBuffer(uint8_t *dst_buf, uint32_t *dst_buf_offset_ptr, uint32_t dst_buf_size,
                          const uint8_t *src_buf, const uint32_t src_buf_len)
{
    uint32_t ch = 0;
    for (ch = 0; ch < src_buf_len; ch++) {
//        PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size,
//                        "%c",
//                        (isprint((uint8_t)src_buf[ch]) ||
//                        src_buf[ch] == '\n' ||
//                        src_buf[ch] == '\r') ? (uint8_t)src_buf[ch] : '.');

        PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size,
                        "%c",
                        (uint8_t)src_buf[ch]);
    }
    dst_buf[dst_buf_size - 1] = 0;

    return;
}

保存后重新編譯安裝,查看效果如下:

image.png

修改請(qǐng)求頭及響應(yīng)頭格式

為了減少后期工作,想直接把源碼輸出的格式改成想要的 。修改output-json-http.c的JsonHttpLogJSONHeaders方法如下:

static void JsonHttpLogJSONHeaders(json_t *js, uint32_t direction, htp_tx_t *tx) {
    htp_table_t *headers = direction & LOG_HTTP_REQ_HEADERS ?
                           tx->request_headers : tx->response_headers;
    char name[MAX_SIZE_HEADER_NAME] = {0};
    char value[MAX_SIZE_HEADER_VALUE] = {0};
    char head[6];
    size_t n = htp_table_size(headers);
    if (direction & LOG_HTTP_REQ_HEADERS) {
        strcpy(head, "_ReqH");
    } else {
        strcpy(head, "_ResH");
    }

    for (size_t i = 0; i < n; i++) {
        htp_header_t *h = htp_table_get_index(headers, i, NULL);
        if (h == NULL) {
            continue;
        }
        size_t size_name = bstr_len(h->name) < MAX_SIZE_HEADER_NAME - 1 ?
                           bstr_len(h->name) : MAX_SIZE_HEADER_NAME - 1;
        memcpy(name, bstr_ptr(h->name), size_name);
        name[size_name] = '\0';
        size_t size_value = bstr_len(h->value) < MAX_SIZE_HEADER_VALUE - 1 ?
                            bstr_len(h->value) : MAX_SIZE_HEADER_VALUE - 1;
        memcpy(value, bstr_ptr(h->value), size_value);
        value[size_value] = '\0';
        strcat(name, head);
        json_object_set_new(js,name, SCJsonString(value));
    }
}

最后結(jié)果為:這兒就是把請(qǐng)求頭和響應(yīng)頭都單獨(dú)拿出來了,沒有用數(shù)組。請(qǐng)求頭都以_ReqH結(jié)尾。響應(yīng)頭皆以_ResH結(jié)尾


image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容