Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
shutdown.cpp File Reference
#include "shutdown.h"
#include "error_message.h"
#include "mutex.hpp"
#include "utils.h"
#include <sddl.h>
#include <iostream>
#include <thread>

Go to the source code of this file.

Macros

#define PANO_STOP_EVENT_NAME   L"PanoptesStop"
#define PANO_STOP_EXIT_WAIT_MS   15000
#define PANO_SHUTDOWN_WINDOW_CLASS   L"PanoptesShutdownWindow"

Functions

ERRORCODE InitializeShutdown ()
 Creates the stop event and installs the console handler and session-end window. The stop event can only be opened by SYSTEM and Administrators when the service runs elevated. Fails if another process already holds an object with the event's name.
void WaitForShutdownRequest ()
 Blocks until a shutdown is requested.
void NotifyShutdownComplete ()
 Tells console handlers and the session-end window that teardown has finished, so they can return and let Windows end the process.
ERRORCODE RequestServiceStop ()
 Implements –stop: sets a running service's stop event and waits for it to exit.

Macro Definition Documentation

◆ PANO_SHUTDOWN_WINDOW_CLASS

#define PANO_SHUTDOWN_WINDOW_CLASS   L"PanoptesShutdownWindow"

Definition at line 18 of file shutdown.cpp.

◆ PANO_STOP_EVENT_NAME

#define PANO_STOP_EVENT_NAME   L"PanoptesStop"

Definition at line 13 of file shutdown.cpp.

Referenced by InitializeShutdown(), and RequestServiceStop().

◆ PANO_STOP_EXIT_WAIT_MS

#define PANO_STOP_EXIT_WAIT_MS   15000

Definition at line 16 of file shutdown.cpp.

Referenced by RequestServiceStop().

Function Documentation

◆ InitializeShutdown()

ERRORCODE InitializeShutdown ( )

Creates the stop event and installs the console handler and session-end window. The stop event can only be opened by SYSTEM and Administrators when the service runs elevated. Fails if another process already holds an object with the event's name.

Returns
PANO_SUCCESS, or WINDOWS_ERR if the event or window could not be created

Definition at line 113 of file shutdown.cpp.

114{
115 // Elevated, the event is restricted to SYSTEM and Administrators so an unelevated process
116 // cannot stop the service. Unelevated Debug runs use the default security so --stop still
117 // works from the same user
118 SECURITY_ATTRIBUTES security{ sizeof(security), NULL, FALSE };
119 PSECURITY_DESCRIPTOR descriptor = NULL;
120 if (IsRunningAsAdmin())
121 {
122 if (!ConvertStringSecurityDescriptorToSecurityDescriptorW(L"D:P(A;;GA;;;SY)(A;;GA;;;BA)",
123 SDDL_REVISION_1, &descriptor, NULL))
124 {
125 return WINDOWS_ERR;
126 }
127 security.lpSecurityDescriptor = descriptor;
128 }
129
130 g_StopEvent = CreateEventW(&security, TRUE, FALSE, PANO_STOP_EVENT_NAME);
131 DWORD error = GetLastError();
132 if (descriptor != NULL)
133 {
134 LocalFree(descriptor);
135 }
136 if (g_StopEvent == NULL)
137 {
138 std::cerr << "Failed to create the stop event. Error code: " << error << std::endl;
139 return WINDOWS_ERR;
140 }
141 if (error == ERROR_ALREADY_EXISTS)
142 {
143 // Named objects disappear with their last handle, so another process is holding one
144 // with this name, possibly to stop the service through it. Do not use it
145 std::cerr << "The stop event already exists in another process" << std::endl;
146 CloseHandle(g_StopEvent);
147 g_StopEvent = NULL;
148 return WINDOWS_ERR;
149 }
150
151 g_ShutdownComplete = CreateEventW(NULL, TRUE, FALSE, NULL);
152 if (g_ShutdownComplete == NULL)
153 {
154 return WINDOWS_ERR;
155 }
156
157 SetConsoleCtrlHandler(ConsoleHandler, TRUE);
158
159 HANDLE ready = CreateEventW(NULL, TRUE, FALSE, NULL);
160 if (ready == NULL)
161 {
162 return WINDOWS_ERR;
163 }
164 HWND window = NULL;
165 std::thread(ShutdownWindowThread, ready, &window).detach();
166 WaitForSingleObject(ready, INFINITE);
167 CloseHandle(ready);
168
169 if (window == NULL)
170 {
171 std::cerr << "Failed to create the session end window" << std::endl;
172 return WINDOWS_ERR;
173 }
174
175 return PANO_SUCCESS;
176}
#define PANO_SUCCESS
#define WINDOWS_ERR
#define PANO_STOP_EVENT_NAME
Definition shutdown.cpp:13
bool IsRunningAsAdmin()
Definition utils.cpp:10

