155 lines
5.6 KiB
C++
155 lines
5.6 KiB
C++
// strawberry-fleet-seed: dev tool that writes a deterministic synthetic telemetry store
|
|
// (execs + connects + one detection + one positive label) for fleet validation. Not installed.
|
|
#include "strawberry/database.hpp"
|
|
#include "strawberry/learn/rarity.hpp"
|
|
#include "strawberry/store/log_store.hpp"
|
|
#include "strawberry/telemetry.hpp"
|
|
|
|
#include <cstdint>
|
|
#include <cstdlib>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <random>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace strawberry {
|
|
namespace {
|
|
|
|
constexpr int64_t kSec = 1'000'000'000LL;
|
|
constexpr int64_t kT0 = 1'800'000'000LL * kSec;
|
|
|
|
uint64_t fnv1a(const std::string& s) {
|
|
uint64_t h = 1469598103934665603ULL;
|
|
for (unsigned char c : s) {
|
|
h ^= c;
|
|
h *= 1099511628211ULL;
|
|
}
|
|
return h;
|
|
}
|
|
|
|
int usage() {
|
|
std::cerr << "usage: strawberry-fleet-seed <store_dir> [--node <name>] [--execs <N>] [--seed <S>] [--start-offset-s <sec>] [--model-dir <dir>]\n";
|
|
return 2;
|
|
}
|
|
|
|
int run(int argc, char** argv) {
|
|
std::string store_dir;
|
|
std::string node = "node";
|
|
long long execs = 200;
|
|
long long start_offset_s = 0;
|
|
unsigned long long seed = 1;
|
|
std::string model_dir;
|
|
for (int i = 1; i < argc; ++i) {
|
|
const std::string a = argv[i];
|
|
if (a == "--node" && i + 1 < argc) {
|
|
node = argv[++i];
|
|
} else if (a == "--execs" && i + 1 < argc) {
|
|
char* end = nullptr;
|
|
execs = std::strtoll(argv[++i], &end, 10);
|
|
if (*end != '\0' || execs < 1) return usage();
|
|
} else if (a == "--start-offset-s" && i + 1 < argc) {
|
|
char* end = nullptr;
|
|
start_offset_s = std::strtoll(argv[++i], &end, 10);
|
|
if (*end != '\0' || start_offset_s < 0) return usage();
|
|
} else if (a == "--seed" && i + 1 < argc) {
|
|
char* end = nullptr;
|
|
seed = std::strtoull(argv[++i], &end, 10);
|
|
if (*end != '\0') return usage();
|
|
} else if (a == "--model-dir" && i + 1 < argc) {
|
|
model_dir = argv[++i];
|
|
} else if (!a.empty() && a[0] != '-' && store_dir.empty()) {
|
|
store_dir = a;
|
|
} else {
|
|
return usage();
|
|
}
|
|
}
|
|
if (store_dir.empty()) return usage();
|
|
|
|
Database db;
|
|
if (!db.open(store_dir)) {
|
|
std::cerr << "strawberry-fleet-seed: cannot open store " << store_dir << "\n";
|
|
return 1;
|
|
}
|
|
std::mt19937_64 rng(seed ^ fnv1a(node));
|
|
const std::vector<std::string> common = {"/usr/bin/bash", "/usr/bin/ls", "/usr/bin/curl", "/usr/bin/python3"};
|
|
const std::vector<std::string> specific = {"/opt/" + node + "/agent", "/opt/" + node + "/worker"};
|
|
|
|
std::vector<TelemetryEvent> events;
|
|
long long detections = 0;
|
|
learn::RarityModel rarity;
|
|
int64_t last_ts = kT0;
|
|
for (long long i = 0; i < execs; ++i) {
|
|
const bool node_specific = (rng() % 5) == 0;
|
|
const auto& pool = node_specific ? specific : common;
|
|
const std::string& exe = pool[rng() % pool.size()];
|
|
const long long seq = start_offset_s + i;
|
|
const int64_t ts = kT0 + seq * kSec;
|
|
ProcessExecRecord r;
|
|
r.timestamp_ns = ts;
|
|
r.pid = static_cast<int>(1000 + seq);
|
|
r.process_node_id = start_offset_s == 0 ? ("proc_" + std::to_string(i))
|
|
: ("proc_" + std::to_string(start_offset_s) + "_" + std::to_string(i));
|
|
r.parent_node_id = "";
|
|
r.exe = exe;
|
|
r.cmdline = exe + " x";
|
|
r.source = "live";
|
|
events.push_back({store::RecordType::process_exec, encode_process_exec(r)});
|
|
if (!model_dir.empty()) {
|
|
last_ts = ts;
|
|
learn::ExecContext ctx{"", "", exe, r.cmdline};
|
|
learn::score_and_observe(rarity, learn::exec_tuples(ctx));
|
|
}
|
|
if (i % 5 == 0) {
|
|
BehaviorEvent b;
|
|
b.type = BehaviorEvent::Type::connect;
|
|
b.timestamp_ns = static_cast<uint64_t>(ts + kSec / 2);
|
|
b.pid = r.pid;
|
|
b.family = 2;
|
|
b.address = "93.184.216.34";
|
|
b.port = 443;
|
|
events.push_back({store::RecordType::connect, encode_behavior(b)});
|
|
if (!model_dir.empty()) {
|
|
learn::score_and_observe(
|
|
rarity, {{learn::TupleKind::exe_connect, learn::connect_tuple(exe, 2, b.address, 443)}});
|
|
}
|
|
}
|
|
if (i % 8 == 0) {
|
|
++detections;
|
|
ScanResultRecord s;
|
|
s.timestamp_ns = ts + kSec / 4;
|
|
s.pid = r.pid;
|
|
s.process_node_id = r.process_node_id;
|
|
s.rules = {"Seed_Detect"};
|
|
events.push_back({store::RecordType::scan_result, encode_scan_result(s)});
|
|
}
|
|
}
|
|
bool ok = db.append_telemetry(events, true);
|
|
|
|
db.close();
|
|
if (!model_dir.empty()) {
|
|
std::error_code ec;
|
|
std::filesystem::create_directories(model_dir, ec);
|
|
const std::string rpath = model_dir + "/rarity.state";
|
|
std::ofstream f(rpath, std::ios::binary);
|
|
const std::string bytes = rarity.encode_state(last_ts);
|
|
f.write(bytes.data(), static_cast<std::streamsize>(bytes.size()));
|
|
f.close();
|
|
if (ec || !f) {
|
|
std::cerr << "strawberry-fleet-seed: cannot write " << rpath << "\n";
|
|
return 1;
|
|
}
|
|
}
|
|
if (!ok) {
|
|
std::cerr << "strawberry-fleet-seed: append failed\n";
|
|
return 1;
|
|
}
|
|
std::cout << "seeded " << store_dir << ": " << execs << " execs, ~" << detections << " detections, node=" << node << (model_dir.empty() ? "" : ", rarity.state written") << "\n";
|
|
return 0;
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace strawberry
|
|
|
|
int main(int argc, char** argv) { return strawberry::run(argc, argv); }
|