Skip to content

Commit 8d05285

Browse files
committed
Adds support to https audit log output
This functionality was built for test only.
1 parent e5acc95 commit 8d05285

File tree

9 files changed

+165
-2
lines changed

9 files changed

+165
-2
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ libmodsecurity_la_SOURCES = \
182182
transaction.cc \
183183
audit_log/audit_log.cc \
184184
audit_log/writer.cc \
185+
audit_log/writer/https.cc \
185186
audit_log/writer/serial.cc \
186187
audit_log/writer/parallel.cc \
187188
modsecurity.cc \

src/audit_log/audit_log.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <fstream>
2323

24+
#include "audit_log/writer/https.h"
2425
#include "audit_log/writer/parallel.h"
2526
#include "audit_log/writer/serial.h"
2627
#include "utils/regex.h"
@@ -184,6 +185,9 @@ bool AuditLog::init() {
184185
if (m_type == SerialAuditLogType) {
185186
m_writer = new audit_log::writer::Serial(this);
186187
}
188+
if (m_type == HttpsAuditLogType) {
189+
m_writer = new audit_log::writer::Https(this);
190+
}
187191
m_writer->refCountIncrease();
188192

189193
if (m_writer == NULL || m_writer->init() == false) {

src/audit_log/audit_log.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class AuditLog {
4242
enum AuditLogType {
4343
SerialAuditLogType,
4444
ParallelAuditLogType,
45+
HttpsAuditLogType
4546
};
4647

4748
enum AuditLogStatus {

src/audit_log/writer/https.cc

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* ModSecurity, http://www.modsecurity.org/
3+
* Copyright (c) 2015 Trustwave Holdings, Inc. (http://www.trustwave.com/)
4+
*
5+
* You may not use this file except in compliance with
6+
* the License. You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* If any of the files related to licensing are missing or if you have any
11+
* other questions related to licensing please contact Trustwave Holdings, Inc.
12+
* directly using the email address security@modsecurity.org.
13+
*
14+
*/
15+
16+
#include "audit_log/writer/https.h"
17+
18+
#include <time.h>
19+
#include <stdio.h>
20+
#include <string.h>
21+
#include <sys/types.h>
22+
#include <sys/stat.h>
23+
#include <fcntl.h>
24+
25+
#include <fstream>
26+
#include <mutex>
27+
28+
#include "audit_log/audit_log.h"
29+
#include "modsecurity/transaction.h"
30+
#include "src/utils.h"
31+
#include "utils/md5.h"
32+
#include "utils/https_client.h"
33+
34+
namespace modsecurity {
35+
namespace audit_log {
36+
namespace writer {
37+
38+
39+
Https::~Https() {
40+
}
41+
42+
43+
bool Https::init() {
44+
return true;
45+
}
46+
47+
48+
bool Https::write(Transaction *transaction, int parts) {
49+
Utils::HttpsClient m_http_client;
50+
transaction->debug(7, "Sending logs to: " + m_audit->m_path1);
51+
52+
std::string log = transaction->toJSON(parts);
53+
m_http_client.setRequestType("application/json");
54+
m_http_client.setRequestBody(log.c_str());
55+
m_http_client.download(m_audit->m_path1);
56+
return true;
57+
}
58+
59+
} // namespace writer
60+
} // namespace audit_log
61+
} // namespace modsecurity

src/audit_log/writer/https.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* ModSecurity, http://www.modsecurity.org/
3+
* Copyright (c) 2015 Trustwave Holdings, Inc. (http://www.trustwave.com/)
4+
*
5+
* You may not use this file except in compliance with
6+
* the License. You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* If any of the files related to licensing are missing or if you have any
11+
* other questions related to licensing please contact Trustwave Holdings, Inc.
12+
* directly using the email address security@modsecurity.org.
13+
*
14+
*/
15+
16+
#ifdef __cplusplus
17+
#include <iostream>
18+
#include <fstream>
19+
#include <string>
20+
#endif
21+
22+
#ifndef SRC_AUDIT_LOG_WRITER_HTTPS_H_
23+
#define SRC_AUDIT_LOG_WRITER_HTTPS_H_
24+
25+
#include "audit_log/writer.h"
26+
#include "modsecurity/transaction.h"
27+
28+
#ifdef __cplusplus
29+
30+
namespace modsecurity {
31+
namespace audit_log {
32+
namespace writer {
33+
34+
/** @ingroup ModSecurity_CPP_API */
35+
class Https : public audit_log::Writer {
36+
public:
37+
explicit Https(audit_log::AuditLog *audit)
38+
: audit_log::Writer(audit) { }
39+
40+
~Https() override;
41+
42+
void refCountIncrease() override {
43+
m_refereceCount++;
44+
}
45+
46+
47+
void refCountDecreaseAndCheck() override {
48+
m_refereceCount--;
49+
if (m_refereceCount == 0) {
50+
delete this;
51+
}
52+
}
53+
54+
bool init() override;
55+
bool write(Transaction *transaction, int parts) override;
56+
57+
};
58+
59+
} // namespace writer
60+
} // namespace audit_log
61+
} // namespace modsecurity
62+
#endif
63+
64+
#endif // SRC_AUDIT_LOG_WRITER_HTTPS_H_

src/parser/seclang-parser.yy

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ using modsecurity::Variables::Tx;
180180
%token <std::string> CONFIG_VALUE_ON
181181
%token <std::string> CONFIG_VALUE_OFF
182182
%token <std::string> CONFIG_VALUE_DETC
183+
%token <std::string> CONFIG_VALUE_HTTPS
183184
%token <std::string> CONFIG_VALUE_SERIAL
184185
%token <std::string> CONFIG_VALUE_PARALLEL
185186
%token <std::string> CONFIG_VALUE_RELEVANT_ONLY
@@ -354,6 +355,10 @@ audit_log:
354355
{
355356
driver.audit_log->setType(modsecurity::audit_log::AuditLog::ParallelAuditLogType);
356357
}
358+
| CONFIG_DIR_AUDIT_TPE CONFIG_VALUE_HTTPS
359+
{
360+
driver.audit_log->setType(modsecurity::audit_log::AuditLog::HttpsAuditLogType);
361+
}
357362
;
358363

359364
actings:

src/parser/seclang-scanner.ll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ CONFIG_VALUE_OFF (?i:Off)
144144
CONFIG_VALUE_DETC (?i:DetectionOnly)
145145
CONFIG_VALUE_SERIAL (?i:Serial)
146146
CONFIG_VALUE_PARALLEL (?i:Parallel|Concurrent)
147+
CONFIG_VALUE_HTTPS (?i:https)
147148
CONFIG_VALUE_RELEVANT_ONLY (?i:RelevantOnly)
148149

149150
CONFIG_VALUE_PROCESS_PARTIAL (?i:ProcessPartial)
@@ -152,7 +153,7 @@ CONFIG_VALUE_REJECT (?i:Reject)
152153
CONFIG_VALUE_ABORT (?i:Abort)
153154
CONFIG_VALUE_WARN (?i:Warn)
154155

155-
CONFIG_VALUE_PATH [0-9A-Za-z_/\.\-\*]+
156+
CONFIG_VALUE_PATH [0-9A-Za-z_\/\.\-\*\:]+
156157
AUDIT_PARTS [ABCDEFHJKIZ]+
157158
CONFIG_VALUE_NUMBER [0-9]+
158159

@@ -294,6 +295,7 @@ CONFIG_DIR_UNICODE_MAP_FILE (?i:SecUnicodeMapFile)
294295
{CONFIG_VALUE_OFF} { return yy::seclang_parser::make_CONFIG_VALUE_OFF(yytext, *driver.loc.back()); }
295296
{CONFIG_VALUE_SERIAL} { return yy::seclang_parser::make_CONFIG_VALUE_SERIAL(yytext, *driver.loc.back()); }
296297
{CONFIG_VALUE_PARALLEL} { return yy::seclang_parser::make_CONFIG_VALUE_PARALLEL(yytext, *driver.loc.back()); }
298+
{CONFIG_VALUE_HTTPS} { return yy::seclang_parser::make_CONFIG_VALUE_HTTPS(yytext, *driver.loc.back()); }
297299
{CONFIG_VALUE_DETC} { return yy::seclang_parser::make_CONFIG_VALUE_DETC(yytext, *driver.loc.back()); }
298300
{CONFIG_VALUE_RELEVANT_ONLY} { return yy::seclang_parser::make_CONFIG_VALUE_RELEVANT_ONLY(yytext, *driver.loc.back()); }
299301
{CONFIG_VALUE_PROCESS_PARTIAL} { return yy::seclang_parser::make_CONFIG_VALUE_PROCESS_PARTIAL(yytext, *driver.loc.back()); }

src/utils/https_client.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ void HttpsClient::setKey(const std::string& key) {
5050
m_key = "ModSec-key: " + key;
5151
}
5252

53+
void HttpsClient::setRequestBody(const std::string& requestBody) {
54+
m_requestBody = requestBody;
55+
}
56+
57+
void HttpsClient::setRequestType(const std::string& requestType) {
58+
m_requestType = requestType;
59+
}
60+
61+
5362
#ifdef MSC_WITH_CURL
5463
bool HttpsClient::download(const std::string &uri) {
5564
CURL *curl;
@@ -68,6 +77,12 @@ bool HttpsClient::download(const std::string &uri) {
6877

6978
headers_chunk = curl_slist_append(headers_chunk, uniqueId.c_str());
7079
headers_chunk = curl_slist_append(headers_chunk, status.c_str());
80+
81+
if (m_requestType.empty() == false) {
82+
std::string hdr = "Content-Type: " + m_requestType;
83+
headers_chunk = curl_slist_append(headers_chunk, hdr.c_str());
84+
}
85+
7186
if (m_key.empty() == false) {
7287
headers_chunk = curl_slist_append(headers_chunk, m_key.c_str());
7388
}
@@ -91,6 +106,10 @@ bool HttpsClient::download(const std::string &uri) {
91106
/* We want Curl to return error in case there is an HTTP error code */
92107
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
93108

109+
if (m_requestBody.empty() == false) {
110+
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, m_requestBody.c_str());
111+
}
112+
94113
res = curl_easy_perform(curl);
95114

96115
curl_slist_free_all(headers_chunk);

src/utils/https_client.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,24 @@ class HttpsClient {
3636
HttpsClient()
3737
: content(""),
3838
error(""),
39-
m_key("") { }
39+
m_key(""),
40+
m_requestBody(""),
41+
m_requestType("") { }
4042

4143
bool download(const std::string &uri);
4244
std::string content;
4345

4446
static size_t handle(char * data, size_t size, size_t nmemb, void * p);
4547
size_t handle_impl(char * data, size_t size, size_t nmemb);
4648
void setKey(const std::string& key);
49+
void setRequestType(const std::string& requestType);
50+
void setRequestBody(const std::string& requestType);
4751

4852
std::string error;
4953
private:
5054
std::string m_key;
55+
std::string m_requestBody;
56+
std::string m_requestType;
5157
};
5258

5359

0 commit comments

Comments
 (0)