HyperDbg Debugger
Loading...
Searching...
No Matches
dump.cpp File Reference
#include "pch.h"

Functions

VOID CommandDumpHelp ()
 help of the .dump command
VOID CommandDump (vector< CommandToken > CommandTokens, string Command)
 .dump command handler
VOID CommandDumpSaveIntoFile (PVOID Buffer, UINT32 Length)
 Saves the received buffers into the files.

Variables

BOOLEAN g_IsSerialConnectedToRemoteDebuggee
 Shows if the debugger was connected to remote debuggee over (A remote guest).
ACTIVE_DEBUGGING_PROCESS g_ActiveProcessDebuggingState
 State of active debugging thread.
HANDLE DumpFileHandle
 Holds the handle of the dump file.

Function Documentation

◆ CommandDump()

VOID CommandDump ( vector< CommandToken > CommandTokens,
string Command )

.dump command handler

Parameters
CommandTokens
Command
Returns
VOID
63{
64 wstring Filepath;
65 UINT32 ActualLength;
66 UINT32 Iterator;
67 UINT32 Pid = 0;
68 UINT32 Length = 0;
69 UINT64 StartAddress = 0;
70 UINT64 EndAddress = 0;
71 BOOLEAN IsFirstCommand = TRUE;
72 BOOLEAN NextIsProcId = FALSE;
73 BOOLEAN NextIsPath = FALSE;
74 BOOLEAN IsTheFirstAddr = FALSE;
75 BOOLEAN IsTheSecondAddr = FALSE;
76 BOOLEAN IsDumpPathSpecified = FALSE;
77 string FirstCommand = GetLowerStringFromCommandToken(CommandTokens.front());
79
80 if (CommandTokens.size() <= 4)
81 {
82 ShowMessages("err, incorrect use of the '.dump' command\n\n");
84 return;
85 }
86
87 //
88 // By default if the user-debugger is active, we use these commands
89 // on the memory layout of the debuggee process
90 //
92 {
93 Pid = g_ActiveProcessDebuggingState.ProcessId;
94 }
95
96 for (auto Section : CommandTokens)
97 {
98 if (IsFirstCommand == TRUE)
99 {
100 IsFirstCommand = FALSE;
101 continue;
102 }
103 else if (NextIsProcId)
104 {
105 if (!ConvertTokenToUInt32(Section, &Pid))
106 {
107 ShowMessages("please specify a correct hex value for process id\n\n");
109 return;
110 }
111 NextIsProcId = FALSE;
112 continue;
113 }
114 else if (NextIsPath)
115 {
116 //
117 // Convert path to wstring
118 //
120 IsDumpPathSpecified = TRUE;
121
122 NextIsPath = FALSE;
123 }
124 else if (CompareLowerCaseStrings(Section, "pid"))
125 {
126 NextIsProcId = TRUE;
127 continue;
128 }
129 else if (CompareLowerCaseStrings(Section, "path"))
130 {
131 NextIsPath = TRUE;
132 continue;
133 }
134 //
135 // Check the 'From' address
136 //
137 else if (!IsTheFirstAddr &&
139 {
140 IsTheFirstAddr = TRUE;
141 }
142 //
143 // Check the 'To' address
144 //
145 else if (!IsTheSecondAddr &&
147 {
148 IsTheSecondAddr = TRUE;
149 }
150 else
151 {
152 //
153 // invalid input
154 //
155 ShowMessages("err, couldn't resolve error at '%s'\n\n",
158
159 return;
160 }
161 }
162
163 //
164 // Check if 'pid' is not specified
165 //
166 if (NextIsProcId)
167 {
168 ShowMessages("please specify a correct hex value for process id\n\n");
170 return;
171 }
172
173 //
174 // Check if 'path' is either specified, not completely specified
175 //
176 if (NextIsPath || !IsDumpPathSpecified)
177 {
178 ShowMessages("please specify a correct path for saving the dump\n\n");
180 return;
181 }
182
183 //
184 // Check if start address or end address is null
185 //
186 if (!IsTheFirstAddr || !IsTheSecondAddr)
187 {
188 ShowMessages("err, please specify the start and end address in hex format\n");
189 return;
190 }
191
192 //
193 // Check if end address is bigger than start address
194 //
195 if (StartAddress >= EndAddress)
196 {
197 ShowMessages("err, please note that the 'to' address should be greater than the 'from' address\n");
198 return;
199 }
200
201 //
202 // Check to prevent using process id in d* and u* commands
203 //
205 {
207 return;
208 }
209
210 if (Pid == 0)
211 {
212 //
213 // Default process we read from current process
214 //
216 }
217
218 //
219 // Check whether it's physical or virtual address
220 //
221 if (!FirstCommand.compare("!dump"))
222 {
224 }
225
226 //
227 // Create or open the file for writing the dump file
228 //
229 // TEMPORARY LINUX SHIM (same as in pe.cpp): std::wstring stores native
230 // wchar_t (4 bytes on Linux), but the HyperDbg WCHAR type is 2 bytes
231 // (UINT16) and PlatformOpenFileForWriting takes a const WCHAR *. On Linux
232 // the cast is a bogus 2-byte reinterpretation, acceptable only because
233 // Linux file I/O is still stubbed (the path is never opened). On Windows
234 // WCHAR == wchar_t, so it is a plain correct pointer. TODO(Linux): remove
235 // once real file I/O lands and convert wchar_t -> 2-byte WCHAR properly.
236 //
237#ifdef __linux__
238 DumpFileHandle = PlatformOpenFileForWriting((const WCHAR *)Filepath.c_str());
239#else
240 DumpFileHandle = PlatformOpenFileForWriting(Filepath.c_str());
241#endif
242
243 if (DumpFileHandle == INVALID_HANDLE_VALUE)
244 {
245 ShowMessages("err, unable to create or open the file\n");
246 return;
247 }
248
249 //
250 // Compute the length
251 //
252 Length = (UINT32)(EndAddress - StartAddress);
253
254 ActualLength = NULL;
255 Iterator = Length / PAGE_SIZE;
256
257 for (SIZE_T i = 0; i <= Iterator; i++)
258 {
259 UINT64 Address = StartAddress + (i * PAGE_SIZE);
260
261 if (Length >= PAGE_SIZE)
262 {
263 ActualLength = PAGE_SIZE;
264 }
265 else
266 {
267 ActualLength = Length;
268 }
269
270 Length -= ActualLength;
271
272 if (ActualLength != 0)
273 {
274 // ShowMessages("address: 0x%llx | actual length: 0x%llx\n", Address, ActualLength);
275
278 Address,
279 MemoryType,
281 Pid,
282 ActualLength,
283 NULL);
284 }
285 }
286
287 //
288 // Close the file handle if it's not already closed
289 //
290 if (DumpFileHandle != NULL)
291 {
294 }
295
296 ShowMessages("the dump file is saved at: %ls\n", Filepath.c_str());
297}
BOOLEAN g_IsSerialConnectedToRemoteDebuggee
Shows if the debugger was connected to remote debuggee over (A remote guest).
Definition globals.h:253
ACTIVE_DEBUGGING_PROCESS g_ActiveProcessDebuggingState
State of active debugging thread.
Definition globals.h:372
UCHAR BOOLEAN
Definition BasicTypes.h:35
#define TRUE
Definition BasicTypes.h:114
#define FALSE
Definition BasicTypes.h:113
unsigned int UINT32
Definition BasicTypes.h:54
@ READ_FROM_KERNEL
Definition RequestStructures.h:245
enum _DEBUGGER_READ_MEMORY_TYPE DEBUGGER_READ_MEMORY_TYPE
different type of addresses
@ DEBUGGER_SHOW_COMMAND_DUMP
Definition RequestStructures.h:284
@ DEBUGGER_READ_PHYSICAL_ADDRESS
Definition RequestStructures.h:255
@ DEBUGGER_READ_VIRTUAL_ADDRESS
Definition RequestStructures.h:256
std::string GetCaseSensitiveStringFromCommandToken(CommandToken TargetToken)
Get case sensitive string from command token.
Definition common.cpp:467
VOID StringToWString(std::wstring &ws, const std::string &s)
convert std::string to std::wstring
Definition common.cpp:850
std::string GetLowerStringFromCommandToken(CommandToken TargetToken)
Get lower case string from command token.
Definition common.cpp:484
BOOLEAN CompareLowerCaseStrings(CommandToken TargetToken, const CHAR *StringToCompare)
Compare lower case strings.
Definition common.cpp:503
BOOLEAN ConvertTokenToUInt32(CommandToken TargetToken, PUINT32 Result)
check and convert command token to a 32 bit unsigned integer
Definition common.cpp:546
VOID CommandDumpHelp()
help of the .dump command
Definition dump.cpp:36
HANDLE DumpFileHandle
Holds the handle of the dump file.
Definition dump.cpp:28
#define ASSERT_MESSAGE_CANNOT_SPECIFY_PID
Definition common.h:39
#define PAGE_SIZE
Size of each page (4096 bytes).
Definition common.h:80
NULL()
Definition test-case-generator.py:530
HANDLE PlatformOpenFileForWriting(const WCHAR *Path)
Platform independent wrapper to create/open a file for writing.
Definition platform-lib-calls.c:392
BOOLEAN PlatformCloseFile(HANDLE FileHandle)
Platform independent wrapper to close a file opened by PlatformOpenFileForWriting.
Definition platform-lib-calls.c:440
UINT32 PlatformGetCurrentProcessId(VOID)
Platform independent wrapper for GetCurrentProcessId / getpid.
Definition platform-lib-calls.c:185
VOID HyperDbgShowMemoryOrDisassemble(DEBUGGER_SHOW_MEMORY_STYLE Style, UINT64 Address, DEBUGGER_READ_MEMORY_TYPE MemoryType, DEBUGGER_READ_READING_TYPE ReadingType, UINT32 Pid, UINT32 Size, PDEBUGGER_DT_COMMAND_OPTIONS DtDetails)
Show memory or disassembler.
Definition readmem.cpp:193
BOOLEAN SymbolConvertNameOrExprToAddress(const string &TextToConvert, PUINT64 Result)
check and convert string to a 64 bit unsigned integer and also check for symbol object names and eval...
Definition symbol.cpp:359

◆ CommandDumpHelp()

VOID CommandDumpHelp ( )

help of the .dump command

Returns
VOID
37{
38 ShowMessages(".dump & !dump : saves memory context into a file.\n\n");
39
40 ShowMessages("syntax : \t.dump [FromAddress (hex)] [ToAddress (hex)] [pid ProcessId (hex)] [path Path (string)]\n");
41 ShowMessages("\nIf you want to dump physical memory then add '!' at the "
42 "start of the command\n\n");
43
44 ShowMessages("\n");
45 ShowMessages("\t\te.g : .dump 401000 40b000 path c:\\rev\\dump1.dmp\n");
46 ShowMessages("\t\te.g : .dump 401000 40b000 pid 1c0 path c:\\rev\\desktop\\dump2.dmp\n");
47 ShowMessages("\t\te.g : .dump fffff801deadb000 fffff801deade054 path c:\\rev\\dump3.dmp\n");
48 ShowMessages("\t\te.g : .dump fffff801deadb000 fffff801deade054 path c:\\rev\\dump4.dmp\n");
49 ShowMessages("\t\te.g : .dump 00007ff8349f2000 00007ff8349f8000 path c:\\rev\\dump5.dmp\n");
50 ShowMessages("\t\te.g : .dump @rax+@rcx @rax+@rcx+1000 path c:\\rev\\dump6.dmp\n");
51 ShowMessages("\t\te.g : !dump 1000 2100 path c:\\rev\\dump7.dmp\n");
52}

◆ CommandDumpSaveIntoFile()

VOID CommandDumpSaveIntoFile ( PVOID Buffer,
UINT32 Length )

Saves the received buffers into the files.

Parameters
Buffer
Length
Returns
VOID
309{
310 //
311 // Check if handle is valid
312 //
313 if (DumpFileHandle == NULL)
314 {
315 ShowMessages("err, invalid handle for saving the dump buffer is specified\n");
316 return;
317 }
318
319 //
320 // Write the buffer into the dump file
321 //
322 if (!PlatformWriteFile(DumpFileHandle, Buffer, Length))
323 {
324 ShowMessages("err, unable to write buffer into the dump\n");
325
328
329 return;
330 }
331}
BOOLEAN PlatformWriteFile(HANDLE FileHandle, const VOID *Buffer, DWORD NumberOfBytes)
Platform independent wrapper to write a buffer to an open file.
Definition platform-lib-calls.c:420

Variable Documentation

◆ DumpFileHandle

HANDLE DumpFileHandle

Holds the handle of the dump file.

◆ g_ActiveProcessDebuggingState

ACTIVE_DEBUGGING_PROCESS g_ActiveProcessDebuggingState
extern

State of active debugging thread.

372{0};

◆ g_IsSerialConnectedToRemoteDebuggee

BOOLEAN g_IsSerialConnectedToRemoteDebuggee
extern

Shows if the debugger was connected to remote debuggee over (A remote guest).