HyperDbg/hyperdbg/hyperdbg-cli/hyperdbg-cli.cpp

166 lines
3.5 KiB
C++
Raw Normal View History

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)
2020-04-11 10:28:37 -07:00
* @brief Main HyperDbg Cli source coede
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
*/
2020-03-24 06:35:02 -07:00
#include <Windows.h>
2020-04-11 20:43:08 -07:00
#include <string>
2020-03-24 06:35:02 -07:00
#include <conio.h>
2021-03-22 18:19:39 +04:30
#include <iostream>
#include <vector>
2022-06-24 03:19:41 -07:00
#include "SDK/HyperDbgSdk.h"
2022-07-15 16:18:13 -07:00
#include "SDK/Imports/HyperDbgCtrlImports.h"
2020-03-24 06:35:02 -07:00
2020-04-11 20:43:08 -07:00
using namespace std;
2022-06-22 13:20:14 -07:00
#pragma comment(lib, "HPRDBGCTRL.lib")
2020-03-24 06:35:02 -07:00
2020-04-11 10:28:37 -07:00
/**
* @brief CLI main function
2022-06-22 13:20:14 -07:00
*
* @param argc
* @param argv
* @return int
2020-04-11 10:28:37 -07:00
*/
2021-03-22 18:19:39 +04:30
int
main(int argc, char * argv[])
{
2022-11-16 14:25:37 +09:00
bool ExitFromDebugger = false;
string PreviousCommand;
bool Reset = false;
2021-03-22 18:19:39 +04: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");
2021-03-22 18:19:39 +04:30
if (argc != 1)
{
//
// User-passed arguments to the debugger
//
if (!strcmp(argv[1], "--script"))
{
//
2022-11-16 14:25:37 +09:00
// Handle the script
//
2022-11-16 14:25:37 +09:00
HyperDbgScriptReadFileAndExecuteCommandline(argc, argv);
2021-03-22 18:19:39 +04:30
}
else
{
printf("err, invalid command line options passed to the HyperDbg!\n");
2021-03-22 18:19:39 +04:30
return 1;
}
}
while (!ExitFromDebugger)
{
2022-07-15 14:52:07 -07:00
HyperDbgShowSignature();
2021-03-22 18:19:39 +04:30
string CurrentCommand = "";
2021-10-09 02:14:36 +03:30
2021-12-04 02:50:37 +03:30
//
// Clear multiline
//
Reset = true;
2021-10-09 02:14:36 +03:30
GetMultiLinecCommand:
2021-10-09 02:14:36 +03:30
string TempCommand = "";
getline(cin, TempCommand);
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
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
//
2022-11-16 14:25:37 +09:00
if (HyperDbgCheckMultilineCommand((char *)TempCommand.c_str(), Reset) == true)
2021-10-09 02:14:36 +03:30
{
//
// It's a multi-line command
//
Reset = false;
2021-10-09 02:14:36 +03:30
//
// Save the command with a space separator
//
CurrentCommand += TempCommand + "\n";
2021-10-09 02:14:36 +03:30
//
// Show a small signature
//
printf("> ");
2021-10-09 02:14:36 +03:30
//
// Get next command
//
goto GetMultiLinecCommand;
}
else
{
//
// Reset for future commands
//
Reset = true;
2021-10-09 02:14:36 +03:30
//
// Either the multi-line is finished or it's a
// single line command
//
CurrentCommand += TempCommand;
}
if (!CurrentCommand.compare("") &&
2022-07-15 14:52:07 -07:00
HyperDbgContinuePreviousCommand())
{
//
// Retry the previous command
//
CurrentCommand = PreviousCommand;
}
else
{
//
// Save previous command
//
PreviousCommand = CurrentCommand;
}
2022-07-15 14:52:07 -07:00
int CommandExecutionResult = HyperDbgInterpreter((char *)CurrentCommand.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
//
ExitFromDebugger = true;
}
if (CommandExecutionResult != 2)
{
printf("\n");
}
}
return 0;
2020-03-24 06:35:02 -07:00
}