First release of open-appsec source code

This commit is contained in:
roybarda
2022-10-26 19:33:19 +03:00
parent 3883109caf
commit a883352f79
1353 changed files with 276290 additions and 1 deletions

View File

@@ -0,0 +1,7 @@
add_subdirectory(cpnano_base64)
ADD_DEFINITIONS(-Wno-deprecated-declarations)
add_library(encryptor encryptor.cc "cpnano_base64/base64.cc")
add_subdirectory(encryptor_ut)

View File

@@ -0,0 +1,6 @@
add_executable(cpnano_base64 cpnano_base64.cc base64.cc)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COMPILE_FLAGS}")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_LINK_FLAGS}")
install(PROGRAMS $<TARGET_FILE:cpnano_base64> DESTINATION bin/)
install(PROGRAMS $<TARGET_FILE:cpnano_base64> DESTINATION orchestration/)

View File

@@ -0,0 +1,68 @@
// 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 <stdlib.h>
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <istream>
#include <ostream>
#include "base64.h"
using namespace std;
const string Base64::base64_base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
string
Base64::encodeBase64(const string &input)
{
string out;
int val = 0, val_base = -6;
for (unsigned char c : input) {
val = (val << 8) + c;
val_base += 8;
while (val_base >= 0) {
out.push_back(base64_base[(val >> val_base) & 0x3F]);
val_base -= 6;
}
}
// -6 indicates the number of bits to take from each character
// (6 bits is enough to present a range of 0 to 63)
if (val_base > -6) out.push_back(base64_base[((val << 8) >> (val_base + 8)) & 0x3F]);
while (out.size() % 4) out.push_back('=');
return out;
}
string
Base64::decodeBase64(const string &input)
{
vector<int> mapper(256, -1);
for (int i = 0; i < 64; i++) mapper[base64_base[i]] = i;
string out;
int val = 0, val_base = -8;
for (unsigned char c : input) {
if (mapper[c] == -1) break;
val = (val << 6) + mapper[c];
val_base += 6;
if (val_base >= 0) {
out.push_back(char((val >> val_base) & 0xFF));
val_base -= 8;
}
}
return out;
}

View File

@@ -0,0 +1,29 @@
// 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 __BASE64_H__
#define __BASE64_H__
#include <string>
class Base64
{
public:
static std::string encodeBase64(const std::string &input);
static std::string decodeBase64(const std::string &input);
private:
static const std::string base64_base;
};
#endif // __BASE64_H__

View File

@@ -0,0 +1,52 @@
// 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 <stdlib.h>
#include <iostream>
#include <string>
#include <iterator>
#include <istream>
#include <ostream>
#include "base64.h"
using namespace std;
// LCOV_EXCL_START Reason: main func tested in systemtest
int
main(int argc, char **argv)
{
if (argc < 2) {
cerr << "No arguments were provided" << endl;
exit(1);
}
// don't skip the whitespace while reading
cin >> noskipws;
// use stream iterators to copy the stream to a string
istream_iterator<char> it(cin);
istream_iterator<char> end;
string input(it, end);
if (string(argv[1]) == "-d" || string(argv[1]) == "--decode") {
cout << Base64::decodeBase64(input) << endl;
} else if(string(argv[1]) == "-e" || string(argv[1]) == "--encode") {
cout << Base64::encodeBase64(input) << endl;
} else {
cerr << "Argument provided is illegal (options are -d|-e). Provided arg: " << string(argv[1]) << endl;
exit(2);
}
exit(0);
}
// LCOV_EXCL_STOP

View File

