Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
scan_cli.cpp
Go to the documentation of this file.
1#include <Windows.h>
2#include <cstdio>
3#include <chrono>
4#include <iostream>
5#include <string>
6#include "scanner_ipc.hpp"
7#include "absl/flags/flag.h"
8#include "absl/flags/parse.h"
9#include <nlohmann/json.hpp>
10
11ABSL_FLAG(std::string, file, "", "File for Panoptes To Scan");
12ABSL_FLAG(int, timeout, 120, "Seconds to wait for every extension's result (YARA, ML)");
13
14static std::string Join(const std::vector<std::string>& Items) {
15 std::string out;
16 for (const auto& item : Items) {
17 out += (out.empty() ? "" : ", ") + item;
18 }
19 return out;
20}
21
22static int Run();
23
24// gRPC keeps working on its own threads after the last channel is gone (even past
25// grpc_shutdown_blocking). Returning from main runs the C runtime's exit handlers while they do,
26// and a thread then uses a lock the runtime has already torn down: an access violation on exit
27// after every successful scan. So once the output is flushed, the process ends with _exit,
28// which skips those handlers; Windows stops the other threads first. The CRT is linked
29// statically, so nothing else is unloaded either
30int main() {
31 int result = Run();
32 std::cout.flush();
33 fflush(stdout);
34 fflush(stderr);
35 _exit(result);
36}
37
38static int Run() {
39 absl::ParseCommandLine(__argc, __argv);
40 if (absl::GetFlag(FLAGS_file).empty()) {
41 printf("ERROR: No file specified\n");
42 printf("Usage: PanoptesScanCLI.exe -file <FILE_TO_SCAN> [-timeout <SECONDS>]\n");
43 return 1;
44 }
45
46 std::string fileToScan = absl::GetFlag(FLAGS_file);
48
49 // The service sends the file to every running extension (PanoptesYara, and PanoptesML when
50 // installed) that hasn't scanned it yet. Each reports on its own, so wait until none is
51 // pending. The waiting requests are polls: they start no scan and show no notification
52 ScanReply reply;
53 std::string error;
54 if (!client.QueuePeScan(fileToScan, "", false, reply, error)) {
55 std::cout << "An Error Occured:" << "\n" << error << std::endl;
56 return 1;
57 }
58 if (!reply.Pending.empty()) {
59 std::cout << "Waiting for: " << Join(reply.Pending) << std::endl;
60 }
61 auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(absl::GetFlag(FLAGS_timeout));
62 while (!reply.Pending.empty() && std::chrono::steady_clock::now() < deadline) {
63 Sleep(500);
64 std::string fileHash = reply.FileHash;
65 if (!client.QueuePeScan(fileToScan, fileHash, true, reply, error)) {
66 std::cout << "An Error Occured:" << "\n" << error << std::endl;
67 return 1;
68 }
69 }
70
71 if (reply.Results.empty()) {
72 std::cout << "No scan results" << (reply.Pending.empty() ? "" : " after " + std::to_string(absl::GetFlag(FLAGS_timeout)) + " seconds") << std::endl;
73 return 1;
74 }
75 nlohmann::json j = nlohmann::json::parse(reply.Results);
76 std::string prettyJson = j.dump(4, ' ', true);
77 std::cout << "Scan Results:" << "\n" << prettyJson << std::endl;
78 if (!reply.Pending.empty()) {
79 std::cout << "No result yet from: " << Join(reply.Pending) << std::endl;
80 return 1;
81 }
82 return 0;
83}
The Panoptes Service client used to talk to the service over gRPC.
bool QueuePeScan(std::string PePath, std::string FileHash, std::string &message)
Send a request to the Panoptes Service to scan a PE file.
ABSL_FLAG(std::string, file, "", "File for Panoptes To Scan")
int main()
Definition scan_cli.cpp:30
What the service answered a scan request with.
std::vector< std::string > Pending
std::string Results
std::string FileHash