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

180 lines
4.2 KiB
C++
Raw Normal View History

2020-04-11 08:27:51 -07:00
/**
* @file hyperdbg-cli.cpp
* @author Sina Karvandi (sina@rayanfam.com)
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>
2020-04-11 10:28:37 -07:00
#include "Definition.h"
#include "Configuration.h"
2020-04-12 03:09:26 -07:00
#include "details.h"
2020-03-24 06:35:02 -07:00
2020-03-26 11:02:50 -07:00
#pragma comment(lib, "HPRDBGCTRL.lib")
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
//
// Header file of HPRDBGCTRL
// Imports
//
2021-03-22 18:19:39 +04:30
extern "C" {
__declspec(dllimport) int HyperdbgLoadVmm();
__declspec(dllimport) int HyperdbgUnload();
__declspec(dllimport) int HyperdbgInstallVmmDriver();
__declspec(dllimport) int HyperdbgUninstallDriver();
__declspec(dllimport) int HyperdbgStopDriver();
__declspec(dllimport) int HyperdbgInterpreter(char * Command);
__declspec(dllimport) void HyperdbgShowSignature();
__declspec(dllimport) bool HyperdbgContinuePreviousCommand();
__declspec(dllimport) bool HyperDbgCheckMultilineCommand(std::string & CurrentCommand, bool Reset);
2021-03-22 18:19:39 +04:30
__declspec(dllimport) void HyperdbgSetTextMessageCallback(Callback handler);
2020-04-01 09:34:24 -07:00
}
2020-03-24 06:35:02 -07:00
2020-04-11 10:28:37 -07:00
/**
* @brief CLI main function
2020-08-28 04:03:12 -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[])
{
bool ExitFromDebugger = false;
string PreviousCommand;
bool Reset = false;
2021-03-22 18:19:39 +04:30
2021-04-24 16:50:12 +04:30
printf("HyperDbg Debugger [core version: v%s]\n", Version);
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"))
{
char ScriptBuffer[MAX_PATH + 10] = {0};
//
// Executing the script
//
sprintf_s(ScriptBuffer, sizeof(ScriptBuffer), ".script %s", argv[2]);
HyperdbgInterpreter(ScriptBuffer);
printf("\n");
}
else
{
printf("invalid command line options passed to HyperDbg !\n");
return 1;
}
}
while (!ExitFromDebugger)
{
HyperdbgShowSignature();
string CurrentCommand = "";
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
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
//
if (HyperDbgCheckMultilineCommand(TempCommand, 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("") &&
HyperdbgContinuePreviousCommand())
{
//
// Retry the previous command
//
CurrentCommand = PreviousCommand;
}
else
{
//
// Save previous command
//
PreviousCommand = CurrentCommand;
}
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
}