8#include "panoptes.pb.h"
17#pragma region Peer Check
20static bool PeerPort(
const std::string& peer, USHORT& port)
22 size_t colon = peer.find_last_of(
':');
23 if (colon == std::string::npos || colon + 1 == peer.size()) {
26 unsigned long value = 0;
27 for (
size_t i = colon + 1; i < peer.size(); i++) {
28 if (peer[i] <
'0' || peer[i] >
'9') {
31 value = value * 10 + (peer[i] -
'0');
41static USHORT TablePort(DWORD port)
43 return _byteswap_ushort((USHORT)port);
47static bool FindConnectionOwner(ULONG family, USHORT clientPort, USHORT serverPort, DWORD& processId)
49 std::vector<BYTE> buffer;
54 result = GetExtendedTcpTable(buffer.empty() ? NULL : buffer.data(), &size, FALSE, family,
55 TCP_TABLE_OWNER_PID_CONNECTIONS, 0);
56 }
while (result == ERROR_INSUFFICIENT_BUFFER);
57 if (result != NO_ERROR) {
61 if (family == AF_INET) {
62 auto table = (
const MIB_TCPTABLE_OWNER_PID*)buffer.data();
63 for (DWORD i = 0; i < table->dwNumEntries; i++) {
64 const auto& row = table->table[i];
65 if (TablePort(row.dwLocalPort) == clientPort && TablePort(row.dwRemotePort) == serverPort) {
66 processId = row.dwOwningPid;
72 auto table = (
const MIB_TCP6TABLE_OWNER_PID*)buffer.data();
73 for (DWORD i = 0; i < table->dwNumEntries; i++) {
74 const auto& row = table->table[i];
75 if (TablePort(row.dwLocalPort) == clientPort && TablePort(row.dwRemotePort) == serverPort) {
76 processId = row.dwOwningPid;
84static std::wstring ProcessImagePath(HANDLE process)
87 DWORD length = ARRAYSIZE(path);
88 return QueryFullProcessImageNameW(process, 0, path, &length) ? std::wstring(path, length) : L
"";
91bool IsDetectorPeer(
const std::string& peer,
int serverPort, std::string& why)
93 USHORT clientPort = 0;
94 if (!PeerPort(peer, clientPort)) {
95 why =
"unrecognized peer " + peer;
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;
106 HANDLE process = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, processId);
107 if (process == NULL) {
108 why =
"cannot open process " + std::to_string(processId);
111 std::wstring image = ProcessImagePath(process);
112 CloseHandle(process);
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);
124#pragma region Response
127#define PANO_PROCESS_BREAK_ON_TERMINATION 29
132static bool IsCriticalProcess(HANDLE process)
139 return critical != 0;
143static std::string TerminateDetectedProcess(
const Detection& detection, HANDLE& process,
bool& terminated,
147 DWORD processId = detection.process_id();
148 if (processId == 0 || processId == 4) {
149 return "refused: system process";
151 if (processId == GetCurrentProcessId()) {
152 return "refused: the service itself";
155 process = OpenProcess(PROCESS_QUERY_INFORMATION | SYNCHRONIZE, FALSE, processId);
156 if (process == NULL) {
157 DWORD error = GetLastError();
158 return (error == ERROR_INVALID_PARAMETER) ?
"not needed: the process has exited"
159 :
"refused: cannot open the process (error " + std::to_string(error) +
")";
161 if (WaitForSingleObject(process, 0) == WAIT_OBJECT_0) {
162 return "not needed: the process has exited";
165 return "refused: a process the service started";
167 if (IsCriticalProcess(process)) {
168 return "refused: critical system process";
173 FILETIME created, exited, kernel, user;
175 matched.dwLowDateTime = (DWORD)(detection.timestamp() & 0xFFFFFFFF);
176 matched.dwHighDateTime = (DWORD)((UINT64)detection.timestamp() >> 32);
177 if (detection.timestamp() == 0) {
178 return "refused: the detection has no event time";
180 if (!GetProcessTimes(process, &created, &exited, &kernel, &user) || CompareFileTime(&created, &matched) > 0) {
181 return "refused: the process ID was reused after the match";
184 image = ProcessImagePath(process);
187 return "kill failed (error " + std::to_string(result) +
")";
195 if (detection.action() != DETECTION_ACTION_KILL && detection.action() != DETECTION_ACTION_QUARANTINE) {
199 return "alerted (EnforceDetectionActions is off)";
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) {
209 std::string image =
ToString(imagePath);
211 WaitForSingleObject(process, 5000);
213 outcome +=
"; not quarantined: image path unknown";
216 outcome +=
"; not quarantined: " + image +
" is in Exclusions";
219 outcome +=
MoveFileToQuarantine(image) ?
"; quarantined " + image :
"; quarantine of " + image +
" failed";
222 if (process != NULL) {
223 CloseHandle(process);
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 IsServiceChildProcess(HANDLE process)
Whether a process is one the service started (a container or the detector).
std::wstring DetectorExecutablePath()
The path the service starts the detector from: PanoptesDetector.exe in the service's folder....
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 localhos...
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 h...
#define PANO_PROCESS_BREAK_ON_TERMINATION
NTSTATUS(NTAPI * NtQueryInformationProcess_t)(HANDLE, ULONG, PVOID, ULONG, PULONG)
ERRORCODE TerminateProcessByDriver(UINT64 processId)
Asks the driver to terminate a process (IOCTL_PANOPTES_TERMINATE_PROCESS).
bool MoveFileToQuarantine(std::string filePath)
Moves a file into C:\ProgramData\Panoptes\Quarantine.
std::string ToString(const std::wstring &wstr)