• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OISF / suricata / 23374838686

21 Mar 2026 07:29AM UTC coverage: 59.341% (-20.0%) from 79.315%
23374838686

Pull #15075

github

web-flow
Merge 90b4e834f into 6587e363a
Pull Request #15075: Stack 8001 v16.4

38 of 70 new or added lines in 10 files covered. (54.29%)

34165 existing lines in 563 files now uncovered.

119621 of 201584 relevant lines covered (59.34%)

650666.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.92
/src/output-json-http.c
1
/* Copyright (C) 2007-2021 Open Information Security Foundation
2
 *
3
 * You can copy, redistribute or modify this Program under the terms of
4
 * the GNU General Public License version 2 as published by the Free
5
 * Software Foundation.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * version 2 along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15
 * 02110-1301, USA.
16
 */
17

18
/**
19
 * \file
20
 *
21
 * \author Tom DeCanio <td@npulsetech.com>
22
 *
23
 * Implements HTTP JSON logging portion of the engine.
24
 */
25

26
#include "suricata-common.h"
27
#include "detect.h"
28
#include "pkt-var.h"
29
#include "conf.h"
30

31
#include "threads.h"
32
#include "threadvars.h"
33
#include "tm-threads.h"
34

35
#include "util-print.h"
36
#include "util-unittest.h"
37

38
#include "util-debug.h"
39

40
#include "output.h"
41
#include "app-layer-htp.h"
42
#include "app-layer-htp-file.h"
43
#include "app-layer-htp-xff.h"
44
#include "app-layer.h"
45
#include "app-layer-parser.h"
46
#include "util-privs.h"
47
#include "util-buffer.h"
48
#include "util-proto-name.h"
49
#include "util-logopenfile.h"
50
#include "util-time.h"
51
#include "output-json.h"
52
#include "output-json-alert.h"
53
#include "output-json-http.h"
54
#include "util-byte.h"
55

56
typedef struct LogHttpFileCtx_ {
57
    uint32_t flags; /** Store mode */
58
    uint64_t fields;/** Store fields */
59
    HttpXFFCfg *xff_cfg;
60
    HttpXFFCfg *parent_xff_cfg;
61
    OutputJsonCtx *eve_ctx;
62
} LogHttpFileCtx;
63

64
typedef struct JsonHttpLogThread_ {
65
    LogHttpFileCtx *httplog_ctx;
66
    uint32_t uri_cnt;
67
    OutputJsonThreadCtx *ctx;
68
} JsonHttpLogThread;
69

70
#define MAX_SIZE_HEADER_NAME 256
123,986✔
71
#define MAX_SIZE_HEADER_VALUE 2048
123,988✔
72

73
#define LOG_HTTP_DEFAULT 0
1✔
74
#define LOG_HTTP_EXTENDED 1
10,666✔
75
#define LOG_HTTP_REQUEST 2 /* request field */
76
#define LOG_HTTP_ARRAY 4 /* require array handling */
77
#define LOG_HTTP_REQ_HEADERS 8
74,656✔
78
#define LOG_HTTP_RES_HEADERS 16
31,996✔
79

