HyperDbg Debugger
Loading...
Searching...
No Matches
hwdbg-scripts.h File Reference

Headers for the hardware scripts for hwdbg. More...

Go to the source code of this file.

Functions

VOID HwdbgScriptPrintScriptBuffer (CHAR *ScriptBuffer, UINT32 ScriptBufferSize)
 Print the actual script.
BOOLEAN HwdbgScriptCreateHwdbgScript (CHAR *ScriptBuffer, UINT32 ScriptBufferSize, const TCHAR *HardwareScriptFilePathToSave)
 Create hwdbg script.
BOOLEAN HwdbgScriptSendScriptPacket (HWDBG_INSTANCE_INFORMATION *InstanceInfo, const TCHAR *FileName, UINT32 NumberOfSymbols, HWDBG_SHORT_SYMBOL *Buffer, UINT32 BufferLength)
 Sends a HyperDbg (hwdbg) script packet to the hwdbg.
BOOLEAN HwdbgScriptGetScriptBufferFromRawString (string ScriptString, PVOID *CodeBuffer, UINT64 *BufferAddress, UINT32 *BufferLength, UINT32 *Pointer)
BOOLEAN HwdbgScriptRunScript (const CHAR *Script, const TCHAR *InstanceFilePathToRead, const TCHAR *HardwareScriptFilePathToSave, UINT32 InitialBramBufferSize)
 Run script in hwdbg.

Detailed Description

Headers for the hardware scripts for hwdbg.

Author
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Version
0.11
Date
2024-09-30

Function Documentation

◆ HwdbgScriptCreateHwdbgScript()

BOOLEAN HwdbgScriptCreateHwdbgScript ( CHAR * ScriptBuffer,
UINT32 ScriptBufferSize,
const TCHAR * HardwareScriptFilePathToSave )

Create hwdbg script.

