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

243 lines
9 KiB
C++
Raw Permalink Normal View History

2021-02-02 15:15:47 -08:00
/**
* @file process.cpp
2022-01-18 22:38:56 +03:30
* @author Sina Karvandi (sina@hyperdbg.org)
2021-02-02 15:15:47 -08:00
* @brief show and change process
* @details
* @version 0.1
* @date 2021-02-02
*
* @copyright This project is released under the GNU Public License v3.
*
*/
#include "pch.h"
2021-02-02 15:15:47 -08:00
//
// Global Variables
//
extern BOOLEAN g_IsSerialConnectedToRemoteDebuggee;
/**
2023-07-13 16:05:42 +09:00
* @brief help of the .process command
2021-02-02 15:15:47 -08:00
*
* @return VOID
*/
2021-03-22 18:19:39 +04:30
VOID
CommandProcessHelp()
{
2022-04-10 22:24:56 +04:30
ShowMessages(".process, .process2 : shows and changes the processes. "
"This command needs public symbols for ntoskrnl.exe if "
"you want to see the processes list. Please visit the "
"documentation to know about the difference between '.process' "
"and '.process2'.\n\n");
2022-02-09 02:16:58 +03:30
ShowMessages("syntax : \t.process\n");
ShowMessages("syntax : \t.process [list]\n");
ShowMessages("syntax : \t.process [pid ProcessId (hex)]\n");
ShowMessages("syntax : \t.process [process Eprocess (hex)]\n");
ShowMessages("syntax : \t.process2 [pid ProcessId (hex)]\n");
ShowMessages("syntax : \t.process2 [process Eprocess (hex)]\n");
ShowMessages("\n");
2021-03-22 18:19:39 +04:30
ShowMessages("\t\te.g : .process\n");
ShowMessages("\t\te.g : .process list\n");
2021-03-22 18:19:39 +04:30
ShowMessages("\t\te.g : .process pid 4\n");
ShowMessages("\t\te.g : .process2 pid 4\n");
2021-11-06 13:08:44 +03:30
ShowMessages("\t\te.g : .process process ffff948c`c2349280\n");
2021-02-02 15:15:47 -08:00
}
/**
* @brief .process command handler
*
2024-07-30 15:47:35 +09:00
* @param CommandTokens
* @param Command
2024-07-30 15:47:35 +09:00
*
2021-02-02 15:15:47 -08:00
* @return VOID
*/
2021-03-22 18:19:39 +04:30
VOID
CommandProcess(vector<CommandToken> CommandTokens, string Command)
2021-03-22 18:19:39 +04:30
{
UINT32 TargetProcessId = 0;
UINT64 TargetProcess = 0;
UINT64 AddressOfActiveProcessHead = 0; // nt!PsActiveProcessHead
UINT32 OffsetOfImageFileName = 0; // nt!_EPROCESS.ImageFileName
UINT32 OffsetOfUniqueProcessId = 0; // nt!_EPROCESS.UniqueProcessId
UINT32 OffsetOfActiveProcessLinks = 0; // nt!_EPROCESS.ActiveProcessLinks
BOOLEAN ResultOfGettingOffsets = FALSE;
BOOLEAN IsSetByClkIntr = FALSE;
DEBUGGEE_PROCESS_LIST_NEEDED_DETAILS ProcessListNeededItems = {0};
2021-02-02 15:15:47 -08:00
2024-07-30 15:47:35 +09:00
if (CommandTokens.size() >= 4)
2021-03-22 18:19:39 +04:30
{
2024-07-30 15:47:35 +09:00
ShowMessages("incorrect use of the '%s'\n\n",
GetCaseSensitiveStringFromCommandToken(CommandTokens.at(0)).c_str());
2021-03-22 18:19:39 +04:30
CommandProcessHelp();
return;
}
2021-02-02 15:15:47 -08:00
2024-07-30 15:47:35 +09:00
if (CommandTokens.size() == 1)
2021-03-22 18:19:39 +04:30
{
//
// Check if it's connected to a remote debuggee or not
2021-03-22 18:19:39 +04:30
//
if (!g_IsSerialConnectedToRemoteDebuggee)
{
//
// Get the process details in VMI mode
//
ObjectShowProcessesOrThreadDetails(TRUE);
}
else
{
//
// Send the packet to get current process
//
KdSendSwitchProcessPacketToDebuggee(DEBUGGEE_DETAILS_AND_SWITCH_PROCESS_GET_PROCESS_DETAILS,
NULL,
NULL,
FALSE,
NULL);
}
2021-03-22 18:19:39 +04:30
}
2024-07-30 15:47:35 +09:00
else if (CommandTokens.size() == 2)
{
2024-07-30 15:47:35 +09:00
if (CompareLowerCaseStrings(CommandTokens.at(1), "list"))
{
//
// Query for nt!_EPROCESS.ImageFileName, nt!_EPROCESS.UniqueProcessId,
// nt!_EPROCESS.UniqueProcessId offset from the top of nt!_EPROCESS,
// and nt!PsActiveProcessHead address and check if we find them or not,
// otherwise, it means that the PDB for ntoskrnl.exe is not available
//
if (ScriptEngineGetFieldOffsetWrapper((CHAR *)"nt!_EPROCESS", (CHAR *)"ActiveProcessLinks", &OffsetOfActiveProcessLinks) &&
ScriptEngineGetFieldOffsetWrapper((CHAR *)"nt!_EPROCESS", (CHAR *)"ImageFileName", &OffsetOfImageFileName) &&
ScriptEngineGetFieldOffsetWrapper((CHAR *)"nt!_EPROCESS", (CHAR *)"UniqueProcessId", &OffsetOfUniqueProcessId) &&
SymbolConvertNameOrExprToAddress("nt!PsActiveProcessHead", &AddressOfActiveProcessHead))
{
//
// For test offsets and addresses
//
/*
ShowMessages("Address of ActiveProcessHead : %llx\n", AddressOfActiveProcessHead);
ShowMessages("Offset Of ActiveProcessLinks : 0x%x\n", OffsetOfActiveProcessLinks);
ShowMessages("Offset Of ImageFileName : 0x%x\n", OffsetOfImageFileName);
ShowMessages("Offset Of UniqueProcessId : 0x%x\n", OffsetOfUniqueProcessId);
*/
ProcessListNeededItems.PsActiveProcessHead = AddressOfActiveProcessHead;
ProcessListNeededItems.ActiveProcessLinksOffset = OffsetOfActiveProcessLinks;
ProcessListNeededItems.ImageFileNameOffset = OffsetOfImageFileName;
ProcessListNeededItems.UniquePidOffset = OffsetOfUniqueProcessId;
2021-11-11 01:33:04 +03:30
if (!g_IsSerialConnectedToRemoteDebuggee)
{
//
// Get list of processes in VMI mode
//
2022-05-12 03:00:09 +04:30
ObjectShowProcessesOrThreadList(TRUE,
&ProcessListNeededItems,
NULL,
NULL);
}
else
{
//
// Send the packet to show list of process
//
KdSendSwitchProcessPacketToDebuggee(DEBUGGEE_DETAILS_AND_SWITCH_PROCESS_GET_PROCESS_LIST,
NULL,
NULL,
FALSE,
&ProcessListNeededItems);
}
}
else
{
ShowMessages("err, the need offset to iterate over processes not found, "
"make sure to load ntoskrnl.exe's PDB file. use '.help .sym' for "
"more information\n");
return;
}
}
else
{
ShowMessages(
"err, unknown parameter at '%s'\n\n",
2024-07-30 15:47:35 +09:00
GetCaseSensitiveStringFromCommandToken(CommandTokens.at(1)).c_str());
CommandProcessHelp();
return;
}
}
2024-07-30 15:47:35 +09:00
else if (CommandTokens.size() == 3)
2021-03-22 18:19:39 +04:30
{
//
// Check if it's connected to a remote debuggee or not
//
if (!g_IsSerialConnectedToRemoteDebuggee)
{
ShowMessages("err, you're not connected to any debuggee in Debugger Mode, "
"you can use the '.attach', or the '.detach' commands if you're "
"operating in VMI Mode\n");
return;
}
2024-07-30 15:47:35 +09:00
if (CompareLowerCaseStrings(CommandTokens.at(1), "pid"))
2021-11-06 13:08:44 +03:30
{
2024-07-30 15:47:35 +09:00
if (!ConvertTokenToUInt32(CommandTokens.at(2), &TargetProcessId))
2021-11-06 13:08:44 +03:30
{
ShowMessages(
"please specify a correct hex value for the process id that you "
"want to operate on it\n\n");
CommandProcessHelp();
return;
}
}
2024-07-30 15:47:35 +09:00
else if (CompareLowerCaseStrings(CommandTokens.at(1), "process"))
2021-11-06 13:08:44 +03:30
{
2024-07-30 15:47:35 +09:00
if (!SymbolConvertNameOrExprToAddress(GetCaseSensitiveStringFromCommandToken(CommandTokens.at(2)), &TargetProcess))
2021-11-06 13:08:44 +03:30
{
ShowMessages(
"please specify a correct hex value for the process (nt!_EPROCESS) that you "
"want to operate on it\n\n");
CommandProcessHelp();
return;
}
}
else
2021-03-22 18:19:39 +04:30
{
ShowMessages(
2021-11-06 13:08:44 +03:30
"err, unknown parameter at '%s'\n\n",
2024-07-30 15:47:35 +09:00
GetCaseSensitiveStringFromCommandToken(CommandTokens.at(2)).c_str());
2021-03-22 18:19:39 +04:30
CommandProcessHelp();
return;
}
2021-02-02 15:15:47 -08:00
//
// Check for switching method
//
2024-07-30 15:47:35 +09:00
if (CompareLowerCaseStrings(CommandTokens.at(0), ".process2"))
{
IsSetByClkIntr = FALSE;
}
else
{
IsSetByClkIntr = TRUE;
}
2021-03-22 18:19:39 +04:30
//
// Send the packet to change process
//
KdSendSwitchProcessPacketToDebuggee(DEBUGGEE_DETAILS_AND_SWITCH_PROCESS_PERFORM_SWITCH,
2021-11-08 13:51:53 +03:30
TargetProcessId,
TargetProcess,
IsSetByClkIntr,
NULL);
2021-03-22 18:19:39 +04:30
}
else
{
ShowMessages("invalid parameter\n\n");
CommandProcessHelp();
return;
}
2021-02-02 15:15:47 -08:00
}