80
typedef enum {
81
    HTTP_FIELD_ACCEPT = 0,
82
    HTTP_FIELD_ACCEPT_CHARSET,
83
    HTTP_FIELD_ACCEPT_ENCODING,
84
    HTTP_FIELD_ACCEPT_LANGUAGE,
85
    HTTP_FIELD_ACCEPT_DATETIME,
86
    HTTP_FIELD_AUTHORIZATION,
87
    HTTP_FIELD_CACHE_CONTROL,
88
    HTTP_FIELD_COOKIE,
89
    HTTP_FIELD_FROM,
90
    HTTP_FIELD_MAX_FORWARDS,
91
    HTTP_FIELD_ORIGIN,
92
    HTTP_FIELD_PRAGMA,
93
    HTTP_FIELD_PROXY_AUTHORIZATION,
94
    HTTP_FIELD_RANGE,
95
    HTTP_FIELD_TE,
96
    HTTP_FIELD_VIA,
97
    HTTP_FIELD_X_REQUESTED_WITH,
98
    HTTP_FIELD_DNT,
99
    HTTP_FIELD_X_FORWARDED_PROTO,
100
    HTTP_FIELD_X_AUTHENTICATED_USER,
101
    HTTP_FIELD_X_FLASH_VERSION,
102
    HTTP_FIELD_ACCEPT_RANGES,
103
    HTTP_FIELD_AGE,
104
    HTTP_FIELD_ALLOW,
105
    HTTP_FIELD_CONNECTION,
106
    HTTP_FIELD_CONTENT_ENCODING,
107
    HTTP_FIELD_CONTENT_LANGUAGE,
108
    HTTP_FIELD_CONTENT_LENGTH,
109
    HTTP_FIELD_CONTENT_LOCATION,
110
    HTTP_FIELD_CONTENT_MD5,
111
    HTTP_FIELD_CONTENT_RANGE,
112
    HTTP_FIELD_CONTENT_TYPE,
113
    HTTP_FIELD_DATE,
114
    HTTP_FIELD_ETAG,
115
    HTTP_FIELD_EXPIRES,
116
    HTTP_FIELD_LAST_MODIFIED,
117
    HTTP_FIELD_LINK,
118
    HTTP_FIELD_LOCATION,
119
    HTTP_FIELD_PROXY_AUTHENTICATE,
120
    HTTP_FIELD_REFERRER,
121
    HTTP_FIELD_REFRESH,
122
    HTTP_FIELD_RETRY_AFTER,
123
    HTTP_FIELD_SERVER,
124
    HTTP_FIELD_SET_COOKIE,
125
    HTTP_FIELD_TRAILER,
126
    HTTP_FIELD_TRANSFER_ENCODING,
127
    HTTP_FIELD_UPGRADE,
128
    HTTP_FIELD_VARY,
129
    HTTP_FIELD_WARNING,
130
    HTTP_FIELD_WWW_AUTHENTICATE,
131
    HTTP_FIELD_TRUE_CLIENT_IP,
132
    HTTP_FIELD_ORG_SRC_IP,
133
    HTTP_FIELD_X_BLUECOAT_VIA,
134
    HTTP_FIELD_SIZE
135
} HttpField;
136

137
struct {
138
    const char *config_field;
139
    const char *htp_field;
140
    uint32_t flags;
141
} http_fields[] = {
142
    { "accept", "accept", LOG_HTTP_REQUEST },
143
    { "accept_charset", "accept-charset", LOG_HTTP_REQUEST },
144
    { "accept_encoding", "accept-encoding", LOG_HTTP_REQUEST },
145
    { "accept_language", "accept-language", LOG_HTTP_REQUEST },
146
    { "accept_datetime", "accept-datetime", LOG_HTTP_REQUEST },
147
    { "authorization", "authorization", LOG_HTTP_REQUEST },
148
    { "cache_control", "cache-control", LOG_HTTP_REQUEST },
149
    { "cookie", "cookie", LOG_HTTP_REQUEST | LOG_HTTP_ARRAY },
150
    { "from", "from", LOG_HTTP_REQUEST },
151
    { "max_forwards", "max-forwards", LOG_HTTP_REQUEST },
152
    { "origin", "origin", LOG_HTTP_REQUEST },
153
    { "pragma", "pragma", LOG_HTTP_REQUEST },
154
    { "proxy_authorization", "proxy-authorization", LOG_HTTP_REQUEST },
155
    { "range", "range", LOG_HTTP_REQUEST },
156
    { "te", "te", LOG_HTTP_REQUEST },
157
    { "via", "via", LOG_HTTP_REQUEST },
158
    { "x_requested_with", "x-requested-with", LOG_HTTP_REQUEST },
159
    { "dnt", "dnt", LOG_HTTP_REQUEST },
160
    { "x_forwarded_proto", "x-forwarded-proto", LOG_HTTP_REQUEST },
161
    { "x_authenticated_user", "x-authenticated-user", LOG_HTTP_REQUEST },
162
    { "x_flash_version", "x-flash-version", LOG_HTTP_REQUEST },
163
    { "accept_range", "accept-range", 0 },
164
    { "age", "age", 0 },
165
    { "allow", "allow", 0 },
166
    { "connection", "connection", 0 },
167
    { "content_encoding", "content-encoding", 0 },
168
    { "content_language", "content-language", 0 },
169
    { "content_length", "content-length", 0 },
170
    { "content_location", "content-location", 0 },
171
    { "content_md5", "content-md5", 0 },
172
    { "content_range", "content-range", 0 },
173
    { "content_type", "content-type", 0 },
174
    { "date", "date", 0 },
175
    { "etag", "etags", 0 },
176
    { "expires", "expires", 0 },
177
    { "last_modified", "last-modified", 0 },
178
    { "link", "link", 0 },
179
    { "location", "location", 0 },
180
    { "proxy_authenticate", "proxy-authenticate", 0 },
181
    { "referer", "referer", LOG_HTTP_EXTENDED },
182
    { "refresh", "refresh", 0 },
183
    { "retry_after", "retry-after", 0 },
184
    { "server", "server", 0 },
185
    { "set_cookie", "set-cookie", 0 },
186
    { "trailer", "trailer", 0 },
187
    { "transfer_encoding", "transfer-encoding", 0 },
188
    { "upgrade", "upgrade", 0 },
189
    { "vary", "vary", 0 },
190
    { "warning", "warning", 0 },
191
    { "www_authenticate", "www-authenticate", 0 },
192
    { "true_client_ip", "true-client-ip", LOG_HTTP_REQUEST },
193
    { "org_src_ip", "org-src-ip", LOG_HTTP_REQUEST },
194
    { "x_bluecoat_via", "x-bluecoat-via", LOG_HTTP_REQUEST },
195
};
196

