mirror of
https://github.com/openappsec/openappsec.git
synced 2025-11-16 01:12:18 +03:00
change gzipped detection
This commit is contained in:
@@ -12,6 +12,7 @@ add_library(waap_clib
|
|||||||
ParserJson.cc
|
ParserJson.cc
|
||||||
ParserMultipartForm.cc
|
ParserMultipartForm.cc
|
||||||
ParserRaw.cc
|
ParserRaw.cc
|
||||||
|
ParserGzip.cc
|
||||||
ParserUrlEncode.cc
|
ParserUrlEncode.cc
|
||||||
ParserXML.cc
|
ParserXML.cc
|
||||||
ParserDelimiter.cc
|
ParserDelimiter.cc
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
#include "ParserXML.h"
|
#include "ParserXML.h"
|
||||||
#include "ParserHTML.h"
|
#include "ParserHTML.h"
|
||||||
#include "ParserBinary.h"
|
#include "ParserBinary.h"
|
||||||
|
#include "ParserGzip.h"
|
||||||
#include "ParserMultipartForm.h"
|
#include "ParserMultipartForm.h"
|
||||||
#include "ParserPercentEncode.h"
|
#include "ParserPercentEncode.h"
|
||||||
#include "ParserPairs.h"
|
#include "ParserPairs.h"
|
||||||
@@ -1261,6 +1262,10 @@ DeepParser::createInternalParser(
|
|||||||
dbgTrace(D_WAAP_DEEP_PARSER) << "Starting to parse an HTML file";
|
dbgTrace(D_WAAP_DEEP_PARSER) << "Starting to parse an HTML file";
|
||||||
m_parsersDeque.push_back(std::make_shared<BufferedParser<ParserHTML>>(*this, parser_depth + 1));
|
m_parsersDeque.push_back(std::make_shared<BufferedParser<ParserHTML>>(*this, parser_depth + 1));
|
||||||
offset = 0;
|
offset = 0;
|
||||||
|
} else if (isBodyPayload && Waap::Util::isGzipped(cur_val)){
|
||||||
|
dbgTrace(D_WAAP_DEEP_PARSER) << "Starting to parse a gzip file";
|
||||||
|
m_parsersDeque.push_back(std::make_shared<BufferedParser<ParserGzip>>(*this, parser_depth + 1));
|
||||||
|
offset = 0;
|
||||||
} else if (cur_val.size() > 0 && signatures->php_serialize_identifier.hasMatch(cur_val)) {
|
} else if (cur_val.size() > 0 && signatures->php_serialize_identifier.hasMatch(cur_val)) {
|
||||||
// PHP value detected
|
// PHP value detected
|
||||||
dbgTrace(D_WAAP_DEEP_PARSER) << "Starting to parse phpSerializedData";
|
dbgTrace(D_WAAP_DEEP_PARSER) << "Starting to parse phpSerializedData";
|
||||||
|
|||||||
115
components/security_apps/waap/waap_clib/ParserGzip.cc
Executable file
115
components/security_apps/waap/waap_clib/ParserGzip.cc
Executable file
@@ -0,0 +1,115 @@
|
|||||||
|
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
|
||||||
|
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
#include "ParserGzip.h"
|
||||||
|
#include "debug.h"
|
||||||
|
|
||||||
|
USE_DEBUG_FLAG(D_WAAP_PARSER_GZIP);
|
||||||
|
|
||||||
|
const std::string ParserGzip::m_parserName = "ParserGzip";
|
||||||
|
|
||||||
|
ParserGzip::ParserGzip(IParserStreamReceiver &receiver, size_t parser_depth)
|
||||||
|
:m_receiver(receiver), m_key("gzip"), m_state(s_start), m_stream(nullptr) {
|
||||||
|
}
|
||||||
|
|
||||||
|
ParserGzip::~ParserGzip() {
|
||||||
|
if (m_stream != nullptr) {
|
||||||
|
finiCompressionStream(m_stream);
|
||||||
|
m_stream = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t ParserGzip::push(const char *buf, size_t len) {
|
||||||
|
dbgTrace(D_WAAP_PARSER_GZIP) << "len=" << (unsigned long int)len << ")";
|
||||||
|
|
||||||
|
if (len == 0) {
|
||||||
|
dbgTrace(D_WAAP_PARSER_GZIP) << "end of data signal! m_state=" << m_state;
|
||||||
|
|
||||||
|
// flush
|
||||||
|
if (m_state != s_start) { // only emit if at least something was pushed
|
||||||
|
if (m_receiver.onKvDone() != 0) {
|
||||||
|
m_state = s_error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
DecompressionResult res;
|
||||||
|
switch (m_state) {
|
||||||
|
case s_start:
|
||||||
|
dbgTrace(D_WAAP_PARSER_GZIP) << "s_start";
|
||||||
|
if (m_receiver.onKey(m_key.data(), m_key.size()) != 0) {
|
||||||
|
m_state = s_error;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
m_stream = initCompressionStream();
|
||||||
|
m_state = s_forward;
|
||||||
|
// fallthrough //
|
||||||
|
CP_FALL_THROUGH;
|
||||||
|
case s_forward:
|
||||||
|
dbgTrace(D_WAAP_PARSER_GZIP) << "s_forward";
|
||||||
|
res = decompressData(
|
||||||
|
m_stream,
|
||||||
|
len,
|
||||||
|
reinterpret_cast<const unsigned char *>(buf));
|
||||||
|
dbgTrace(D_WAAP_PARSER_GZIP) << "res: " << res.ok
|
||||||
|
<< ", size: " << res.num_output_bytes
|
||||||
|
<< ", is last: " << res.is_last_chunk;
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
m_state = s_error;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (res.num_output_bytes != 0 &&
|
||||||
|
m_receiver.onValue(reinterpret_cast<const char *>(res.output), res.num_output_bytes) != 0) {
|
||||||
|
m_state = s_error;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (res.is_last_chunk) {
|
||||||
|
m_state = s_done;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case s_done:
|
||||||
|
if (len > 0) {
|
||||||
|
dbgTrace(D_WAAP_PARSER_GZIP) << " unexpected data after completion, len=" << len;
|
||||||
|
m_state = s_error;
|
||||||
|
return 0; // Return 0 to indicate error
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case s_error:
|
||||||
|
dbgTrace(D_WAAP_PARSER_GZIP) << "s_error";
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ParserGzip::finish() {
|
||||||
|
push(NULL, 0);
|
||||||
|
if (m_state != s_done) {
|
||||||
|
m_state = s_error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string &
|
||||||
|
ParserGzip::name() const {
|
||||||
|
return m_parserName;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ParserGzip::error() const {
|
||||||
|
return m_state == s_error;
|
||||||
|
}
|
||||||
46
components/security_apps/waap/waap_clib/ParserGzip.h
Executable file
46
components/security_apps/waap/waap_clib/ParserGzip.h
Executable file
@@ -0,0 +1,46 @@
|
|||||||
|
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
|
||||||
|
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
#ifndef __PARSER_GZIP_H_
|
||||||
|
#define __PARSER_GZIP_H_
|
||||||
|
|
||||||
|
#include "ParserBase.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include "compression_utils.h"
|
||||||
|
|
||||||
|
class ParserGzip : public ParserBase {
|
||||||
|
public:
|
||||||
|
ParserGzip(IParserStreamReceiver &receiver, size_t parser_depth);
|
||||||
|
virtual ~ParserGzip();
|
||||||
|
size_t push(const char *data, size_t data_len);
|
||||||
|
void finish();
|
||||||
|
virtual const std::string &name() const;
|
||||||
|
bool error() const;
|
||||||
|
virtual size_t depth() { return 1; }
|
||||||
|
private:
|
||||||
|
enum state {
|
||||||
|
s_start,
|
||||||
|
s_forward,
|
||||||
|
s_done,
|
||||||
|
s_error
|
||||||
|
};
|
||||||
|
|
||||||
|
IParserStreamReceiver &m_receiver;
|
||||||
|
std::string m_key;
|
||||||
|
state m_state;
|
||||||
|
CompressionStream * m_stream;
|
||||||
|
|
||||||
|
static const std::string m_parserName;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // __PARSER_GZIP_H_
|
||||||
@@ -44,14 +44,6 @@ static const string defaultSharedStorageHost = "appsec-shared-storage-svc";
|
|||||||
#define SHARED_STORAGE_HOST_ENV_NAME "SHARED_STORAGE_HOST"
|
#define SHARED_STORAGE_HOST_ENV_NAME "SHARED_STORAGE_HOST"
|
||||||
#define LEARNING_HOST_ENV_NAME "LEARNING_HOST"
|
#define LEARNING_HOST_ENV_NAME "LEARNING_HOST"
|
||||||
|
|
||||||
static bool
|
|
||||||
isGZipped(const string &stream)
|
|
||||||
{
|
|
||||||
if (stream.size() < 2) return false;
|
|
||||||
auto unsinged_stream = reinterpret_cast<const u_char *>(stream.data());
|
|
||||||
return unsinged_stream[0] == 0x1f && unsinged_stream[1] == 0x8b;
|
|
||||||
}
|
|
||||||
|
|
||||||
void yieldIfPossible(const string& func, int line)
|
void yieldIfPossible(const string& func, int line)
|
||||||
{
|
{
|
||||||
// Check if we are in the main loop
|
// Check if we are in the main loop
|
||||||
@@ -73,7 +65,7 @@ bool RestGetFile::loadJson(const string& json)
|
|||||||
string json_str;
|
string json_str;
|
||||||
|
|
||||||
json_str = json;
|
json_str = json;
|
||||||
if (!isGZipped(json_str))
|
if (!Waap::Util::isGzipped(json_str))
|
||||||
{
|
{
|
||||||
return ClientRest::loadJson(json_str);
|
return ClientRest::loadJson(json_str);
|
||||||
}
|
}
|
||||||
@@ -343,7 +335,7 @@ void SerializeToFileBase::saveData()
|
|||||||
}
|
}
|
||||||
|
|
||||||
string decompress(string fileContent) {
|
string decompress(string fileContent) {
|
||||||
if (!isGZipped(fileContent)) {
|
if (!Waap::Util::isGzipped(fileContent)) {
|
||||||
dbgTrace(D_WAAP_SERIALIZE) << "file note zipped";
|
dbgTrace(D_WAAP_SERIALIZE) << "file note zipped";
|
||||||
return fileContent;
|
return fileContent;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ ValueStatsAnalyzer::ValueStatsAnalyzer(const std::string &cur_val)
|
|||||||
bool lastNul = false; // whether last processed character was ASCII NUL
|
bool lastNul = false; // whether last processed character was ASCII NUL
|
||||||
size_t curValLength = cur_val.length();
|
size_t curValLength = cur_val.length();
|
||||||
|
|
||||||
if (curValLength == 0) {
|
if (curValLength == 0 || Waap::Util::isGzipped(cur_val)) {
|
||||||
canSplitSemicolon = false;
|
canSplitSemicolon = false;
|
||||||
canSplitPipe = false;
|
canSplitPipe = false;
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -1912,6 +1912,14 @@ base64Decode(const string &input)
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
isGzipped(const string &stream)
|
||||||
|
{
|
||||||
|
if (stream.size() < 2) return false;
|
||||||
|
auto unsinged_stream = reinterpret_cast<const u_char *>(stream.data());
|
||||||
|
return unsinged_stream[0] == 0x1f && unsinged_stream[1] == 0x8b;
|
||||||
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
containsInvalidUtf8(const string &payload)
|
containsInvalidUtf8(const string &payload)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1135,6 +1135,7 @@ namespace Util {
|
|||||||
std::string obfuscateXor(const std::string& toEncrypt);
|
std::string obfuscateXor(const std::string& toEncrypt);
|
||||||
std::string obfuscateXorBase64(const std::string& toEncrypt);
|
std::string obfuscateXorBase64(const std::string& toEncrypt);
|
||||||
|
|
||||||
|
bool isGzipped(const std::string &stream);
|
||||||
bool containsInvalidUtf8(const std::string &payload);
|
bool containsInvalidUtf8(const std::string &payload);
|
||||||
|
|
||||||
bool containsPercentEncoding(const std::string &payload);
|
bool containsPercentEncoding(const std::string &payload);
|
||||||
|
|||||||
@@ -106,6 +106,7 @@ DEFINE_FLAG(D_COMPONENT, D_ALL)
|
|||||||
DEFINE_FLAG(D_WAAP_PARSER_GQL, D_WAAP_PARSER)
|
DEFINE_FLAG(D_WAAP_PARSER_GQL, D_WAAP_PARSER)
|
||||||
DEFINE_FLAG(D_WAAP_PARSER_MULTIPART_FORM, D_WAAP_PARSER)
|
DEFINE_FLAG(D_WAAP_PARSER_MULTIPART_FORM, D_WAAP_PARSER)
|
||||||
DEFINE_FLAG(D_WAAP_PARSER_RAW, D_WAAP_PARSER)
|
DEFINE_FLAG(D_WAAP_PARSER_RAW, D_WAAP_PARSER)
|
||||||
|
DEFINE_FLAG(D_WAAP_PARSER_GZIP, D_WAAP_PARSER)
|
||||||
DEFINE_FLAG(D_WAAP_PARSER_URLENCODE, D_WAAP_PARSER)
|
DEFINE_FLAG(D_WAAP_PARSER_URLENCODE, D_WAAP_PARSER)
|
||||||
DEFINE_FLAG(D_WAAP_PARSER_PHPSERIALIZE, D_WAAP_PARSER)
|
DEFINE_FLAG(D_WAAP_PARSER_PHPSERIALIZE, D_WAAP_PARSER)
|
||||||
DEFINE_FLAG(D_WAAP_PARSER_PERCENT, D_WAAP_PARSER)
|
DEFINE_FLAG(D_WAAP_PARSER_PERCENT, D_WAAP_PARSER)
|
||||||
|
|||||||
Reference in New Issue
Block a user