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-01-22 21:57:14 +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-11-29 00:22:02 +00:00
|
|
|
|
2015-01-30 18:44:25 +00:00
|
|
|
#include <boost/property_tree/json_parser.hpp>
|
|
|
|
|
2015-11-09 02:31:50 +00:00
|
|
|
#include <osquery/database.h>
|
|
|
|
#include <osquery/flags.h>
|
2014-12-03 23:14:02 +00:00
|
|
|
#include <osquery/logger.h>
|
|
|
|
#include <osquery/tables.h>
|
2014-11-29 00:22:02 +00:00
|
|
|
|
2015-05-03 20:37:11 +00:00
|
|
|
namespace pt = boost::property_tree;
|
|
|
|
|
2014-11-29 00:22:02 +00:00
|
|
|
namespace osquery {
|
|
|
|
|
2015-11-09 02:31:50 +00:00
|
|
|
FLAG(bool, disable_caching, false, "Disable scheduled query caching");
|
|
|
|
|
|
|
|
size_t TablePlugin::kCacheInterval = 0;
|
|
|
|
size_t TablePlugin::kCacheStep = 0;
|
|
|
|
|
2015-11-09 09:19:45 +00:00
|
|
|
const std::map<ColumnType, std::string> kColumnTypeNames = {
|
|
|
|
{UNKNOWN_TYPE, "UNKNOWN"},
|
|
|
|
{TEXT_TYPE, "TEXT"},
|
|
|
|
{INTEGER_TYPE, "INTEGER"},
|
|
|
|
{BIGINT_TYPE, "BIGINT"},
|
|
|
|
{UNSIGNED_BIGINT_TYPE, "UNSIGNED BIGINT"},
|
|
|
|
{DOUBLE_TYPE, "DOUBLE"},
|
|
|
|
{BLOB_TYPE, "BLOB"},
|
|
|
|
};
|
|
|
|
|
2015-02-23 05:56:52 +00:00
|
|
|
Status TablePlugin::addExternal(const std::string& name,
|
|
|
|
const PluginResponse& response) {
|
|
|
|
// Attach the table.
|
|
|
|
if (response.size() == 0) {
|
|
|
|
// Invalid table route info.
|
|
|
|
return Status(1, "Invalid route info");
|
2014-11-29 00:22:02 +00:00
|
|
|
}
|
2015-01-30 18:44:25 +00:00
|
|
|
|
2015-02-23 05:56:52 +00:00
|
|
|
// Use the SQL registry to attach the name/definition.
|
|
|
|
return Registry::call("sql", "sql", {{"action", "attach"}, {"table", name}});
|
2015-01-30 18:44:25 +00:00
|
|
|
}
|
|
|
|
|
2015-02-23 05:56:52 +00:00
|
|
|
void TablePlugin::removeExternal(const std::string& name) {
|
|
|
|
// Detach the table name.
|
|
|
|
Registry::call("sql", "sql", {{"action", "detach"}, {"table", name}});
|
2015-01-30 18:44:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TablePlugin::setRequestFromContext(const QueryContext& context,
|
|
|
|
PluginRequest& request) {
|
2015-05-03 20:37:11 +00:00
|
|
|
pt::ptree tree;
|
2015-01-30 18:44:25 +00:00
|
|
|
tree.put("limit", context.limit);
|
|
|
|
|
|
|
|
// The QueryContext contains a constraint map from column to type information
|
|
|
|
// and the list of operand/expression constraints applied to that column from
|
|
|
|
// the query given.
|
2015-05-03 20:37:11 +00:00
|
|
|
pt::ptree constraints;
|
2015-01-30 18:44:25 +00:00
|
|
|
for (const auto& constraint : context.constraints) {
|
2015-05-03 20:37:11 +00:00
|
|
|
pt::ptree child;
|
2015-01-30 18:44:25 +00:00
|
|
|
child.put("name", constraint.first);
|
|
|
|
constraint.second.serialize(child);
|
|
|
|
constraints.push_back(std::make_pair("", child));
|
|
|
|
}
|
|
|
|
tree.add_child("constraints", constraints);
|
|
|
|
|
|
|
|
// Write the property tree as a JSON string into the PluginRequest.
|
|
|
|
std::ostringstream output;
|
2015-07-15 02:09:55 +00:00
|
|
|
try {
|
|
|
|
pt::write_json(output, tree, false);
|
|
|
|
} catch (const pt::json_parser::json_parser_error& e) {
|
|
|
|
// The content could not be represented as JSON.
|
|
|
|
}
|
2015-01-30 18:44:25 +00:00
|
|
|
request["context"] = output.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TablePlugin::setContextFromRequest(const PluginRequest& request,
|
|
|
|
QueryContext& context) {
|
|
|
|
if (request.count("context") == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read serialized context from PluginRequest.
|
2015-05-03 20:37:11 +00:00
|
|
|
pt::ptree tree;
|
|
|
|
try {
|
|
|
|
std::stringstream input;
|
|
|
|
input << request.at("context");
|
|
|
|
pt::read_json(input, tree);
|
|
|
|
} catch (const pt::json_parser::json_parser_error& e) {
|
|
|
|
return;
|
|
|
|
}
|
2015-01-30 18:44:25 +00:00
|
|
|
|
|
|
|
// Set the context limit and deserialize each column constraint list.
|
2015-11-09 02:31:50 +00:00
|
|
|
context.limit = tree.get<int>("limit", 0);
|
2015-01-30 18:44:25 +00:00
|
|
|
for (const auto& constraint : tree.get_child("constraints")) {
|
|
|
|
auto column_name = constraint.second.get<std::string>("name");
|
|
|
|
context.constraints[column_name].unserialize(constraint.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Status TablePlugin::call(const PluginRequest& request,
|
|
|
|
PluginResponse& response) {
|
|
|
|
response.clear();
|
|
|
|
// TablePlugin API calling requires an action.
|
|
|
|
if (request.count("action") == 0) {
|
|
|
|
return Status(1, "Table plugins must include a request action");
|
|
|
|
}
|
|
|
|
|
2015-02-23 05:56:52 +00:00
|
|
|
if (request.at("action") == "generate") {
|
2016-05-09 17:32:33 +00:00
|
|
|
// The "generate" action runs the table implementation using a PluginRequest
|
|
|
|
// with optional serialized QueryContext and returns the QueryData results
|
|
|
|
// as the PluginRequest data.
|
|
|
|
|
|
|
|
// Create a fake table implementation for caching.
|
2015-01-30 18:44:25 +00:00
|
|
|
QueryContext context;
|
|
|
|
if (request.count("context") > 0) {
|
|
|
|
setContextFromRequest(request, context);
|
|
|
|
}
|
2015-11-09 02:31:50 +00:00
|
|
|
response = generate(context);
|
2015-01-30 18:44:25 +00:00
|
|
|
} else if (request.at("action") == "columns") {
|
2016-05-09 17:32:33 +00:00
|
|
|
// The "columns" action returns a PluginRequest filled with column
|
|
|
|
// information such as name and type.
|
2015-02-11 03:18:56 +00:00
|
|
|
const auto& column_list = columns();
|
2015-01-30 18:44:25 +00:00
|
|
|
for (const auto& column : column_list) {
|
2016-05-18 19:23:52 +00:00
|
|
|
response.push_back({{"name", std::get<0>(column)},
|
|
|
|
{"type", columnTypeName(std::get<1>(column))},
|
|
|
|
{"op", INTEGER(std::get<2>(column))}});
|
2015-01-30 18:44:25 +00:00
|
|
|
}
|
2015-02-23 05:56:52 +00:00
|
|
|
} else if (request.at("action") == "definition") {
|
2015-01-31 01:52:14 +00:00
|
|
|
response.push_back({{"definition", columnDefinition()}});
|
2015-01-30 18:44:25 +00:00
|
|
|
} else {
|
|
|
|
return Status(1, "Unknown table plugin action: " + request.at("action"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status(0, "OK");
|
|
|
|
}
|
|
|
|
|
2015-02-11 03:18:56 +00:00
|
|
|
std::string TablePlugin::columnDefinition() const {
|
2015-05-20 20:27:53 +00:00
|
|
|
return osquery::columnDefinition(columns());
|
2015-02-23 05:56:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PluginResponse TablePlugin::routeInfo() const {
|
|
|
|
// Route info consists of only the serialized column information.
|
|
|
|
PluginResponse response;
|
|
|
|
for (const auto& column : columns()) {
|
2016-05-18 19:23:52 +00:00
|
|
|
response.push_back({{"name", std::get<0>(column)},
|
|
|
|
{"type", columnTypeName(std::get<1>(column))},
|
|
|
|
{"op", INTEGER(std::get<2>(column))}});
|
2015-02-23 05:56:52 +00:00
|
|
|
}
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2015-11-09 02:31:50 +00:00
|
|
|
bool TablePlugin::isCached(size_t step) {
|
|
|
|
return (!FLAGS_disable_caching && step < last_cached_ + last_interval_);
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryData TablePlugin::getCache() const {
|
|
|
|
VLOG(1) << "Retrieving results from cache for table: " << getName();
|
|
|
|
// Lookup results from database and deserialize.
|
|
|
|
std::string content;
|
|
|
|
getDatabaseValue(kQueries, "cache." + getName(), content);
|
|
|
|
QueryData results;
|
|
|
|
deserializeQueryDataJSON(content, results);
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TablePlugin::setCache(size_t step,
|
|
|
|
size_t interval,
|
|
|
|
const QueryData& results) {
|
|
|
|
// Serialize QueryData and save to database.
|
|
|
|
std::string content;
|
|
|
|
if (!FLAGS_disable_caching && serializeQueryDataJSON(results, content)) {
|
|
|
|
last_cached_ = step;
|
|
|
|
last_interval_ = interval;
|
|
|
|
setDatabaseValue(kQueries, "cache." + getName(), content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-23 05:56:52 +00:00
|
|
|
std::string columnDefinition(const TableColumns& columns) {
|
2015-01-31 01:52:14 +00:00
|
|
|
std::string statement = "(";
|
2015-02-23 05:56:52 +00:00
|
|
|
for (size_t i = 0; i < columns.size(); ++i) {
|
2016-05-18 19:23:52 +00:00
|
|
|
const auto& column = columns.at(i);
|
2015-11-09 09:19:45 +00:00
|
|
|
statement +=
|
2016-05-18 19:23:52 +00:00
|
|
|
"`" + std::get<0>(column) + "` " + columnTypeName(std::get<1>(column));
|
2015-02-23 05:56:52 +00:00
|
|
|
if (i < columns.size() - 1) {
|
2015-01-30 18:44:25 +00:00
|
|
|
statement += ", ";
|
|
|
|
}
|
|
|
|
}
|
2015-02-11 03:18:56 +00:00
|
|
|
return statement += ")";
|
2015-01-30 18:44:25 +00:00
|
|
|
}
|
2015-01-31 01:52:14 +00:00
|
|
|
|
2015-02-23 05:56:52 +00:00
|
|
|
std::string columnDefinition(const PluginResponse& response) {
|
|
|
|
TableColumns columns;
|
|
|
|
for (const auto& column : response) {
|
2016-05-18 19:23:52 +00:00
|
|
|
columns.push_back(make_tuple(
|
|
|
|
column.at("name"),
|
|
|
|
columnTypeName(column.at("type")),
|
|
|
|
(ColumnOptions)AS_LITERAL(INTEGER_LITERAL, column.at("op"))));
|
2015-02-23 05:56:52 +00:00
|
|
|
}
|
|
|
|
return columnDefinition(columns);
|
|
|
|
}
|
|
|
|
|
2015-11-09 09:19:45 +00:00
|
|
|
ColumnType columnTypeName(const std::string& type) {
|
|
|
|
for (const auto& col : kColumnTypeNames) {
|
|
|
|
if (col.second == type) {
|
|
|
|
return col.first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return UNKNOWN_TYPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConstraintList::exists(const ConstraintOperatorFlag ops) const {
|
|
|
|
if (ops == ANY_OP) {
|
|
|
|
return (constraints_.size() > 0);
|
|
|
|
} else {
|
|
|
|
for (const struct Constraint& c : constraints_) {
|
|
|
|
if (c.op & ops) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-23 05:56:52 +00:00
|
|
|
bool ConstraintList::matches(const std::string& expr) const {
|
|
|
|
// Support each SQL affinity type casting.
|
2015-11-09 09:19:45 +00:00
|
|
|
if (affinity == TEXT_TYPE) {
|
2015-02-23 05:56:52 +00:00
|
|
|
return literal_matches<TEXT_LITERAL>(expr);
|
2015-11-09 09:19:45 +00:00
|
|
|
} else if (affinity == INTEGER_TYPE) {
|
2015-02-23 05:56:52 +00:00
|
|
|
INTEGER_LITERAL lexpr = AS_LITERAL(INTEGER_LITERAL, expr);
|
|
|
|
return literal_matches<INTEGER_LITERAL>(lexpr);
|
2015-11-09 09:19:45 +00:00
|
|
|
} else if (affinity == BIGINT_TYPE) {
|
2015-02-23 05:56:52 +00:00
|
|
|
BIGINT_LITERAL lexpr = AS_LITERAL(BIGINT_LITERAL, expr);
|
|
|
|
return literal_matches<BIGINT_LITERAL>(lexpr);
|
2015-11-09 09:19:45 +00:00
|
|
|
} else if (affinity == UNSIGNED_BIGINT_TYPE) {
|
2015-02-23 05:56:52 +00:00
|
|
|
UNSIGNED_BIGINT_LITERAL lexpr = AS_LITERAL(UNSIGNED_BIGINT_LITERAL, expr);
|
|
|
|
return literal_matches<UNSIGNED_BIGINT_LITERAL>(lexpr);
|
|
|
|
} else {
|
2015-07-15 02:09:55 +00:00
|
|
|
// Unsupported affinity type.
|
2015-02-23 05:56:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool ConstraintList::literal_matches(const T& base_expr) const {
|
|
|
|
bool aggregate = true;
|
|
|
|
for (size_t i = 0; i < constraints_.size(); ++i) {
|
|
|
|
T constraint_expr = AS_LITERAL(T, constraints_[i].expr);
|
|
|
|
if (constraints_[i].op == EQUALS) {
|
|
|
|
aggregate = aggregate && (base_expr == constraint_expr);
|
|
|
|
} else if (constraints_[i].op == GREATER_THAN) {
|
|
|
|
aggregate = aggregate && (base_expr > constraint_expr);
|
|
|
|
} else if (constraints_[i].op == LESS_THAN) {
|
|
|
|
aggregate = aggregate && (base_expr < constraint_expr);
|
|
|
|
} else if (constraints_[i].op == GREATER_THAN_OR_EQUALS) {
|
|
|
|
aggregate = aggregate && (base_expr >= constraint_expr);
|
|
|
|
} else if (constraints_[i].op == LESS_THAN_OR_EQUALS) {
|
|
|
|
aggregate = aggregate && (base_expr <= constraint_expr);
|
|
|
|
} else {
|
|
|
|
// Unsupported constraint.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!aggregate) {
|
|
|
|
// Speed up comparison.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::set<std::string> ConstraintList::getAll(ConstraintOperator op) const {
|
|
|
|
std::set<std::string> set;
|
|
|
|
for (size_t i = 0; i < constraints_.size(); ++i) {
|
|
|
|
if (constraints_[i].op == op) {
|
|
|
|
// TODO: this does not apply a distinct.
|
|
|
|
set.insert(constraints_[i].expr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConstraintList::serialize(boost::property_tree::ptree& tree) const {
|
|
|
|
boost::property_tree::ptree expressions;
|
|
|
|
for (const auto& constraint : constraints_) {
|
|
|
|
boost::property_tree::ptree child;
|
|
|
|
child.put("op", constraint.op);
|
|
|
|
child.put("expr", constraint.expr);
|
|
|
|
expressions.push_back(std::make_pair("", child));
|
|
|
|
}
|
|
|
|
tree.add_child("list", expressions);
|
2015-11-09 09:19:45 +00:00
|
|
|
tree.put("affinity", columnTypeName(affinity));
|
2015-01-31 01:52:14 +00:00
|
|
|
}
|
2015-01-22 21:57:14 +00:00
|
|
|
|
2015-02-23 05:56:52 +00:00
|
|
|
void ConstraintList::unserialize(const boost::property_tree::ptree& tree) {
|
|
|
|
// Iterate through the list of operand/expressions, then set the constraint
|
|
|
|
// type affinity.
|
|
|
|
for (const auto& list : tree.get_child("list")) {
|
|
|
|
Constraint constraint(list.second.get<unsigned char>("op"));
|
|
|
|
constraint.expr = list.second.get<std::string>("expr");
|
|
|
|
constraints_.push_back(constraint);
|
|
|
|
}
|
2015-11-09 09:19:45 +00:00
|
|
|
affinity = columnTypeName(tree.get<std::string>("affinity", "UNKNOWN"));
|
2015-02-23 05:56:52 +00:00
|
|
|
}
|
2015-11-24 20:29:03 +00:00
|
|
|
|
|
|
|
bool QueryContext::hasConstraint(const std::string& column,
|
|
|
|
ConstraintOperator op) const {
|
|
|
|
if (constraints.count(column) == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return constraints.at(column).exists(op);
|
|
|
|
}
|
2016-02-17 07:10:08 +00:00
|
|
|
|
|
|
|
Status QueryContext::expandConstraints(
|
|
|
|
const std::string& column,
|
|
|
|
ConstraintOperator op,
|
|
|
|
std::set<std::string>& output,
|
|
|
|
std::function<Status(const std::string& constraint,
|
|
|
|
std::set<std::string>& output)> predicate) {
|
|
|
|
for (const auto& constraint : constraints[column].getAll(op)) {
|
|
|
|
auto status = predicate(constraint, output);
|
|
|
|
if (!status) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Status(0);
|
|
|
|
}
|
2014-11-29 00:22:02 +00:00
|
|
|
}
|