HyperDbg/hyperdbg/libhyperdbg/code/debugger/commands/meta-commands/attach.cpp

126 lines
3.2 KiB
C++
Raw Permalink Normal View History

2020-08-28 04:03:12 -07:00
/**
* @file attach.cpp
2022-01-18 22:38:56 +03:30
* @author Sina Karvandi (sina@hyperdbg.org)
2020-08-28 04:03:12 -07:00
* @brief .attach command
* @details
* @version 0.1
* @date 2020-08-27
*
* @copyright This project is released under the GNU Public License v3.
*
*/
#include "pch.h"
2020-08-28 04:03:12 -07:00
2022-01-06 23:24:05 +03:30
//
// Global Variables
//
2022-03-03 00:27:34 +03:30
extern BOOLEAN g_IsSerialConnectedToRemoteDebugger;
2022-01-06 23:24:05 +03:30
2020-08-28 04:03:12 -07:00
/**
2023-07-13 16:05:42 +09:00
* @brief help of the .attach command
2020-08-28 04:03:12 -07:00
*
* @return VOID
*/
2021-03-22 18:19:39 +04:30
VOID
CommandAttachHelp()
{
2022-04-10 22:24:56 +04:30
ShowMessages(".attach : attaches to debug a thread in VMI Mode.\n\n");
2022-02-09 02:16:58 +03:30
ShowMessages("syntax : \t.attach [pid ProcessId (hex)]\n");
ShowMessages("\n");
2021-03-22 18:19:39 +04:30
ShowMessages("\t\te.g : .attach pid b60 \n");
2020-09-17 04:03:06 -07:00
}
2020-08-28 04:03:12 -07:00
/**
* @brief .attach command handler
*
2024-07-30 15:07:17 +09:00
* @param CommandTokens
* @param Command
2024-07-30 15:07:17 +09:00
*
2020-08-28 04:03:12 -07:00
* @return VOID
*/
2021-03-22 18:19:39 +04:30
VOID
CommandAttach(vector<CommandToken> CommandTokens, string Command)
2021-03-22 18:19:39 +04:30
{
2024-03-15 13:49:02 +09:00
UINT32 TargetPid = 0;
2021-03-22 18:19:39 +04:30
BOOLEAN NextIsPid = FALSE;
//
// Disable user-mode debugger in this version
//
#if ActivateUserModeDebugger == FALSE
if (!g_IsSerialConnectedToRemoteDebugger)
{
ShowMessages("the user-mode debugger in VMI Mode is still in the beta version and not stable. "
"we decided to exclude it from this release and release it in future versions. "
"if you want to test the user-mode debugger in VMI Mode, you should build "
"HyperDbg with special instructions. But attaching/switching to other processes\n"
"are fully supported in the Debugger Mode.\n"
"(it's not recommended to use it in VMI Mode yet!)\n");
return;
}
#endif // !ActivateUserModeDebugger
2022-01-06 23:24:05 +03:30
//
// It's a attach to a target PID
//
2024-07-30 15:07:17 +09:00
if (CommandTokens.size() >= 4)
2021-03-22 18:19:39 +04:30
{
ShowMessages("incorrect use of the '%s'\n\n",
GetCaseSensitiveStringFromCommandToken(CommandTokens.at(0)).c_str());
2020-09-12 14:57:06 -07:00
CommandAttachHelp();
return;
2021-03-22 18:19:39 +04:30
}
2020-09-12 14:57:06 -07:00
for (auto Section = CommandTokens.begin() + 1; Section != CommandTokens.end(); Section++)
{
//
2022-01-06 23:24:05 +03:30
// Find out whether the user enters pid or not
//
2022-01-06 23:24:05 +03:30
if (NextIsPid)
2021-03-22 18:19:39 +04:30
{
2022-01-06 23:24:05 +03:30
NextIsPid = FALSE;
2021-03-22 18:19:39 +04:30
if (!ConvertTokenToUInt32(*Section, &TargetPid))
2021-03-22 18:19:39 +04:30
{
2022-01-06 23:24:05 +03:30
ShowMessages("please specify a correct hex value for process id\n\n");
CommandAttachHelp();
return;
2021-03-22 18:19:39 +04:30
}
2022-01-06 23:24:05 +03:30
}
else if (CompareLowerCaseStrings(*Section, "pid"))
2021-03-22 18:19:39 +04:30
{
2022-01-06 23:24:05 +03:30
//
// next item is a pid for the process
//
NextIsPid = TRUE;
2021-03-22 18:19:39 +04:30
}
2024-07-30 15:07:17 +09:00
else
{
ShowMessages("unknown parameter at '%s'\n\n",
GetCaseSensitiveStringFromCommandToken(*Section).c_str());
2024-07-30 15:07:17 +09:00
CommandAttachHelp();
return;
}
2022-01-06 23:24:05 +03:30
}
2020-09-12 14:57:06 -07:00
2022-01-06 23:24:05 +03:30
//
// Check if the process id is empty or not
//
if (TargetPid == 0)
{
ShowMessages("please specify a hex value for process id\n\n");
CommandAttachHelp();
return;
2020-08-28 04:03:12 -07:00
}
2021-03-22 18:19:39 +04:30
2022-01-06 23:24:05 +03:30
//
// Perform attach to target process
//
ShowMessages("Attaching to process 0x%llx (%lld)...\n\n", TargetPid, TargetPid);
UdAttachToProcess(TargetPid, NULL, NULL, FALSE);
2020-08-28 04:03:12 -07:00
}