/lib/modsecurity-apache_2.6.1/apache2/re_operators.c
C | 3908 lines | 2693 code | 706 blank | 509 comment | 915 complexity | 05d1cb24dc3e8fcd1044bde4468344d8 MD5 | raw file
Possible License(s): Apache-2.0
Large files files are truncated, but you can click here to view the full file
- /*
- * ModSecurity for Apache 2.x, http://www.modsecurity.org/
- * Copyright (c) 2004-2011 Trustwave Holdings, Inc. (http://www.trustwave.com/)
- *
- * You may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * If any of the files related to licensing are missing or if you have any
- * other questions related to licensing please contact Trustwave Holdings, Inc.
- * directly using the email address security@modsecurity.org.
- */
- #include "re.h"
- #include "msc_pcre.h"
- #include "msc_geo.h"
- #include "msc_gsb.h"
- #include "apr_lib.h"
- #include "apr_strmatch.h"
- #include "acmp.h"
- #include "msc_util.h"
- #if !defined(WIN32) || !defined(WINNT)
- #include <regex.h>
- #include <arpa/inet.h>
- #endif
- /**
- *
- */
- void msre_engine_op_register(msre_engine *engine, const char *name,
- fn_op_param_init_t fn1, fn_op_execute_t fn2)
- {
- msre_op_metadata *metadata = (msre_op_metadata *)apr_pcalloc(engine->mp,
- sizeof(msre_op_metadata));
- if (metadata == NULL) return;
- metadata->name = name;
- metadata->param_init = fn1;
- metadata->execute = fn2;
- apr_table_setn(engine->operators, name, (void *)metadata);
- }
- /**
- *
- */
- msre_op_metadata *msre_engine_op_resolve(msre_engine *engine, const char *name) {
- return (msre_op_metadata *)apr_table_get(engine->operators, name);
- }
- /* -- Operators -- */
- /* unconditionalMatch */
- static int msre_op_unconditionalmatch_execute(modsec_rec *msr, msre_rule *rule,
- msre_var *var, char **error_msg)
- {
- *error_msg = "Unconditional match in SecAction.";
- /* Always match. */
- return 1;
- }
- /* noMatch */
- static int msre_op_nomatch_execute(modsec_rec *msr, msre_rule *rule,
- msre_var *var, char **error_msg)
- {
- *error_msg = "No match.";
- /* Never match. */
- return 0;
- }
- /* ipmatch */
- /*
- * \brief Init function to ipmatch operator
- *
- * \param rule Pointer to the rule
- * \param error_msg Pointer to error msg
- *
- * \retval 1 On Success
- * \retval 0 On Fail
- */
- static int msre_op_ipmatch_param_init(msre_rule *rule, char **error_msg) {
- apr_status_t rv;
- char *str = NULL;
- char *saved = NULL;
- char *param = NULL;
- msre_ipmatch *current;
- msre_ipmatch **last = &rule->ip_op;
- if (error_msg == NULL)
- return -1;
- else
- *error_msg = NULL;
- param = apr_pstrdup(rule->ruleset->mp, rule->op_param);
- str = apr_strtok(param, ",", &saved);
- while( str != NULL) {
- const char *ipstr, *mask, *sep;
- /* get the IP address and mask strings */
- sep = strchr(str, '/');
- if (sep) {
- ipstr = apr_pstrndup(rule->ruleset->mp, str, (sep - str) );
- mask = apr_pstrdup(rule->ruleset->mp, (sep + 1) );
- }
- else {
- ipstr = apr_pstrdup(rule->ruleset->mp, str);
- mask = NULL;
- }
- /* create a new msre_ipmatch containing a new apr_ipsubnet_t*, and add it to the linked list */
- current = apr_pcalloc(rule->ruleset->mp, sizeof(msre_ipmatch));
- rv = apr_ipsubnet_create(¤t->ipsubnet, ipstr, mask, rule->ruleset->mp);
- if ( rv != APR_SUCCESS ) {
- char msgbuf[120];
- apr_strerror(rv, msgbuf, sizeof msgbuf);
- *error_msg = apr_pstrcat(rule->ruleset->mp, "Error: ", msgbuf, NULL);
- return -1;
- }
- current->address = str;
- current->next = NULL;
- *last = current;
- last = ¤t->next;
- str = apr_strtok(NULL, ",",&saved);
- }
- return 1;
- }
- /*
- * \brief Execution function to ipmatch operator
- *
- * \param msr Pointer internal modsec request structure
- * \param rule Pointer to the rule
- * \param var Pointer to variable structure
- * \param error_msg Pointer to error msg
- *
- * \retval -1 On Failure
- * \retval 1 On Match
- * \retval 0 On No Match
- */
- static int msre_op_ipmatch_execute(modsec_rec *msr, msre_rule *rule, msre_var *var, char **error_msg) {
- msre_ipmatch *current = rule->ip_op;
- apr_sockaddr_t *sa;
- if (error_msg == NULL)
- return -1;
- else
- *error_msg = NULL;
- if(current == NULL) {
- msr_log(msr, 1, "ipMatch Internal Error: ipmatch value is null.");
- return 0;
- }
- /* create an apr_sockaddr_t for the value string */
- if ( apr_sockaddr_info_get(&sa, var->value, APR_UNSPEC, 0, 0, msr->mp) != APR_SUCCESS ) {
- msr_log(msr, 1, "ipMatch Internal Error: Invalid ip address.");
- return 0;
- }
- /* look through the linked list for a match */
- while (current) {
- if (apr_ipsubnet_test(current->ipsubnet, sa)) {
- *error_msg = apr_psprintf(msr->mp, "IPmatch \"%s\" matched \"%s\" at %s.", var->value, current->address, var->name);
- return 1;
- }
- current = current->next;
- }
- return 0;
- }
- /* rsub */
- static char *param_remove_escape(msre_rule *rule, char *str, int len) {
- char *parm = apr_palloc(rule->ruleset->mp, len);
- char *ret = parm;
- for(;*str!='\0';str++) {
- if(*str != '\\') {
- *parm++ = *str;
- } else {
- str++;
- if(*str != '/') {
- str--;
- *parm++ = *str;
- } else {
- *parm++ = *str;
- }
- }
- }
- *parm = '\0';
- return ret;
- }
- /*
- * \brief Init function to rsub operator
- *
- * \param rule Pointer to the rule
- * \param error_msg Pointer to error msg
- *
- * \retval 1 On Success
- * \retval 0 On Fail
- */
- #if !defined(MSC_TEST)
- static int msre_op_rsub_param_init(msre_rule *rule, char **error_msg) {
- ap_regex_t *regex;
- const char *pattern = NULL;
- const char *line = NULL;
- char *reg_pattern = NULL;
- char *replace = NULL;
- char *e_pattern = NULL;
- char *e_replace = NULL;
- char *flags = NULL;
- char *data = NULL;
- char delim;
- int ignore_case = 0;
- if (error_msg == NULL) return -1;
- *error_msg = NULL;
- line = rule->op_param;
- if (apr_tolower(*line) != 's') {
- *error_msg = apr_psprintf(rule->ruleset->mp, "Error rsub operator format, must be s/ pattern");
- return 0;
- }
- data = apr_pstrdup(rule->ruleset->mp, line);
- delim = *++data;
- if (delim)
- reg_pattern = ++data;
- if (reg_pattern) {
- if (*data != delim) {
- for(;*data != '\0' ;data++) {
- if(*data == delim) {
- data--;
- if(*data == '\\') {
- data++;
- continue;
- }
- break;
- }
- }
- }
- if (*data) {
- *++data = '\0';
- ++data;
- replace = data;
- }
- }
- if (replace) {
- if (*data != delim) {
- for(;*data != '\0' ;data++) {
- if(*data == delim) {
- data--;
- if(*data == '\\') {
- data++;
- continue;
- }
- break;
- }
- }
- }
- if (*data) {
- *++data = '\0';
- flags = ++data;
- }
- }
- if (!delim || !reg_pattern || !replace) {
- *error_msg = apr_psprintf(rule->ruleset->mp, "Error rsub operator format - must be s/regex/str/[flags]");
- return -1;
- }
- e_replace = param_remove_escape(rule, replace, strlen(replace));
- rule->sub_str = apr_pstrmemdup(rule->ruleset->mp, e_replace, strlen(e_replace));
- if (flags) {
- while (*flags) {
- delim = apr_tolower(*flags);
- if (delim == 'i')
- ignore_case = 1;
- else if (delim == 'd')
- rule->escape_re = 1;
- else
- *error_msg = apr_psprintf(rule->ruleset->mp, "Regex flag not supported");
- flags++;
- }
- }
- e_pattern = param_remove_escape(rule, reg_pattern, strlen(reg_pattern));
- pattern = apr_pstrndup(rule->ruleset->mp, e_pattern, strlen(e_pattern));
- if(strstr(pattern,"%{") == NULL) {
- regex = ap_pregcomp(rule->ruleset->mp, pattern, AP_REG_EXTENDED |
- (ignore_case ? AP_REG_ICASE : 0));
- rule->sub_regex = regex;
- } else {
- rule->re_precomp = 1;
- rule->re_str = apr_pstrndup(rule->ruleset->mp, pattern, strlen(pattern));
- rule->sub_regex = NULL;
- }
- return 1; /* OK */
- }
- /*
- * \brief Execution function to rsub operator
- *
- * \param msr Pointer internal modsec request structure
- * \param rule Pointer to the rule
- * \param var Pointer to variable structure
- * \param error_msg Pointer to error msg
- *
- * \retval -1 On Failure
- * \retval 1 On Match
- * \retval 0 On No Match
- */
- static int msre_op_rsub_execute(modsec_rec *msr, msre_rule *rule, msre_var *var, char **error_msg) {
- msc_string *str = (msc_string *)apr_pcalloc(msr->mp, sizeof(msc_string));
- msc_string *re_pattern = (msc_string *)apr_pcalloc(msr->mp, sizeof(msc_string));
- char *offset = NULL;
- int sub = 0, so = 0, p_len = 0;
- char *replace = NULL;
- char *data = NULL, *pattern = NULL;
- unsigned int size = var->value_len;
- int output_body = 0, input_body = 0, count = 0;
- ap_regmatch_t pmatch[AP_MAX_REG_MATCH];
- if (error_msg == NULL) return -1;
- *error_msg = NULL;
- if(strcmp(var->name,"STREAM_OUTPUT_BODY") == 0 ) {
- output_body = 1;
- } else if(strcmp(var->name,"STREAM_INPUT_BODY") == 0 ) {
- input_body = 1;
- } else {
- msr_log(msr,9,"Operator rsub only works with STREAM_* variables");
- return -1;
- }
- if(rule->re_precomp == 1) {
- re_pattern->value = apr_pstrndup(msr->mp, rule->re_str, strlen(rule->re_str));
- re_pattern->value_len = strlen(re_pattern->value);
- expand_macros(msr, re_pattern, rule, msr->mp);
- if(strlen(re_pattern->value) > 0) {
- if(rule->escape_re == 1) {
- pattern = log_escape_re(msr->mp, re_pattern->value);
- if (msr->txcfg->debuglog_level >= 6) {
- msr_log(msr, 6, "Escaping pattern [%s]",pattern);
- }
- rule->sub_regex = ap_pregcomp(msr->mp, pattern, AP_REG_EXTENDED);
- } else {
- rule->sub_regex = ap_pregcomp(msr->mp, re_pattern->value, AP_REG_EXTENDED);
- }
- }
- else {
- rule->sub_regex = NULL;
- }
- }
- if(rule->sub_regex == NULL) {
- *error_msg = "Internal Error: regex data is null.";
- return 0;
- }
- str->value = apr_pstrndup(msr->mp, rule->sub_str, strlen(rule->sub_str));
- str->value_len = strlen(str->value);
- if(strstr(rule->sub_str,"%{") != NULL)
- expand_macros(msr, str, rule, msr->mp);
- replace = apr_pstrndup(msr->mp, str->value, str->value_len);
- data = apr_pcalloc(msr->mp, var->value_len+(AP_MAX_REG_MATCH*strlen(replace))+1);
- if(replace == NULL || data == NULL) {
- *error_msg = "Internal Error: cannot allocate memory";
- return -1;
- }
- memcpy(data,var->value,var->value_len);
- size += (AP_MAX_REG_MATCH*strlen(replace)+2);
- if (ap_regexec(rule->sub_regex, data ,AP_MAX_REG_MATCH, pmatch, 0)) return 0;
- for (offset = replace; *offset; offset++)
- if (*offset == '\\' && *(offset + 1) > '0' && *(offset + 1) <= '9') {
- so = pmatch [*(offset + 1) - 48].rm_so;
- p_len = pmatch [*(offset + 1) - 48].rm_eo - so;
- if (so < 0 || strlen (replace) + p_len - 1 > size) return 0;
- memmove (offset + p_len, offset + 2, strlen (offset) - 1);
- memmove (offset, data + so, p_len);
- offset = offset + p_len - 2;
- }
- sub = -1;
- for (offset = data; !ap_regexec(rule->sub_regex, offset, 1, pmatch, 0); ) {
- p_len = pmatch [0].rm_eo - pmatch [0].rm_so;
- count++;
- offset += pmatch [0].rm_so;
- if (var->value_len - p_len + strlen(replace) + 1 > size) return 0;
- memmove (offset + strlen (replace), offset + p_len, strlen (offset) - p_len + 1);
- memmove (offset, replace, strlen (replace));
- offset += strlen (replace);
- if (sub >= 0) break;
- }
- size -= (((AP_MAX_REG_MATCH - count)*(strlen(replace))) + p_len+2);
- if(msr->stream_output_data != NULL && output_body == 1) {
- char *stream_output_data = NULL;
- stream_output_data = (char *)realloc(msr->stream_output_data, size+1);
- msr->stream_output_length = size;
- if(stream_output_data == NULL) {
- free (msr->stream_output_data);
- msr->stream_output_data = NULL;
- return -1;
- }
- var->value_len = size;
- msr->of_stream_changed = 1;
- msr->stream_output_data = (char *)stream_output_data;
- if(msr->stream_output_data != NULL)
- apr_cpystrn(msr->stream_output_data, data, size);
- }
- if(msr->stream_input_data != NULL && input_body == 1) {
- char *stream_input_data = NULL;
- stream_input_data = (char *)realloc(msr->stream_input_data, size+1);
- msr->stream_input_length = size;
- if(stream_input_data == NULL) {
- free (msr->stream_input_data);
- msr->stream_input_data = NULL;
- return -1;
- }
- var->value_len = size;
- msr->stream_input_data = (char *)stream_input_data;
- if(msr->stream_input_data != NULL)
- apr_cpystrn(msr->stream_input_data, data, size);
- msr->if_stream_changed = 1;
- }
- if (! *error_msg) {
- *error_msg = apr_psprintf(msr->mp, "Operator rsub succeeded.");
- }
- return 1;
- }
- #endif /* MSC_TEST */
- /* rx */
- static int msre_op_rx_param_init(msre_rule *rule, char **error_msg) {
- const char *errptr = NULL;
- int erroffset;
- msc_regex_t *regex;
- const char *pattern = rule->op_param;
- if (error_msg == NULL) return -1;
- *error_msg = NULL;
- /* Compile pattern */
- regex = msc_pregcomp_ex(rule->ruleset->mp, pattern, PCRE_DOTALL | PCRE_DOLLAR_ENDONLY, &errptr, &erroffset, msc_pcre_match_limit, msc_pcre_match_limit_recursion);
- if (regex == NULL) {
- *error_msg = apr_psprintf(rule->ruleset->mp, "Error compiling pattern (offset %d): %s",
- erroffset, errptr);
- return 0;
- }
- rule->op_param_data = regex;
- return 1; /* OK */
- }
- static int msre_op_rx_execute(modsec_rec *msr, msre_rule *rule, msre_var *var, char **error_msg) {
- msc_regex_t *regex = (msc_regex_t *)rule->op_param_data;
- const char *target;
- unsigned int target_length;
- char *my_error_msg = NULL;
- int ovector[33];
- int capture = 0;
- int matched_bytes = 0;
- int matched = 0;
- int rc;
- char *qspos = NULL;
- const char *parm = NULL;
- msc_parm *mparm = NULL;
- if (error_msg == NULL) return -1;
- *error_msg = NULL;
- if (regex == NULL) {
- *error_msg = "Internal Error: regex data is null.";
- return -1;
- }
- /* If the given target is null run against an empty
- * string. This is a behaviour consistent with previous
- * releases.
- */
- if (var->value == NULL) {
- target = "";
- target_length = 0;
- } else {
- target = var->value;
- target_length = var->value_len;
- }
- /* Are we supposed to capture subexpressions? */
- capture = apr_table_get(rule->actionset->actions, "capture") ? 1 : 0;
- matched_bytes = apr_table_get(rule->actionset->actions, "sanitizeMatchedBytes") ? 1 : 0;
- matched = apr_table_get(rule->actionset->actions, "sanitizeMatched") ? 1 : 0;
- /* Show when the regex captures but "capture" is not set */
- if (msr->txcfg->debuglog_level >= 6) {
- int capcount = 0;
- rc = msc_fullinfo(regex, PCRE_INFO_CAPTURECOUNT, &capcount);
- if (msr->txcfg->debuglog_level >= 6) {
- if ((capture == 0) && (capcount > 0)) {
- msr_log(msr, 6, "Ignoring regex captures since \"capture\" action is not enabled.");
- }
- }
- }
- /* We always use capture so that ovector can be used as working space
- * and no memory has to be allocated for any backreferences.
- */
- rc = msc_regexec_capture(regex, target, target_length, ovector, 30, &my_error_msg);
- if ((rc == PCRE_ERROR_MATCHLIMIT) || (rc == PCRE_ERROR_RECURSIONLIMIT)) {
- msc_string *s = (msc_string *)apr_pcalloc(msr->mp, sizeof(msc_string));
- if (s == NULL) return -1;
- s->name = apr_pstrdup(msr->mp, "MSC_PCRE_LIMITS_EXCEEDED");
- s->name_len = strlen(s->name);
- s->value = apr_pstrdup(msr->mp, "1");
- s->value_len = 1;
- if ((s->name == NULL)||(s->value == NULL)) return -1;
- apr_table_setn(msr->tx_vars, s->name, (void *)s);
- *error_msg = apr_psprintf(msr->mp,
- "Rule %pp [id \"%s\"][file \"%s\"][line \"%d\"] - "
- "Execution error - "
- "PCRE limits exceeded (%d): %s",
- rule,((rule->actionset != NULL)&&(rule->actionset->id != NULL)) ? rule->actionset->id : "-",
- rule->filename != NULL ? rule->filename : "-",
- rule->line_num,rc, my_error_msg);
- msr_log(msr, 3, "%s.", *error_msg);
- return 0; /* No match. */
- }
- else if (rc < -1) {
- *error_msg = apr_psprintf(msr->mp, "Regex execution failed (%d): %s",
- rc, my_error_msg);
- return -1;
- }
- /* Handle captured subexpressions. */
- if (capture && rc > 0) {
- int i;
- /* Unset any of the previously set capture variables. */
- apr_table_unset(msr->tx_vars, "0");
- apr_table_unset(msr->tx_vars, "1");
- apr_table_unset(msr->tx_vars, "2");
- apr_table_unset(msr->tx_vars, "3");
- apr_table_unset(msr->tx_vars, "4");
- apr_table_unset(msr->tx_vars, "5");
- apr_table_unset(msr->tx_vars, "6");
- apr_table_unset(msr->tx_vars, "7");
- apr_table_unset(msr->tx_vars, "8");
- apr_table_unset(msr->tx_vars, "9");
- /* Use the available captures. */
- for(i = 0; i < rc; i++) {
- msc_string *s = (msc_string *)apr_pcalloc(msr->mp, sizeof(msc_string));
- if (s == NULL) return -1;
- s->name = apr_psprintf(msr->mp, "%d", i);
- s->name_len = strlen(s->name);
- s->value = apr_pstrmemdup(msr->mp,
- target + ovector[2 * i], ovector[2 * i + 1] - ovector[2 * i]);
- s->value_len = (ovector[2 * i + 1] - ovector[2 * i]);
- if ((s->name == NULL)||(s->value == NULL)) return -1;
- apr_table_addn(msr->tx_vars, s->name, (void *)s);
- if(((matched == 1) || (matched_bytes == 1)) && (var != NULL) && (var->name != NULL)) {
- qspos = apr_psprintf(msr->mp, "%s", var->name);
- parm = strstr(qspos, ":");
- if (parm != NULL) {
- parm++;
- mparm = apr_palloc(msr->mp, sizeof(msc_parm));
- if (mparm == NULL)
- continue;
- mparm->value = apr_pstrmemdup(msr->mp,s->value,s->value_len);
- mparm->pad_1 = rule->actionset->arg_min;
- mparm->pad_2 = rule->actionset->arg_max;
- apr_table_addn(msr->pattern_to_sanitize, parm, (void *)mparm);
- } else {
- mparm = apr_palloc(msr->mp, sizeof(msc_parm));
- if (mparm == NULL)
- continue;
- mparm->value = apr_pstrmemdup(msr->mp,s->value,s->value_len);
- apr_table_addn(msr->pattern_to_sanitize, qspos, (void *)mparm);
- }
- }
- if (msr->txcfg->debuglog_level >= 9) {
- msr_log(msr, 9, "Added regex subexpression to TX.%d: %s", i,
- log_escape_nq_ex(msr->mp, s->value, s->value_len));
- }
- }
- }
- if (rc != PCRE_ERROR_NOMATCH) { /* Match. */
- /* We no longer escape the pattern here as it is done when logging */
- char *pattern = apr_pstrdup(msr->mp, log_escape(msr->mp, regex->pattern ? regex->pattern : "<Unknown Match>"));
- /* This message will be logged. */
- if (strlen(pattern) > 252) {
- *error_msg = apr_psprintf(msr->mp, "Pattern match \"%.252s ...\" at %s.",
- pattern, var->name);
- } else {
- *error_msg = apr_psprintf(msr->mp, "Pattern match \"%s\" at %s.",
- pattern, var->name);
- }
- return 1;
- }
- /* No match. */
- return 0;
- }
- /* pm */
- static char *parse_pm_content(const char *op_parm, unsigned short int op_len, msre_rule *rule, char **error_msg) {
- char *parm = NULL;
- char *content = NULL;
- unsigned short int offset = 0;
- char converted = 0;
- int i, x;
- unsigned char bin = 0, esc = 0, bin_offset = 0;
- unsigned char bin_parm[3], c = 0;
- char *processed = NULL;
- content = apr_pstrdup(rule->ruleset->mp, op_parm);
- if (content == NULL) {
- *error_msg = apr_psprintf(rule->ruleset->mp, "Error allocating memory for pattern matching content.");
- return NULL;
- }
- while (offset < op_len && apr_isspace(content[offset])) {
- offset++;
- };
- op_len = strlen(content);
- if (content[offset] == '\"' && content[op_len-1] == '\"') {
- parm = apr_pstrdup(rule->ruleset->mp, content + offset + 1);
- if (parm == NULL) {
- *error_msg = apr_psprintf(rule->ruleset->mp, "Error allocating memory for pattern matching content.");
- return NULL;
- }
- parm[op_len - offset - 2] = '\0';
- } else {
- parm = apr_pstrdup(rule->ruleset->mp, content + offset);
- if (parm == NULL) {
- *error_msg = apr_psprintf(rule->ruleset->mp, "Error allocating memory for pattern matching content.");
- return NULL;
- }
- }
- op_len = strlen(parm);
- if (op_len == 0) {
- *error_msg = apr_psprintf(rule->ruleset->mp, "Content length is 0.");
- return NULL;
- }
- for (i = 0, x = 0; i < op_len; i++) {
- if (parm[i] == '|') {
- if (bin) {
- bin = 0;
- } else {
- bin = 1;
- }
- } else if(!esc && parm[i] == '\\') {
- esc = 1;
- } else {
- if (bin) {
- if (apr_isdigit(parm[i]) ||
- parm[i] == 'A' || parm[i] == 'a' ||
- parm[i] == 'B' || parm[i] == 'b' ||
- parm[i] == 'C' || parm[i] == 'c' ||
- parm[i] == 'D' || parm[i] == 'd' ||
- parm[i] == 'E' || parm[i] == 'e' ||
- parm[i] == 'F' || parm[i] == 'f')
- {
- bin_parm[bin_offset] = (char)parm[i];
- bin_offset++;
- if (bin_offset == 2) {
- c = strtol((char *)bin_parm, (char **) NULL, 16) & 0xFF;
- bin_offset = 0;
- parm[x] = c;
- x++;
- converted = 1;
- }
- } else if (parm[i] == ' ') {
- }
- } else if (esc) {
- if (parm[i] == ':' ||
- parm[i] == ';' ||
- parm[i] == '\\' ||
- parm[i] == '\"')
- {
- parm[x] = parm[i];
- x++;
- } else {
- *error_msg = apr_psprintf(rule->ruleset->mp, "Unsupported escape sequence.");
- return NULL;
- }
- esc = 0;
- converted = 1;
- } else {
- parm[x] = parm[i];
- x++;
- }
- }
- }
- if (converted) {
- op_len = x;
- }
- processed = apr_pstrmemdup(rule->ruleset->mp, parm, op_len);
- if (processed == NULL) {
- *error_msg = apr_psprintf(rule->ruleset->mp, "Error allocating memory for pattern matching content.");
- return NULL;
- }
- return processed;
- }
- static int msre_op_pm_param_init(msre_rule *rule, char **error_msg) {
- ACMP *p;
- const char *phrase;
- const char *next;
- unsigned short int op_len;
- if ((rule->op_param == NULL)||(strlen(rule->op_param) == 0)) {
- *error_msg = apr_psprintf(rule->ruleset->mp, "Missing parameter for operator 'pm'.");
- return 0; /* ERROR */
- }
- op_len = strlen(rule->op_param);
- p = acmp_create(0, rule->ruleset->mp);
- if (p == NULL) return 0;
- phrase = apr_pstrdup(rule->ruleset->mp, parse_pm_content(rule->op_param, op_len, rule, error_msg));
- if(phrase == NULL)
- phrase = apr_pstrdup(rule->ruleset->mp, rule->op_param);
- /* Loop through phrases */
- /* ENH: Need to allow quoted phrases w/space */
- for (;;) {
- while((apr_isspace(*phrase) != 0) && (*phrase != '\0')) phrase++;
- if (*phrase == '\0') break;
- next = phrase;
- while((apr_isspace(*next) == 0) && (*next != 0)) next++;
- acmp_add_pattern(p, phrase, NULL, NULL, next - phrase);
- phrase = next;
- }
- acmp_prepare(p);
- rule->op_param_data = p;
- return 1;
- }
- /* pmFromFile */
- static int msre_op_pmFromFile_param_init(msre_rule *rule, char **error_msg) {
- char errstr[1024];
- char buf[HUGE_STRING_LEN + 1];
- char *fn;
- char *next;
- char *start;
- char *end;
- const char *rulefile_path;
- char *processed = NULL;
- unsigned short int op_len;
- apr_status_t rc;
- apr_file_t *fd;
- ACMP *p;
- if ((rule->op_param == NULL)||(strlen(rule->op_param) == 0)) {
- *error_msg = apr_psprintf(rule->ruleset->mp, "Missing parameter for operator 'pmFromFile'.");
- return 0; /* ERROR */
- }
- p = acmp_create(0, rule->ruleset->mp);
- if (p == NULL) return 0;
- fn = apr_pstrdup(rule->ruleset->mp, rule->op_param);
- /* Get the path of the rule filename to use as a base */
- rulefile_path = apr_pstrndup(rule->ruleset->mp, rule->filename, strlen(rule->filename) - strlen(apr_filepath_name_get(rule->filename)));
- #ifdef DEBUG_CONF
- fprintf(stderr, "Rulefile path: \"%s\"\n", rulefile_path);
- #endif
- /* Loop through filenames */
- /* ENH: Need to allow quoted filenames w/space */
- for (;;) {
- const char *rootpath = NULL;
- const char *filepath = NULL;
- int line = 0;
- /* Trim whitespace */
- while((apr_isspace(*fn) != 0) && (*fn != '\0')) fn++;
- if (*fn == '\0') break;
- next = fn;
- while((apr_isspace(*next) == 0) && (*next != '\0')) next++;
- while((apr_isspace(*next) != 0) && (*next != '\0')) *(next++) = '\0';
- /* Add path of the rule filename for a relative phrase filename */
- filepath = fn;
- if (apr_filepath_root(&rootpath, &filepath, APR_FILEPATH_TRUENAME, rule->ruleset->mp) != APR_SUCCESS) {
- /* We are not an absolute path. It could mean an error, but
- * let that pass through to the open call for a better error */
- apr_filepath_merge(&fn, rulefile_path, fn, APR_FILEPATH_TRUENAME, rule->ruleset->mp);
- }
- /* Open file and read */
- rc = apr_file_open(&fd, fn, APR_READ | APR_BUFFERED | APR_FILE_NOCLEANUP, 0, rule->ruleset->mp);
- if (rc != APR_SUCCESS) {
- *error_msg = apr_psprintf(rule->ruleset->mp, "Could not open phrase file \"%s\": %s", fn, apr_strerror(rc, errstr, 1024));
- return 0;
- }
- #ifdef DEBUG_CONF
- fprintf(stderr, "Loading phrase file: \"%s\"\n", fn);
- #endif
- /* Read one pattern per line skipping empty/commented */
- for(;;) {
- line++;
- rc = apr_file_gets(buf, HUGE_STRING_LEN, fd);
- if (rc == APR_EOF) break;
- if (rc != APR_SUCCESS) {
- *error_msg = apr_psprintf(rule->ruleset->mp, "Could not read \"%s\" line %d: %s", fn, line, apr_strerror(rc, errstr, 1024));
- return 0;
- }
- op_len = strlen(buf);
- processed = apr_pstrdup(rule->ruleset->mp, parse_pm_content(buf, op_len, rule, error_msg));
- /* Trim Whitespace */
- if(processed != NULL)
- start = processed;
- else
- start = buf;
- while ((apr_isspace(*start) != 0) && (*start != '\0')) start++;
- if(processed != NULL)
- end = processed + strlen(processed);
- else
- end = buf + strlen(buf);
- if (end > start) end--;
- while ((end > start) && (apr_isspace(*end) != 0)) end--;
- if (end > start) {
- *(++end) = '\0';
- }
- /* Ignore empty lines and comments */
- if ((start == end) || (*start == '#')) continue;
- acmp_add_pattern(p, start, NULL, NULL, (end - start));
- }
- fn = next;
- }
- if (fd != NULL) apr_file_close(fd);
- acmp_prepare(p);
- rule->op_param_data = p;
- return 1;
- }
- static int msre_op_pm_execute(modsec_rec *msr, msre_rule *rule, msre_var *var, char **error_msg) {
- const char *match = NULL;
- apr_status_t rc = 0;
- int capture;
- ACMPT pt;
- /* Nothing to read */
- if ((var->value == NULL) || (var->value_len == 0)) return 0;
- /* Are we supposed to capture subexpressions? */
- capture = apr_table_get(rule->actionset->actions, "capture") ? 1 : 0;
- pt.parser = (ACMP *)rule->op_param_data;
- pt.ptr = NULL;
- rc = acmp_process_quick(&pt, &match, var->value, var->value_len);
- if (rc) {
- char *match_escaped = log_escape(msr->mp, match ? match : "<Unknown Match>");
- /* This message will be logged. */
- if (strlen(match_escaped) > 252) {
- *error_msg = apr_psprintf(msr->mp, "Matched phrase \"%.252s ...\" at %s.",
- match_escaped, var->name);
- } else {
- *error_msg = apr_psprintf(msr->mp, "Matched phrase \"%s\" at %s.",
- match_escaped, var->name);
- }
- /* Handle capture as tx.0=match */
- if (capture) {
- int i;
- msc_string *s = (msc_string *)apr_pcalloc(msr->mp, sizeof(msc_string));
- if (s == NULL) return -1;
- s->name = "0";
- s->name_len = strlen(s->name);
- s->value = apr_pstrdup(msr->mp, match);
- if (s->value == NULL) return -1;
- s->value_len = strlen(s->value);
- apr_table_setn(msr->tx_vars, s->name, (void *)s);
- if (msr->txcfg->debuglog_level >= 9) {
- msr_log(msr, 9, "Added phrase match to TX.0: %s",
- log_escape_nq_ex(msr->mp, s->value, s->value_len));
- }
- /* Unset the remaining ones (from previous invocations). */
- for(i = rc; i <= 9; i++) {
- char buf[2];
- apr_snprintf(buf, sizeof(buf), "%d", i);
- apr_table_unset(msr->tx_vars, buf);
- }
- }
- return 1;
- }
- return rc;
- }
- /* gsbLookup */
- /*
- * \brief Reduce /./ to /
- *
- * \param pool Pointer to the memory pool
- * \param domain Input data
- *
- * \retval domain On Failure
- * \retval url On Success
- */
- static const char *gsb_replace_tpath(apr_pool_t *pool, const char *domain, int len) {
- char *pos = NULL, *data = NULL;
- char *url = NULL;
- int match = 0;
- url = apr_palloc(pool, len + 1);
- data = apr_palloc(pool, len + 1);
- memset(data, 0, len+1);
- memset(url, 0, len+1);
- memcpy(url, domain, len);
- while(( pos = strstr(url , "/./" )) != NULL) {
- match = 1;
- data[0] = '\0';
- strncat(data, url, pos - url);
- strcat(data , "/");
- strcat(data ,pos + strlen("/./"));
- strncpy(url , data, len);
- }
- if(match == 0)
- return domain;
- return url;
- }
- /*
- * \brief Reduce doble dot to single dot
- *
- * \param msr Pointer to the modsec resource
- * \param domain Input data
- *
- * \retval domain On Failure
- * \retval reduced On Success
- */
- static const char *gsb_reduce_char(apr_pool_t *pool, const char *domain) {
- char *ptr = apr_pstrdup(pool, domain);
- char *data = NULL;
- char *reduced = NULL;
- int skip = 0;
- if(ptr == NULL)
- return domain;
- data = apr_pcalloc(pool, strlen(ptr));
- if(data == NULL)
- return domain;
- reduced = data;
- while(*ptr != '\0') {
- switch(*ptr) {
- case '.':
- ptr++;
- if(*ptr == '.')
- skip = 1;
- ptr--;
- break;
- case '/':
- ptr++;
- if(*ptr == '/')
- skip = 1;
- ptr--;
- break;
- }
- if(skip == 0) {
- *data = *ptr;
- data++;
- }
- ptr++;
- skip = 0;
- }
- *data = '\0'; --data;
- if(*data == '.')
- *data = '\0';
- else
- ++data;
- return reduced;
- }
- /*
- * \brief Verify function to gsbLookup operator
- *
- * \param msr Pointer to the modsec resource
- * \param match Pointer to input data
- * \param match_length Input size
- *
- * \retval -1 On Failure
- * \retval 1 On Match
- * \retval 0 On No Match
- */
- static int verify_gsb(gsb_db *gsb, modsec_rec *msr, const char *match, unsigned int match_length) {
- apr_md5_ctx_t ctx;
- apr_status_t rc;
- unsigned char digest[APR_MD5_DIGESTSIZE];
- const char *hash = NULL;
- const char *search = NULL;
- memset(digest, 0, sizeof(digest));
- apr_md5_init(&ctx);
- if ((rc = apr_md5_update(&ctx, match, match_length)) != APR_SUCCESS)
- return -1;
- apr_md5_final(digest, &ctx);
- hash = apr_psprintf(msr->mp, "%s", bytes2hex(msr->mp, digest, 16));
- if ((hash != NULL) && (gsb->gsb_table != NULL)) {
- search = apr_hash_get(gsb->gsb_table, hash, APR_HASH_KEY_STRING);
- if (search != NULL)
- return 1;
- }
- return 0;
- }
- /*
- * \brief Init function to gsbLookup operator
- *
- * \param rule Pointer to the rule
- * \param error_msg Pointer to error msg
- *
- * \retval 1 On Success
- * \retval 0 On Fail
- */
- static int msre_op_gsbLookup_param_init(msre_rule *rule, char **error_msg) {
- const char *errptr = NULL;
- int erroffset;
- msc_regex_t *regex;
- if (error_msg == NULL) return -1;
- *error_msg = NULL;
- /* Compile rule->op_param */
- regex = msc_pregcomp_ex(rule->ruleset->mp, rule->op_param, PCRE_DOTALL | PCRE_MULTILINE, &errptr, &erroffset, msc_pcre_match_limit, msc_pcre_match_limit_recursion);
- if (regex == NULL) {
- *error_msg = apr_psprintf(rule->ruleset->mp, "Error compiling pattern (offset %d): %s",
- erroffset, errptr);
- return 0;
- }
- rule->op_param_data = regex;
- return 1; /* OK */
- }
- /*
- * \brief Execution function to gsbLookup operator
- *
- * \param msr Pointer internal modsec request structure
- * \param rule Pointer to the rule
- * \param var Pointer to variable structure
- * \param error_msg Pointer to error msg
- *
- * \retval -1 On Failure
- * \retval 1 On Match
- * \retval 0 On No Match
- */
- static int msre_op_gsbLookup_execute(modsec_rec *msr, msre_rule *rule, msre_var *var, char **error_msg) {
- msc_regex_t *regex = (msc_regex_t *)rule->op_param_data;
- char *my_error_msg = NULL;
- int ovector[33];
- unsigned int offset = 0;
- gsb_db *gsb = msr->txcfg->gsb;
- const char *match = NULL;
- unsigned int match_length;
- unsigned int canon_length;
- int rv, i, ret, count_slash;
- unsigned int j = 0;
- unsigned int size = var->value_len;
- char *base = NULL, *domain = NULL, *savedptr = NULL;
- char *str = NULL, *canon = NULL, *dot = NULL;
- char *data = NULL, *ptr = NULL, *url = NULL;
- int capture, domain_len;
- int d_pos = -1;
- int s_pos = -1;
- if (error_msg == NULL) return -1;
- *error_msg = NULL;
- if(regex == NULL) {
- *error_msg = "Internal Error: regex is null.";
- return 0;
- }
- if(gsb == NULL) {
- msr_log(msr, 1, "GSB lookup failed without a database. Set SecGsbLookupDB.");
- return 0;
- }
- data = apr_pcalloc(rule->ruleset->mp, var->value_len+1);
- if(data == NULL) {
- *error_msg = "Internal Error: cannot allocate memory for data.";
- return -1;
- }
- capture = apr_table_get(rule->actionset->actions, "capture") ? 1 : 0;
- memcpy(data,var->value,var->value_len);
- while (offset < size && (rv = msc_regexec_ex(regex, data, size, offset, PCRE_NOTEMPTY, ovector, 30, &my_error_msg)) >= 0)
- {
- for(i = 0; i < rv; ++i)
- {
- match = apr_psprintf(rule->ruleset->mp, "%.*s", ovector[2*i+1] - ovector[2*i], data + ovector[2*i]);
- if (match == NULL) {
- *error_msg = "Internal Error: cannot allocate memory for match.";
- return -1;
- }
- match = remove_escape(rule->ruleset->mp, match, strlen(match));
- match = gsb_replace_tpath(rule->ruleset->mp, match, strlen(match));
- match = gsb_reduce_char(rule->ruleset->mp, match);
- match_length = strlen(match);
- strtolower_inplace((unsigned char *)match);
- if((strstr(match,"http") == NULL) && (match_length > 0) && (strchr(match,'.'))) {
- /* full url */
- if (msr->txcfg->debuglog_level >= 4) {
- msr_log(msr, 4, "GSB: Successfully extracted url: %s", match);
- }
- ret = verify_gsb(gsb, msr, match, match_length);
- if(ret > 0) {
- set_match_to_tx(msr, capture, match, 0);
- if (! *error_msg) {
- *error_msg = apr_psprintf(msr->mp, "Gsb lookup for \"%s\" succeeded.",
- log_escape_nq(msr->mp, match));
- }
- str = apr_pstrdup(rule->ruleset->mp,match);
- base = apr_strtok(str,"/",&savedptr);
- if(base != NULL)
- set_match_to_tx(msr, capture, base, 1);
- return 1;
- }
- /* append / in the end of full url */
- if ((match[match_length -1] != '/') && (strchr(match,'?') == NULL)) {
- canon = apr_psprintf(rule->ruleset->mp, "%s/", match);
- if (canon != NULL) {
- canon_length = strlen(canon);
- ret = verify_gsb(gsb, msr, canon, canon_length);
- if(ret > 0) {
- set_match_to_tx(msr, capture, match, 0);
- if (! *error_msg) {
- *error_msg = apr_psprintf(msr->mp, "Gsb lookup for \"%s\" succeeded.",
- log_escape_nq(msr->mp, canon));
- }
- str = apr_pstrdup(rule->ruleset->mp,match);
- base = apr_strtok(str,"/",&savedptr);
- if(base != NULL)
- set_match_to_tx(msr, capture, base, 1);
- return 1;
- }
- }
- }
- /* Parsing full url */
- domain = apr_pstrdup(rule->ruleset->mp, match);
- domain_len = strlen(domain);
- if(*domain != '/') {
- if(domain[domain_len-1] == '.')
- domain[domain_len-1] = '\0';
- if(domain[domain_len-1] == '/' && domain[domain_len-2] == '.') {
- domain[domain_len-2] = '/';
- domain[domain_len-1] = '\0';
- }
- dot = strchr(domain,'.');
- if(dot != NULL) {
- canon = apr_pstrdup(rule->ruleset->mp, domain);
- ret = verify_gsb(gsb, msr, canon, strlen(canon));
- if(ret > 0) {
- set_match_to_tx(msr, capture, canon, 0);
- if (! *error_msg) {
- *error_msg = apr_psprintf(msr->mp, "Gsb lookup for \"%s\" succeeded.",
- log_escape_nq(msr->mp, canon));
- }
- str = apr_pstrdup(rule->ruleset->mp,match);
- base = apr_strtok(str,"/",&savedptr);
- if(base != NULL)
- set_match_to_tx(msr, capture, base, 1);
- return 1;
- }
- base = apr_strtok(canon,"?",&savedptr);
- if(base != NULL) {
- ret = verify_gsb(gsb, msr, base, strlen(base));
- if(ret > 0) {
- set_match_to_tx(msr, capture, base, 0);
- if (! *error_msg) {
- *error_msg = apr_psprintf(msr->mp, "Gsb lookup for \"%s\" succeeded.",
- log_escape_nq(msr->mp, base));
- }
- str = apr_pstrdup(rule->ruleset->mp,match);
- base = apr_strtok(str,"/",&savedptr);
- if(base != NULL)
- set_match_to_tx(msr, capture, base, 1);
- return 1;
- }
- }
- url = apr_palloc(rule->ruleset->mp, strlen(canon));
- count_slash = 0;
- while(*canon != '\0') {
- switch (*canon) {
- case '/':
- ptr = apr_psprintf(rule->ruleset->mp,"%s/",url);
- ret = verify_gsb(gsb, msr, ptr, strlen(ptr));
- if(ret > 0) {
- set_match_to_tx(msr, capture, ptr, 0);
- if (! *error_msg) {
- *error_msg = apr_psprintf(msr->mp, "Gsb lookup for \"%s\" succeeded.",
- log_escape_nq(msr->mp, ptr));
- }
- str = apr_pstrdup(rule->ruleset->mp,match);
- base = apr_strtok(str,"/",&savedptr);
- if(base != NULL)
- set_match_to_tx(msr, capture, base, 1);
- return 1;
- }
- break;
- }
- url[count_slash] = *canon;
- count_slash++;
- canon++;
- }
- }
- }
- /* Do the same for subdomains */
- for(j=0; j<strlen(match); j++) {
- if(match[j] == '/') {
- s_pos = j;
- break;
- }
- }
- str = apr_pstrdup(rule->ruleset->mp, match);
- while (*str != '\0') {
- switch(*str) {
- case '.':
- domain++;
- domain_len = strlen(domain);
- d_pos = strchr(domain,'.') - domain;
- if(s_pos >= 0 && d_pos >= 0 && d_pos > s_pos)
- break;
- if(*domain != '/') {
- if(domain[domain_len-1] == '.')
- domain[domain_len-1] = '\0';
- if(domain[domain_len-1] == '/' && domain[domain_len-2] == '.') {
- domain[domain_len-2] = '/';
- domain[domain_len-1] = '\0';
- }
- dot = strchr(domain,'.');
- if(dot != NULL) {
- canon = apr_pstrdup(rule->ruleset->mp, domain);
- ret = verify_gsb(gsb, msr, canon, strlen(canon));
- if(ret > 0) {
- set_match_to_tx(msr, capture, canon, 0);
- if (! *error_msg) {
- *error_msg = apr_psprintf(msr->mp, "Gsb lookup for \"%s\" succeeded.",
- log_escape_nq(msr->mp, canon));
- }
- str = apr_pstrdup(rule->ruleset->mp,match);
- base = apr_strtok(str,"/",&savedptr);
- if(base != NULL)
- set_match_to_tx(msr, capture, base, 1);
- return 1;
- }
- base = apr_strtok(canon,"?",&savedptr);
- if(base != NULL) {
- ret = verify_gsb(gsb, msr, base, strlen(base));
- if(ret > 0) {
- set_match_to_tx(msr, capture, base, 0);
- if (! *error_msg) {
- *error_msg = apr_psprintf(msr->mp, "Gsb lookup for \"%s\" succeeded.",
- log_escape_nq(msr->mp, base));
- }
- str = apr_pstrdup(rule->ruleset->mp,match);
- base = apr_strtok(str,"/",&savedptr);
- if(base != NULL)
- set_match_to_tx(msr, capture, base, 1);
- return 1;
- }
- }
- url = apr_palloc(rule->ruleset->mp, strlen(canon));
- count_slash = 0;
- while(*canon != '\0') {
- switch (*canon) {
- case '/':
- ptr = apr_psprintf(rule->ruleset->mp,"%s/",url);
- ret = verify_gsb(gsb, msr, ptr, strlen(ptr));
- if(ret > 0) {
- set_match_to_tx(msr, capture, ptr, 0);
- if (! *error_msg) {
- *error_msg = apr_psprintf(msr->mp, "Gsb lookup for \"%s\" succeeded.",
- log_escape_nq(msr->mp, ptr));
- }
- str = apr_pstrdup(rule->ruleset->mp,match);
- base = apr_strtok(str,"/",&savedptr);
- if(base != NULL)
- set_match_to_tx(msr, capture, base, 1);
- return 1;
- }
- break;
- }
- url[count_slash] = *canon;
- count_slash++;
- canon++;
- }
- }
- }
- break;
- }
- domain = str;
- domain++;
- str++;
- }
- }
- }
- offset = ovector[1];
- }
- return 0;
- }
- /* within */
- static int msre_op_within_execute(modsec_rec *msr, msre_rule *rule, msre_var *var, char **error_msg) {
- msc_string *str = (msc_string *)apr_pcalloc(msr->mp, sizeof(msc_string));
- const char *match = NULL;
- const char *target;
- unsigned int match_length;
- unsigned int target_length = 0;
- unsigned int i, i_max;
- str->value = (char *)rule->op_param;
- str->value_len = strlen(str->value);
- if (error_msg == NULL) return -1;
- *error_msg = NULL;
- if (str->value == NULL) {
- *error_msg = "Internal Error: match string is null.";
- return -1;
- }
- expand_macros(msr, str, rule, msr->mp);
- match = (const char *)str->value;
- match_length = str->value_len;
- /* If the given target is null we give up without a match */
- if (var->value == NULL) {
- /* No match. */
- return 0;
- }
- target = var->value;
- target_length = var->value_len;
- /* The empty string always matches */
- if (target_length == 0) {
- /* Match. */
- *error_msg = apr_psprintf(msr->mp, "String match within \"\" at %s.",
- var->name);
- return 1;
- }
- /* This is impossible to match */
- if (target_length > match_length) {
- /* No match. */
- return 0;
- }
- /* scan for first character, then compare from there until we
- …
Large files files are truncated, but you can click here to view the full file