Adds support to the hexDecode transformation

Issue: #973
This commit is contained in:
Felipe Zimmerle 2016-05-25 18:49:34 -03:00
parent 2b056485d0
commit bd2e95953c
3 changed files with 42 additions and 18 deletions

View File

@ -24,32 +24,52 @@
#include "modsecurity/transaction.h" #include "modsecurity/transaction.h"
#include "actions/transformations/transformation.h" #include "actions/transformations/transformation.h"
#include "src/utils.h"
namespace modsecurity { namespace modsecurity {
namespace actions { namespace actions {
namespace transformations { namespace transformations {
HexDecode::HexDecode(std::string action) std::string HexDecode::evaluate(std::string value,
: Transformation(action) { Transaction *transaction) {
this->action_kind = 1; std::string ret;
unsigned char *input = NULL;
int size = 0;
input = reinterpret_cast<unsigned char *>
(malloc(sizeof(char) * value.length()+1));
if (input == NULL) {
return "";
}
memcpy(input, value.c_str(), value.length()+1);
size = inplace(input, value.length());
ret.assign(reinterpret_cast<char *>(input), size);
free(input);
return ret;
} }
std::string HexDecode::evaluate(std::string value, int HexDecode::inplace(unsigned char *data, int len) {
Transaction *transaction) { unsigned char *d = data;
int len = value.length(); int i, count = 0;
std::string newString;
for (int i=0; i< len; i+=2) { if ((data == NULL) || (len == 0)) {
std::string byte = value.substr(i, 2); return 0;
char chr = static_cast<char>(static_cast<int>(strtol(byte.c_str(),
NULL, 16)));
newString.push_back(chr);
} }
return newString; for (i = 0; i <= len - 2; i += 2) {
*d++ = x2c(&data[i]);
count++;
}
*d = '\0';
return count;
} }

View File

@ -30,9 +30,12 @@ namespace transformations {
class HexDecode : public Transformation { class HexDecode : public Transformation {
public: public:
explicit HexDecode(std::string action); explicit HexDecode(std::string action) : Transformation(action) { }
std::string evaluate(std::string exp, std::string evaluate(std::string exp,
Transaction *transaction) override; Transaction *transaction) override;
static int inplace(unsigned char *data, int len);
}; };
} // namespace transformations } // namespace transformations

View File

@ -45,11 +45,12 @@ std::string Utf8ToUnicode::evaluate(std::string value,
return ""; return "";
} }
inplace(input, value.size() + 1, &changed);
memcpy(input, value.c_str(), value.length()+1); memcpy(input, value.c_str(), value.length()+1);
ret.assign(reinterpret_cast<char *>(input), 10); inplace(input, value.size() + 1, &changed);
ret.assign(reinterpret_cast<char *>(input),
strlen(reinterpret_cast<char *>(input)));
free(input); free(input);
return ret; return ret;