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/TerminalUI.cpp
Hermes / DyForge / src / TerminalUI.cpp
1#include "TerminalUI.h"
2#include "../communication/CommunicationManager.h"
3#include <iostream>
4#include <iomanip>
5#include <sstream>
6#include <filesystem>
7#include <windows.h>
8#include <tlhelp32.h>
9#include <shellapi.h>
10 
11namespace DyForge {
12 
13TerminalUI::TerminalUI()
14 : isInitialized(false)
15 , selectedProcessId(0)
16 , isDLLInjected(false)
17 , isAnalysisRunning(false)
18 , isWebServerRunning(false)
19{
20}
21 
22TerminalUI::~TerminalUI() {
23 Cleanup();
24}
25 
26bool TerminalUI::Initialize() {
27 if (isInitialized) return true;
28 
29 // Inizializza la console
30 HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
31 if (hConsole == INVALID_HANDLE_VALUE) {
32 return false;
33 }
34 
35 // Imposta il titolo della console
36 SetConsoleTitle(L"DyForge - Terminal UI");
37 
38 // Imposta il buffer della console
39 CONSOLE_SCREEN_BUFFER_INFO csbi;
40 GetConsoleScreenBufferInfo(hConsole, &csbi);
41 csbi.dwSize.Y = 9999;
42 SetConsoleScreenBufferSize(hConsole, csbi.dwSize);
43 
44 isInitialized = true;
45 return true;
46}
47 
48void TerminalUI::Cleanup() {
49 if (!isInitialized) return;
50 
51 // Eject DLL se necessario
52 if (isDLLInjected && selectedProcessId != 0) {
53 EjectDLL(selectedProcessId);
54 }
55 
56 // Stop web server se necessario
57 if (isWebServerRunning) {
58 StopWebServer();
59 }
60 
61 // Stop analysis se necessario
62 if (isAnalysisRunning) {
63 StopAnalysis();
64 }
65 
66 isInitialized = false;
67}
68 
69void TerminalUI::ShowMainMenu() {
70 while (true) {
71 ClearScreen();
72 PrintHeader();
73 PrintStatus();
74 
75 std::cout << "\nMenu Principale:\n";
76 std::cout << "1. Seleziona Processo\n";
77 std::cout << "2. Comandi\n";
78 std::cout << "3. Analisi\n";
79 std::cout << "4. Mod\n";
80 std::cout << "5. Web Server\n";
81 std::cout << "0. Esci\n";
82 std::cout << "\nScelta: ";
83 
84 std::string choice = GetInput("");
85
86 if (choice == "0") break;
87 else if (choice == "1") ShowProcessSelection();
88 else if (choice == "2") ShowCommandMenu();
89 else if (choice == "3") ShowAnalysisMenu();
90 else if (choice == "4") ShowModMenu();
91 else if (choice == "5") ShowWebServerMenu();
92 }
93}
94 
95void TerminalUI::ShowProcessSelection() {
96 while (true) {
97 ClearScreen();
98 PrintHeader();
99 PrintStatus();
100 
101 std::cout << "\nSelezione Processo:\n";
102 PrintProcessList();
103 
104 std::cout << "\n1. Seleziona Processo\n";
105 std::cout << "2. Inietta DLL\n";
106 std::cout << "3. Eject DLL\n";
107 std::cout << "0. Torna al Menu Principale\n";
108 std::cout << "\nScelta: ";
109 
110 std::string choice = GetInput("");
111
112 if (choice == "0") break;
113 else if (choice == "1") {
114 DWORD processId;
115 if (SelectProcess(processId)) {
116 selectedProcessId = processId;
117 SetColor(COLOR_SUCCESS);
118 std::cout << "Processo selezionato: " << processId << std::endl;
119 ResetColor();
120 }
121 }
122 else if (choice == "2") {
123 if (selectedProcessId == 0) {
124 SetColor(COLOR_ERROR);
125 std::cout << "Nessun processo selezionato!" << std::endl;
126 ResetColor();
127 }
128 else if (InjectDLL(selectedProcessId)) {
129 isDLLInjected = true;
130 SetColor(COLOR_SUCCESS);
131 std::cout << "DLL iniettata con successo!" << std::endl;
132 ResetColor();
133 }
134 }
135 else if (choice == "3") {
136 if (!isDLLInjected) {
137 SetColor(COLOR_ERROR);
138 std::cout << "DLL non iniettata!" << std::endl;
139 ResetColor();
140 }
141 else if (EjectDLL(selectedProcessId)) {
142 isDLLInjected = false;
143 SetColor(COLOR_SUCCESS);
144 std::cout << "DLL ejectata con successo!" << std::endl;
145 ResetColor();
146 }
147 }
148 
149 WaitForKey();
150 }
151}
152 
153void TerminalUI::ShowCommandMenu() {
154 while (true) {
155 ClearScreen();
156 PrintHeader();
157 PrintStatus();
158 
159 std::cout << "\nMenu Comandi:\n";
160 std::cout << "1. Avvia Analisi\n";
161 std::cout << "2. Stop Analisi\n";
162 std::cout << "3. Avvia Web Server\n";
163 std::cout << "4. Stop Web Server\n";
164 std::cout << "0. Torna al Menu Principale\n";
165 std::cout << "\nScelta: ";
166 
167 std::string choice = GetInput("");
168
169 if (choice == "0") break;
170 else if (choice == "1") {
171 if (!isDLLInjected) {
172 SetColor(COLOR_ERROR);
173 std::cout << "DLL non iniettata!" << std::endl;
174 ResetColor();
175 }
176 else if (StartAnalysis()) {
177 isAnalysisRunning = true;
178 SetColor(COLOR_SUCCESS);
179 std::cout << "Analisi avviata con successo!" << std::endl;
180 ResetColor();
181 }
182 }
183 else if (choice == "2") {
184 if (!isAnalysisRunning) {
185 SetColor(COLOR_ERROR);
186 std::cout << "Analisi non in esecuzione!" << std::endl;
187 ResetColor();
188 }
189 else if (StopAnalysis()) {
190 isAnalysisRunning = false;
191 SetColor(COLOR_SUCCESS);
192 std::cout << "Analisi fermata con successo!" << std::endl;
193 ResetColor();
194 }
195 }
196 else if (choice == "3") {
197 if (!isDLLInjected) {
198 SetColor(COLOR_ERROR);
199 std::cout << "DLL non iniettata!" << std::endl;
200 ResetColor();
201 }
202 else if (StartWebServer()) {
203 isWebServerRunning = true;
204 SetColor(COLOR_SUCCESS);
205 std::cout << "Web Server avviato con successo!" << std::endl;
206 ResetColor();
207 }
208 }
209 else if (choice == "4") {
210 if (!isWebServerRunning) {
211 SetColor(COLOR_ERROR);
212 std::cout << "Web Server non in esecuzione!" << std::endl;
213 ResetColor();
214 }
215 else if (StopWebServer()) {
216 isWebServerRunning = false;
217 SetColor(COLOR_SUCCESS);
218 std::cout << "Web Server fermato con successo!" << std::endl;
219 ResetColor();
220 }
221 }
222 
223 WaitForKey();
224 }
225}
226 
227void TerminalUI::ShowAnalysisMenu() {
228 while (true) {
229 ClearScreen();
230 PrintHeader();
231 PrintStatus();
232 
233 std::cout << "\nMenu Analisi:\n";
234 std::cout << "1. Avvia Analisi\n";
235 std::cout << "2. Stop Analisi\n";
236 std::cout << "3. Mostra Risultati\n";
237 std::cout << "0. Torna al Menu Principale\n";
238 std::cout << "\nScelta: ";
239 
240 std::string choice = GetInput("");
241
242 if (choice == "0") break;
243 else if (choice == "1") {
244 if (StartAnalysis()) {
245 SetColor(COLOR_SUCCESS);
246 std::cout << "Analisi avviata con successo!" << std::endl;
247 ResetColor();
248 }
249 }
250 else if (choice == "2") {
251 if (StopAnalysis()) {
252 SetColor(COLOR_SUCCESS);
253 std::cout << "Analisi fermata con successo!" << std::endl;
254 ResetColor();
255 }
256 }
257 else if (choice == "3") {
258 std::string results;
259 if (m_commManager.GetAnalysisResults(results)) {
260 std::cout << "\nRisultati Analisi:\n" << results << std::endl;
261 }
262 }
263 
264 WaitForKey();
265 }
266}
267 
268void TerminalUI::ShowModMenu() {
269 while (true) {
270 ClearScreen();
271 PrintHeader();
272 PrintStatus();
273 
274 std::cout << "\nMenu Mod:\n";
275 std::cout << "1. Carica Mod\n";
276 std::cout << "2. Scarica Mod\n";
277 std::cout << "3. Lista Mod\n";
278 std::cout << "0. Torna al Menu Principale\n";
279 std::cout << "\nScelta: ";
280 
281 std::string choice = GetInput("");
282
283 if (choice == "0") break;
284 else if (choice == "1") {
285 std::string modPath = GetInput("Inserisci il percorso della mod: ");
286 if (LoadMod(modPath)) {
287 SetColor(COLOR_SUCCESS);
288 std::cout << "Mod caricata con successo!" << std::endl;
289 ResetColor();
290 }
291 }
292 else if (choice == "2") {
293 std::string modName = GetInput("Inserisci il nome della mod: ");
294 if (UnloadMod(modName)) {
295 SetColor(COLOR_SUCCESS);
296 std::cout << "Mod scaricata con successo!" << std::endl;
297 ResetColor();
298 }
299 }
300 else if (choice == "3") {
301 std::string modList;
302 if (m_commManager.GetModInfo(modList)) {
303 std::cout << "\nMod Caricate:\n" << modList << std::endl;
304 }
305 }
306 
307 WaitForKey();
308 }
309}
310 
311void TerminalUI::ShowWebServerMenu() {
312 while (true) {
313 ClearScreen();
314 PrintHeader();
315 PrintStatus();
316 
317 std::cout << "\nMenu Web Server:\n";
318 std::cout << "1. Avvia Web Server\n";
319 std::cout << "2. Stop Web Server\n";
320 std::cout << "3. Apri Dashboard\n";
321 std::cout << "0. Torna al Menu Principale\n";
322 std::cout << "\nScelta: ";
323 
324 std::string choice = GetInput("");
325
326 if (choice == "0") break;
327 else if (choice == "1") {
328 if (!isDLLInjected) {
329 SetColor(COLOR_ERROR);
330 std::cout << "DLL non iniettata!" << std::endl;
331 ResetColor();
332 }
333 else if (StartWebServer()) {
334 isWebServerRunning = true;
335 SetColor(COLOR_SUCCESS);
336 std::cout << "Web Server avviato con successo!" << std::endl;
337 ResetColor();
338 }
339 }
340 else if (choice == "2") {
341 if (!isWebServerRunning) {
342 SetColor(COLOR_ERROR);
343 std::cout << "Web Server non in esecuzione!" << std::endl;
344 ResetColor();
345 }
346 else if (StopWebServer()) {
347 isWebServerRunning = false;
348 SetColor(COLOR_SUCCESS);
349 std::cout << "Web Server fermato con successo!" << std::endl;
350 ResetColor();
351 }
352 }
353 else if (choice == "3") {
354 if (!isWebServerRunning) {
355 SetColor(COLOR_ERROR);
356 std::cout << "Web Server non in esecuzione!" << std::endl;
357 ResetColor();
358 }
359 else {
360 ShellExecuteA(NULL, "open", "http://localhost:8080", NULL, NULL, SW_SHOWNORMAL);
361 }
362 }
363 
364 WaitForKey();
365 }
366}
367 
368bool TerminalUI::SelectProcess(DWORD& processId) {
369 HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
370 if (hSnapshot == INVALID_HANDLE_VALUE) {
371 return false;
372 }
373 
374 PROCESSENTRY32 pe32;
375 pe32.dwSize = sizeof(PROCESSENTRY32);
376 
377 if (!Process32First(hSnapshot, &pe32)) {
378 CloseHandle(hSnapshot);
379 return false;
380 }
381 
382 std::vector<std::pair<DWORD, std::wstring>> processes;
383 do {
384 processes.push_back({pe32.th32ProcessID, pe32.szExeFile});
385 } while (Process32Next(hSnapshot, &pe32));
386 
387 CloseHandle(hSnapshot);
388 
389 // Mostra la lista dei processi
390 std::cout << "\nProcessi disponibili:\n";
391 for (size_t i = 0; i < processes.size(); i++) {
392 std::wcout << std::setw(3) << i + 1 << ". " << processes[i].second << " (PID: " << processes[i].first << ")\n";
393 }
394 
395 // Chiedi all'utente di selezionare un processo
396 std::string input = GetInput("\nSeleziona un processo (numero): ");
397 try {
398 size_t index = std::stoul(input) - 1;
399 if (index < processes.size()) {
400 processId = processes[index].first;
401 return true;
402 }
403 }
404 catch (...) {
405 // Ignora errori di conversione
406 }
407 
408 return false;
409}
410 
411bool TerminalUI::InjectDLL(DWORD processId) {
412 if (!m_commManager.InjectDLL(processId)) {
413 SetColor(COLOR_ERROR);
414 std::cout << "Errore durante l'iniezione della DLL: " << m_commManager.GetLastError() << std::endl;
415 ResetColor();
416 return false;
417 }
418 isDLLInjected = true;
419 return true;
420}
421 
422bool TerminalUI::EjectDLL(DWORD processId) {
423 if (!m_commManager.EjectDLL(processId)) {
424 SetColor(COLOR_ERROR);
425 std::cout << "Errore durante l'eiezione della DLL: " << m_commManager.GetLastError() << std::endl;
426 ResetColor();
427 return false;
428 }
429 isDLLInjected = false;
430 return true;
431}
432 
433bool TerminalUI::StartAnalysis() {
434 if (!isDLLInjected) {
435 SetColor(COLOR_ERROR);
436 std::cout << "DLL non iniettata!" << std::endl;
437 ResetColor();
438 return false;
439 }
440 
441 if (!m_commManager.StartAnalysis()) {
442 SetColor(COLOR_ERROR);
443 std::cout << "Errore durante l'avvio dell'analisi: " << m_commManager.GetLastError() << std::endl;
444 ResetColor();
445 return false;
446 }
447 isAnalysisRunning = true;
448 return true;
449}
450 
451bool TerminalUI::StopAnalysis() {
452 if (!isAnalysisRunning) {
453 SetColor(COLOR_ERROR);
454 std::cout << "Analisi non in esecuzione!" << std::endl;
455 ResetColor();
456 return false;
457 }
458 
459 if (!m_commManager.StopAnalysis()) {
460 SetColor(COLOR_ERROR);
461 std::cout << "Errore durante l'arresto dell'analisi: " << m_commManager.GetLastError() << std::endl;
462 ResetColor();
463 return false;
464 }
465 isAnalysisRunning = false;
466 return true;
467}
468 
469bool TerminalUI::StartWebServer() {
470 if (!isDLLInjected) {
471 SetColor(COLOR_ERROR);
472 std::cout << "DLL non iniettata!" << std::endl;
473 ResetColor();
474 return false;
475 }
476 
477 if (!m_commManager.StartWebServer()) {
478 SetColor(COLOR_ERROR);
479 std::cout << "Errore durante l'avvio del web server: " << m_commManager.GetLastError() << std::endl;
480 ResetColor();
481 return false;
482 }
483 return true;
484}
485 
486bool TerminalUI::StopWebServer() {
487 if (!m_commManager.StopWebServer()) {
488 SetColor(COLOR_ERROR);
489 std::cout << "Errore durante l'arresto del web server: " << m_commManager.GetLastError() << std::endl;
490 ResetColor();
491 return false;
492 }
493 return true;
494}
495 
496bool TerminalUI::LoadMod(const std::string& modPath) {
497 if (!isDLLInjected) {
498 SetColor(COLOR_ERROR);
499 std::cout << "DLL non iniettata!" << std::endl;
500 ResetColor();
501 return false;
502 }
503 
504 if (!std::filesystem::exists(modPath)) {
505 SetColor(COLOR_ERROR);
506 std::cout << "File mod non trovato!" << std::endl;
507 ResetColor();
508 return false;
509 }
510 
511 if (!m_commManager.LoadMod(modPath)) {
512 SetColor(COLOR_ERROR);
513 std::cout << "Errore durante il caricamento della mod: " << m_commManager.GetLastError() << std::endl;
514 ResetColor();
515 return false;
516 }
517 return true;
518}
519 
520bool TerminalUI::UnloadMod(const std::string& modName) {
521 if (!isDLLInjected) {
522 SetColor(COLOR_ERROR);
523 std::cout << "DLL non iniettata!" << std::endl;
524 ResetColor();
525 return false;
526 }
527 
528 if (!m_commManager.UnloadMod(modName)) {
529 SetColor(COLOR_ERROR);
530 std::cout << "Errore durante lo scaricamento della mod: " << m_commManager.GetLastError() << std::endl;
531 ResetColor();
532 return false;
533 }
534 return true;
535}
536 
537void TerminalUI::ClearScreen() {
538 system("cls");
539}
540 
541void TerminalUI::PrintHeader() {
542 SetColor(COLOR_HEADER);
543 std::cout << "╔════════════════════════════════════════════════════════════╗\n";
544 std::cout << "║ DyForge Terminal UI ║\n";
545 std::cout << "╚════════════════════════════════════════════════════════════╝\n";
546 ResetColor();
547}
548 
549void TerminalUI::PrintProcessList() {
550 HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
551 if (hSnapshot == INVALID_HANDLE_VALUE) {
552 return;
553 }
554 
555 PROCESSENTRY32 pe32;
556 pe32.dwSize = sizeof(PROCESSENTRY32);
557 
558 if (!Process32First(hSnapshot, &pe32)) {
559 CloseHandle(hSnapshot);
560 return;
561 }
562 
563 std::cout << std::setw(5) << "PID" << " | " << std::setw(40) << "Nome Processo" << " | " << "Stato\n";
564 std::cout << std::string(60, '-') << "\n";
565 
566 do {
567 std::wcout << std::setw(5) << pe32.th32ProcessID << " | "
568 << std::setw(40) << pe32.szExeFile << " | ";
569
570 if (pe32.th32ProcessID == selectedProcessId) {
571 SetColor(COLOR_SUCCESS);
572 std::cout << "Selezionato";
573 ResetColor();
574 }
575 else {
576 std::cout << "-";
577 }
578 std::cout << "\n";
579 } while (Process32Next(hSnapshot, &pe32));
580 
581 CloseHandle(hSnapshot);
582}
583 
584void TerminalUI::PrintStatus() {
585 std::cout << "\nStato:\n";
586 std::cout << "Processo: " << (selectedProcessId ? std::to_string(selectedProcessId) : "Nessuno") << "\n";
587 std::cout << "DLL: " << (isDLLInjected ? "Iniettata" : "Non iniettata") << "\n";
588 std::cout << "Analisi: " << (isAnalysisRunning ? "In esecuzione" : "Fermata") << "\n";
589 std::cout << "Web Server: " << (isWebServerRunning ? "In esecuzione" : "Fermato") << "\n";
590}
591 
592void TerminalUI::WaitForKey() {
593 std::cout << "\nPremi un tasto per continuare...";
594 _getch();
595}
596 
597std::string TerminalUI::GetInput(const std::string& prompt) {
598 std::string input;
599 std::cout << prompt;
600 std::getline(std::cin, input);
601 return input;
602}
603 
604bool TerminalUI::ConfirmAction(const std::string& message) {
605 std::cout << message << " (s/n): ";
606 std::string input = GetInput("");
607 return input == "s" || input == "S";
608}
609 
610void TerminalUI::SetColor(int color) {
611 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
612}
613 
614void TerminalUI::ResetColor() {
615 SetColor(COLOR_DEFAULT);
616}
617 
618} // namespace DyForge