HyperDbg/hyperdbg/libhyperdbg/code/debugger/commands/extension-commands/exception.cpp

185 lines
5.9 KiB
C++
Raw Permalink Normal View History

2020-06-03 06:59:28 -07:00
/**
* @file exception.cpp
2022-01-18 22:38:56 +03:30
* @author Sina Karvandi (sina@hyperdbg.org)
2020-06-03 06:59:28 -07:00
* @brief !exception command
* @details
* @version 0.1
* @date 2020-06-03
*
* @copyright This project is released under the GNU Public License v3.
*
*/
#include "pch.h"
2020-06-03 06:59:28 -07:00
2020-08-28 04:03:12 -07:00
/**
2023-07-13 16:05:42 +09:00
* @brief help of the !exception command
*
* @return VOID
2020-08-28 04:03:12 -07:00
*/
2021-03-22 18:19:39 +04:30
VOID
CommandExceptionHelp()
{
2022-04-10 22:24:56 +04:30
ShowMessages("!exception : monitors the first 32 entry of IDT (starting from "
2021-03-22 18:19:39 +04:30
"zero).\n\n");
2021-03-22 18:19:39 +04:30
ShowMessages(
2022-02-11 02:53:28 +03:30
"syntax : \t!exception [IdtIndex (hex)] [pid ProcessId (hex)] "
2023-10-11 15:03:51 +09:00
"[core CoreId (hex)] [imm IsImmediate (yesno)] [sc EnableShortCircuiting (onoff)] "
"[stage CallingStage (prepostall)] [buffer PreAllocatedBuffer (hex)] [script { Script (string) }] "
2024-07-19 17:03:43 +09:00
"[asm condition { Condition (assembly/hex) }] [asm code { Code (assembly/hex) }] [output {OutputName (string)}]\n");
2024-07-20 22:05:39 +09:00
ShowMessages("\nnote: monitoring page-faults (entry 0xe) is implemented differently (for more information, check the documentation).\n");
ShowMessages("\n");
2021-03-22 18:19:39 +04:30
ShowMessages("\t\te.g : !exception\n");
ShowMessages("\t\te.g : !exception 0xe\n");
ShowMessages("\t\te.g : !exception pid 400\n");
ShowMessages("\t\te.g : !exception core 2 pid 400\n");
2024-07-20 22:05:39 +09:00
ShowMessages("\t\te.g : !exception 0xe stage post script { printf(\"page-fault occurred at: %%llx\\n\", @cr2); }\n");
ShowMessages("\t\te.g : !exception asm code { nop; nop; nop }\n");
2020-06-03 06:59:28 -07:00
}
2020-08-28 04:03:12 -07:00
/**
* @brief !exception command handler
*
2024-07-30 18:44:15 +09:00
* @param CommandTokens
* @param Command
2024-07-30 18:44:15 +09:00
*
* @return VOID
2020-08-28 04:03:12 -07:00
*/
2021-03-22 18:19:39 +04:30
VOID
CommandException(vector<CommandToken> CommandTokens, string Command)
2021-03-22 18:19:39 +04:30
{
PDEBUGGER_GENERAL_EVENT_DETAIL Event = NULL;
PDEBUGGER_GENERAL_ACTION ActionBreakToDebugger = NULL;
PDEBUGGER_GENERAL_ACTION ActionCustomCode = NULL;
PDEBUGGER_GENERAL_ACTION ActionScript = NULL;
UINT32 EventLength;
UINT32 ActionBreakToDebuggerLength = 0;
UINT32 ActionCustomCodeLength = 0;
UINT32 ActionScriptLength = 0;
UINT64 SpecialTarget = DEBUGGER_EVENT_EXCEPTIONS_ALL_FIRST_32_ENTRIES;
BOOLEAN GetEntry = FALSE;
DEBUGGER_EVENT_PARSING_ERROR_CAUSE EventParsingErrorCause;
2020-06-03 06:59:28 -07:00
2021-03-22 18:19:39 +04:30
//
// Interpret and fill the general event and action fields
//
//
if (!InterpretGeneralEventAndActionsFields(
2024-07-30 18:44:15 +09:00
&CommandTokens,
2021-03-22 18:19:39 +04:30
EXCEPTION_OCCURRED,
&Event,
&EventLength,
&ActionBreakToDebugger,
&ActionBreakToDebuggerLength,
&ActionCustomCode,
&ActionCustomCodeLength,
&ActionScript,
&ActionScriptLength,
&EventParsingErrorCause))
2021-03-22 18:19:39 +04:30
{
2020-06-03 06:59:28 -07:00
return;
2021-03-22 18:19:39 +04:30
}
2020-08-28 04:03:12 -07:00
2021-03-22 18:19:39 +04:30
//
// Interpret command specific details (if any)
//
2024-07-30 18:44:15 +09:00
for (auto Section : CommandTokens)
2021-03-22 18:19:39 +04:30
{
2024-07-30 18:44:15 +09:00
if (CompareLowerCaseStrings(Section, "!exception"))
2021-03-22 18:19:39 +04:30
{
continue;
}
else if (!GetEntry)
{
//
// It's probably an index
//
2024-07-30 18:44:15 +09:00
if (!ConvertTokenToUInt64(Section, &SpecialTarget))
2021-03-22 18:19:39 +04:30
{
//
2024-03-17 19:00:14 +09:00
// Unknown parameter
2021-03-22 18:19:39 +04:30
//
2024-07-30 18:44:15 +09:00
ShowMessages("unknown parameter '%s'\n\n",
GetCaseSensitiveStringFromCommandToken(Section).c_str());
2021-03-22 18:19:39 +04:30
CommandExceptionHelp();
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
2021-03-22 18:19:39 +04:30
return;
}
else
{
//
// Check if entry is valid or not (start from zero)
//
if (SpecialTarget >= 31)
{
//
// Entry is invalid (this command is designed for just first 32
// entries)
//
ShowMessages("the entry should be between 0x0 to 0x1f or first 32 "
"entries'\n\n");
2021-03-22 18:19:39 +04:30
CommandExceptionHelp();
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
2021-03-22 18:19:39 +04:30
return;
}
GetEntry = TRUE;
}
}
else
{
//
2024-03-17 19:00:14 +09:00
// Unknown parameter
2021-03-22 18:19:39 +04:30
//
2024-07-30 18:44:15 +09:00
ShowMessages("unknown parameter '%s'\n\n",
GetCaseSensitiveStringFromCommandToken(Section).c_str());
2021-03-22 18:19:39 +04:30
CommandExceptionHelp();
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
2021-03-22 18:19:39 +04:30
return;
2020-06-03 06:59:28 -07:00
}
}
2021-03-22 18:19:39 +04:30
//
// Set the target exception (if not specific then it means all exceptions)
//
2023-10-14 21:28:08 +09:00
Event->Options.OptionalParam1 = SpecialTarget;
2020-08-28 04:03:12 -07:00
//
// Send the ioctl to the kernel for event registration
2020-08-28 04:03:12 -07:00
//
2021-03-22 18:19:39 +04:30
if (!SendEventToKernel(Event, EventLength))
{
//
// There was an error, probably the handle was not initialized
// we have to free the Action before exit, it is because, we
// already freed the Event and string buffers
//
2022-05-08 22:13:44 +04:30
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
2021-03-22 18:19:39 +04:30
return;
}
2020-06-03 06:59:28 -07:00
2020-08-28 04:03:12 -07:00
//
2021-03-22 18:19:39 +04:30
// Add the event to the kernel
2020-08-28 04:03:12 -07:00
//
if (!RegisterActionToEvent(Event,
ActionBreakToDebugger,
2022-03-13 01:33:21 +03:30
ActionBreakToDebuggerLength,
ActionCustomCode,
ActionCustomCodeLength,
ActionScript,
ActionScriptLength))
2021-03-22 18:19:39 +04:30
{
//
// There was an error
//
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
2021-03-22 18:19:39 +04:30
return;
}
2020-06-03 06:59:28 -07:00
}