Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'Host' header value to 'hostname' in rule message field, if exists #2906

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions headers/modsecurity/rule_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class RuleMessage {
m_ruleLine(rule->getLineNumber()),
m_saveMessage(true),
m_serverIpAddress(trans->m_serverIpAddress),
m_requestHostName(trans->m_requestHostName),
m_severity(0),
m_uriNoQueryStringDecoded(trans->m_uri_no_query_string_decoded),
m_ver(rule->m_ver),
Expand All @@ -92,6 +93,7 @@ class RuleMessage {
m_ruleLine(rule->m_ruleLine),
m_saveMessage(rule->m_saveMessage),
m_serverIpAddress(rule->m_serverIpAddress),
m_requestHostName(rule->m_requestHostName),
m_severity(rule->m_severity),
m_uriNoQueryStringDecoded(rule->m_uriNoQueryStringDecoded),
m_ver(rule->m_ver),
Expand All @@ -117,6 +119,7 @@ class RuleMessage {
m_ruleLine(ruleMessage.m_ruleLine),
m_saveMessage(ruleMessage.m_saveMessage),
m_serverIpAddress(ruleMessage.m_serverIpAddress),
m_requestHostName(ruleMessage.m_requestHostName),
m_severity(ruleMessage.m_severity),
m_uriNoQueryStringDecoded(ruleMessage.m_uriNoQueryStringDecoded),
m_ver(ruleMessage.m_ver),
Expand All @@ -142,6 +145,7 @@ class RuleMessage {
m_ruleLine = ruleMessage.m_ruleLine;
m_saveMessage = ruleMessage.m_saveMessage;
m_serverIpAddress = ruleMessage.m_serverIpAddress;
m_requestHostName = ruleMessage.m_requestHostName;
m_severity = ruleMessage.m_severity;
m_uriNoQueryStringDecoded = ruleMessage.m_uriNoQueryStringDecoded;
m_ver = ruleMessage.m_ver;
Expand Down Expand Up @@ -201,6 +205,7 @@ class RuleMessage {
int m_ruleLine;
bool m_saveMessage;
std::shared_ptr<std::string> m_serverIpAddress;
std::shared_ptr<std::string> m_requestHostName;
int m_severity;
std::shared_ptr<std::string> m_uriNoQueryStringDecoded;
std::string m_ver;
Expand Down
5 changes: 5 additions & 0 deletions headers/modsecurity/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,11 @@ class Transaction : public TransactionAnchoredVariables, public TransactionSecMa
*/
std::shared_ptr<std::string> m_serverIpAddress;

/**
* Holds the request's hostname
*/
std::shared_ptr<std::string> m_requestHostName;

/**
* Holds the raw URI that was requested.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/rule_message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ std::string RuleMessage::_details(const RuleMessage *rm) {
msg.append(" [tag \"" + utils::string::toHexIfNeeded(a, true) + "\"]");
}

msg.append(" [hostname \"" + *rm->m_serverIpAddress.get() \
+ "\"]");
msg.append(" [hostname \"" + *rm->m_requestHostName.get() + "\"]");

msg.append(" [uri \"" + utils::string::limitTo(200, *rm->m_uriNoQueryStringDecoded.get()) + "\"]");
msg.append(" [unique_id \"" + *rm->m_id + "\"]");
msg.append(" [ref \"" + utils::string::limitTo(200, rm->m_reference) + "\"]");
Expand Down
4 changes: 4 additions & 0 deletions src/transaction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ Transaction::Transaction(ModSecurity *ms, RulesSet *rules, void *logCbData)
m_clientIpAddress(std::make_shared<std::string>("")),
m_httpVersion(""),
m_serverIpAddress(std::make_shared<std::string>("")),
m_requestHostName(std::make_shared<std::string>("")),
m_uri(""),
m_uri_no_query_string_decoded(std::make_shared<std::string>("")),
m_ARGScombinedSizeDouble(0),
Expand Down Expand Up @@ -180,6 +181,7 @@ Transaction::Transaction(ModSecurity *ms, RulesSet *rules, char *id, void *logCb
m_clientIpAddress(std::make_shared<std::string>("")),
m_httpVersion(""),
m_serverIpAddress(std::make_shared<std::string>("")),
m_requestHostName(std::make_shared<std::string>("")),
m_uri(""),
m_uri_no_query_string_decoded(std::make_shared<std::string>("")),
m_ARGScombinedSizeDouble(0),
Expand Down Expand Up @@ -316,6 +318,7 @@ int Transaction::processConnection(const char *client, int cPort,
const char *server, int sPort) {
m_clientIpAddress = std::unique_ptr<std::string>(new std::string(client));
m_serverIpAddress = std::unique_ptr<std::string>(new std::string(server));
m_requestHostName = std::unique_ptr<std::string>(new std::string(server));
this->m_clientPort = cPort;
this->m_serverPort = sPort;
ms_dbg(4, "Transaction context created.");
Expand Down Expand Up @@ -706,6 +709,7 @@ int Transaction::addRequestHeader(const std::string& key,
if (keyl == "host") {
std::vector<std::string> host = utils::string::split(value, ':');
m_variableServerName.set(host[0], m_variableOffset);
m_requestHostName = std::unique_ptr<std::string>(new std::string(host[0]));
}
m_variableOffset = m_variableOffset + value.size() + 1;

Expand Down
42 changes: 42 additions & 0 deletions test/test-cases/regression/rulemessage_host.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[
{
"enabled":1,
"version_min":300000,
"version_max":0,
"title":"Testing 'hostname' field in rule message",
"client":{
"ip":"200.249.12.31",
"port":2313
},
"server":{
"ip":"200.249.12.31",
"port":80
},
"request":{
"headers":{
"Host":"host.modsecurity.org"
},
"uri":"\/?q=/bin/bash",
"method":"GET",
"http_version":1.1,
"body":""
},
"response":{
"headers":{
"Content-Type":"text\/html; charset=utf-8\n\r",
"Content-Length":"10\n\r"
},
"body":[
"No answer."
]
},
"expected":{
"http_code":403,
"error_log":"hostname \"host.modsecurity.org\""
},
"rules":[
"SecRuleEngine On",
"SecRule ARGS \"@rx bash\" \"id:1,t:none,deny\""
]
}
]