Skip to content

Commit 0ae4c19

Browse files
authored
Merge pull request #143 from BonnyAD9/http-invalid-method
Parse http even if the method is invalid
2 parents c3167b7 + 516ac5c commit 0ae4c19

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

process/http.cpp

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ void copy_str(char *dst, ssize_t size, const char *begin, const char *end)
180180
}
181181

182182
memcpy(dst, begin, len);
183-
183+
184184
if (len >= 1 && dst[len - 1] == '\n') {
185185
len--;
186186
}
@@ -201,7 +201,11 @@ bool HTTPPlugin::is_request(const char *data, int payload_len)
201201
}
202202
memcpy(chars, data, 4);
203203
chars[4] = 0;
204-
return valid_http_method(chars);
204+
205+
// 'valid_http_method' can quicky confirm valid http methods.
206+
// 'invalid_http_method' is slower but can check if it is http request even
207+
// if the method is invalid.
208+
return valid_http_method(chars) || invalid_http_method(data, payload_len);
205209
}
206210

207211
bool HTTPPlugin::is_response(const char *data, int payload_len)
@@ -521,6 +525,46 @@ bool HTTPPlugin::valid_http_method(const char *method) const
521525
!strcmp(method, "PATC"));
522526
}
523527

528+
/**
529+
* @brief Check if the payload is http request even with invalid method.
530+
*
531+
* @param [in] payload Packet payload data.
532+
* @param payload_len Length packet payload.
533+
* @return True if the packet is http request.
534+
*/
535+
bool HTTPPlugin::invalid_http_method(const char *data, int payload_len) const
536+
{
537+
// arbitrary value, if the method is longer it propably isnt http request
538+
// so don't look further
539+
const int MAX_METHOD_LENGTH = 32;
540+
541+
// METHOD URI HTTP/VERSION
542+
// | | |
543+
// | | +---- uri_end
544+
// | +---- method_end
545+
// +---- data
546+
547+
// check if there is space in the first HTTP_MAX_METHOD_LENGTH chars
548+
int len = std::min(payload_len, MAX_METHOD_LENGTH);
549+
auto method_end = static_cast<const char *>(memchr(data, ' ', len));
550+
if (method_end == nullptr)
551+
return false;
552+
553+
payload_len -= method_end - data - 1;
554+
if (payload_len <= 0)
555+
return false;
556+
557+
auto uri_end = static_cast<const char *>(memchr(method_end + 1, ' ', payload_len));
558+
if (method_end == nullptr)
559+
return false;
560+
561+
payload_len -= uri_end - method_end;
562+
if (payload_len <= 4)
563+
return false;
564+
565+
return memcmp(uri_end + 1, "HTTP", 4) == 0;
566+
}
567+
524568
/**
525569
* \brief Add new extension http request header into flow record.
526570
* \param [in] data Packet payload data.

process/http.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ struct RecordExtHTTP : public RecordExt {
8787
bool req;
8888
bool resp;
8989

90-
char method[10];
90+
char method[16];
9191
char host[64];
9292
char uri[128];
9393
char user_agent[128];
@@ -228,6 +228,7 @@ class HTTPPlugin : public ProcessPlugin
228228
void add_ext_http_request(const char *data, int payload_len, Flow &flow);
229229
void add_ext_http_response(const char *data, int payload_len, Flow &flow);
230230
bool valid_http_method(const char *method) const;
231+
bool invalid_http_method(const char *payload, int payload_len) const;
231232

232233
RecordExtHTTP *recPrealloc;/**< Preallocated extension. */
233234
bool flow_flush; /**< Tell storage plugin to flush current Flow. */

0 commit comments

Comments
 (0)