197
static void EveHttpLogJSONBasic(SCJsonBuilder *js, htp_tx_t *tx)
198
{
18,870✔
199
    /* hostname */
200
    if (htp_tx_request_hostname(tx) != NULL) {
18,870✔
201
        SCJbSetStringFromBytes(js, "hostname", bstr_ptr(htp_tx_request_hostname(tx)),
15,678✔
202
                (uint32_t)bstr_len(htp_tx_request_hostname(tx)));
15,678✔
203
    }
15,678✔
204

205
    /* port */
206
    /* NOTE: this field will be set ONLY if the port is present in the
207
     * hostname. It may be present in the header "Host" or in the URL.
208
     * There is no connection (from the suricata point of view) between this
209
     * port and the TCP destination port of the flow.
210
     */
211
    if (htp_tx_request_port_number(tx) >= 0) {
18,870✔
212
        SCJbSetUint(js, "http_port", htp_tx_request_port_number(tx));
4,506✔
213
    }
4,506✔
214

215
    /* uri */
216
    if (htp_tx_request_uri(tx) != NULL) {
18,870✔
217
        SCJbSetStringFromBytes(js, "url", bstr_ptr(htp_tx_request_uri(tx)),
18,485✔
218
                (uint32_t)bstr_len(htp_tx_request_uri(tx)));
18,485✔
219
    }
18,485✔
220

221
    if (htp_tx_request_headers(tx) != NULL) {
18,870✔
222
        /* user agent */
223
        const htp_header_t *h_user_agent = htp_tx_request_header(tx, "user-agent");
18,870✔
224
        if (h_user_agent != NULL) {
18,870✔
225
            SCJbSetStringFromBytes(js, "http_user_agent", htp_header_value_ptr(h_user_agent),
15,371✔
226
                    (uint32_t)htp_header_value_len(h_user_agent));
15,371✔
227
        }
15,371✔
228

229
        /* x-forwarded-for */
230
        const htp_header_t *h_x_forwarded_for = htp_tx_request_header(tx, "x-forwarded-for");
18,870✔
231
        if (h_x_forwarded_for != NULL) {
18,870✔
232
            SCJbSetStringFromBytes(js, "xff", htp_header_value_ptr(h_x_forwarded_for),
20✔
233
                    (uint32_t)htp_header_value_len(h_x_forwarded_for));
20✔
234
        }
20✔
235
    }
18,870✔
236

237
    /* content-type */
238
    if (htp_tx_response_headers(tx) != NULL) {
18,870✔
239
        const htp_header_t *h_content_type = htp_tx_response_header(tx, "content-type");
18,870✔
240
        if (h_content_type != NULL) {
18,870✔
241
            uint32_t len = (uint32_t)htp_header_value_len(h_content_type);
14,127✔
242
            const uint8_t *p = memchr(htp_header_value_ptr(h_content_type), ';', len);
14,127✔
243
            if (p != NULL)
14,127✔
244
                len = (uint32_t)(p - htp_header_value_ptr(h_content_type));
5,824✔
245
            SCJbSetStringFromBytes(
14,127✔
246
                    js, "http_content_type", htp_header_value_ptr(h_content_type), len);
14,127✔
247
        }
14,127✔
248
        const htp_header_t *h_content_range = htp_tx_response_header(tx, "content-range");
18,870✔
249
        if (h_content_range != NULL) {
18,870✔
250
            SCJbOpenObject(js, "content_range");
3,391✔
251
            SCJbSetStringFromBytes(js, "raw", htp_header_value_ptr(h_content_range),
3,391✔
252
                    (uint32_t)htp_header_value_len(h_content_range));
3,391✔
253
            HTTPContentRange crparsed;
3,391✔
254
            if (HTPParseContentRange(htp_header_value(h_content_range), &crparsed) == 0) {
3,391✔
255
                if (crparsed.start >= 0)
3,359✔
256
                    SCJbSetUint(js, "start", crparsed.start);
3,359✔
257
                if (crparsed.end >= 0)
3,359✔
258
                    SCJbSetUint(js, "end", crparsed.end);
3,359✔
259
                if (crparsed.size >= 0)
3,359✔
260
                    SCJbSetUint(js, "size", crparsed.size);
3,359✔
261
            }
3,359✔
262
            SCJbClose(js);
3,391✔
263
        }
3,391✔
264
    }
18,870✔
265
}
18,870✔
266

