2020-05-27 09:59:17 -07:00
|
|
|
/**
|
|
|
|
|
* @file syscall-sysret.cpp
|
2022-01-18 22:38:56 +03:30
|
|
|
* @author Sina Karvandi (sina@hyperdbg.org)
|
2020-05-27 09:59:17 -07:00
|
|
|
* @brief !syscall and !sysret commands
|
|
|
|
|
* @details
|
|
|
|
|
* @version 0.1
|
|
|
|
|
* @date 2020-05-27
|
|
|
|
|
*
|
|
|
|
|
* @copyright This project is released under the GNU Public License v3.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2022-05-04 16:38:21 -07:00
|
|
|
#include "pch.h"
|
2020-05-27 09:59:17 -07:00
|
|
|
|
2020-08-28 04:03:12 -07:00
|
|
|
/**
|
2023-07-13 16:05:42 +09:00
|
|
|
* @brief help of the !syscall command
|
2020-10-22 06:27:28 -07:00
|
|
|
*
|
|
|
|
|
* @return VOID
|
2020-08-28 04:03:12 -07:00
|
|
|
*/
|
2021-03-22 18:19:39 +04:30
|
|
|
VOID
|
|
|
|
|
CommandSyscallHelp()
|
|
|
|
|
{
|
2022-04-10 22:24:56 +04:30
|
|
|
ShowMessages("!syscall : monitors and hooks all execution of syscall "
|
2021-07-19 17:31:59 +04:30
|
|
|
"instructions (by accessing memory and checking for instructions).\n\n");
|
2022-04-10 22:24:56 +04:30
|
|
|
ShowMessages("!syscall2 : monitors and hooks all execution of syscall "
|
2021-07-19 17:31:59 +04:30
|
|
|
"instructions (by emulating all #UDs).\n\n");
|
2022-04-17 23:05:24 +04:30
|
|
|
|
2023-10-11 15:03:51 +09:00
|
|
|
ShowMessages("syntax : \t!syscall [SyscallNumber (hex)] [pid ProcessId (hex)] [core CoreId (hex)] "
|
|
|
|
|
"[imm IsImmediate (yesno)] [sc EnableShortCircuiting (onoff)] [stage CallingStage (prepostall)] "
|
2024-07-19 17:03:43 +09:00
|
|
|
"[buffer PreAllocatedBuffer (hex)] [script { Script (string) }] [asm condition { Condition (assembly/hex) }] "
|
|
|
|
|
"[asm code { Code (assembly/hex) }] [output {OutputName (string)}]\n");
|
2023-10-11 15:03:51 +09:00
|
|
|
ShowMessages("syntax : \t!syscall2 [SyscallNumber (hex)] [pid ProcessId (hex)] [core CoreId (hex)] "
|
|
|
|
|
"[imm IsImmediate (yesno)] [sc EnableShortCircuiting (onoff)] [stage CallingStage (prepostall)] "
|
2024-07-19 17:03:43 +09:00
|
|
|
"[buffer PreAllocatedBuffer (hex)] [script { Script (string) }] [asm condition { Condition (assembly/hex) }] "
|
|
|
|
|
"[asm code { Code (assembly/hex) }] [output {OutputName (string)}]\n");
|
2021-03-22 18:19:39 +04:30
|
|
|
|
2022-04-17 23:05:24 +04:30
|
|
|
ShowMessages("\n");
|
2021-03-22 18:19:39 +04:30
|
|
|
ShowMessages("\t\te.g : !syscall\n");
|
2021-07-19 17:31:59 +04:30
|
|
|
ShowMessages("\t\te.g : !syscall2\n");
|
2021-03-22 18:19:39 +04:30
|
|
|
ShowMessages("\t\te.g : !syscall 0x55\n");
|
2021-07-19 17:31:59 +04:30
|
|
|
ShowMessages("\t\te.g : !syscall2 0x55\n");
|
2021-03-22 18:19:39 +04:30
|
|
|
ShowMessages("\t\te.g : !syscall 0x55 pid 400\n");
|
|
|
|
|
ShowMessages("\t\te.g : !syscall 0x55 core 2 pid 400\n");
|
2021-07-19 17:31:59 +04:30
|
|
|
ShowMessages("\t\te.g : !syscall2 0x55 core 2 pid 400\n");
|
2024-07-20 22:05:39 +09:00
|
|
|
ShowMessages("\t\te.g : !syscall script { printf(\"system-call num: %%llx, at process id: %%x\\n\", @rax, $pid); }\n");
|
|
|
|
|
ShowMessages("\t\te.g : !syscall asm code { nop; nop; nop }\n");
|
2020-05-27 09:59:17 -07:00
|
|
|
}
|
|
|
|
|
|
2020-08-28 04:03:12 -07:00
|
|
|
/**
|
2023-07-13 16:05:42 +09:00
|
|
|
* @brief help of the !sysret command
|
2020-10-22 06:27:28 -07:00
|
|
|
*
|
|
|
|
|
* @return VOID
|
2020-08-28 04:03:12 -07:00
|
|
|
*/
|
2021-03-22 18:19:39 +04:30
|
|
|
VOID
|
|
|
|
|
CommandSysretHelp()
|
|
|
|
|
{
|
2022-04-10 22:24:56 +04:30
|
|
|
ShowMessages("!sysret : monitors and hooks all execution of sysret "
|
2021-07-19 17:31:59 +04:30
|
|
|
"instructions (by accessing memory and checking for instructions).\n\n");
|
2022-04-10 22:24:56 +04:30
|
|
|
ShowMessages("!sysret2 : monitors and hooks all execution of sysret "
|
2021-07-19 17:31:59 +04:30
|
|
|
"instructions (by emulating all #UDs).\n\n");
|
2022-04-18 01:48:28 +04:30
|
|
|
|
2022-02-11 02:53:28 +03:30
|
|
|
ShowMessages("syntax : \t!sysret [pid ProcessId (hex)] [core CoreId (hex)] "
|
2023-06-07 19:24:47 +09:00
|
|
|
"[imm IsImmediate (yesno)] [sc EnableShortCircuiting (onoff)] [buffer PreAllocatedBuffer (hex)] "
|
2024-07-19 17:03:43 +09:00
|
|
|
"[script { Script (string) }] [asm condition { Condition (assembly/hex) }] [asm code { Code (assembly/hex) }]\n");
|
2021-03-22 18:19:39 +04:30
|
|
|
|
2022-04-18 01:48:28 +04:30
|
|
|
ShowMessages("\n");
|
2021-03-22 18:19:39 +04:30
|
|
|
ShowMessages("\t\te.g : !sysret\n");
|
2021-07-19 17:31:59 +04:30
|
|
|
ShowMessages("\t\te.g : !sysret2\n");
|
2021-03-22 18:19:39 +04:30
|
|
|
ShowMessages("\t\te.g : !sysret pid 400\n");
|
2021-07-19 17:31:59 +04:30
|
|
|
ShowMessages("\t\te.g : !sysret2 pid 400\n");
|
2021-03-22 18:19:39 +04:30
|
|
|
ShowMessages("\t\te.g : !sysret core 2 pid 400\n");
|
2021-07-19 17:31:59 +04:30
|
|
|
ShowMessages("\t\te.g : !sysret2 core 2 pid 400\n");
|
2024-07-20 22:05:39 +09:00
|
|
|
ShowMessages("\t\te.g : !sysret script { printf(\"SYSRET instruction is executed at process id: %%x\\n\", $pid); }\n");
|
|
|
|
|
ShowMessages("\t\te.g : !sysret asm code { nop; nop; nop }\n");
|
2020-05-27 09:59:17 -07:00
|
|
|
}
|
|
|
|
|
|
2020-08-28 04:03:12 -07:00
|
|
|
/**
|
2021-07-19 17:31:59 +04:30
|
|
|
* @brief !syscall, !syscall2 and !sysret, !sysret2 commands handler
|
2020-10-22 06:27:28 -07:00
|
|
|
*
|
2024-07-30 18:44:15 +09:00
|
|
|
* @param CommandTokens
|
2024-07-31 14:08:14 +09:00
|
|
|
* @param Command
|
2024-07-30 18:44:15 +09:00
|
|
|
*
|
2021-02-10 15:19:01 -08:00
|
|
|
* @return VOID
|
2020-08-28 04:03:12 -07:00
|
|
|
*/
|
2021-03-22 18:19:39 +04:30
|
|
|
VOID
|
2024-07-31 14:08:14 +09:00
|
|
|
CommandSyscallAndSysret(vector<CommandToken> CommandTokens, string Command)
|
2021-03-22 18:19:39 +04:30
|
|
|
{
|
2021-08-21 20:48:17 +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_SYSCALL_ALL_SYSRET_OR_SYSCALLS;
|
|
|
|
|
BOOLEAN GetSyscallNumber = FALSE;
|
|
|
|
|
DEBUGGER_EVENT_PARSING_ERROR_CAUSE EventParsingErrorCause;
|
2021-12-29 21:10:09 +03:30
|
|
|
string Cmd;
|
2020-05-27 09:59:17 -07:00
|
|
|
|
2021-03-22 18:19:39 +04:30
|
|
|
//
|
|
|
|
|
// Interpret and fill the general event and action fields
|
|
|
|
|
//
|
|
|
|
|
//
|
2024-07-30 18:44:15 +09:00
|
|
|
Cmd = GetLowerStringFromCommandToken(CommandTokens.at(0));
|
|
|
|
|
|
2021-12-29 21:10:09 +03:30
|
|
|
if (!Cmd.compare("!syscall") || !Cmd.compare("!syscall2"))
|
2021-03-22 18:19:39 +04:30
|
|
|
{
|
|
|
|
|
if (!InterpretGeneralEventAndActionsFields(
|
2024-07-30 18:44:15 +09:00
|
|
|
&CommandTokens,
|
2021-03-22 18:19:39 +04:30
|
|
|
SYSCALL_HOOK_EFER_SYSCALL,
|
|
|
|
|
&Event,
|
|
|
|
|
&EventLength,
|
|
|
|
|
&ActionBreakToDebugger,
|
|
|
|
|
&ActionBreakToDebuggerLength,
|
|
|
|
|
&ActionCustomCode,
|
|
|
|
|
&ActionCustomCodeLength,
|
|
|
|
|
&ActionScript,
|
2021-08-21 20:48:17 +04:30
|
|
|
&ActionScriptLength,
|
|
|
|
|
&EventParsingErrorCause))
|
2021-03-22 18:19:39 +04:30
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-05-27 09:59:17 -07:00
|
|
|
}
|
2021-03-22 18:19:39 +04:30
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!InterpretGeneralEventAndActionsFields(
|
2024-07-30 18:44:15 +09:00
|
|
|
&CommandTokens,
|
2021-03-22 18:19:39 +04:30
|
|
|
SYSCALL_HOOK_EFER_SYSRET,
|
|
|
|
|
&Event,
|
|
|
|
|
&EventLength,
|
|
|
|
|
&ActionBreakToDebugger,
|
|
|
|
|
&ActionBreakToDebuggerLength,
|
|
|
|
|
&ActionCustomCode,
|
|
|
|
|
&ActionCustomCodeLength,
|
|
|
|
|
&ActionScript,
|
2021-08-21 20:48:17 +04:30
|
|
|
&ActionScriptLength,
|
|
|
|
|
&EventParsingErrorCause))
|
2021-03-22 18:19:39 +04:30
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-05-27 09:59:17 -07:00
|
|
|
}
|
|
|
|
|
|
2021-03-22 18:19:39 +04:30
|
|
|
//
|
|
|
|
|
// Interpret command specific details (if any)
|
|
|
|
|
//
|
2020-06-13 22:39:53 -07:00
|
|
|
|
2021-03-22 18:19:39 +04:30
|
|
|
//
|
|
|
|
|
// Currently we do not support any extra argument for !sysret command
|
|
|
|
|
// it is because we don't know how to find syscall number in sysret
|
|
|
|
|
// and we don't wanna deal with dynamic mapping of rcx (user stack)
|
|
|
|
|
// in vmx-root
|
|
|
|
|
//
|
2021-12-29 21:10:09 +03:30
|
|
|
if (!Cmd.compare("!syscall") || !Cmd.compare("!syscall2"))
|
2021-03-22 18:19:39 +04:30
|
|
|
{
|
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, "!syscall") ||
|
|
|
|
|
CompareLowerCaseStrings(Section, "!syscall2") ||
|
|
|
|
|
CompareLowerCaseStrings(Section, "!sysret") ||
|
|
|
|
|
CompareLowerCaseStrings(Section, "!sysret2"))
|
2021-03-22 18:19:39 +04:30
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-07-19 17:31:59 +04:30
|
|
|
|
2021-03-22 18:19:39 +04:30
|
|
|
else if (!GetSyscallNumber)
|
|
|
|
|
{
|
|
|
|
|
//
|
|
|
|
|
// It's probably a syscall address
|
|
|
|
|
//
|
2025-09-02 00:52:27 +02:00
|
|
|
if (!SymbolConvertNameOrExprToAddress(
|
|
|
|
|
GetCaseSensitiveStringFromCommandToken(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-07-19 17:31:59 +04:30
|
|
|
|
2021-12-29 21:10:09 +03:30
|
|
|
if (!Cmd.compare("!syscall") || !Cmd.compare("!syscall2"))
|
2021-03-22 18:19:39 +04:30
|
|
|
{
|
|
|
|
|
CommandSyscallHelp();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CommandSysretHelp();
|
|
|
|
|
}
|
2022-05-09 01:34:07 +04:30
|
|
|
|
|
|
|
|
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
|
2021-03-22 18:19:39 +04:30
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
GetSyscallNumber = 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-07-19 17:31:59 +04:30
|
|
|
|
2021-12-29 21:10:09 +03:30
|
|
|
if (!Cmd.compare("!syscall") || !Cmd.compare("!syscall2"))
|
2021-03-22 18:19:39 +04:30
|
|
|
{
|
|
|
|
|
CommandSyscallHelp();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CommandSysretHelp();
|
|
|
|
|
}
|
2022-05-09 01:34:07 +04:30
|
|
|
|
|
|
|
|
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
|
2021-03-22 18:19:39 +04:30
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-28 04:03:12 -07:00
|
|
|
|
|
|
|
|
//
|
2021-03-22 18:19:39 +04:30
|
|
|
// Set the target syscall
|
2020-08-28 04:03:12 -07:00
|
|
|
//
|
2023-10-14 21:28:08 +09:00
|
|
|
Event->Options.OptionalParam1 = SpecialTarget;
|
2021-03-22 18:19:39 +04:30
|
|
|
}
|
2020-08-28 04:03:12 -07:00
|
|
|
|
2021-07-19 17:31:59 +04:30
|
|
|
//
|
|
|
|
|
// Set whether it's !syscall or !syscall2 or !sysret or !sysret2
|
|
|
|
|
//
|
2021-12-29 21:10:09 +03:30
|
|
|
if (!Cmd.compare("!syscall2") || !Cmd.compare("!sysret2"))
|
2021-07-19 17:31:59 +04:30
|
|
|
{
|
|
|
|
|
//
|
|
|
|
|
// It's a !syscall2 or !sysret2
|
|
|
|
|
//
|
2023-10-14 21:28:08 +09:00
|
|
|
Event->Options.OptionalParam2 = DEBUGGER_EVENT_SYSCALL_SYSRET_HANDLE_ALL_UD;
|
2021-07-19 17:31:59 +04:30
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//
|
|
|
|
|
// It's a !syscall or !sysret
|
|
|
|
|
//
|
2023-10-14 21:28:08 +09:00
|
|
|
Event->Options.OptionalParam2 = DEBUGGER_EVENT_SYSCALL_SYSRET_SAFE_ACCESS_MEMORY;
|
2021-07-19 17:31:59 +04:30
|
|
|
}
|
|
|
|
|
|
2021-03-22 18:19:39 +04:30
|
|
|
//
|
2022-05-08 22:13:44 +04:30
|
|
|
// Send the ioctl to the kernel for event registration
|
2021-03-22 18:19:39 +04:30
|
|
|
//
|
|
|
|
|
if (!SendEventToKernel(Event, EventLength))
|
|
|
|
|
{
|
2020-08-28 04:03:12 -07:00
|
|
|
//
|
2021-03-22 18:19:39 +04:30
|
|
|
// 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
|
2020-08-28 04:03:12 -07:00
|
|
|
//
|
2022-05-08 22:13:44 +04:30
|
|
|
|
2022-05-09 01:34:07 +04:30
|
|
|
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
|
2020-08-28 04:03:12 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2020-06-13 22:39:53 -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
|
|
|
//
|
2022-05-09 01:34:07 +04:30
|
|
|
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
|
|
|
|
|
//
|
2022-05-09 01:34:07 +04:30
|
|
|
|
|
|
|
|
FreeEventsAndActionsMemory(Event, ActionBreakToDebugger, ActionCustomCode, ActionScript);
|
2021-03-22 18:19:39 +04:30
|
|
|
return;
|
2020-10-22 06:27:28 -07:00
|
|
|
}
|
2020-05-27 12:09:57 -07:00
|
|
|
}
|