Parameters
ScriptBuffer
ScriptBufferSize
HardwareScriptFilePathToSave
Returns
BOOLEAN
236{
237 UINT32 NumberOfStagesForScript = 0;
238 UINT32 NumberOfOperandsImplemented = 0;
239 UINT32 NumberOfOperandsForScript = 0;
240 SIZE_T NewCompressedBufferSize = 0;
241 SIZE_T NumberOfNeededFlipFlopsInTargetDevice = 0;
242 SIZE_T NumberOfBytesPerChunk = 0;
243 HWDBG_SHORT_SYMBOL * NewScriptBuffer = NULL;
244
245 //
246 // *** Check the script capabilities with the generated script ***
247 //
249 ScriptBuffer,
250 ScriptBufferSize / sizeof(SYMBOL),
251 &NumberOfStagesForScript,
252 &NumberOfOperandsForScript,
253 &NumberOfOperandsImplemented))
254 {
255 ShowMessages("\n[-] target script is NOT supported by this instance of hwdbg!\n");
256 return FALSE;
257 }
258 else
259 {
260 ShowMessages("\n[+] target script is supported by this instance of hwdbg!\n");
261 }
262
263 //
264 // *** Compress the script buffer based on the instance info ***
265 //
267 (SYMBOL *)ScriptBuffer,
268 ScriptBufferSize,
269 NumberOfStagesForScript,
270 &NewScriptBuffer,
271 &NewCompressedBufferSize,
272 &NumberOfBytesPerChunk))
273 {
274 ShowMessages("err, unable to compress the script buffer\n");
275 return FALSE;
276 }
277
278 //
279 // Print the hwdbg script buffer
280 //
282 NewCompressedBufferSize,
283 NumberOfStagesForScript,
284 NumberOfOperandsForScript,
285 NewScriptBuffer,
286 NumberOfNeededFlipFlopsInTargetDevice,
287 NumberOfBytesPerChunk,
288 NumberOfOperandsImplemented);
289
290 //
291 // *** Write script configuration packet into a file ***
292 //
293
294 //
295 // Write script configuration packet into a file
296 //
298 HardwareScriptFilePathToSave,
299 NumberOfStagesForScript,
300 NumberOfOperandsImplemented,
301 NewScriptBuffer,
302 NewCompressedBufferSize))
303 {
304 ShowMessages("err, unable to write script buffer\n");
305 return FALSE;
306 }
307
308 //
309 // *** Free the allocated memory ***
310 //
311
312 //
313 // Free the allocated memory for the short symbol buffer
314 //
315 if (NewScriptBuffer != NULL)
316 {
318 }
319
320 //
321 // The script buffer is created successfully
322 //
323 return TRUE;
324}
#define TRUE
Definition BasicTypes.h:114
#define FALSE
Definition BasicTypes.h:113
unsigned int UINT32
Definition BasicTypes.h:54
IMPORT_EXPORT_HYPERDBG_SCRIPT_ENGINE BOOLEAN HardwareScriptInterpreterCheckScriptBufferWithScriptCapabilities(HWDBG_INSTANCE_INFORMATION *InstanceInfo, PVOID ScriptBuffer, UINT32 CountOfScriptSymbolChunks, UINT32 *NumberOfStages, UINT32 *NumberOfOperands, UINT32 *NumberOfOperandsImplemented)
Check the script capabilities with the target script buffer.
Definition hardware.c:82
IMPORT_EXPORT_HYPERDBG_SCRIPT_ENGINE VOID HardwareScriptInterpreterFreeHwdbgShortSymbolBuffer(HWDBG_SHORT_SYMBOL *NewShortSymbolBuffer)
Function free the short symbol buffer.
Definition hardware.c:706
HWDBG_INSTANCE_INFORMATION g_HwdbgInstanceInfo
Instance information of the current hwdbg debuggee.
Definition globals.h:691
VOID HwdbgScriptPrintFinalScriptBufferAndHardwareDetails(HWDBG_INSTANCE_INFORMATION *InstanceInfo, SIZE_T NewCompressedBufferSize, UINT32 NumberOfStagesForScript, UINT32 NumberOfOperandsForScript, HWDBG_SHORT_SYMBOL *NewScriptBuffer, SIZE_T NumberOfNeededFlipFlopsInTargetDevice, SIZE_T NumberOfBytesPerChunk, UINT32 NumberOfOperandsImplemented)
Print the hwdbg script buffer and hardware details.
Definition hwdbg-scripts.cpp:150
BOOLEAN HwdbgScriptCompressScriptBuffer(HWDBG_INSTANCE_INFORMATION *InstanceInfo, SYMBOL *ScriptBuffer, SIZE_T ScriptBufferSize, UINT32 NumberOfStagesForScript, HWDBG_SHORT_SYMBOL **NewScriptBuffer, SIZE_T *NewCompressedBufferSize, SIZE_T *NumberOfBytesPerChunk)
Compress the script buffer.
Definition hwdbg-scripts.cpp:61
BOOLEAN HwdbgScriptWriteScriptConfigurationPacketIntoFile(HWDBG_INSTANCE_INFORMATION *InstanceInfo, const CHAR *FileName, UINT32 NumberOfStagesForScript, UINT32 NumberOfOperandsImplemented, HWDBG_SHORT_SYMBOL *NewScriptBuffer, SIZE_T NewCompressedBufferSize)
Write script configuration packet into a file.
Definition hwdbg-scripts.cpp:191
NULL()
Definition test-case-generator.py:530
Definition ScriptEngineCommonDefinitions.h:16
Definition ScriptEngineCommonDefinitions.h:6

◆ HwdbgScriptGetScriptBufferFromRawString()

BOOLEAN HwdbgScriptGetScriptBufferFromRawString ( string ScriptString,
PVOID * CodeBuffer,
UINT64 * BufferAddress,
UINT32 * BufferLength,
UINT32 * Pointer )

Get script buffer from raw string