References ERRORCODE, IsRunningAsAdmin(), PANO_STOP_EVENT_NAME, PANO_SUCCESS, and WINDOWS_ERR.

Referenced by WinMain().

◆ NotifyShutdownComplete()

void NotifyShutdownComplete ( )

Tells console handlers and the session-end window that teardown has finished, so they can return and let Windows end the process.

Definition at line 183 of file shutdown.cpp.

184{
185 if (g_ShutdownComplete != NULL)
186 {
187 SetEvent(g_ShutdownComplete);
188 }
189}

Referenced by CleanupWithError(), and WinMain().

◆ RequestServiceStop()

ERRORCODE RequestServiceStop ( )

Implements –stop: sets a running service's stop event and waits for it to exit.

Returns
PANO_SUCCESS once the service has exited, NOT_ADMIN if the event could not be opened for lack of rights, or WINDOWS_ERR if no service is running or it did not exit in time

Definition at line 191 of file shutdown.cpp.

192{
193 HANDLE stopEvent = OpenEventW(EVENT_MODIFY_STATE, FALSE, PANO_STOP_EVENT_NAME);
194 if (stopEvent == NULL)
195 {
196 DWORD error = GetLastError();
197 if (error == ERROR_ACCESS_DENIED)
198 {
199 return NOT_ADMIN;
200 }
201 std::cerr << "Panoptes is not running" << std::endl;
202 return WINDOWS_ERR;
203 }
204
205 SetEvent(stopEvent);
206 CloseHandle(stopEvent);
207
208 // The service holds the mutex until the end of its teardown
209 ULONGLONG deadline = GetTickCount64() + PANO_STOP_EXIT_WAIT_MS;
210 while (GetTickCount64() < deadline)
211 {
212 HANDLE mutex = OpenMutexA(SYNCHRONIZE, FALSE, SERVICE_MUTEX);
213 if (mutex == NULL)
214 {
215 return PANO_SUCCESS;
216 }
217 CloseHandle(mutex);
218 Sleep(100);
219 }
220
221 std::cerr << "Panoptes did not stop within " << PANO_STOP_EXIT_WAIT_MS / 1000 << " seconds" << std::endl;
222 return WINDOWS_ERR;
223}
#define NOT_ADMIN
#define SERVICE_MUTEX
#define PANO_STOP_EXIT_WAIT_MS
Definition shutdown.cpp:16

References ERRORCODE, NOT_ADMIN, PANO_STOP_EVENT_NAME, PANO_STOP_EXIT_WAIT_MS, PANO_SUCCESS, SERVICE_MUTEX, and WINDOWS_ERR.

Referenced by WinMain().

◆ WaitForShutdownRequest()

void WaitForShutdownRequest ( )

Blocks until a shutdown is requested.

Definition at line 178 of file shutdown.cpp.

179{
180 WaitForSingleObject(g_StopEvent, INFINITE);
181}

Referenced by WinMain().