Adds RemoveNulls trasnformation

This commit is contained in:
Felipe Zimmerle
2015-08-05 22:03:26 -03:00
parent 62d004cf04
commit 6dad6af4a9
7 changed files with 74 additions and 12 deletions

View File

@@ -15,6 +15,8 @@
#include "actions/transformations/remove_nulls.h"
#include <string.h>
#include <iostream>
#include <string>
#include <algorithm>
@@ -30,20 +32,31 @@ namespace ModSecurity {
namespace actions {
namespace transformations {
RemoveNulls::RemoveNulls(std::string action)
: Transformation(action) {
this->action_kind = 1;
}
std::string RemoveNulls::evaluate(std::string value,
Assay *assay) {
/**
* @todo Implement the transformation RemoveNulls
*/
assay->debug(4, "Transformation RemoveNulls is not implemented yet.");
return value;
int64_t i, j;
char *input = reinterpret_cast<char *>(malloc(value.size()
* sizeof(char)));
memcpy(input, value.c_str(), value.size());
i = j = 0;
while (i < value.size()) {
if (input[i] != '\0') {
input[j] = input[i];
j++;
}
i++;
}
std::string ret(input, 0, j);
free(input);
return ret;
}
} // namespace transformations
} // namespace actions
} // namespace ModSecurity

View File

@@ -30,7 +30,9 @@ namespace transformations {
class RemoveNulls : public Transformation {
public:
explicit RemoveNulls(std::string action);
explicit RemoveNulls(std::string action)
: Transformation(action) { }
std::string evaluate(std::string exp,
Assay *assay) override;
};

View File

@@ -94,7 +94,7 @@ Transformation* Transformation::instantiate(std::string a) {
IF_MATCH(parity_zero_7bit) { return new ParityZero7bit(a); }
IF_MATCH(remove_comments_char) { return new RemoveCommentsChar(a); }
IF_MATCH(remove_comments) { return new RemoveComments(a); }
IF_MATCH(remove_nulls) { return new RemoveNulls(a); }
IF_MATCH(removeNulls) { return new RemoveNulls(a); }
IF_MATCH(remove_whitespace) { return new RemoveWhitespace(a); }
IF_MATCH(replace_comments) { return new ReplaceComments(a); }
IF_MATCH(replace_nulls) { return new ReplaceNulls(a); }

View File

@@ -809,5 +809,20 @@ static unsigned char x2c(unsigned char *what) {
}
std::string string_to_hex(const std::string& input) {
static const char* const lut = "0123456789ABCDEF";
size_t len = input.length();
std::string output;
output.reserve(2 * len);
for (size_t i = 0; i < len; ++i) {
const unsigned char c = input[i];
output.push_back(lut[c >> 4]);
output.push_back(lut[c & 15]);
}
return output;
}
} // namespace ModSecurity

View File

@@ -40,6 +40,7 @@ namespace ModSecurity {
int html_entities_decode_inplace(unsigned char *input, int input_len);
int normalize_path_inplace(unsigned char *input, int input_len,
int win, int *changed);
std::string string_to_hex(const std::string& input);
} // namespace ModSecurity
#define SRC_UTILS_H_