Parameters
ScriptBuffer
CodeBuffer
BufferAddress
BufferLength
Pointer
Returns
BOOLEAN
341{
342 PVOID ResultingCodeBuffer = NULL;
343
344 //
345 // Run script engine handler
346 //
347 ResultingCodeBuffer = ScriptEngineParseWrapper((CHAR *)ScriptString.c_str(), TRUE);
348
349 if (ResultingCodeBuffer == NULL)
350 {
351 //
352 // return to show that this item contains an script error
353 //
354 return FALSE;
355 }
356
357 //
358 // Print symbols (test)
359 //
360 // PrintSymbolBufferWrapper(ResultingCodeBuffer);
361
362 //
363 // Set the buffer and length
364 //
365 *BufferAddress = ScriptEngineWrapperGetHead(ResultingCodeBuffer);
366 *BufferLength = ScriptEngineWrapperGetSize(ResultingCodeBuffer);
367 *Pointer = ScriptEngineWrapperGetPointer(ResultingCodeBuffer);
368
369 //
370 // Set the code buffer
371 //
372 *CodeBuffer = ResultingCodeBuffer;
373
374 //
375 // The script buffer is copied successfully
376 //
377 return TRUE;
378}
void * PVOID
Definition BasicTypes.h:56
char CHAR
Definition BasicTypes.h:33
UINT32 ScriptEngineWrapperGetSize(PVOID SymbolBuffer)
wrapper for getting size
Definition script-engine-wrapper.cpp:815
UINT32 ScriptEngineWrapperGetPointer(PVOID SymbolBuffer)
wrapper for getting pointer
Definition script-engine-wrapper.cpp:829
PVOID ScriptEngineParseWrapper(CHAR *Expr, BOOLEAN ShowErrorMessageIfAny)
ScriptEngineParse wrapper.
Definition script-engine-wrapper.cpp:298
UINT64 ScriptEngineWrapperGetHead(PVOID SymbolBuffer)
wrapper for getting head
Definition script-engine-wrapper.cpp:803

◆ HwdbgScriptPrintScriptBuffer()

VOID HwdbgScriptPrintScriptBuffer ( CHAR * ScriptBuffer,
UINT32 ScriptBufferSize )

Print the actual script.

Parameters
ScriptBuffer
ScriptBufferSize
Returns
VOID
30{
31 //
32 // Print the actual script
33 //
34 ShowMessages("\nHyperDbg (general) script buffer (size=%d, flip-flops (just script)=%d):\n\n",
35 ScriptBufferSize,
36 ScriptBufferSize * 8 // Converted to bits
37 );
38
39 for (SIZE_T i = 0; i < ScriptBufferSize; i++)
40 {
41 ShowMessages("%02X ", (UINT8)ScriptBuffer[i]);
42 }
43
44 ShowMessages("\n");
45}
unsigned char UINT8
Definition BasicTypes.h:52

◆ HwdbgScriptRunScript()

BOOLEAN HwdbgScriptRunScript ( const CHAR * Script,
const TCHAR * InstanceFilePathToRead,
const TCHAR * HardwareScriptFilePathToSave,
UINT32 InitialBramBufferSize )

Run script in hwdbg.

Parameters
Script
InstanceFilePathToRead
HardwareScriptFilePathToSave
InitialBramBufferSize
Returns
BOOLEAN
464{
465 PVOID CodeBuffer;
466 UINT64 BufferAddress;
467 UINT32 BufferLength;
468 UINT32 Pointer;
469
470 //
471 // Load the instance info
472 //
473 if (!HwdbgLoadInstanceInfo(InstanceFilePathToRead, InitialBramBufferSize))
474 {
475 //
476 // Unable to load the instance info
477 //
478 return FALSE;
479 }
480
481 //
482 // Get the script buffer from the raw string (script)
483 //
485 &CodeBuffer,
486 &BufferAddress,
487 &BufferLength,
488 &Pointer))
489 {
490 //
491 // Unable to get script buffer from script
492 //
493 return FALSE;
494 }
495
496 //
497 // Print the actual script
498 //
499 HwdbgScriptPrintScriptBuffer((CHAR *)BufferAddress, BufferLength);
500
501 //
502 // Create hwdbg script
503 //
504 if (!HwdbgScriptCreateHwdbgScript((CHAR *)BufferAddress,
505 BufferLength,
506 HardwareScriptFilePathToSave))
507 {
508 ShowMessages("err, unable to create hwdbg script\n");
509 return FALSE;
510 }
511
512 //
513 // Return the result
514 //
515 return TRUE;
516}
BOOLEAN HwdbgLoadInstanceInfo(const TCHAR *InstanceFilePathToRead, UINT32 InitialBramBufferSize)
Load the instance info.
Definition hwdbg-interpreter.cpp:648
VOID HwdbgScriptPrintScriptBuffer(CHAR *ScriptBuffer, UINT32 ScriptBufferSize)
Print the actual script.
Definition hwdbg-scripts.cpp:29
BOOLEAN HwdbgScriptGetScriptBufferFromRawString(string ScriptString, PVOID *CodeBuffer, UINT64 *BufferAddress, UINT32 *BufferLength, UINT32 *Pointer)
Definition hwdbg-scripts.cpp:336
BOOLEAN HwdbgScriptCreateHwdbgScript(CHAR *ScriptBuffer, UINT32 ScriptBufferSize, const TCHAR *HardwareScriptFilePathToSave)
Create hwdbg script.
Definition hwdbg-scripts.cpp:233