267
static void EveHttpLogJSONExtended(SCJsonBuilder *js, htp_tx_t *tx)
268
{
18,870✔
269
    /* referer */
270
    const htp_header_t *h_referer = NULL;
18,870✔
271
    if (htp_tx_request_headers(tx) != NULL) {
18,870✔
272
        h_referer = htp_tx_request_header(tx, "referer");
18,870✔
273
    }
18,870✔
274
    if (h_referer != NULL) {
18,870✔
275
        SCJbSetStringFromBytes(js, "http_refer", htp_header_value_ptr(h_referer),
5,891✔
276
                (uint32_t)htp_header_value_len(h_referer));
5,891✔
277
    }
5,891✔
278

279
    /* method */
280
    if (htp_tx_request_method(tx) != NULL) {
18,870✔
281
        SCJbSetStringFromBytes(js, "http_method", bstr_ptr(htp_tx_request_method(tx)),
17,269✔
282
                (uint32_t)bstr_len(htp_tx_request_method(tx)));
17,269✔
283
    }
17,269✔
284

285
    /* protocol */
286
    if (htp_tx_request_protocol(tx) != NULL) {
18,870✔
287
        SCJbSetStringFromBytes(js, "protocol", bstr_ptr(htp_tx_request_protocol(tx)),
16,848✔
288
                (uint32_t)bstr_len(htp_tx_request_protocol(tx)));
16,848✔
289
    }
16,848✔
290

291
    /* response status */
292
    const int resp = htp_tx_response_status_number(tx);
18,870✔
293
    if (resp > 0) {
18,870✔
294
        SCJbSetUint(js, "status", (uint32_t)resp);
16,440✔
295
    } else if (htp_tx_response_status(tx) != NULL) {
16,440✔
296
        SCJbSetStringFromBytes(js, "status_string", bstr_ptr(htp_tx_response_status(tx)),
471✔
297
                (uint32_t)bstr_len(htp_tx_response_status(tx)));
471✔
298
    }
471✔
299

300
    const htp_header_t *h_location = htp_tx_response_header(tx, "location");
18,870✔
301
    if (h_location != NULL) {
18,870✔
302
        SCJbSetStringFromBytes(js, "redirect", htp_header_value_ptr(h_location),
609✔
303
                (uint32_t)htp_header_value_len(h_location));
609✔
304
    }
609✔
305

306
    /* length */
307
    SCJbSetUint(js, "length", htp_tx_response_message_len(tx));
18,870✔
308
}
18,870✔
309

310
static void EveHttpLogJSONHeaders(
311
        SCJsonBuilder *js, uint32_t direction, htp_tx_t *tx, LogHttpFileCtx *http_ctx)
