Some checks failed
Build and Test / build (debug, clang) (push) Has been cancelled
Build and Test / build (debug, gcc) (push) Has been cancelled
Build and Test / build (release, clang) (push) Has been cancelled
Build and Test / build (release, gcc) (push) Has been cancelled
Build and Test / docker (push) Has been cancelled
Wire the policy, feeds, containment, exec guard, behavior, governor, crash safety and event store into the daemon, detection pipeline, CLI and meson build (zstd now required); Dockerfile, conan, systemd unit (LimitCORE=0, resource limits) and installer updates; existing tests adapted (store paths are directories now). Detections are counted and notified even when evidence cannot be persisted (persist_failures in status). prevention_timeout_ms is clamped and behavior dedupe handles out-of-order events. Docs: API contract, developer guide, plan, UX, agent rules. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
311 lines
11 KiB
C++
311 lines
11 KiB
C++
#include "strawberry/process_query.hpp"
|
|
#include "strawberry/parse_num.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <cctype>
|
|
#include <cstdint>
|
|
#include <map>
|
|
#include <optional>
|
|
#include <set>
|
|
#include <string_view>
|
|
|
|
namespace {
|
|
|
|
std::string lower_copy(std::string_view input) {
|
|
std::string output;
|
|
output.reserve(input.size());
|
|
for (unsigned char ch : input) {
|
|
output.push_back(static_cast<char>(std::tolower(ch)));
|
|
}
|
|
return output;
|
|
}
|
|
|
|
std::string property_value(const strawberry::GraphNode& node, std::string_view key) {
|
|
const auto it = node.properties.find(std::string(key));
|
|
if (it == node.properties.end()) {
|
|
return {};
|
|
}
|
|
return it->second;
|
|
}
|
|
|
|
std::string metadata_value(const strawberry::IndicatorEntry& entry, std::string_view key) {
|
|
const auto it = entry.metadata.find(std::string(key));
|
|
if (it == entry.metadata.end()) {
|
|
return {};
|
|
}
|
|
return it->second;
|
|
}
|
|
|
|
std::string process_node_for(const strawberry::IndicatorEntry& entry) {
|
|
if (auto id = metadata_value(entry, "process_node_id"); !id.empty()) {
|
|
return id;
|
|
}
|
|
const auto pid = metadata_value(entry, "pid");
|
|
return pid.empty() ? std::string{} : "process_" + pid;
|
|
}
|
|
|
|
bool contains_ci(std::string_view haystack, std::string_view needle) {
|
|
if (needle.empty()) {
|
|
return true;
|
|
}
|
|
return lower_copy(haystack).find(lower_copy(needle)) != std::string::npos;
|
|
}
|
|
|
|
void add_field(std::vector<std::string>& fields, std::string field) {
|
|
if (std::find(fields.begin(), fields.end(), field) == fields.end()) {
|
|
fields.push_back(std::move(field));
|
|
}
|
|
}
|
|
|
|
void add_process_id(std::map<std::string, std::vector<std::string>>& matched,
|
|
const std::string& process_id,
|
|
const std::string& field) {
|
|
if (process_id.empty()) {
|
|
return;
|
|
}
|
|
add_field(matched[process_id], field);
|
|
}
|
|
|
|
std::optional<uint64_t> parse_u64(std::string_view value) {
|
|
return strawberry::parse_number<uint64_t>(value);
|
|
}
|
|
|
|
void include_seen_timestamp(strawberry::ProcessSearchResult& result, uint64_t timestamp) {
|
|
if (timestamp == 0) {
|
|
return;
|
|
}
|
|
if (!result.first_seen || timestamp < *result.first_seen) {
|
|
result.first_seen = timestamp;
|
|
}
|
|
if (!result.last_seen || timestamp > *result.last_seen) {
|
|
result.last_seen = timestamp;
|
|
}
|
|
}
|
|
|
|
void include_seen_timestamp(strawberry::ProcessSearchResult& result, std::string_view timestamp) {
|
|
if (const auto parsed = parse_u64(timestamp)) {
|
|
include_seen_timestamp(result, *parsed);
|
|
}
|
|
}
|
|
|
|
void add_unique_node(std::vector<strawberry::GraphNode>& nodes, strawberry::GraphNode node) {
|
|
const auto exists = std::any_of(nodes.begin(), nodes.end(), [&node](const auto& existing) {
|
|
return existing.id == node.id;
|
|
});
|
|
if (!exists) {
|
|
nodes.push_back(std::move(node));
|
|
}
|
|
}
|
|
|
|
std::vector<strawberry::GraphNode> child_processes_for(strawberry::Database& database,
|
|
const strawberry::GraphNode& process) {
|
|
std::vector<strawberry::GraphNode> children;
|
|
|
|
for (const auto& edge : database.get_node_edges(process.id)) {
|
|
if (edge.from_id != process.id || edge.relationship_type != "spawned") {
|
|
continue;
|
|
}
|
|
|
|
auto child = database.find_graph_node(edge.to_id);
|
|
if (child && child->type == "process") {
|
|
add_unique_node(children, *child);
|
|
}
|
|
}
|
|
|
|
const auto pid = property_value(process, "pid");
|
|
if (!pid.empty()) {
|
|
for (const auto& node : database.list_graph_nodes("process")) {
|
|
if (node.id == process.id) {
|
|
continue;
|
|
}
|
|
// Prefer the stable parent id; fall back to PID for older evidence.
|
|
const auto parent_node_id = property_value(node, "parent_node_id");
|
|
const bool is_child = !parent_node_id.empty()
|
|
? parent_node_id == process.id
|
|
: property_value(node, "parent_pid") == pid;
|
|
if (is_child) {
|
|
add_unique_node(children, node);
|
|
}
|
|
}
|
|
}
|
|
|
|
std::sort(children.begin(), children.end(), [](const auto& left, const auto& right) {
|
|
const auto left_pid = property_value(left, "pid");
|
|
const auto right_pid = property_value(right, "pid");
|
|
if (left_pid != right_pid) return left_pid < right_pid;
|
|
return left.id < right.id;
|
|
});
|
|
|
|
return children;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
namespace strawberry {
|
|
|
|
std::vector<ProcessSearchResult> ProcessQuery::search(Database& database,
|
|
const std::string& query,
|
|
int max_related_depth) {
|
|
if (!database.is_open() || query.empty()) {
|
|
return {};
|
|
}
|
|
|
|
std::map<std::string, std::vector<std::string>> matched;
|
|
|
|
for (const auto& node : database.list_graph_nodes("process")) {
|
|
const auto pid = property_value(node, "pid");
|
|
const auto path = property_value(node, "path");
|
|
const auto hash = property_value(node, "hash");
|
|
const auto cmdline = property_value(node, "cmdline");
|
|
const auto parent_pid = property_value(node, "parent_pid");
|
|
|
|
if (node.id == query || pid == query) {
|
|
add_process_id(matched, node.id, "pid");
|
|
}
|
|
if (parent_pid == query) {
|
|
add_process_id(matched, node.id, "parent_pid");
|
|
}
|
|
if (contains_ci(path, query)) {
|
|
add_process_id(matched, node.id, "path");
|
|
}
|
|
if (!hash.empty() && contains_ci(hash, query)) {
|
|
add_process_id(matched, node.id, "hash");
|
|
}
|
|
if (contains_ci(cmdline, query)) {
|
|
add_process_id(matched, node.id, "cmdline");
|
|
}
|
|
}
|
|
|
|
const auto indicators = database.list_indicators();
|
|
for (const auto& indicator : indicators) {
|
|
const auto pid = metadata_value(indicator, "pid");
|
|
const auto parent_pid = metadata_value(indicator, "parent_pid");
|
|
const auto process_path = metadata_value(indicator, "process_path");
|
|
const auto cmdline = metadata_value(indicator, "cmdline");
|
|
const auto process_id = process_node_for(indicator);
|
|
|
|
if (pid == query) {
|
|
add_process_id(matched, process_id, "indicator.pid");
|
|
}
|
|
if (parent_pid == query) {
|
|
add_process_id(matched, process_id, "indicator.parent_pid");
|
|
}
|
|
if (contains_ci(indicator.rule_name, query)) {
|
|
add_process_id(matched, process_id, "indicator.rule_name");
|
|
}
|
|
if (!indicator.hash.empty() && contains_ci(indicator.hash, query)) {
|
|
add_process_id(matched, process_id, "indicator.hash");
|
|
}
|
|
if (contains_ci(process_path, query)) {
|
|
add_process_id(matched, process_id, "indicator.process_path");
|
|
}
|
|
if (contains_ci(cmdline, query)) {
|
|
add_process_id(matched, process_id, "indicator.cmdline");
|
|
}
|
|
}
|
|
|
|
const auto findings = database.list_findings();
|
|
for (const auto& finding : findings) {
|
|
const bool finding_match =
|
|
contains_ci(finding.id, query)
|
|
|| contains_ci(finding.title, query)
|
|
|| contains_ci(finding.summary, query)
|
|
|| contains_ci(finding.severity, query);
|
|
|
|
std::set<std::string> process_ids;
|
|
for (const auto& node_id : finding.related_node_ids) {
|
|
if (node_id.rfind("process_", 0) == 0) {
|
|
process_ids.insert(node_id);
|
|
}
|
|
}
|
|
|
|
if (finding_match) {
|
|
for (const auto& process_id : process_ids) {
|
|
add_process_id(matched, process_id, "finding");
|
|
}
|
|
continue;
|
|
}
|
|
|
|
for (const auto& [key, value] : finding.metadata) {
|
|
if (contains_ci(key, query) || contains_ci(value, query)) {
|
|
for (const auto& process_id : process_ids) {
|
|
add_process_id(matched, process_id, "finding.metadata");
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
std::vector<ProcessSearchResult> results;
|
|
for (auto& [process_id, fields] : matched) {
|
|
auto node = database.find_graph_node(process_id);
|
|
if (!node || node->type != "process") {
|
|
continue;
|
|
}
|
|
|
|
ProcessSearchResult result;
|
|
result.process = *node;
|
|
result.matched_fields = std::move(fields);
|
|
include_seen_timestamp(result, property_value(result.process, "event_timestamp_ns"));
|
|
const auto parent_pid = property_value(result.process, "parent_pid");
|
|
auto parent_node_id = property_value(result.process, "parent_node_id");
|
|
if (parent_node_id.empty() && !parent_pid.empty()) {
|
|
parent_node_id = "process_" + parent_pid;
|
|
}
|
|
if (!parent_node_id.empty()) {
|
|
auto parent = database.find_graph_node(parent_node_id);
|
|
if (parent && parent->type == "process") {
|
|
result.parent_process = *parent;
|
|
}
|
|
}
|
|
result.child_processes = child_processes_for(database, result.process);
|
|
result.related_nodes = database.find_related_nodes(process_id, max_related_depth);
|
|
|
|
for (const auto& edge : database.get_node_edges(process_id)) {
|
|
include_seen_timestamp(result, edge.timestamp);
|
|
}
|
|
|
|
for (const auto& related : result.related_nodes) {
|
|
include_seen_timestamp(result, property_value(related, "event_timestamp_ns"));
|
|
}
|
|
|
|
for (const auto& indicator : indicators) {
|
|
if (process_node_for(indicator) == process_id) {
|
|
include_seen_timestamp(result, indicator.timestamp);
|
|
include_seen_timestamp(result, metadata_value(indicator, "event_timestamp_ns"));
|
|
}
|
|
}
|
|
|
|
for (const auto& finding : findings) {
|
|
if (std::find(finding.related_node_ids.begin(),
|
|
finding.related_node_ids.end(),
|
|
process_id) != finding.related_node_ids.end()) {
|
|
result.findings.push_back(finding);
|
|
include_seen_timestamp(result, finding.first_seen);
|
|
include_seen_timestamp(result, finding.last_seen);
|
|
}
|
|
}
|
|
|
|
std::sort(result.matched_fields.begin(), result.matched_fields.end());
|
|
std::sort(result.findings.begin(), result.findings.end(), [](const auto& left, const auto& right) {
|
|
if (left.score != right.score) return left.score > right.score;
|
|
if (left.last_seen != right.last_seen) return left.last_seen > right.last_seen;
|
|
return left.id < right.id;
|
|
});
|
|
|
|
results.push_back(std::move(result));
|
|
}
|
|
|
|
std::sort(results.begin(), results.end(), [](const auto& left, const auto& right) {
|
|
const auto left_pid = property_value(left.process, "pid");
|
|
const auto right_pid = property_value(right.process, "pid");
|
|
if (left_pid != right_pid) {
|
|
return left_pid < right_pid;
|
|
}
|
|
return left.process.id < right.process.id;
|
|
});
|
|
|
|
return results;
|
|
}
|
|
|
|
} // namespace strawberry
|