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.
|
|
|
|
*
|
|
|
|
*/
|
2015-02-05 00:54:44 +00:00
|
|
|
|
2015-04-27 23:40:05 +00:00
|
|
|
#include <stdio.h>
|
2016-07-14 21:15:32 +00:00
|
|
|
#include <string.h>
|
2015-02-05 00:54:44 +00:00
|
|
|
|
2016-07-01 21:56:07 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
2016-03-11 08:30:20 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2016-07-25 18:58:55 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
#include <linenoise.h>
|
|
|
|
#else
|
2016-07-14 21:15:32 +00:00
|
|
|
#include <readline/readline.h>
|
2016-07-25 18:58:55 +00:00
|
|
|
#endif
|
2016-07-14 21:15:32 +00:00
|
|
|
|
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
|
|
|
|
2014-12-03 23:14:02 +00:00
|
|
|
#include <osquery/core.h>
|
2016-03-20 23:05:13 +00:00
|
|
|
#include <osquery/database.h>
|
2015-05-11 08:21:57 +00:00
|
|
|
#include <osquery/extensions.h>
|
2015-11-22 04:36:32 +00:00
|
|
|
#include <osquery/flags.h>
|
2016-07-14 21:15:32 +00:00
|
|
|
#include <osquery/logger.h>
|
2016-07-07 22:16:28 +00:00
|
|
|
#include <osquery/system.h>
|
2015-03-18 19:01:58 +00:00
|
|
|
|
2016-07-07 22:16:28 +00:00
|
|
|
#include "osquery/core/process.h"
|
2015-05-11 08:21:57 +00:00
|
|
|
#include "osquery/core/watcher.h"
|
2015-03-18 19:01:58 +00:00
|
|
|
#include "osquery/devtools/devtools.h"
|
2016-07-01 21:56:07 +00:00
|
|
|
#include "osquery/filesystem/fileops.h"
|
2015-11-22 04:36:32 +00:00
|
|
|
#include "osquery/sql/sqlite_util.h"
|
|
|
|
|
|
|
|
namespace osquery {
|
|
|
|
|
|
|
|
SHELL_FLAG(int32,
|
|
|
|
profile,
|
|
|
|
0,
|
|
|
|
"Enable profile mode when non-0, set number of iterations");
|
|
|
|
HIDDEN_FLAG(int32,
|
|
|
|
profile_delay,
|
|
|
|
0,
|
|
|
|
"Sleep a number of seconds before and after the profiling");
|
2016-03-20 23:05:13 +00:00
|
|
|
|
|
|
|
DECLARE_bool(disable_caching);
|
2015-11-22 04:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int profile(int argc, char *argv[]) {
|
|
|
|
std::string query;
|
2016-07-01 21:56:07 +00:00
|
|
|
if (!osquery::platformIsatty(stdin)) {
|
2015-11-22 04:36:32 +00:00
|
|
|
std::getline(std::cin, query);
|
|
|
|
} else if (argc < 2) {
|
|
|
|
// No query input provided via stdin or as a positional argument.
|
|
|
|
fprintf(stderr, "No query provided via stdin or args to profile...\n");
|
|
|
|
return 2;
|
|
|
|
} else {
|
|
|
|
query = std::string(argv[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (osquery::FLAGS_profile_delay > 0) {
|
2016-07-01 21:56:07 +00:00
|
|
|
osquery::sleepFor(osquery::FLAGS_profile_delay);
|
2015-11-22 04:36:32 +00:00
|
|
|
}
|
|
|
|
|
2016-03-20 23:05:13 +00:00
|
|
|
// Perform some duplication from Initializer with respect to database setup.
|
|
|
|
osquery::DatabasePlugin::setAllowOpen(true);
|
|
|
|
osquery::Registry::setActive("database", "ephemeral");
|
|
|
|
|
2015-11-22 04:36:32 +00:00
|
|
|
auto dbc = osquery::SQLiteDBManager::get();
|
|
|
|
for (size_t i = 0; i < static_cast<size_t>(osquery::FLAGS_profile); ++i) {
|
|
|
|
osquery::QueryData results;
|
2016-02-05 03:12:48 +00:00
|
|
|
auto status = osquery::queryInternal(query, results, dbc->db());
|
2016-02-25 01:58:58 +00:00
|
|
|
dbc->clearAffectedTables();
|
2015-11-22 04:36:32 +00:00
|
|
|
if (!status) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Query failed (%d): %s\n",
|
|
|
|
status.getCode(),
|
|
|
|
status.what().c_str());
|
|
|
|
return status.getCode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (osquery::FLAGS_profile_delay > 0) {
|
2016-07-01 21:56:07 +00:00
|
|
|
osquery::sleepFor(osquery::FLAGS_profile_delay);
|
2015-11-22 04:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-02-05 01:53:27 +00:00
|
|
|
|
2016-07-14 21:15:32 +00:00
|
|
|
// readline completion expects strings to be malloced. readline will free them
|
|
|
|
// later.
|
|
|
|
char *copy_string(const std::string &str) {
|
|
|
|
char *copy = (char *)malloc(str.size() + 1);
|
|
|
|
if (copy == nullptr) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Memory allocation failed during shell autocompletion. Exiting!");
|
|
|
|
osquery::Initializer::shutdown(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
strncpy(copy, str.c_str(), str.size() + 1);
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2016-07-25 18:58:55 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
void table_completion_function(char const *prefix, linenoiseCompletions *lc) {
|
|
|
|
std::vector<std::string> tables = osquery::Registry::names("table");
|
|
|
|
size_t index = 0;
|
|
|
|
|
|
|
|
while (index < tables.size()) {
|
|
|
|
const std::string &table = tables.at(index);
|
|
|
|
++index;
|
|
|
|
|
|
|
|
if (boost::algorithm::starts_with(table, prefix)) {
|
|
|
|
linenoiseAddCompletion(lc, table.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2016-07-14 21:15:32 +00:00
|
|
|
char *completion_generator(const char *text, int state) {
|
|
|
|
static std::vector<std::string> tables;
|
|
|
|
static size_t index;
|
|
|
|
|
|
|
|
if (state == 0) {
|
|
|
|
// new completion attempt
|
|
|
|
tables = osquery::Registry::names("table");
|
|
|
|
index = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (index < tables.size()) {
|
|
|
|
const std::string &table = tables.at(index);
|
|
|
|
++index;
|
|
|
|
|
|
|
|
if (boost::algorithm::starts_with(table, text)) {
|
|
|
|
return copy_string(table);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
char **table_completion_function(const char *text, int start, int end) {
|
|
|
|
return rl_completion_matches(text, &completion_generator);
|
|
|
|
}
|
2016-07-25 18:58:55 +00:00
|
|
|
#endif
|
2016-07-14 21:15:32 +00:00
|
|
|
|
2014-07-31 00:35:19 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
2015-02-03 05:21:36 +00:00
|
|
|
// Parse/apply flags, start registry, load logger/config plugins.
|
2016-08-10 03:27:42 +00:00
|
|
|
osquery::Initializer runner(argc, argv, osquery::ToolType::SHELL);
|
2016-02-09 05:50:08 +00:00
|
|
|
|
|
|
|
// The shell will not use a worker process.
|
|
|
|
// It will initialize a watcher thread for potential auto-loaded extensions.
|
|
|
|
runner.initWorkerWatcher();
|
|
|
|
|
|
|
|
// Check for shell-specific switches and positional arguments.
|
2016-07-01 21:56:07 +00:00
|
|
|
if (argc > 1 || !osquery::platformIsatty(stdin) || osquery::FLAGS_A.size() > 0 ||
|
2016-05-12 16:22:05 +00:00
|
|
|
osquery::FLAGS_pack.size() > 0 || osquery::FLAGS_L ||
|
|
|
|
osquery::FLAGS_profile > 0) {
|
2015-11-22 04:36:32 +00:00
|
|
|
// A query was set as a positional argument, via stdin, or profiling is on.
|
2015-04-27 23:40:05 +00:00
|
|
|
osquery::FLAGS_disable_events = true;
|
2016-03-20 23:05:13 +00:00
|
|
|
osquery::FLAGS_disable_caching = true;
|
2015-05-11 08:21:57 +00:00
|
|
|
// The shell may have loaded table extensions, if not, disable the manager.
|
|
|
|
if (!osquery::Watcher::hasManagedExtensions()) {
|
|
|
|
osquery::FLAGS_disable_extensions = true;
|
|
|
|
}
|
2015-04-27 23:40:05 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 18:58:55 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
linenoiseSetCompletionCallback(table_completion_function);
|
|
|
|
#else
|
2016-07-14 21:15:32 +00:00
|
|
|
// Set up readline autocompletion
|
|
|
|
rl_attempted_completion_function = table_completion_function;
|
2016-07-25 18:58:55 +00:00
|
|
|
#endif
|
2016-07-14 21:15:32 +00:00
|
|
|
|
2015-11-22 04:36:32 +00:00
|
|
|
int retcode = 0;
|
|
|
|
if (osquery::FLAGS_profile <= 0) {
|
|
|
|
runner.start();
|
2014-09-23 01:35:12 +00:00
|
|
|
|
2015-11-22 04:36:32 +00:00
|
|
|
// Virtual tables will be attached to the shell's in-memory SQLite DB.
|
|
|
|
retcode = osquery::launchIntoShell(argc, argv);
|
2016-03-20 23:05:13 +00:00
|
|
|
// Finally shutdown.
|
|
|
|
runner.requestShutdown();
|
2015-11-22 04:36:32 +00:00
|
|
|
} else {
|
|
|
|
retcode = profile(argc, argv);
|
|
|
|
}
|
2014-09-23 01:35:12 +00:00
|
|
|
|
|
|
|
return retcode;
|
2014-07-31 00:35:19 +00:00
|
|
|
}
|