Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
detector.h File Reference
#include <string>

Go to the source code of this file.

Functions

bool IsDetectorPeer (const std::string &peer, int serverPort, std::string &why)
 Whether a gRPC peer is PanoptesDetector.exe from the service's folder. The server listens on localhost without credentials, so this keeps other local processes from reading the event stream or asking the service to kill processes.
std::string RespondToDetection (const Detection &detection, const Configuration &configuration)
 Carries out a detection's action and describes what was done, for the log. Kill and quarantine only happen with EnforceDetectionActions; they are refused for system processes, the service and its children, critical processes, and a process ID reused since the match.

Function Documentation

◆ IsDetectorPeer()

bool IsDetectorPeer ( const std::string & peer,
int serverPort,
std::string & why )

Whether a gRPC peer is PanoptesDetector.exe from the service's folder. The server listens on localhost without credentials, so this keeps other local processes from reading the event stream or asking the service to kill processes.

Parameters
peergrpc::ServerContext::peer(), e.g. "ipv4:127.0.0.1:50123"
serverPortThe port the service listens on
whySet to the reason when the peer is refused

Definition at line 91 of file detector.cpp.

92{
93 USHORT clientPort = 0;
94 if (!PeerPort(peer, clientPort)) {
95 why = "unrecognized peer " + peer;
96 return false;
97 }
98
99 DWORD processId = 0;
100 if (!FindConnectionOwner(AF_INET, clientPort, (USHORT)serverPort, processId) &&
101 !FindConnectionOwner(AF_INET6, clientPort, (USHORT)serverPort, processId)) {
102 why = "no local process owns the connection from " + peer;
103 return false;
104 }
105
106 HANDLE process = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, processId);
107 if (process == NULL) {
108 why = "cannot open process " + std::to_string(processId);
109 return false;
110 }
111 std::wstring image = ProcessImagePath(process);
112 CloseHandle(process);
113
114 std::wstring expected = DetectorExecutablePath();
115 if (image.empty() || expected.empty() || _wcsicmp(image.c_str(), expected.c_str()) != 0) {
116 why = "process " + std::to_string(processId) + " (" + ToString(image) + ") is not " + ToString(expected);
117 return false;
118 }
119 return true;
120}
std::wstring DetectorExecutablePath()
The path the service starts the detector from: PanoptesDetector.exe in the service's folder....
std::string ToString(const std::wstring &wstr)
Definition utils.cpp:51

References DetectorExecutablePath(), and ToString().

◆ RespondToDetection()

std::string RespondToDetection ( const Detection & detection,
const Configuration & configuration )

Carries out a detection's action and describes what was done, for the log. Kill and quarantine only happen with EnforceDetectionActions; they are refused for system processes, the service and its children, critical processes, and a process ID reused since the match.

Definition at line 193 of file detector.cpp.

194{
195 if (detection.action() != DETECTION_ACTION_KILL && detection.action() != DETECTION_ACTION_QUARANTINE) {
196 return "alerted";
197 }
198 if (!configuration.m_enforceDetectionActions) {
199 return "alerted (EnforceDetectionActions is off)";
200 }
201
202 HANDLE process = NULL;
203 bool terminated = false;
204 std::wstring imagePath;
205 std::string outcome = TerminateDetectedProcess(detection, process, terminated, imagePath);
206 if (detection.action() == DETECTION_ACTION_QUARANTINE && terminated) {
207 // The image is resolved by the service rather than taken from the detection, whose path
208 // comes from the event and may be empty
209 std::string image = ToString(imagePath);
210 // A running image cannot be moved, so wait for the kill to complete
211 WaitForSingleObject(process, 5000);
212 if (image.empty()) {
213 outcome += "; not quarantined: image path unknown";
214 }
215 else if (Configuration::IsPathInList(configuration.m_exclusions, image)) {
216 outcome += "; not quarantined: " + image + " is in Exclusions";
217 }
218 else {
219 outcome += MoveFileToQuarantine(image) ? "; quarantined " + image : "; quarantine of " + image + " failed";
220 }
221 }
222 if (process != NULL) {
223 CloseHandle(process);
224 }
225 return outcome;
226}
static bool IsPathInList(const std::vector< std::string > &patterns, const std::string &path)
Check whether a path matches any pattern in an exclusion list. A pattern with no wildcard matches eve...
std::vector< std::string > m_exclusions
Path patterns that are not scanned (see IsPathInList).
bool m_enforceDetectionActions
Whether the service carries out the kill and quarantine actions of detector rules....
bool MoveFileToQuarantine(std::string filePath)
Moves a file into C:\ProgramData\Panoptes\Quarantine.
Definition grpc.cpp:71

References Configuration::IsPathInList(), Configuration::m_enforceDetectionActions, Configuration::m_exclusions, MoveFileToQuarantine(), and ToString().