Adds support to the transformations parity[even|odd|zero]7bit

Issues: #968, #969, #967
This commit is contained in:
Felipe Zimmerle
2016-05-27 10:45:05 -03:00
parent 59b1fe0305
commit 8d49903279
8 changed files with 126 additions and 52 deletions

View File

@@ -30,25 +30,52 @@ namespace modsecurity {
namespace actions {
namespace transformations {
ParityEven7bit::ParityEven7bit(std::string action)
: Transformation(action) {
this->action_kind = 1;
}
std::string ParityEven7bit::evaluate(std::string value,
Transaction *transaction) {
/**
* @todo Implement the transformation ParityEven7bit
*/
if (transaction) {
#ifndef NO_LOGS
transaction->debug(4, "Transformation ParityEven7bit is not" \
" implemented yet.");
#endif
std::string ret;
unsigned char *input = NULL;
input = reinterpret_cast<unsigned char *>
(malloc(sizeof(char) * value.length()+1));
if (input == NULL) {
return "";
}
return value;
memcpy(input, value.c_str(), value.length()+1);
inplace(input, value.length());
ret.assign(reinterpret_cast<char *>(input), value.length());
free(input);
return ret;
}
bool ParityEven7bit::inplace(unsigned char *input, u_int64_t input_len) {
u_int64_t i;
i = 0;
while (i < input_len) {
unsigned int x = input[i];
input[i] ^= input[i] >> 4;
input[i] &= 0xf;
if ((0x6996 >> input[i]) & 1) {
input[i] = x | 0x80;
} else {
input[i] = x & 0x7f;
}
i++;
}
return true;
}
} // namespace transformations
} // namespace actions
} // namespace modsecurity

View File

@@ -30,9 +30,10 @@ namespace transformations {
class ParityEven7bit : public Transformation {
public:
explicit ParityEven7bit(std::string action);
std::string evaluate(std::string exp,
Transaction *transaction) override;
explicit ParityEven7bit(std::string action) : Transformation(action) { }
std::string evaluate(std::string exp, Transaction *transaction) override;
static bool inplace(unsigned char *input, u_int64_t input_len);
};
} // namespace transformations

View File

@@ -30,25 +30,51 @@ namespace modsecurity {
namespace actions {
namespace transformations {
ParityOdd7bit::ParityOdd7bit(std::string action)
: Transformation(action) {
this->action_kind = 1;
}
std::string ParityOdd7bit::evaluate(std::string value,
Transaction *transaction) {
/**
* @todo Implement the transformation ParityOdd7bit
*/
if (transaction) {
#ifndef NO_LOGS
transaction->debug(4, "Transformation ParityOdd7bit is not " \
"implemented yet.");
#endif
std::string ret;
unsigned char *input = NULL;
input = reinterpret_cast<unsigned char *>
(malloc(sizeof(char) * value.length()+1));
if (input == NULL) {
return "";
}
return value;
memcpy(input, value.c_str(), value.length()+1);
inplace(input, value.length());
ret.assign(reinterpret_cast<char *>(input), value.length());
free(input);
return ret;
}
bool ParityOdd7bit::inplace(unsigned char *input, u_int64_t input_len) {
u_int64_t i;
i = 0;
while (i < input_len) {
unsigned int x = input[i];
input[i] ^= input[i] >> 4;
input[i] &= 0xf;
if ((0x6996 >> input[i]) & 1) {
input[i] = x & 0x7f;
} else {
input[i] = x | 0x80;
}
i++;
}
return true;
}
} // namespace transformations
} // namespace actions
} // namespace modsecurity

View File

@@ -30,9 +30,10 @@ namespace transformations {
class ParityOdd7bit : public Transformation {
public:
explicit ParityOdd7bit(std::string action);
std::string evaluate(std::string exp,
Transaction *transaction) override;
explicit ParityOdd7bit(std::string action) : Transformation(action) { }
std::string evaluate(std::string exp, Transaction *transaction) override;
static bool inplace(unsigned char *input, u_int64_t input_len);
};
} // namespace transformations

View File

@@ -30,25 +30,43 @@ namespace modsecurity {
namespace actions {
namespace transformations {
ParityZero7bit::ParityZero7bit(std::string action)
: Transformation(action) {
this->action_kind = 1;
}
std::string ParityZero7bit::evaluate(std::string value,
Transaction *transaction) {
/**
* @todo Implement the transformation ParityZero7bit
*/
if (transaction) {
#ifndef NO_LOGS
transaction->debug(4, "Transformation ParityZero7bit is not" \
"implemented yet.");
#endif
std::string ret;
unsigned char *input = NULL;
input = reinterpret_cast<unsigned char *>
(malloc(sizeof(char) * value.length()+1));
if (input == NULL) {
return "";
}
return value;
memcpy(input, value.c_str(), value.length()+1);
inplace(input, value.length());
ret.assign(reinterpret_cast<char *>(input), value.length());
free(input);
return ret;
}
bool ParityZero7bit::inplace(unsigned char *input, u_int64_t input_len) {
u_int64_t i;
i = 0;
while (i < input_len) {
input[i] &= 0x7f;
i++;
}
return true;
}
} // namespace transformations
} // namespace actions
} // namespace modsecurity

View File

@@ -30,9 +30,10 @@ namespace transformations {
class ParityZero7bit : public Transformation {
public:
explicit ParityZero7bit(std::string action);
std::string evaluate(std::string exp,
Transaction *transaction) override;
explicit ParityZero7bit(std::string action) : Transformation(action) { }
std::string evaluate(std::string exp, Transaction *transaction) override;
static bool inplace(unsigned char *input, u_int64_t input_len);
};
} // namespace transformations

View File

@@ -94,9 +94,9 @@ Transformation* Transformation::instantiate(std::string a) {
IF_MATCH(normalizePath) { return new NormalisePath(a); }
IF_MATCH(normalisePath) { return new NormalisePath(a); }
IF_MATCH(normalisePath) { return new NormalisePath(a); }
IF_MATCH(parity_even_7bit) { return new ParityEven7bit(a); }
IF_MATCH(parity_odd_7bit) { return new ParityOdd7bit(a); }
IF_MATCH(parity_zero_7bit) { return new ParityZero7bit(a); }
IF_MATCH(parityEven7bit) { return new ParityEven7bit(a); }
IF_MATCH(parityOdd7bit) { return new ParityOdd7bit(a); }
IF_MATCH(parityZero7bit) { return new ParityZero7bit(a); }
IF_MATCH(removeCommentsChar) { return new RemoveCommentsChar(a); }
IF_MATCH(removeComments) { return new RemoveComments(a); }
IF_MATCH(removeNulls) { return new RemoveNulls(a); }