312
{
21,330✔
313
    const htp_headers_t *headers = direction & LOG_HTTP_REQ_HEADERS ? htp_tx_request_headers(tx)
21,330✔
314
                                                                    : htp_tx_response_headers(tx);
21,330✔
315
    char name[MAX_SIZE_HEADER_NAME] = {0};
21,330✔
316
    char value[MAX_SIZE_HEADER_VALUE] = {0};
21,330✔
317
    size_t n = htp_headers_size(headers);
21,330✔
318
    SCJsonBuilderMark mark = { 0, 0, 0 };
21,330✔
319
    SCJbGetMark(js, &mark);
21,330✔
320
    bool array_empty = true;
21,330✔
321
    SCJbOpenArray(js, direction & LOG_HTTP_REQ_HEADERS ? "request_headers" : "response_headers");
21,330✔
322
    for (size_t i = 0; i < n; i++) {
145,315✔
323
        const htp_header_t *h = htp_headers_get_index(headers, i);
123,985✔
324
        if ((http_ctx->flags & direction) == 0 && http_ctx->fields != 0) {
123,985✔
UNCOV
325
            bool tolog = false;
×
UNCOV
326
            for (HttpField f = HTTP_FIELD_ACCEPT; f < HTTP_FIELD_SIZE; f++) {
×
UNCOV
327
                if ((http_ctx->fields & (1ULL << f)) != 0) {
×
328
                    /* prevent logging a field twice if extended logging is
329
                     enabled */
UNCOV
330
                    if (((http_ctx->flags & LOG_HTTP_EXTENDED) == 0) ||
×
UNCOV
331
                            ((http_ctx->flags & LOG_HTTP_EXTENDED) !=
×
UNCOV
332
                                    (http_fields[f].flags & LOG_HTTP_EXTENDED))) {
×
UNCOV
333
                        if (bstr_cmp_c_nocase(htp_header_name(h), http_fields[f].htp_field)) {
×
UNCOV
334
                            tolog = true;
×
UNCOV
335
                            break;
×
UNCOV
336
                        }
×
UNCOV
337
                    }
×
UNCOV
338
                }
×
UNCOV
339
            }
×
UNCOV
340
            if (!tolog) {
×
UNCOV
341
                continue;
×
UNCOV
342
            }
×
UNCOV
343
        }
×
344
        array_empty = false;
123,985✔
345
        SCJbStartObject(js);
123,985✔
346
        size_t size_name = htp_header_name_len(h) < MAX_SIZE_HEADER_NAME - 1
123,985✔
347
                                   ? htp_header_name_len(h)
123,985✔
348
                                   : MAX_SIZE_HEADER_NAME - 1;
123,985✔
349
        memcpy(name, htp_header_name_ptr(h), size_name);
123,985✔
350
        name[size_name] = '\0';
123,985✔
351
        SCJbSetString(js, "name", name);
123,985✔
352
        size_t size_value = htp_header_value_len(h) < MAX_SIZE_HEADER_VALUE - 1
123,985✔
353
                                    ? htp_header_value_len(h)
123,985✔
354
                                    : MAX_SIZE_HEADER_VALUE - 1;
123,985✔
355
        memcpy(value, htp_header_value_ptr(h), size_value);
123,985✔
356
        value[size_value] = '\0';
123,985✔
357
        SCJbSetString(js, "value", value);
123,985✔
358
        SCJbClose(js);
123,985✔
359
    }
123,985✔
360
    if (array_empty) {
21,330✔
361
        SCJbRestoreMark(js, &mark);
4,162✔
362
    } else {
17,168✔
363
        // Close array.
364
        SCJbClose(js);
17,168✔
365
    }
17,168✔
366
}
21,330✔
367

368
static void BodyPrintableBuffer(SCJsonBuilder *js, HtpBody *body, const char *key)
369
{
5,004✔
370
    if (body->sb != NULL && body->sb->region.buf != NULL) {
5,004✔
371
        const uint8_t *body_data;
1,455✔
372
        uint32_t body_data_len;
1,455✔
373
        uint64_t body_offset;
1,455✔
374

375
        if (StreamingBufferGetData(body->sb, &body_data,
1,455✔
376
                                   &body_data_len, &body_offset) == 0) {
1,455✔
377
            return;
×
378
        }
×
379

380
        SCJbSetPrintAsciiString(js, key, body_data, body_data_len);
1,455✔
381
    }
1,455✔
382
}
5,004✔
383

384
void EveHttpLogJSONBodyPrintable(SCJsonBuilder *js, Flow *f, uint64_t tx_id)
385
{
2,502✔
386
    HtpState *htp_state = (HtpState *)FlowGetAppState(f);
2,502✔
387
    if (htp_state) {
2,502✔
388
        htp_tx_t *tx = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_HTTP1, htp_state, tx_id);
2,502✔
389
        if (tx) {
2,502✔
390
            HtpTxUserData *htud = (HtpTxUserData *)htp_tx_get_user_data(tx);
2,502✔
391
            BodyPrintableBuffer(js, &htud->request_body, "http_request_body_printable");
2,502✔
392
            BodyPrintableBuffer(js, &htud->response_body, "http_response_body_printable");
2,502✔
393
        }
2,502✔
394
    }
2,502✔
395
}
2,502✔
396

