mirror of
https://github.com/owasp-modsecurity/ModSecurity.git
synced 2025-09-30 03:34:29 +03:00
Add the ability to build custom request body parser extensions.
Add an example for a request body parser extension.
This commit is contained in:
@@ -38,6 +38,7 @@ APR_DECLARE_OPTIONAL_FN(void, modsec_register_variable,
|
||||
unsigned int argc_min, unsigned int argc_max,
|
||||
void *fn_validate, void *fn_generate,
|
||||
unsigned int is_cacheable, unsigned int availability));
|
||||
APR_DECLARE_OPTIONAL_FN(void, modsec_register_reqbody_processor, (const char *name, void *fn_init, void *fn_process, void *fn_complete));
|
||||
#endif
|
||||
|
||||
/* ap_get_server_version() is gone in 2.3.0.
|
||||
|
@@ -1116,6 +1116,25 @@ static void modsec_register_variable(const char *name, unsigned int type,
|
||||
fprintf(stderr,"modsecurity is NULL\n");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is exported for other Apache modules to
|
||||
* register new request body processors.
|
||||
*/
|
||||
static void modsec_register_reqbody_processor(const char *name,
|
||||
void *fn_init,
|
||||
void *fn_process,
|
||||
void *fn_complete)
|
||||
{
|
||||
if (modsecurity != NULL) {
|
||||
|
||||
msre_engine_reqbody_processor_register(modsecurity->msre, name,
|
||||
(fn_reqbody_processor_init_t)fn_init,
|
||||
(fn_reqbody_processor_init_t)fn_process,
|
||||
(fn_reqbody_processor_init_t)fn_complete);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/**
|
||||
@@ -1168,6 +1187,7 @@ static void register_hooks(apr_pool_t *mp) {
|
||||
APR_REGISTER_OPTIONAL_FN(modsec_register_tfn);
|
||||
APR_REGISTER_OPTIONAL_FN(modsec_register_operator);
|
||||
APR_REGISTER_OPTIONAL_FN(modsec_register_variable);
|
||||
APR_REGISTER_OPTIONAL_FN(modsec_register_reqbody_processor);
|
||||
#endif
|
||||
|
||||
/* Main hooks */
|
||||
|
@@ -103,6 +103,7 @@ msc_engine *modsecurity_create(apr_pool_t *mp, int processing_mode) {
|
||||
msre_engine_register_default_operators(msce->msre);
|
||||
msre_engine_register_default_tfns(msce->msre);
|
||||
msre_engine_register_default_actions(msce->msre);
|
||||
// TODO: msre_engine_register_default_reqbody_processors(msce->msre);
|
||||
|
||||
return msce;
|
||||
}
|
||||
|
@@ -21,6 +21,7 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
typedef struct rule_exception rule_exception;
|
||||
@@ -369,6 +370,9 @@ struct modsec_rec {
|
||||
* are to allow phases 1-2 only.
|
||||
*/
|
||||
unsigned int allow_scope;
|
||||
|
||||
/* Generic request body processor context to be used by custom parsers. */
|
||||
void *reqbody_processor_ctx;
|
||||
};
|
||||
|
||||
struct directory_config {
|
||||
|
@@ -17,10 +17,30 @@
|
||||
*
|
||||
*/
|
||||
#include "modsecurity.h"
|
||||
#include "re.h"
|
||||
#include "msc_parsers.h"
|
||||
|
||||
#define CHUNK_CAPACITY 8192
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
void msre_engine_reqbody_processor_register(msre_engine *engine,
|
||||
const char *name, void *fn_init, void *fn_process, void *fn_complete)
|
||||
{
|
||||
msre_reqbody_processor_metadata *metadata =
|
||||
(msre_reqbody_processor_metadata *)apr_pcalloc(engine->mp,
|
||||
sizeof(msre_reqbody_processor_metadata));
|
||||
if (metadata == NULL) return;
|
||||
|
||||
metadata->name = name;
|
||||
metadata->init = fn_init;
|
||||
metadata->process = fn_process;
|
||||
metadata->complete = fn_complete;
|
||||
apr_table_setn(engine->reqbody_processors, name, (void *)metadata);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare to accept the request body (part 2).
|
||||
*/
|
||||
@@ -77,8 +97,25 @@ apr_status_t modsecurity_request_body_start(modsec_rec *msr, char **error_msg) {
|
||||
|
||||
if (msr->msc_reqbody_processor != NULL) {
|
||||
char *my_error_msg = NULL;
|
||||
msre_reqbody_processor_metadata *metadata =
|
||||
(msre_reqbody_processor_metadata *)apr_table_get(msr->modsecurity->msre->reqbody_processors, msr->msc_reqbody_processor);
|
||||
|
||||
|
||||
if (strcmp(msr->msc_reqbody_processor, "MULTIPART") == 0) {
|
||||
if (metadata != NULL) {
|
||||
if ( (metadata->init != NULL)
|
||||
&& (metadata->init(msr, &my_error_msg) < 0))
|
||||
{
|
||||
*error_msg = apr_psprintf(msr->mp,
|
||||
"%s parsing error (init): %s",
|
||||
msr->msc_reqbody_processor,
|
||||
my_error_msg);
|
||||
msr->msc_reqbody_error = 1;
|
||||
msr->msc_reqbody_error_msg = my_error_msg;
|
||||
msr_log(msr, 2, "%s", *error_msg);
|
||||
}
|
||||
}
|
||||
// TODO: All these below need to be registered in the same way as above
|
||||
else if (strcmp(msr->msc_reqbody_processor, "MULTIPART") == 0) {
|
||||
if (multipart_init(msr, &my_error_msg) < 0) {
|
||||
*error_msg = apr_psprintf(msr->mp, "Multipart parsing error (init): %s", my_error_msg);
|
||||
msr->msc_reqbody_error = 1;
|
||||
@@ -86,8 +123,7 @@ apr_status_t modsecurity_request_body_start(modsec_rec *msr, char **error_msg) {
|
||||
msr_log(msr, 2, "%s", *error_msg);
|
||||
}
|
||||
}
|
||||
else
|
||||
if (strcmp(msr->msc_reqbody_processor, "XML") == 0) {
|
||||
else if (strcmp(msr->msc_reqbody_processor, "XML") == 0) {
|
||||
if (xml_init(msr, &my_error_msg) < 0) {
|
||||
*error_msg = apr_psprintf(msr->mp, "XML parsing error (init): %s", my_error_msg);
|
||||
msr->msc_reqbody_error = 1;
|
||||
@@ -95,8 +131,7 @@ apr_status_t modsecurity_request_body_start(modsec_rec *msr, char **error_msg) {
|
||||
msr_log(msr, 2, "%s", *error_msg);
|
||||
}
|
||||
}
|
||||
else
|
||||
if (strcmp(msr->msc_reqbody_processor, "URLENCODED") == 0) {
|
||||
else if (strcmp(msr->msc_reqbody_processor, "URLENCODED") == 0) {
|
||||
/* Do nothing, URLENCODED processor does not support streaming yet. */
|
||||
}
|
||||
else {
|
||||
@@ -268,10 +303,27 @@ apr_status_t modsecurity_request_body_store(modsec_rec *msr,
|
||||
* data to it first (but only if it did not report an
|
||||
* error on previous invocations).
|
||||
*/
|
||||
if ((msr->msc_reqbody_processor != NULL)&&(msr->msc_reqbody_error == 0)) {
|
||||
if ((msr->msc_reqbody_processor != NULL) && (msr->msc_reqbody_error == 0)) {
|
||||
char *my_error_msg = NULL;
|
||||
msre_reqbody_processor_metadata *metadata =
|
||||
(msre_reqbody_processor_metadata *)apr_table_get(msr->modsecurity->msre->reqbody_processors, msr->msc_reqbody_processor);
|
||||
|
||||
|
||||
if (strcmp(msr->msc_reqbody_processor, "MULTIPART") == 0) {
|
||||
if (metadata != NULL) {
|
||||
if ( (metadata->process != NULL)
|
||||
&& (metadata->process(msr, data, length, &my_error_msg) < 0))
|
||||
{
|
||||
*error_msg = apr_psprintf(msr->mp,
|
||||
"%s parsing error: %s",
|
||||
msr->msc_reqbody_processor,
|
||||
my_error_msg);
|
||||
msr->msc_reqbody_error = 1;
|
||||
msr->msc_reqbody_error_msg = my_error_msg;
|
||||
msr_log(msr, 2, "%s", *error_msg);
|
||||
}
|
||||
}
|
||||
// TODO: All these below need to be registered in the same way as above
|
||||
else if (strcmp(msr->msc_reqbody_processor, "MULTIPART") == 0) {
|
||||
/* The per-request data length counter will
|
||||
* be updated by the multipart parser.
|
||||
*/
|
||||
@@ -284,8 +336,7 @@ apr_status_t modsecurity_request_body_store(modsec_rec *msr,
|
||||
msr_log(msr, 2, "%s", *error_msg);
|
||||
}
|
||||
}
|
||||
else
|
||||
if (strcmp(msr->msc_reqbody_processor, "XML") == 0) {
|
||||
else if (strcmp(msr->msc_reqbody_processor, "XML") == 0) {
|
||||
/* Increase per-request data length counter. */
|
||||
msr->msc_reqbody_no_files_length += length;
|
||||
|
||||
@@ -297,8 +348,7 @@ apr_status_t modsecurity_request_body_store(modsec_rec *msr,
|
||||
msr_log(msr, 2, "%s", *error_msg);
|
||||
}
|
||||
}
|
||||
else
|
||||
if (strcmp(msr->msc_reqbody_processor, "URLENCODED") == 0) {
|
||||
else if (strcmp(msr->msc_reqbody_processor, "URLENCODED") == 0) {
|
||||
/* Increase per-request data length counter. */
|
||||
msr->msc_reqbody_no_files_length += length;
|
||||
|
||||
@@ -449,10 +499,27 @@ apr_status_t modsecurity_request_body_end(modsec_rec *msr, char **error_msg) {
|
||||
msr->msc_reqbody_read = 1;
|
||||
|
||||
/* Finalise body processing. */
|
||||
if ((msr->msc_reqbody_processor != NULL)&&(msr->msc_reqbody_error == 0)) {
|
||||
if ((msr->msc_reqbody_processor != NULL) && (msr->msc_reqbody_error == 0)) {
|
||||
char *my_error_msg = NULL;
|
||||
msre_reqbody_processor_metadata *metadata =
|
||||
(msre_reqbody_processor_metadata *)apr_table_get(msr->modsecurity->msre->reqbody_processors, msr->msc_reqbody_processor);
|
||||
|
||||
|
||||
if (strcmp(msr->msc_reqbody_processor, "MULTIPART") == 0) {
|
||||
if (metadata != NULL) {
|
||||
if ( (metadata->complete != NULL)
|
||||
&& (metadata->complete(msr, &my_error_msg) < 0))
|
||||
{
|
||||
*error_msg = apr_psprintf(msr->mp,
|
||||
"%s parsing error (complete): %s",
|
||||
msr->msc_reqbody_processor,
|
||||
my_error_msg);
|
||||
msr->msc_reqbody_error = 1;
|
||||
msr->msc_reqbody_error_msg = my_error_msg;
|
||||
msr_log(msr, 2, "%s", *error_msg);
|
||||
}
|
||||
}
|
||||
// TODO: All these below need to be registered in the same way as above
|
||||
else if (strcmp(msr->msc_reqbody_processor, "MULTIPART") == 0) {
|
||||
if (multipart_complete(msr, &my_error_msg) < 0) {
|
||||
*error_msg = apr_psprintf(msr->mp, "Multipart parsing error: %s", my_error_msg);
|
||||
msr->msc_reqbody_error = 1;
|
||||
@@ -469,12 +536,10 @@ apr_status_t modsecurity_request_body_end(modsec_rec *msr, char **error_msg) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else
|
||||
if (strcmp(msr->msc_reqbody_processor, "URLENCODED") == 0) {
|
||||
else if (strcmp(msr->msc_reqbody_processor, "URLENCODED") == 0) {
|
||||
return modsecurity_request_body_end_urlencoded(msr, error_msg);
|
||||
}
|
||||
else
|
||||
if (strcmp(msr->msc_reqbody_processor, "XML") == 0) {
|
||||
else if (strcmp(msr->msc_reqbody_processor, "XML") == 0) {
|
||||
if (xml_complete(msr, &my_error_msg) < 0) {
|
||||
*error_msg = apr_psprintf(msr->mp, "XML parser error: %s", my_error_msg);
|
||||
msr->msc_reqbody_error = 1;
|
||||
|
24
apache2/re.c
24
apache2/re.c
@@ -240,17 +240,29 @@ apr_status_t msre_parse_actions(msre_engine *engine, msre_actionset *actionset,
|
||||
/**
|
||||
* Locates variable metadata given the variable name.
|
||||
*/
|
||||
msre_var_metadata *msre_resolve_var(msre_engine *engine, const char *name) {
|
||||
msre_var_metadata *msre_resolve_var(msre_engine *engine, const char *name)
|
||||
{
|
||||
return (msre_var_metadata *)apr_table_get(engine->variables, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Locates action metadata given the action name.
|
||||
*/
|
||||
msre_action_metadata *msre_resolve_action(msre_engine *engine, const char *name) {
|
||||
msre_action_metadata *msre_resolve_action(msre_engine *engine, const char *name)
|
||||
{
|
||||
return (msre_action_metadata *)apr_table_get(engine->actions, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Locates request body processor metadata given the processor name.
|
||||
*/
|
||||
msre_reqbody_processor_metadata *msre_resolve_reqbody_processor(
|
||||
msre_engine *engine,
|
||||
const char *name)
|
||||
{
|
||||
return (msre_reqbody_processor_metadata *)apr_table_get(engine->reqbody_processors, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new variable instance given the variable name
|
||||
* and an (optional) parameter.
|
||||
@@ -738,14 +750,16 @@ msre_engine *msre_engine_create(apr_pool_t *parent_pool) {
|
||||
engine = apr_pcalloc(mp, sizeof(msre_engine));
|
||||
if (engine == NULL) return NULL;
|
||||
engine->mp = mp;
|
||||
engine->tfns = apr_table_make(mp, 25);
|
||||
engine->tfns = apr_table_make(mp, 50);
|
||||
if (engine->tfns == NULL) return NULL;
|
||||
engine->operators = apr_table_make(mp, 25);
|
||||
if (engine->operators == NULL) return NULL;
|
||||
engine->variables = apr_table_make(mp, 25);
|
||||
engine->variables = apr_table_make(mp, 100);
|
||||
if (engine->variables == NULL) return NULL;
|
||||
engine->actions = apr_table_make(mp, 25);
|
||||
engine->actions = apr_table_make(mp, 50);
|
||||
if (engine->actions == NULL) return NULL;
|
||||
engine->reqbody_processors = apr_table_make(mp, 10);
|
||||
if (engine->reqbody_processors == NULL) return NULL;
|
||||
|
||||
return engine;
|
||||
}
|
||||
|
21
apache2/re.h
21
apache2/re.h
@@ -34,6 +34,7 @@ typedef struct msre_tfn_metadata msre_tfn_metadata;
|
||||
typedef struct msre_actionset msre_actionset;
|
||||
typedef struct msre_action_metadata msre_action_metadata;
|
||||
typedef struct msre_action msre_action;
|
||||
typedef struct msre_reqbody_processor_metadata msre_reqbody_processor_metadata;
|
||||
typedef struct msre_cache_rec msre_cache_rec;
|
||||
|
||||
#include "apr_general.h"
|
||||
@@ -63,6 +64,10 @@ msre_var_metadata DSOLOCAL *msre_resolve_var(msre_engine *engine, const char *na
|
||||
|
||||
msre_action_metadata DSOLOCAL *msre_resolve_action(msre_engine *engine, const char *name);
|
||||
|
||||
msre_reqbody_processor_metadata DSOLOCAL *msre_resolve_reqbody_processor(
|
||||
msre_engine *engine,
|
||||
const char *name);
|
||||
|
||||
msre_var DSOLOCAL *msre_create_var(msre_ruleset *ruleset, const char *name, const char *param,
|
||||
modsec_rec *msr, char **error_msg);
|
||||
|
||||
@@ -91,6 +96,7 @@ struct msre_engine {
|
||||
apr_table_t *operators;
|
||||
apr_table_t *actions;
|
||||
apr_table_t *tfns;
|
||||
apr_table_t *reqbody_processors;
|
||||
};
|
||||
|
||||
msre_engine DSOLOCAL *msre_engine_create(apr_pool_t *parent_pool);
|
||||
@@ -360,6 +366,21 @@ struct msre_action {
|
||||
unsigned int param_plusminus; /* ABSOLUTE_VALUE, POSITIVE_VALUE, NEGATIVE_VALUE */
|
||||
};
|
||||
|
||||
void DSOLOCAL msre_engine_reqbody_processor_register(msre_engine *engine,
|
||||
const char *name, void *fn_init, void *fn_process, void *fn_complete);
|
||||
|
||||
typedef int (*fn_reqbody_processor_init_t)(modsec_rec *msr, char **error_msg);
|
||||
typedef int (*fn_reqbody_processor_process_t)(modsec_rec *msr, const char *buf,
|
||||
unsigned int size, char **error_msg);
|
||||
typedef int (*fn_reqbody_processor_complete_t)(modsec_rec *msr, char **error_msg);
|
||||
|
||||
struct msre_reqbody_processor_metadata {
|
||||
const char *name;
|
||||
fn_reqbody_processor_init_t init;
|
||||
fn_reqbody_processor_process_t process;
|
||||
fn_reqbody_processor_complete_t complete;
|
||||
};
|
||||
|
||||
/* -- MSRE Function Prototypes ---------------------------------------------- */
|
||||
|
||||
msre_var_metadata DSOLOCAL *msre_resolve_var(msre_engine *engine, const char *name);
|
||||
|
Reference in New Issue
Block a user