◆ HwdbgScriptSendScriptPacket()

BOOLEAN HwdbgScriptSendScriptPacket ( HWDBG_INSTANCE_INFORMATION * InstanceInfo,
const TCHAR * FileName,
UINT32 NumberOfSymbols,
HWDBG_SHORT_SYMBOL * Buffer,
UINT32 BufferLength )

Sends a HyperDbg (hwdbg) script packet to the hwdbg.

Parameters
InstanceInfo
FileName
Buffer
BufferLength
Returns
BOOLEAN
396{
397 HWDBG_SCRIPT_BUFFER ScriptBuffer = {0};
398 BOOLEAN Result = FALSE;
399
400 //
401 // Make the packet's structure
402 //
403 ScriptBuffer.scriptNumberOfSymbols = NumberOfSymbols;
404
405 //
406 // Allocate a buffer for storing the header packet + buffer (if not empty)
407 //
408 CHAR * FinalBuffer = (CHAR *)malloc(BufferLength + sizeof(HWDBG_SCRIPT_BUFFER));
409
410 if (!FinalBuffer)
411 {
412 return FALSE;
413 }
414
415 RtlZeroMemory(FinalBuffer, BufferLength + sizeof(HWDBG_SCRIPT_BUFFER));
416
417 //
418 // Copy the packet into the FinalBuffer
419 //
420 memcpy(FinalBuffer, &ScriptBuffer, sizeof(HWDBG_SCRIPT_BUFFER));
421
422 //
423 // Copy the buffer (if available) into the FinalBuffer
424 //
425 if (Buffer != NULL)
426 {
427 memcpy(FinalBuffer + sizeof(HWDBG_SCRIPT_BUFFER), Buffer, BufferLength);
428 }
429
430 //
431 // Here we would send FinalBuffer to the hardware debugger
432 //
434 InstanceInfo,
435 FileName,
438 FinalBuffer,
439 BufferLength + sizeof(HWDBG_SCRIPT_BUFFER));
440
441 //
442 // Free the allocated memory after use
443 //
444 free(FinalBuffer);
445
446 return Result;
447}
UCHAR BOOLEAN
Definition BasicTypes.h:35
@ DEBUGGER_REMOTE_PACKET_TYPE_DEBUGGER_TO_DEBUGGEE_HARDWARE_LEVEL
Definition Connection.h:183
@ hwdbgActionConfigureScriptBuffer
Definition HardwareDebugger.h:68
struct _HWDBG_SCRIPT_BUFFER HWDBG_SCRIPT_BUFFER
The structure of script buffer in hwdbg.
BOOLEAN HwdbgInterpreterSendPacketAndBufferToHwdbg(HWDBG_INSTANCE_INFORMATION *InstanceInfo, const TCHAR *FileName, DEBUGGER_REMOTE_PACKET_TYPE PacketType, HWDBG_ACTION_ENUMS RequestedAction, CHAR *Buffer, UINT32 BufferLength)
Sends a HyperDbg packet + a buffer to the hwdbg.
Definition hwdbg-interpreter.cpp:398
UINT32 scriptNumberOfSymbols
Definition HardwareDebugger.h:196