397
static void BodyBase64Buffer(SCJsonBuilder *js, HtpBody *body, const char *key)
398
{
5,004✔
399
    if (body->sb != NULL && body->sb->region.buf != NULL) {
5,004✔
400
        const uint8_t *body_data;
1,455✔
401
        uint32_t body_data_len;
1,455✔
402
        uint64_t body_offset;
1,455✔
403

404
        if (StreamingBufferGetData(body->sb, &body_data,
1,455✔
405
                                   &body_data_len, &body_offset) == 0) {
1,455✔
406
            return;
×
407
        }
×
408

409
        SCJbSetBase64(js, key, body_data, body_data_len);
1,455✔
410
    }
1,455✔
411
}
5,004✔
412

413
void EveHttpLogJSONBodyBase64(SCJsonBuilder *js, Flow *f, uint64_t tx_id)
414
{
2,502✔
415
    HtpState *htp_state = (HtpState *)FlowGetAppState(f);
2,502✔
416
    if (htp_state) {
2,502✔
417
        htp_tx_t *tx = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_HTTP1, htp_state, tx_id);
2,502✔
418
        if (tx) {
2,502✔
419
            HtpTxUserData *htud = (HtpTxUserData *)htp_tx_get_user_data(tx);
2,502✔
420
            BodyBase64Buffer(js, &htud->request_body, "http_request_body");
2,502✔
421
            BodyBase64Buffer(js, &htud->response_body, "http_response_body");
2,502✔
422
        }
2,502✔
423
    }
2,502✔
424
}
2,502✔
425

426
/* JSON format logging */
427
static void EveHttpLogJSON(JsonHttpLogThread *aft, SCJsonBuilder *js, htp_tx_t *tx, uint64_t tx_id)
428
{
10,665✔
429
    LogHttpFileCtx *http_ctx = aft->httplog_ctx;
10,665✔
430
    SCJbOpenObject(js, "http");
10,665✔
431

432
    EveHttpLogJSONBasic(js, tx);
10,665✔
433
    if (http_ctx->flags & LOG_HTTP_EXTENDED)
10,665✔
434
        EveHttpLogJSONExtended(js, tx);
10,665✔
435
    if (http_ctx->flags & LOG_HTTP_REQ_HEADERS || http_ctx->fields != 0)
10,665✔
436
        EveHttpLogJSONHeaders(js, LOG_HTTP_REQ_HEADERS, tx, http_ctx);
10,665✔
437
    if (http_ctx->flags & LOG_HTTP_RES_HEADERS || http_ctx->fields != 0)
10,665✔
438
        EveHttpLogJSONHeaders(js, LOG_HTTP_RES_HEADERS, tx, http_ctx);
10,665✔
439

440
    SCJbClose(js);
10,665✔
441
}
10,665✔
442

443
static int JsonHttpLogger(ThreadVars *tv, void *thread_data, const Packet *p, Flow *f, void *alstate, void *txptr, uint64_t tx_id)
444
{
10,665✔
445
    SCEnter();
10,665✔
446

447
    htp_tx_t *tx = txptr;
10,665✔
448
    JsonHttpLogThread *jhl = (JsonHttpLogThread *)thread_data;
10,665✔
449

450
    SCJsonBuilder *js = CreateEveHeaderWithTxId(
10,665✔
451
            p, LOG_DIR_FLOW, "http", NULL, tx_id, jhl->httplog_ctx->eve_ctx);
10,665✔
452
    if (unlikely(js == NULL))
10,665✔
453
        return TM_ECODE_OK;
×
454

455
    SCLogDebug("got a HTTP request and now logging !!");
10,665✔
456

457
    EveHttpLogJSON(jhl, js, tx, tx_id);
10,665✔
458
    HttpXFFCfg *xff_cfg = jhl->httplog_ctx->xff_cfg != NULL ?
10,665✔
459
        jhl->httplog_ctx->xff_cfg : jhl->httplog_ctx->parent_xff_cfg;
10,665✔
460

461
    /* xff header */
462
    if ((xff_cfg != NULL) && !(xff_cfg->flags & XFF_DISABLED) && p->flow != NULL) {
10,665✔
463
        int have_xff_ip = 0;
10,665✔
464
        char buffer[XFF_MAXLEN];
10,665✔
465

466
        have_xff_ip = HttpXFFGetIPFromTx(p->flow, tx_id, xff_cfg, buffer, XFF_MAXLEN);
10,665✔
467

468
        if (have_xff_ip) {
10,665✔
469
            if (xff_cfg->flags & XFF_EXTRADATA) {
3✔
470
                SCJbSetString(js, "xff", buffer);
3✔
471
            }
3✔
472
            else if (xff_cfg->flags & XFF_OVERWRITE) {
×
473
                if (p->flowflags & FLOW_PKT_TOCLIENT) {
×
474
                    SCJbSetString(js, "dest_ip", buffer);
×
475
                } else {
×
476
                    SCJbSetString(js, "src_ip", buffer);
×
477
                }
×
478
            }
×
479
        }
3✔
480
    }
10,665✔
481

482
    OutputJsonBuilderBuffer(tv, p, p->flow, js, jhl->ctx);
10,665✔
483
    SCJbFree(js);
10,665✔
484

485
    SCReturnInt(TM_ECODE_OK);
10,665✔
486
}
10,665✔
487

