2014-12-18 18:50:47 +00:00
|
|
|
/*
|
2016-02-11 19:48:58 +00:00
|
|
|
* Copyright (c) 2014-present, Facebook, Inc.
|
2014-12-18 18:50:47 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
2015-05-12 06:31:13 +00:00
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
2014-12-18 18:50:47 +00:00
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
*/
|
2014-12-17 22:03:52 +00:00
|
|
|
|
2016-12-22 08:37:59 +00:00
|
|
|
#include <iomanip>
|
2014-12-17 22:03:52 +00:00
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include <boost/algorithm/string.hpp>
|
2015-01-21 02:02:45 +00:00
|
|
|
#include <boost/archive/iterators/base64_from_binary.hpp>
|
2016-12-22 08:37:59 +00:00
|
|
|
#include <boost/archive/iterators/binary_from_base64.hpp>
|
|
|
|
#include <boost/archive/iterators/transform_width.hpp>
|
|
|
|
#include <boost/uuid/sha1.hpp>
|
2015-01-21 02:02:45 +00:00
|
|
|
|
2017-05-24 16:38:10 +00:00
|
|
|
#include <osquery/logger.h>
|
|
|
|
|
2014-12-17 22:03:52 +00:00
|
|
|
#include "osquery/core/conversions.h"
|
|
|
|
|
|
|
|
namespace bai = boost::archive::iterators;
|
|
|
|
|
|
|
|
namespace osquery {
|
|
|
|
|
|
|
|
typedef bai::binary_from_base64<const char*> base64_str;
|
|
|
|
typedef bai::transform_width<base64_str, 8, 6> base64_dec;
|
2015-02-14 01:40:02 +00:00
|
|
|
typedef bai::transform_width<std::string::const_iterator, 6, 8> base64_enc;
|
|
|
|
typedef bai::base64_from_binary<base64_enc> it_base64;
|
2014-12-17 22:03:52 +00:00
|
|
|
|
|
|
|
std::string base64Decode(const std::string& encoded) {
|
|
|
|
std::string is;
|
|
|
|
std::stringstream os;
|
|
|
|
|
|
|
|
is = encoded;
|
|
|
|
boost::replace_all(is, "\r\n", "");
|
|
|
|
boost::replace_all(is, "\n", "");
|
2015-10-21 20:56:58 +00:00
|
|
|
size_t size = is.size();
|
2014-12-17 22:03:52 +00:00
|
|
|
|
|
|
|
// Remove the padding characters
|
|
|
|
if (size && is[size - 1] == '=') {
|
|
|
|
--size;
|
|
|
|
if (size && is[size - 1] == '=') {
|
|
|
|
--size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size == 0) {
|
2015-02-14 01:40:02 +00:00
|
|
|
return "";
|
2014-12-17 22:03:52 +00:00
|
|
|
}
|
2017-05-24 16:38:10 +00:00
|
|
|
try {
|
|
|
|
std::copy(base64_dec(is.data()),
|
|
|
|
base64_dec(is.data() + size),
|
|
|
|
std::ostream_iterator<char>(os));
|
|
|
|
} catch (const boost::archive::iterators::dataflow_exception& e) {
|
|
|
|
LOG(INFO) << "Could not base64 decode string: " << e.what();
|
|
|
|
return "";
|
|
|
|
}
|
2014-12-17 22:03:52 +00:00
|
|
|
return os.str();
|
|
|
|
}
|
2015-01-21 02:02:45 +00:00
|
|
|
|
|
|
|
std::string base64Encode(const std::string& unencoded) {
|
|
|
|
std::stringstream os;
|
|
|
|
|
2015-02-14 01:40:02 +00:00
|
|
|
if (unencoded.size() == 0) {
|
2015-01-21 02:02:45 +00:00
|
|
|
return std::string();
|
|
|
|
}
|
2015-02-14 01:40:02 +00:00
|
|
|
|
2016-02-12 17:39:20 +00:00
|
|
|
size_t writePaddChars = (3U - unencoded.length() % 3U) % 3U;
|
2015-02-14 01:40:02 +00:00
|
|
|
std::string base64(it_base64(unencoded.begin()), it_base64(unencoded.end()));
|
2016-02-12 17:39:20 +00:00
|
|
|
base64.append(writePaddChars, '=');
|
2015-01-21 02:02:45 +00:00
|
|
|
os << base64;
|
|
|
|
return os.str();
|
|
|
|
}
|
2015-04-09 00:41:54 +00:00
|
|
|
|
|
|
|
bool isPrintable(const std::string& check) {
|
|
|
|
for (const unsigned char ch : check) {
|
|
|
|
if (ch >= 0x7F || ch <= 0x1F) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2016-02-12 17:39:20 +00:00
|
|
|
|
|
|
|
std::vector<std::string> split(const std::string& s, const std::string& delim) {
|
|
|
|
std::vector<std::string> elems;
|
|
|
|
boost::split(elems, s, boost::is_any_of(delim));
|
|
|
|
auto start =
|
2016-11-06 09:17:04 +00:00
|
|
|
std::remove_if(elems.begin(), elems.end(), [](const std::string& t) {
|
|
|
|
return t.size() == 0;
|
2016-02-12 17:39:20 +00:00
|
|
|
});
|
|
|
|
elems.erase(start, elems.end());
|
|
|
|
for (auto& each : elems) {
|
|
|
|
boost::algorithm::trim(each);
|
|
|
|
}
|
|
|
|
return elems;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> split(const std::string& s,
|
|
|
|
const std::string& delim,
|
|
|
|
size_t occurences) {
|
|
|
|
// Split the string normally with the required delimiter.
|
|
|
|
auto content = split(s, delim);
|
|
|
|
// While the result split exceeds the number of requested occurrences, join.
|
|
|
|
std::vector<std::string> accumulator;
|
|
|
|
std::vector<std::string> elems;
|
|
|
|
for (size_t i = 0; i < content.size(); i++) {
|
|
|
|
if (i < occurences) {
|
|
|
|
elems.push_back(content.at(i));
|
|
|
|
} else {
|
|
|
|
accumulator.push_back(content.at(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Join the optional accumulator.
|
|
|
|
if (accumulator.size() > 0) {
|
|
|
|
elems.push_back(join(accumulator, delim));
|
|
|
|
}
|
|
|
|
return elems;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string join(const std::vector<std::string>& s, const std::string& tok) {
|
|
|
|
return boost::algorithm::join(s, tok);
|
|
|
|
}
|
2016-12-22 08:37:59 +00:00
|
|
|
|
|
|
|
std::string getBufferSHA1(const char* buffer, size_t size) {
|
|
|
|
// SHA1 produces 160-bit digests, so allocate (5 * 32) bits.
|
|
|
|
uint32_t digest[5] = {0};
|
|
|
|
boost::uuids::detail::sha1 sha1;
|
|
|
|
sha1.process_bytes(buffer, size);
|
|
|
|
sha1.get_digest(digest);
|
|
|
|
|
|
|
|
// Convert digest to desired hex string representation.
|
|
|
|
std::stringstream result;
|
|
|
|
result << std::hex << std::setfill('0');
|
|
|
|
for (size_t i = 0; i < 5; ++i) {
|
|
|
|
result << std::setw(sizeof(uint32_t) * 2) << digest[i];
|
|
|
|
}
|
|
|
|
return result.str();
|
|
|
|
}
|
2014-12-17 22:03:52 +00:00
|
|
|
}
|