Seregon/Hermes

Hermes/Dyforge is a program written in c++ allows you to inject a dll that can analyze all processes in a program, can be used for mod and reverse engeneering

C/3.8 KB/No license
DyForge/src/communication/CommunicationManager.cpp
Hermes / DyForge / src / communication / CommunicationManager.cpp
1#include "../../include/DyForgeCommunication.h"
2#include <stdexcept>
3#include <sstream>
4#include <chrono>
5#include <thread>
6#include <json/json.h>
7 
8namespace DyForge {
9 
10// Constructor
11CommunicationManager::CommunicationManager(const CommunicationConfig& config)
12 : m_config(config)
13 , m_running(false)
14 , m_analysisRunning(false) {
15}
16 
17// Destructor
18CommunicationManager::~CommunicationManager() {
19 if (m_running) {
20 m_running = false;
21 m_commandCondition.notify_one();
22 if (m_communicationThread.joinable()) {
23 m_communicationThread.join();
24 }
25 }
26}
27 
28// Communication methods
29bool CommunicationManager::SendCommand(const Command& command) {
30 if (!m_running) {
31 return false;
32 }
33
34 std::lock_guard<std::mutex> lock(m_commandMutex);
35 m_commandQueue.push(command);
36 m_commandCondition.notify_one();
37 return true;
38}
39 
40bool CommunicationManager::ReceiveResponse(Response& response) {
41 if (!m_running) {
42 return false;
43 }
44
45 std::lock_guard<std::mutex> lock(m_responseMutex);
46 if (m_responseQueue.empty()) {
47 return false;
48 }
49
50 response = m_responseQueue.front();
51 m_responseQueue.pop();
52 return true;
53}
54 
55bool CommunicationManager::WaitForResponse(Response& response, std::chrono::milliseconds timeout) {
56 if (!m_running) {
57 return false;
58 }
59
60 std::unique_lock<std::mutex> lock(m_responseMutex);
61 if (m_responseCondition.wait_for(lock, timeout, [this] { return !m_responseQueue.empty(); })) {
62 response = m_responseQueue.front();
63 m_responseQueue.pop();
64 return true;
65 }
66
67 return false;
68}
69 
70// Analysis methods
71bool CommunicationManager::StartAnalysis(const AnalysisConfig& config) {
72 if (m_analysisRunning) {
73 return false;
74 }
75
76 m_analysisConfig = config;
77
78 Command command;
79 command.type = CommandType::START_ANALYSIS;
80 command.data = "START_ANALYSIS";
81 command.timestamp = std::chrono::system_clock::now();
82
83 if (!SendCommand(command)) {
84 return false;
85 }
86
87 Response response;
88 if (!WaitForResponse(response, m_config.commandTimeout)) {
89 HandleTimeout();
90 return false;
91 }
92
93 if (response.type != ResponseType::SUCCESS) {
94 HandleError(response.type, response.data);
95 return false;
96 }
97
98 m_analysisRunning = true;
99 return true;
100}
101 
102bool CommunicationManager::StopAnalysis() {
103 if (!m_analysisRunning) {
104 return false;
105 }
106
107 Command command;
108 command.type = CommandType::STOP_ANALYSIS;
109 command.data = "STOP_ANALYSIS";
110 command.timestamp = std::chrono::system_clock::now();
111
112 if (!SendCommand(command)) {
113 return false;
114 }
115
116 Response response;
117 if (!WaitForResponse(response, m_config.commandTimeout)) {
118 HandleTimeout();
119 return false;
120 }
121
122 if (response.type != ResponseType::SUCCESS) {
123 HandleError(response.type, response.data);
124 return false;
125 }
126
127 m_analysisRunning = false;
128 return true;
129}
130 
131bool CommunicationManager::GetAnalysisData(std::string& data) {
132 if (!m_analysisRunning) {
133 return false;
134 }
135
136 Command command;
137 command.type = CommandType::GET_ANALYSIS_DATA;
138 command.data = "GET_ANALYSIS_DATA";
139 command.timestamp = std::chrono::system_clock::now();
140
141 if (!SendCommand(command)) {
142 return false;
143 }
144
145 Response response;
146 if (!WaitForResponse(response, m_config.analysisTimeout)) {
147 HandleTimeout();
148 return false;
149 }
150
151 if (response.type != ResponseType::SUCCESS) {
152 HandleError(response.type, response.data);
153 return false;
154 }
155
156 data = response.data;
157 return true;
158}
159 
160// Process methods
161bool CommunicationManager::GetProcessInfo(std::string& info) {
162 Command command;
163 command.type = CommandType::GET_PROCESS_INFO;
164 command.data = "GET_PROCESS_INFO";
165 command.timestamp = std::chrono::system_clock::now();
166
167 if (!SendCommand(command)) {
168 return false;
169 }
170
171 Response response;
172 if (!WaitForResponse(response, m_config.commandTimeout)) {
173 HandleTimeout();
174 return false;
175 }
176
177 if (response.type != ResponseType::SUCCESS) {
178 HandleError(response.type, response.data);
179 return false;
180 }
181
182 info = response.data;
183 return true;
184}
185 
186bool CommunicationManager::GetMemoryInfo(std::string& info) {
187 Command command;
188 command.type = CommandType::GET_MEMORY_INFO;
189 command.data = "GET_MEMORY_INFO";
190 command.timestamp = std::chrono::system_clock::now();
191
192 if (!SendCommand(command)) {
193 return false;
194 }
195
196 Response response;
197 if (!WaitForResponse(response, m_config.commandTimeout)) {
198 HandleTimeout();
199 return false;
200 }
201
202 if (response.type != ResponseType::SUCCESS) {
203 HandleError(response.type, response.data);
204 return false;
205 }
206
207 info = response.data;
208 return true;
209}
210 
211bool CommunicationManager::GetThreadInfo(std::string& info) {
212 Command command;
213 command.type = CommandType::GET_THREAD_INFO;
214 command.data = "GET_THREAD_INFO";
215 command.timestamp = std::chrono::system_clock::now();
216
217 if (!SendCommand(command)) {
218 return false;
219 }
220
221 Response response;
222 if (!WaitForResponse(response, m_config.commandTimeout)) {
223 HandleTimeout();
224 return false;
225 }
226
227 if (response.type != ResponseType::SUCCESS) {
228 HandleError(response.type, response.data);
229 return false;
230 }
231
232 info = response.data;
233 return true;
234}
235 
236bool CommunicationManager::GetModuleInfo(std::string& info) {
237 Command command;
238 command.type = CommandType::GET_MODULE_INFO;
239 command.data = "GET_MODULE_INFO";
240 command.timestamp = std::chrono::system_clock::now();
241
242 if (!SendCommand(command)) {
243 return false;
244 }
245
246 Response response;
247 if (!WaitForResponse(response, m_config.commandTimeout)) {
248 HandleTimeout();
249 return false;
250 }
251
252 if (response.type != ResponseType::SUCCESS) {
253 HandleError(response.type, response.data);
254 return false;
255 }
256
257 info = response.data;
258 return true;
259}
260 
261bool CommunicationManager::GetHookInfo(std::string& info) {
262 Command command;
263 command.type = CommandType::GET_HOOK_INFO;
264 command.data = "GET_HOOK_INFO";
265 command.timestamp = std::chrono::system_clock::now();
266
267 if (!SendCommand(command)) {
268 return false;
269 }
270
271 Response response;
272 if (!WaitForResponse(response, m_config.commandTimeout)) {
273 HandleTimeout();
274 return false;
275 }
276
277 if (response.type != ResponseType::SUCCESS) {
278 HandleError(response.type, response.data);
279 return false;
280 }
281
282 info = response.data;
283 return true;
284}
285 
286bool CommunicationManager::GetModInfo(std::string& info) {
287 Command command;
288 command.type = CommandType::GET_MOD_INFO;
289 command.data = "GET_MOD_INFO";
290 command.timestamp = std::chrono::system_clock::now();
291
292 if (!SendCommand(command)) {
293 return false;
294 }
295
296 Response response;
297 if (!WaitForResponse(response, m_config.commandTimeout)) {
298 HandleTimeout();
299 return false;
300 }
301
302 if (response.type != ResponseType::SUCCESS) {
303 HandleError(response.type, response.data);
304 return false;
305 }
306
307 info = response.data;
308 return true;
309}
310 
311bool CommunicationManager::GetWebServerInfo(std::string& info) {
312 Command command;
313 command.type = CommandType::GET_WEBSERVER_INFO;
314 command.data = "GET_WEBSERVER_INFO";
315 command.timestamp = std::chrono::system_clock::now();
316
317 if (!SendCommand(command)) {
318 return false;
319 }
320
321 Response response;
322 if (!WaitForResponse(response, m_config.commandTimeout)) {
323 HandleTimeout();
324 return false;
325 }
326
327 if (response.type != ResponseType::SUCCESS) {
328 HandleError(response.type, response.data);
329 return false;
330 }
331
332 info = response.data;
333 return true;
334}
335 
336// Custom commands
337bool CommunicationManager::SendCustomCommand(const std::string& command, std::string& response) {
338 Command cmd;
339 cmd.type = CommandType::CUSTOM_COMMAND;
340 cmd.data = command;
341 cmd.timestamp = std::chrono::system_clock::now();
342
343 if (!SendCommand(cmd)) {
344 return false;
345 }
346
347 Response resp;
348 if (!WaitForResponse(resp, m_config.commandTimeout)) {
349 HandleTimeout();
350 return false;
351 }
352
353 if (resp.type != ResponseType::SUCCESS) {
354 HandleError(resp.type, resp.data);
355 return false;
356 }
357
358 response = resp.data;
359 return true;
360}
361 
362// Private methods
363void CommunicationManager::CommunicationThread() {
364 while (m_running) {
365 Command command;
366 {
367 std::unique_lock<std::mutex> lock(m_commandMutex);
368 m_commandCondition.wait(lock, [this] { return !m_commandQueue.empty() || !m_running; });
369
370 if (!m_running) {
371 break;
372 }
373
374 command = m_commandQueue.front();
375 m_commandQueue.pop();
376 }
377
378 if (!ProcessCommand(command)) {
379 HandleError(ResponseType::ERROR, "Failed to process command");
380 }
381 }
382}
383 
384bool CommunicationManager::ProcessCommand(const Command& command) {
385 std::string response;
386 bool success = false;
387 
388 switch (command.type) {
389 case CommandType::INJECT_DLL:
390 success = HandleInjectDLL(command.data, response);
391 break;
392 case CommandType::EJECT_DLL:
393 success = HandleEjectDLL(command.data, response);
394 break;
395 case CommandType::START_ANALYSIS:
396 success = HandleStartAnalysis(command.data, response);
397 break;
398 case CommandType::STOP_ANALYSIS:
399 success = HandleStopAnalysis(command.data, response);
400 break;
401 case CommandType::START_WEBSERVER:
402 success = HandleStartWebServer(command.data, response);
403 break;
404 case CommandType::STOP_WEBSERVER:
405 success = HandleStopWebServer(command.data, response);
406 break;
407 case CommandType::LOAD_MOD:
408 success = HandleLoadMod(command.data, response);
409 break;
410 case CommandType::UNLOAD_MOD:
411 success = HandleUnloadMod(command.data, response);
412 break;
413 case CommandType::GET_ANALYSIS_RESULTS:
414 success = HandleGetAnalysisResults(command.data, response);
415 break;
416 case CommandType::GET_MOD_INFO:
417 success = HandleGetModInfo(command.data, response);
418 break;
419 case CommandType::GET_HOOK_INFO:
420 success = HandleGetHookInfo(command.data, response);
421 break;
422 case CommandType::GET_WEBSERVER_INFO:
423 success = HandleGetWebServerInfo(command.data, response);
424 break;
425 case CommandType::CUSTOM_COMMAND:
426 success = HandleCustomCommand(command.data, response);
427 break;
428 default:
429 response = "Unknown command type";
430 success = false;
431 break;
432 }
433 
434 Response resp;
435 resp.type = success ? ResponseType::SUCCESS : ResponseType::ERROR;
436 resp.data = response;
437 resp.timestamp = std::chrono::system_clock::now();
438 
439 return ProcessResponse(resp);
440}
441 
442bool CommunicationManager::ProcessResponse(const Response& response) {
443 std::lock_guard<std::mutex> lock(m_responseMutex);
444 m_responseQueue.push(response);
445 m_responseCondition.notify_one();
446 return true;
447}
448 
449void CommunicationManager::HandleTimeout() {
450 HandleError(ResponseType::TIMEOUT, "Command timed out");
451}
452 
453void CommunicationManager::HandleError(ResponseType errorType, const std::string& errorMessage) {
454 std::lock_guard<std::mutex> lock(m_errorMutex);
455 m_lastErrorType = errorType;
456 m_lastError = errorMessage;
457
458 Response response;
459 response.type = errorType;
460 response.data = errorMessage;
461 response.timestamp = std::chrono::system_clock::now();
462
463 ProcessResponse(response);
464}
465 
466bool CommunicationManager::HandleInjectDLL(const std::string& data, std::string& response) {
467 try {
468 Json::Value root;
469 Json::Reader reader;
470 if (!reader.parse(data, root)) {
471 response = "Invalid JSON data";
472 return false;
473 }
474 
475 DWORD processId = root["processId"].asUInt();
476 if (processId == 0) {
477 response = "Invalid process ID";
478 return false;
479 }
480 
481 // Implementa la logica di iniezione della DLL
482 // Per ora restituiamo un successo fittizio
483 response = "DLL injected successfully";
484 return true;
485 }
486 catch (const std::exception& e) {
487 response = std::string("Error: ") + e.what();
488 return false;
489 }
490}
491 
492bool CommunicationManager::HandleEjectDLL(const std::string& data, std::string& response) {
493 try {
494 Json::Value root;
495 Json::Reader reader;
496 if (!reader.parse(data, root)) {
497 response = "Invalid JSON data";
498 return false;
499 }
500 
501 DWORD processId = root["processId"].asUInt();
502 if (processId == 0) {
503 response = "Invalid process ID";
504 return false;
505 }
506 
507 // Implementa la logica di eiezione della DLL
508 // Per ora restituiamo un successo fittizio
509 response = "DLL ejected successfully";
510 return true;
511 }
512 catch (const std::exception& e) {
513 response = std::string("Error: ") + e.what();
514 return false;
515 }
516}
517 
518bool CommunicationManager::HandleStartAnalysis(const std::string& data, std::string& response) {
519 try {
520 // Implementa la logica di avvio dell'analisi
521 // Per ora restituiamo un successo fittizio
522 response = "Analysis started successfully";
523 return true;
524 }
525 catch (const std::exception& e) {
526 response = std::string("Error: ") + e.what();
527 return false;
528 }
529}
530 
531bool CommunicationManager::HandleStopAnalysis(const std::string& data, std::string& response) {
532 try {
533 // Implementa la logica di arresto dell'analisi
534 // Per ora restituiamo un successo fittizio
535 response = "Analysis stopped successfully";
536 return true;
537 }
538 catch (const std::exception& e) {
539 response = std::string("Error: ") + e.what();
540 return false;
541 }
542}
543 
544bool CommunicationManager::HandleStartWebServer(const std::string& data, std::string& response) {
545 try {
546 Json::Value root;
547 Json::Reader reader;
548 if (!reader.parse(data, root)) {
549 response = "Invalid JSON data";
550 return false;
551 }
552 
553 int port = root["port"].asInt();
554 if (port <= 0 || port > 65535) {
555 response = "Invalid port number";
556 return false;
557 }
558 
559 // Implementa la logica di avvio del web server
560 // Per ora restituiamo un successo fittizio
561 response = "Web server started successfully";
562 return true;
563 }
564 catch (const std::exception& e) {
565 response = std::string("Error: ") + e.what();
566 return false;
567 }
568}
569 
570bool CommunicationManager::HandleStopWebServer(const std::string& data, std::string& response) {
571 try {
572 // Implementa la logica di arresto del web server
573 // Per ora restituiamo un successo fittizio
574 response = "Web server stopped successfully";
575 return true;
576 }
577 catch (const std::exception& e) {
578 response = std::string("Error: ") + e.what();
579 return false;
580 }
581}
582 
583bool CommunicationManager::HandleLoadMod(const std::string& data, std::string& response) {
584 try {
585 Json::Value root;
586 Json::Reader reader;
587 if (!reader.parse(data, root)) {
588 response = "Invalid JSON data";
589 return false;
590 }
591 
592 std::string modPath = root["path"].asString();
593 if (modPath.empty()) {
594 response = "Invalid mod path";
595 return false;
596 }
597 
598 // Implementa la logica di caricamento della mod
599 // Per ora restituiamo un successo fittizio
600 response = "Mod loaded successfully";
601 return true;
602 }
603 catch (const std::exception& e) {
604 response = std::string("Error: ") + e.what();
605 return false;
606 }
607}
608 
609bool CommunicationManager::HandleUnloadMod(const std::string& data, std::string& response) {
610 try {
611 Json::Value root;
612 Json::Reader reader;
613 if (!reader.parse(data, root)) {
614 response = "Invalid JSON data";
615 return false;
616 }
617 
618 std::string modName = root["name"].asString();
619 if (modName.empty()) {
620 response = "Invalid mod name";
621 return false;
622 }
623 
624 // Implementa la logica di scaricamento della mod
625 // Per ora restituiamo un successo fittizio
626 response = "Mod unloaded successfully";
627 return true;
628 }
629 catch (const std::exception& e) {
630 response = std::string("Error: ") + e.what();
631 return false;
632 }
633}
634 
635bool CommunicationManager::HandleGetAnalysisResults(const std::string& data, std::string& response) {
636 try {
637 // Implementa la logica di recupero dei risultati dell'analisi
638 // Per ora restituiamo un successo fittizio
639 response = "Analysis results retrieved successfully";
640 return true;
641 }
642 catch (const std::exception& e) {
643 response = std::string("Error: ") + e.what();
644 return false;
645 }
646}
647 
648bool CommunicationManager::HandleGetModInfo(const std::string& data, std::string& response) {
649 try {
650 // Implementa la logica di recupero delle informazioni sulle mod
651 // Per ora restituiamo un successo fittizio
652 response = "Mod info retrieved successfully";
653 return true;
654 }
655 catch (const std::exception& e) {
656 response = std::string("Error: ") + e.what();
657 return false;
658 }
659}
660 
661bool CommunicationManager::HandleGetHookInfo(const std::string& data, std::string& response) {
662 try {
663 // Implementa la logica di recupero delle informazioni sugli hook
664 // Per ora restituiamo un successo fittizio
665 response = "Hook info retrieved successfully";
666 return true;
667 }
668 catch (const std::exception& e) {
669 response = std::string("Error: ") + e.what();
670 return false;
671 }
672}
673 
674bool CommunicationManager::HandleGetWebServerInfo(const std::string& data, std::string& response) {
675 try {
676 // Implementa la logica di recupero delle informazioni sul web server
677 // Per ora restituiamo un successo fittizio
678 response = "Web server info retrieved successfully";
679 return true;
680 }
681 catch (const std::exception& e) {
682 response = std::string("Error: ") + e.what();
683 return false;
684 }
685}
686 
687bool CommunicationManager::HandleCustomCommand(const std::string& data, std::string& response) {
688 try {
689 // Implementa la logica di gestione dei comandi personalizzati
690 // Per ora restituiamo un successo fittizio
691 response = "Custom command executed successfully";
692 return true;
693 }
694 catch (const std::exception& e) {
695 response = std::string("Error: ") + e.what();
696 return false;
697 }
698}
699 
700} // namespace DyForge