]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
output-json-http: add functions to log http body
authorGiuseppe Longo <glongo@stamus-networks.com>
Thu, 14 Apr 2016 12:26:58 +0000 (14:26 +0200)
committerVictor Julien <victor@inliniac.net>
Wed, 28 Jun 2017 12:37:42 +0000 (14:37 +0200)
This patch adds two functions that permits to log the http body
in printable or base64 format.

src/output-json-http.c
src/output-json-http.h

index a23308388c559c23a671f28ccd32bb2e900c8548..02127928afa7f9ee999e6895bf0df76f35241356 100644 (file)
@@ -47,7 +47,9 @@
 #include "util-proto-name.h"
 #include "util-logopenfile.h"
 #include "util-time.h"
+#include "util-crypt.h"
 #include "output-json.h"
+#include "output-json-alert.h"
 #include "output-json-http.h"
 
 #ifdef HAVE_LIBJANSSON
@@ -350,6 +352,79 @@ static void JsonHttpLogJSONExtended(json_t *js, htp_tx_t *tx)
     json_object_set_new(js, "length", json_integer(tx->response_message_len));
 }
 
+static void BodyPrintableBuffer(json_t *js, HtpBody *body, const char *key)
+{
+    if (body->sb != NULL && body->sb->buf != NULL) {
+        uint32_t offset = 0;
+        const uint8_t *body_data;
+        uint32_t body_data_len;
+        uint64_t body_offset;
+
+        if (StreamingBufferGetData(body->sb, &body_data,
+                                   &body_data_len, &body_offset) == 0) {
+            return;
+        }
+
+        uint8_t printable_buf[body_data_len + 1];
+        PrintStringsToBuffer(printable_buf, &offset,
+                             sizeof(printable_buf),
+                             body_data, body_data_len);
+        if (offset > 0) {
+            json_object_set_new(js, key, json_string((char *)printable_buf));
+        }
+    }
+}
+
+void JsonHttpLogJSONBodyPrintable(json_t *js, Flow *f, uint64_t tx_id)
+{
+    HtpState *htp_state = (HtpState *)FlowGetAppState(f);
+    if (htp_state) {
+        htp_tx_t *tx = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_HTTP, htp_state, tx_id);
+        if (tx) {
+            HtpTxUserData *htud = (HtpTxUserData *)htp_tx_get_user_data(tx);
+            if (htud != NULL) {
+                BodyPrintableBuffer(js, &htud->request_body, "http_request_body_printable");
+                BodyPrintableBuffer(js, &htud->response_body, "http_response_body_printable");
+            }
+        }
+    }
+}
+
+static void BodyBase64Buffer(json_t *js, HtpBody *body, const char *key)
+{
+    if (body->sb != NULL && body->sb->buf != NULL) {
+        const uint8_t *body_data;
+        uint32_t body_data_len;
+        uint64_t body_offset;
+
+        if (StreamingBufferGetData(body->sb, &body_data,
+                                   &body_data_len, &body_offset) == 0) {
+            return;
+        }
+
+        unsigned long len = body_data_len * 2 + 1;
+        uint8_t encoded[len];
+        if (Base64Encode(body_data, body_data_len, encoded, &len) == SC_BASE64_OK) {
+            json_object_set_new(js, key, json_string((char *)encoded));
+        }
+    }
+}
+
+void JsonHttpLogJSONBodyBase64(json_t *js, Flow *f, uint64_t tx_id)
+{
+    HtpState *htp_state = (HtpState *)FlowGetAppState(f);
+    if (htp_state) {
+        htp_tx_t *tx = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_HTTP, htp_state, tx_id);
+        if (tx) {
+            HtpTxUserData *htud = (HtpTxUserData *)htp_tx_get_user_data(tx);
+            if (htud != NULL) {
+                BodyBase64Buffer(js, &htud->request_body, "http_request_body");
+                BodyBase64Buffer(js, &htud->response_body, "http_response_body");
+            }
+        }
+    }
+}
+
 /* JSON format logging */
 static void JsonHttpLogJSON(JsonHttpLogThread *aft, json_t *js, htp_tx_t *tx, uint64_t tx_id)
 {
@@ -409,7 +484,6 @@ json_t *JsonHttpAddMetadata(const Flow *f, uint64_t tx_id)
 
             JsonHttpLogJSONBasic(hjs, tx);
             JsonHttpLogJSONExtended(hjs, tx);
-
             return hjs;
         }
     }
index a82e928009e0ded537f3b3a08c5e61a397f6992e..a37fe92fe6554cd6149496e905e5952cdd992f12 100644 (file)
@@ -28,6 +28,8 @@ void JsonHttpLogRegister(void);
 
 #ifdef HAVE_LIBJANSSON
 json_t *JsonHttpAddMetadata(const Flow *f, uint64_t tx_id);
+void JsonHttpLogJSONBodyPrintable(json_t *js, Flow *f, uint64_t tx_id);
+void JsonHttpLogJSONBodyBase64(json_t *js, Flow *f, uint64_t tx_id);
 #endif /* HAVE_LIBJANSSON */
 
 #endif /* __OUTPUT_JSON_HTTP_H__ */