488
bool EveHttpAddMetadata(const Flow *f, uint64_t tx_id, SCJsonBuilder *js)
489
{
8,213✔
490
    HtpState *htp_state = (HtpState *)FlowGetAppState(f);
8,213✔
491
    if (htp_state) {
8,213✔
492
        htp_tx_t *tx = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_HTTP1, htp_state, tx_id);
8,213✔
493

494
        if (tx) {
8,213✔
495
            EveHttpLogJSONBasic(js, tx);
8,205✔
496
            EveHttpLogJSONExtended(js, tx);
8,205✔
497
            return true;
8,205✔
498
        }
8,205✔
499
    }
8,213✔
500

501
    return false;
8✔
502
}
8,213✔
503

504
static void OutputHttpLogDeinitSub(OutputCtx *output_ctx)
UNCOV
505
{
×
UNCOV
506
    LogHttpFileCtx *http_ctx = output_ctx->data;
×
UNCOV
507
    if (http_ctx->xff_cfg) {
×
508
        SCFree(http_ctx->xff_cfg);
×
509
    }
×
UNCOV
510
    SCFree(http_ctx);
×
UNCOV
511
    SCFree(output_ctx);
×
UNCOV
512
}
×
513

514
static OutputInitResult OutputHttpLogInitSub(SCConfNode *conf, OutputCtx *parent_ctx)
515
{
1✔
516
    OutputInitResult result = { NULL, false };
1✔
517
    OutputJsonCtx *ojc = parent_ctx->data;
1✔
518

519
    LogHttpFileCtx *http_ctx = SCCalloc(1, sizeof(LogHttpFileCtx));
1✔
520
    if (unlikely(http_ctx == NULL))
1✔
521
        return result;
×
522
    memset(http_ctx, 0x00, sizeof(*http_ctx));
1✔
523

524
    OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
1✔
525
    if (unlikely(output_ctx == NULL)) {
1✔
526
        SCFree(http_ctx);
×
527
        return result;
×
528
    }
×
529

530
    http_ctx->flags = LOG_HTTP_DEFAULT;
1✔
531
    http_ctx->eve_ctx = ojc;
1✔
532

533
    if (conf) {
1✔
534
        const char *extended = SCConfNodeLookupChildValue(conf, "extended");
1✔
535

536
        if (extended != NULL) {
1✔
537
            if (SCConfValIsTrue(extended)) {
1✔
538
                http_ctx->flags = LOG_HTTP_EXTENDED;
1✔
539
            }
1✔
540
        }
1✔
541

542
        const char *all_headers = SCConfNodeLookupChildValue(conf, "dump-all-headers");
1✔
543
        if (all_headers != NULL) {
1✔
544
            if (strncmp(all_headers, "both", 4) == 0) {
1✔
545
                http_ctx->flags |= LOG_HTTP_REQ_HEADERS;
1✔
546
                http_ctx->flags |= LOG_HTTP_RES_HEADERS;
1✔
547
            } else if (strncmp(all_headers, "request", 7) == 0) {
1✔
548
                http_ctx->flags |= LOG_HTTP_REQ_HEADERS;
×
549
            } else if (strncmp(all_headers, "response", 8) == 0) {
×
550
                http_ctx->flags |= LOG_HTTP_RES_HEADERS;
×
551
            }
×
552
        }
1✔
553
        SCConfNode *custom;
1✔
554
        if ((custom = SCConfNodeLookupChild(conf, "custom")) != NULL) {
1✔
UNCOV
555
            if ((http_ctx->flags & (LOG_HTTP_REQ_HEADERS | LOG_HTTP_RES_HEADERS)) ==
×
UNCOV
556
                    (LOG_HTTP_REQ_HEADERS | LOG_HTTP_RES_HEADERS)) {
×
557
                SCLogWarning("No need for custom as dump-all-headers is already present");
×
558
            }
×
UNCOV
559
            SCConfNode *field;
×
UNCOV
560
            TAILQ_FOREACH (field, &custom->head, next) {
×
UNCOV
561
                HttpField f;
×
UNCOV
562
                for (f = HTTP_FIELD_ACCEPT; f < HTTP_FIELD_SIZE; f++) {
×
UNCOV
563
                    if ((strcmp(http_fields[f].config_field, field->val) == 0) ||
×
UNCOV
564
                            (strcasecmp(http_fields[f].htp_field, field->val) == 0)) {
×
UNCOV
565
                        http_ctx->fields |= (1ULL << f);
×
UNCOV
566
                        break;
×
UNCOV
567
                    }
×
UNCOV
568
                }
×
UNCOV
569
            }
×
UNCOV
570
        }
×
571
    }
1✔
572

573
    if (conf != NULL && SCConfNodeLookupChild(conf, "xff") != NULL) {
1✔
574
        http_ctx->xff_cfg = SCCalloc(1, sizeof(HttpXFFCfg));
×
575
        if (http_ctx->xff_cfg != NULL) {
×
576
            HttpXFFGetCfg(conf, http_ctx->xff_cfg);
×
577
        }
×
578
    } else if (ojc->xff_cfg) {
1✔
579
        http_ctx->parent_xff_cfg = ojc->xff_cfg;
1✔
580
    }
1✔
581

582
    output_ctx->data = http_ctx;
1✔
583
    output_ctx->DeInit = OutputHttpLogDeinitSub;
1✔
584

585
    /* enable the logger for the app layer */
586
    SCAppLayerParserRegisterLogger(IPPROTO_TCP, ALPROTO_HTTP1);
1✔
587

588
    result.ctx = output_ctx;
1✔
589
    result.ok = true;
1✔
590
    return result;
1✔
591
}
1✔
592

