MCPcopy Create free account
hub / github.com/dobin/RedEdr / ServiceControlPipeThread

Function ServiceControlPipeThread

RedEdrPplService/control.cpp:22–103  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

20
21
22DWORD WINAPI ServiceControlPipeThread(LPVOID param) {
23 char buffer[PPL_CONFIG_LEN];
24
25 while (keep_running) {
26 LOG_W(LOG_INFO, L"Control: Waiting for client (RedEdr.exe) to connect...");
27 if (!pipeServer.StartAndWaitForClient(false)) {
28 LOG_A(LOG_ERROR, "Error waiting for RedEdr.exe");
29 continue;
30 }
31 LOG_A(LOG_INFO, "Control: Client connected");
32 LOG_A(LOG_INFO, "Control: Connect us back to RedEdr");
33 if (!ConnectEmitterPipe()) { // Connect to the RedEdr pipe
34 LOG_A(LOG_ERROR, "Control: Failed to connect to RedEdr pipe");
35 //break; // Exit the loop if connection fails
36 }
37
38 while (keep_running) {
39 LOG_A(LOG_INFO, "Control: Wait for command");
40 memset(buffer, 0, sizeof(buffer));
41 if (!pipeServer.Receive(buffer, PPL_CONFIG_LEN)) {
42 LOG_A(LOG_ERROR, "Error waiting for RedEdr.exe command");
43 break;
44 }
45
46 LOG_A(LOG_INFO, "Control: Received command: %s", buffer);
47
48 try {
49 // Try to parse as JSON first
50 nlohmann::json j = nlohmann::json::parse(buffer);
51
52 if (j.contains("command")) {
53 std::string command = j["command"];
54
55 if (command == "start") {
56 if (j.contains("targets") && j["targets"].is_array()) {
57 LOG_A(LOG_INFO, "Control: Processing start command with %zu targets", j["targets"].size());
58 std::vector<std::string> targets = j["targets"];
59 g_ProcessResolver.SetTargetNames(targets);
60 g_ProcessResolver.RefreshTargetMatching();
61
62 BOOL doDefenderTrace = j.value("do_defendertrace", false) ? TRUE : FALSE;
63 SetDefenderTraceConfig(doDefenderTrace, targets);
64
65 nlohmann::json start_event;
66 start_event["event"] = "ppl_start";
67 start_event["type"] = "meta";
68 SendEmitterPipe((char *) start_event.dump().c_str());
69 } else {
70 LOG_A(LOG_ERROR, "Control: Start command missing 'targets' array");
71 }
72 }
73 else if (command == "stop") {
74 nlohmann::json stop_event;
75 stop_event["event"] = "ppl_stop";
76 stop_event["type"] = "meta";
77 SendEmitterPipe((char*) stop_event.dump().c_str());
78 } else if (command == "shutdown") {
79 LOG_A(LOG_INFO, "Control: Received JSON command: shutdown");

Callers

nothing calls this directly

Calls 15

ConnectEmitterPipeFunction · 0.85
parseFunction · 0.85
SetDefenderTraceConfigFunction · 0.85
SendEmitterPipeFunction · 0.85
ShutdownEtwtiReaderFunction · 0.85
StartAndWaitForClientMethod · 0.80
ReceiveMethod · 0.80
SetTargetNamesMethod · 0.80
RefreshTargetMatchingMethod · 0.80
dumpMethod · 0.80
ShutdownMethod · 0.80
LOG_WFunction · 0.70

Tested by

no test coverage detected