2023-05-07 02:45:44 +09:00
|
|
|
|
/**
|
2020-04-11 08:27:51 -07:00
|
|
|
|
* @file hyperdbg-cli.cpp
|
2022-01-18 22:38:56 +03:30
|
|
|
|
* @author Sina Karvandi (sina@hyperdbg.org)
|
2026-05-30 20:58:09 +02:00
|
|
|
|
* @brief Main HyperDbg Cli source code
|
2020-04-11 08:27:51 -07:00
|
|
|
|
* @details
|
|
|
|
|
|
* @version 0.1
|
|
|
|
|
|
* @date 2020-04-11
|
2020-07-15 15:41:30 -07:00
|
|
|
|
*
|
2020-04-11 08:27:51 -07:00
|
|
|
|
* @copyright This project is released under the GNU Public License v3.
|
2020-07-15 15:41:30 -07:00
|
|
|
|
*
|
2020-04-11 08:27:51 -07:00
|
|
|
|
*/
|
2024-05-12 15:53:22 +09:00
|
|
|
|
|
2026-05-28 21:45:22 +02:00
|
|
|
|
#ifdef _WIN32
|
2026-06-09 17:24:55 +02:00
|
|
|
|
# include <Windows.h>
|
|
|
|
|
|
# include <conio.h>
|
2026-05-28 21:45:22 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#include <string>
|
2021-03-22 18:19:39 +04:30
|
|
|
|
#include <iostream>
|
2022-02-03 15:44:55 +03:30
|
|
|
|
#include <vector>
|
2026-05-28 21:45:22 +02:00
|
|
|
|
#include <cstring>
|
2022-06-24 03:19:41 -07:00
|
|
|
|
|
|
|
|
|
|
#include "SDK/HyperDbgSdk.h"
|
2024-07-28 20:42:48 +09:00
|
|
|
|
#include "SDK/imports/user/HyperDbgLibImports.h"
|
2020-03-24 06:35:02 -07:00
|
|
|
|
|
2020-04-11 20:43:08 -07:00
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
2020-04-11 10:28:37 -07:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief CLI main function
|
2022-06-22 13:20:14 -07:00
|
|
|
|
*
|
2026-05-30 20:58:09 +02:00
|
|
|
|
* @param argc the number of arguments
|
|
|
|
|
|
* @param argv the arguments
|
|
|
|
|
|
* @return int zero on success, 1 on failure
|
2020-04-11 10:28:37 -07:00
|
|
|
|
*/
|
2021-03-22 18:19:39 +04:30
|
|
|
|
int
|
|
|
|
|
|
main(int argc, char * argv[])
|
|
|
|
|
|
{
|
2024-06-24 19:07:06 +09:00
|
|
|
|
BOOLEAN exit_from_debugger = FALSE;
|
|
|
|
|
|
string previous_command;
|
|
|
|
|
|
BOOLEAN reset = FALSE;
|
2021-03-22 18:19:39 +04:30
|
|
|
|
|
2023-05-07 02:45:44 +09:00
|
|
|
|
//
|
|
|
|
|
|
// Set console output code page to UTF-8
|
|
|
|
|
|
//
|
2026-05-28 21:45:22 +02:00
|
|
|
|
#ifdef _WIN32
|
2023-05-07 02:45:44 +09:00
|
|
|
|
SetConsoleOutputCP(CP_UTF8);
|
2026-05-28 21:45:22 +02:00
|
|
|
|
#endif
|
2023-05-07 02:45:44 +09:00
|
|
|
|
|
2021-12-09 20:50:29 +03:30
|
|
|
|
printf("HyperDbg Debugger [version: %s, build: %s]\n", CompleteVersion, BuildVersion);
|
2021-10-07 13:15:39 +03:30
|
|
|
|
printf("Please visit https://docs.hyperdbg.org for more information...\n");
|
2021-04-24 16:50:12 +04:30
|
|
|
|
printf("HyperDbg is released under the GNU Public License v3 (GPLv3).\n\n");
|
|
|
|
|
|
|
2026-06-09 17:24:55 +02:00
|
|
|
|
#if BETA_VERSION == 1
|
|
|
|
|
|
printf("Notice: This is a beta release and may contain bugs or stability issues. ");
|
|
|
|
|
|
printf("If you encounter any problems, please report them and consider using the previous stable release.\n\n");
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2021-03-22 18:19:39 +04:30
|
|
|
|
if (argc != 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
//
|
|
|
|
|
|
// User-passed arguments to the debugger
|
|
|
|
|
|
//
|
|
|
|
|
|
if (!strcmp(argv[1], "--script"))
|
|
|
|
|
|
{
|
2022-02-03 15:44:55 +03:30
|
|
|
|
//
|
2022-11-16 14:25:37 +09:00
|
|
|
|
// Handle the script
|
2022-02-03 15:44:55 +03:30
|
|
|
|
//
|
2024-06-24 19:07:06 +09:00
|
|
|
|
hyperdbg_u_script_read_file_and_execute_commandline(argc, argv);
|
2021-03-22 18:19:39 +04:30
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2022-02-03 15:44:55 +03:30
|
|
|
|
printf("err, invalid command line options passed to the HyperDbg!\n");
|
2021-03-22 18:19:39 +04:30
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-24 19:07:06 +09:00
|
|
|
|
while (!exit_from_debugger)
|
2021-03-22 18:19:39 +04:30
|
|
|
|
{
|
2024-06-24 19:07:06 +09:00
|
|
|
|
hyperdbg_u_show_signature();
|
2021-03-22 18:19:39 +04:30
|
|
|
|
|
2024-06-24 19:07:06 +09:00
|
|
|
|
string current_command = "";
|
2021-10-09 02:14:36 +03:30
|
|
|
|
|
2021-12-04 02:50:37 +03:30
|
|
|
|
//
|
|
|
|
|
|
// Clear multiline
|
|
|
|
|
|
//
|
2024-06-24 19:07:06 +09:00
|
|
|
|
reset = TRUE;
|
2021-12-04 02:50:37 +03:30
|
|
|
|
|
2026-05-30 20:58:09 +02:00
|
|
|
|
GetMultiLineCommand:
|
2021-08-16 03:02:14 +04:30
|
|
|
|
|
2024-06-24 19:07:06 +09:00
|
|
|
|
string temp_command = "";
|
2021-10-09 02:14:36 +03:30
|
|
|
|
|
2024-06-24 19:07:06 +09:00
|
|
|
|
getline(cin, temp_command);
|
2021-03-22 18:19:39 +04:30
|
|
|
|
|
|
|
|
|
|
if (cin.fail() || cin.eof())
|
|
|
|
|
|
{
|
|
|
|
|
|
cin.clear(); // reset cin state
|
2021-12-04 02:50:37 +03:30
|
|
|
|
|
2021-09-01 16:01:45 +04:30
|
|
|
|
printf("\n\n");
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
// probably sth like CTRL+C pressed
|
|
|
|
|
|
//
|
|
|
|
|
|
continue;
|
2021-03-22 18:19:39 +04:30
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-09 02:14:36 +03:30
|
|
|
|
//
|
|
|
|
|
|
// Check for multi-line commands
|
|
|
|
|
|
//
|
2024-06-24 19:07:06 +09:00
|
|
|
|
if (hyperdbg_u_check_multiline_command((CHAR *)temp_command.c_str(), reset) == TRUE)
|
2021-10-09 02:14:36 +03:30
|
|
|
|
{
|
|
|
|
|
|
//
|
|
|
|
|
|
// It's a multi-line command
|
|
|
|
|
|
//
|
2024-06-24 19:07:06 +09:00
|
|
|
|
reset = FALSE;
|
2021-10-09 02:14:36 +03:30
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
// Save the command with a space separator
|
|
|
|
|
|
//
|
2024-06-24 19:07:06 +09:00
|
|
|
|
current_command += temp_command + "\n";
|
2021-10-09 02:14:36 +03:30
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
// Show a small signature
|
|
|
|
|
|
//
|
2021-10-09 23:36:59 +03:30
|
|
|
|
printf("> ");
|
2021-10-09 02:14:36 +03:30
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
// Get next command
|
|
|
|
|
|
//
|
2026-05-30 20:58:09 +02:00
|
|
|
|
goto GetMultiLineCommand;
|
2021-10-09 02:14:36 +03:30
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2021-12-04 02:33:37 +03:30
|
|
|
|
//
|
|
|
|
|
|
// Reset for future commands
|
|
|
|
|
|
//
|
2024-06-24 19:07:06 +09:00
|
|
|
|
reset = TRUE;
|
2021-12-04 02:33:37 +03:30
|
|
|
|
|
2021-10-09 02:14:36 +03:30
|
|
|
|
//
|
|
|
|
|
|
// Either the multi-line is finished or it's a
|
|
|
|
|
|
// single line command
|
|
|
|
|
|
//
|
2024-06-24 19:07:06 +09:00
|
|
|
|
current_command += temp_command;
|
2021-10-09 02:14:36 +03:30
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-24 19:07:06 +09:00
|
|
|
|
if (!current_command.compare("") && hyperdbg_u_continue_previous_command())
|
2021-08-16 03:02:14 +04:30
|
|
|
|
{
|
|
|
|
|
|
//
|
|
|
|
|
|
// Retry the previous command
|
|
|
|
|
|
//
|
2024-06-24 19:07:06 +09:00
|
|
|
|
current_command = previous_command;
|
2021-08-16 03:02:14 +04:30
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
//
|
|
|
|
|
|
// Save previous command
|
|
|
|
|
|
//
|
2024-06-24 19:07:06 +09:00
|
|
|
|
previous_command = current_command;
|
2021-08-16 03:02:14 +04:30
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-16 16:14:52 +09:00
|
|
|
|
INT CommandExecutionResult = hyperdbg_u_run_command((CHAR *)current_command.c_str());
|
2021-03-22 18:19:39 +04:30
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
// if the debugger encounters an exit state then the return will be 1
|
|
|
|
|
|
//
|
|
|
|
|
|
if (CommandExecutionResult == 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
//
|
|
|
|
|
|
// Exit from the debugger
|
|
|
|
|
|
//
|
2024-06-24 19:07:06 +09:00
|
|
|
|
exit_from_debugger = true;
|
2021-03-22 18:19:39 +04:30
|
|
|
|
}
|
|
|
|
|
|
if (CommandExecutionResult != 2)
|
|
|
|
|
|
{
|
|
|
|
|
|
printf("\n");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
2020-03-24 06:35:02 -07:00
|
|
|
|
}
|