593
static TmEcode JsonHttpLogThreadInit(ThreadVars *t, const void *initdata, void **data)
594
{
1✔
595
    JsonHttpLogThread *aft = SCCalloc(1, sizeof(JsonHttpLogThread));
1✔
596
    if (unlikely(aft == NULL))
1✔
597
        return TM_ECODE_FAILED;
×
598

599
    if(initdata == NULL)
1✔
600
    {
×
601
        SCLogDebug("Error getting context for EveLogHTTP.  \"initdata\" argument NULL");
×
602
        goto error_exit;
×
603
    }
×
604

605
    /* Use the Output Context (file pointer and mutex) */
606
    aft->httplog_ctx = ((OutputCtx *)initdata)->data; //TODO
1✔
607

608
    aft->ctx = CreateEveThreadCtx(t, aft->httplog_ctx->eve_ctx);
1✔
609
    if (!aft->ctx) {
1✔
610
        goto error_exit;
×
611
    }
×
612

613
    *data = (void *)aft;
1✔
614
    return TM_ECODE_OK;
1✔
615

616
error_exit:
×
617
    SCFree(aft);
×
618
    return TM_ECODE_FAILED;
×
619
}
1✔
620

621
static TmEcode JsonHttpLogThreadDeinit(ThreadVars *t, void *data)
UNCOV
622
{
×
UNCOV
623
    JsonHttpLogThread *aft = (JsonHttpLogThread *)data;
×
UNCOV
624
    if (aft == NULL) {
×
625
        return TM_ECODE_OK;
×
626
    }
×
627

UNCOV
628
    FreeEveThreadCtx(aft->ctx);
×
629

630
    /* clear memory */
UNCOV
631
    memset(aft, 0, sizeof(JsonHttpLogThread));
×
632

UNCOV
633
    SCFree(aft);
×
UNCOV
634
    return TM_ECODE_OK;
×
UNCOV
635
}
×
636

637
void JsonHttpLogRegister (void)
638
{
2✔
639
    /* register as child of eve-log */
640
    OutputRegisterTxSubModule(LOGGER_JSON_TX, "eve-log", "JsonHttpLog", "eve-log.http",
2✔
641
            OutputHttpLogInitSub, ALPROTO_HTTP1, JsonHttpLogger, JsonHttpLogThreadInit,
2✔
642
            JsonHttpLogThreadDeinit);
2✔
643
}
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc