2017-12-19 00:04:06 +00:00
|
|
|
/**
|
2017-05-26 18:19:43 +00:00
|
|
|
* Copyright (c) 2014-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
2017-12-19 00:04:06 +00:00
|
|
|
* This source code is licensed under both the Apache 2.0 license (found in the
|
|
|
|
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
|
|
|
|
* in the COPYING file in the root directory of this source tree).
|
|
|
|
* You may select, at your option, one of the above-listed licenses.
|
2017-05-26 18:19:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
|
2018-09-21 18:54:31 +00:00
|
|
|
#include <osquery/carver/carver.h>
|
2017-05-26 18:19:43 +00:00
|
|
|
#include <osquery/flags.h>
|
|
|
|
#include <osquery/logger.h>
|
2018-09-21 18:54:31 +00:00
|
|
|
#include <osquery/utils/conversions/split.h>
|
|
|
|
#include <osquery/utils/mutex.h>
|
2017-05-26 18:19:43 +00:00
|
|
|
|
|
|
|
#include <sqlite3.h>
|
|
|
|
|
|
|
|
namespace osquery {
|
|
|
|
|
2017-06-14 00:27:00 +00:00
|
|
|
/// Global set of requested carve paths.
|
|
|
|
static std::set<std::string> kFunctionCarvePaths;
|
|
|
|
|
|
|
|
/// Mutex to protect access to carve paths.
|
|
|
|
Mutex kFunctionCarveMutex;
|
2017-05-26 18:19:43 +00:00
|
|
|
|
|
|
|
DECLARE_bool(carver_disable_function);
|
|
|
|
|
|
|
|
static void addCarveFile(sqlite3_context* ctx, int argc, sqlite3_value** argv) {
|
|
|
|
if (argc == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SQLITE_NULL == sqlite3_value_type(argv[0])) {
|
|
|
|
sqlite3_result_null(ctx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-14 00:27:00 +00:00
|
|
|
WriteLock lock(kFunctionCarveMutex);
|
|
|
|
std::string path((const char*)sqlite3_value_text(argv[0]));
|
|
|
|
kFunctionCarvePaths.insert(path);
|
2017-05-26 18:19:43 +00:00
|
|
|
|
|
|
|
sqlite3_result_text(
|
|
|
|
ctx, path.c_str(), static_cast<int>(path.size()), SQLITE_TRANSIENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void executeCarve(sqlite3_context* ctx) {
|
2017-06-14 00:27:00 +00:00
|
|
|
WriteLock lock(kFunctionCarveMutex);
|
2017-05-26 18:19:43 +00:00
|
|
|
if (!FLAGS_carver_disable_function) {
|
2017-06-14 00:27:00 +00:00
|
|
|
carvePaths(kFunctionCarvePaths);
|
2017-05-26 18:19:43 +00:00
|
|
|
} else {
|
2017-06-14 00:27:00 +00:00
|
|
|
LOG(WARNING) << "Carver as a function is disabled";
|
2017-05-26 18:19:43 +00:00
|
|
|
}
|
2017-06-14 00:27:00 +00:00
|
|
|
kFunctionCarvePaths.clear();
|
2017-05-26 18:19:43 +00:00
|
|
|
sqlite3_result_text(ctx, "Carve Started", 13, SQLITE_TRANSIENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerOperationExtensions(sqlite3* db) {
|
|
|
|
sqlite3_create_function(db,
|
|
|
|
"carve",
|
|
|
|
1,
|
|
|
|
SQLITE_UTF8,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
addCarveFile,
|
|
|
|
executeCarve);
|
|
|
|
}
|
2018-05-24 11:26:43 +00:00
|
|
|
} // namespace osquery
|