@@ -0,0 +1,78 @@
// 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 "encryptor.h"
#include <stdio.h>
#include <vector>
#include <string.h>
#include <openssl/aes.h>
#include "cpnano_base64/base64.h"
#include "config.h"
#include "debug.h"
using namespace std;
static const int bits = 256;
class Encryptor::Impl : Singleton::Provide<I_Encryptor>::From<Encryptor>
{
// Base64
string base64Encode(const string &input) override;
string base64Decode(const string &input) override;
// Obfuscating
string obfuscateXor(const string &input) override;
string obfuscateXorBase64(const string &input) override;
};
string
Encryptor::Impl::base64Encode(const string &input)
{
return Base64::encodeBase64(input);
}
string
Encryptor::Impl::base64Decode(const string &input)
{
return Base64::decodeBase64(input);
}
string
Encryptor::Impl::obfuscateXor(const string &input)
{
//Any chars will work
static const string key = "CHECKPOINT";
string output;
for (size_t i = 0; i < input.size(); i++) {
output.push_back(input[i] ^ key[i % key.size()]);
}
return output;
}
string
Encryptor::Impl::obfuscateXorBase64(const string &input)
{
string obfuscated = obfuscateXor(input);
return base64Encode(obfuscated);
}
void
Encryptor::preload()
{
registerExpectedConfiguration<string>("encryptor", "Data files directory");
}
Encryptor::Encryptor() : Component("Encryptor"), pimpl(make_unique<Impl>()) {}
Encryptor::~Encryptor() {}

View File

@@ -0,0 +1,7 @@
link_directories(${BOOST_ROOT}/lib)
add_unit_test(
encryptor_ut
"encryptor_ut.cc"
"encryptor;config;singleton;metric;event_is;-lboost_context;-lboost_regex;-lresolv;-lcrypto"
)

View File

@@ -0,0 +1,85 @@
#include "encryptor.h"
#include "config.h"
#include "config_component.h"
#include "cptest.h"
#include "mock/mock_time_get.h"
#include "mock/mock_mainloop.h"
using namespace testing;
using namespace std;
class EncryptorTest : public Test
{
public:
EncryptorTest()
{
i_encryptor = Singleton::Consume<I_Encryptor>::from(encryptor);
}
~EncryptorTest() {}
I_Encryptor *i_encryptor;
Encryptor encryptor;
};
TEST_F(EncryptorTest, doNothing)
{
}
TEST_F(EncryptorTest, registerExpectedConfig)
{
NiceMock<MockMainLoop> mock_mainloop;
NiceMock<MockTimeGet> mock_timer;
::Environment env;
ConfigComponent config;
env.preload();
encryptor.preload();
env.init();
setConfiguration(
string("this is new dir path"),
string("encryptor"),
string("Data files directory")
);
EXPECT_THAT(
getConfiguration<string>("encryptor", "Data files directory"),
IsValue(string("this is new dir path"))
);
env.fini();
}
TEST_F(EncryptorTest, base64Decode)
{
EXPECT_EQ(i_encryptor->base64Decode(""), "");
EXPECT_EQ(i_encryptor->base64Decode("SGVsbG8gV29ybGQh"), "Hello World!");
}
TEST_F(EncryptorTest, base64Encode)
{
EXPECT_EQ(i_encryptor->base64Encode(""), "");
EXPECT_EQ(i_encryptor->base64Encode("Hello World!"), "SGVsbG8gV29ybGQh");
}
TEST_F(EncryptorTest, XOREncrypt)
{
EXPECT_EQ(i_encryptor->obfuscateXor(""), string(""));
EXPECT_EQ(i_encryptor->obfuscateXor("ABCDEF"), string("\x2\xa\x6\x7\xe\x16"));
EXPECT_EQ(i_encryptor->obfuscateXor("CHECKPOINT"), string("\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0", 10));
EXPECT_EQ(i_encryptor->obfuscateXor("asdqweasdqwe"), string("\x22\x3b\x21\x32\x3c\x35\x2e\x3a\x2a\x25\x34\x2d"));
}
TEST_F(EncryptorTest, XORDecrypt)
{
EXPECT_EQ(i_encryptor->obfuscateXor(""), string(""));
EXPECT_EQ(i_encryptor->obfuscateXor(string("\x2\xa\x6\x7\xe\x16")), "ABCDEF");
EXPECT_EQ(i_encryptor->obfuscateXor(string("\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0", 10)), "CHECKPOINT");
EXPECT_EQ(i_encryptor->obfuscateXor(string("\x22\x3b\x21\x32\x3c\x35\x2e\x3a\x2a\x25")), "asdqweasdq");
}
TEST_F(EncryptorTest, XORBase64Encrypt)
{
EXPECT_EQ(i_encryptor->obfuscateXorBase64(""), string(""));
EXPECT_EQ(
i_encryptor->obfuscateXorBase64(string("\x0b\x2d\x29\x2f\x24\x70\x18\x26\x3c\x38\x27\x69")), "SGVsbG8gV29ybGQh"
);
}