#pragma once
#include "BlackBoneDef.h"
#include "Private.h"
#include "VadRoutines.h"
///
/// Allocated physical region entry
///
typedef struct _MEM_PHYS_ENTRY
{
LIST_ENTRY link;
ULONG_PTR size; // Region size
PVOID pMapped; // Mapped address
PMDL pMDL; // Related MDL
PVOID ptr; // Actual ptr in NonPagedPool
} MEM_PHYS_ENTRY, *PMEM_PHYS_ENTRY;
///
/// Per-process list of physical regions
///
typedef struct _MEM_PHYS_PROCESS_ENTRY
{
LIST_ENTRY link;
HANDLE pid; // Process ID
LIST_ENTRY pVadList; // List of mapped regions
} MEM_PHYS_PROCESS_ENTRY, *PMEM_PHYS_PROCESS_ENTRY;
extern LIST_ENTRY g_PhysProcesses;
///
/// Disable process DEP
/// Has no effect on native x64 process
///
/// Request params
/// Status code
NTSTATUS BBDisableDEP( IN PDISABLE_DEP pData );
///
/// Enable/disable process protection flag
///
/// Request params
/// Status code
NTSTATUS BBSetProtection( IN PSET_PROC_PROTECTION pProtection );
///
/// Change handle granted access
///
/// Request params
/// Status code
NTSTATUS BBGrantAccess( IN PHANDLE_GRANT_ACCESS pAccess );
///
/// Allocate/Free process memory
///
/// Request params.
/// Allocated region info.
/// Status code
NTSTATUS BBAllocateFreeMemory( IN PALLOCATE_FREE_MEMORY pAllocFree, OUT PALLOCATE_FREE_MEMORY_RESULT pResult );
///
/// Read/write process memory
///
/// Request params
/// Status code
NTSTATUS BBCopyMemory( IN PCOPY_MEMORY pCopy );
///
/// Change process memory protection
///
/// Request params
/// Status code
NTSTATUS BBProtectMemory( IN PPROTECT_MEMORY pProtect );
///
/// Hide VAD containing target address
///
/// Address info
/// Status code
NTSTATUS BBHideVAD( IN PHIDE_VAD pData );
///
/// Enumerate committed, accessible, non-guarded memory regions
///
/// Target process ID
/// Result
/// Status code
NTSTATUS BBEnumMemRegions( IN PENUM_REGIONS pData, OUT PENUM_REGIONS_RESULT pResult );
///
/// Inject dll into process
///
/// Target PID
/// TFull-qualified dll path
/// Status code
NTSTATUS BBInjectDll( IN PINJECT_DLL pData );
///
/// Change handle granted access
///
/// Request params
/// Status code
NTSTATUS BBUnlinkHandleTable( IN PUNLINK_HTABLE pUnlink );
///
/// Hook SSDT entry
///
/// SSDT index to hook
/// Hook function
/// Original function pointer
/// Status code
NTSTATUS BBHookSSDT( IN ULONG index, IN PVOID newAddr, OUT PVOID *ppOldAddr );
///
/// Restore SSDT hook
///
/// SSDT index to restore
/// Original function address
/// Status code
NTSTATUS BBRestoreSSDT( IN ULONG index, IN PVOID origAddr );
NTSTATUS BBHookInline( IN PVOID origAddr, IN PVOID newAddr );
///
/// Process termination handler
///
/// Parent PID
/// PID
/// TRUE if process was created
VOID BBProcessNotify( IN HANDLE ParentId, IN HANDLE ProcessId, IN BOOLEAN Create );
///
/// Find memory allocation process entry
///
/// Target PID
/// Found entry, NULL if not found
PMEM_PHYS_PROCESS_ENTRY BBLookupPhysProcessEntry( IN HANDLE pid );
///
/// Allocate kernel memory and map into User space. Or free previously allocated memory
///
/// Target process object
/// Request params.
/// Allocated region info.
/// Status code
NTSTATUS BBAllocateFreePhysical( IN PEPROCESS pProcess, IN PALLOCATE_FREE_MEMORY pAllocFree, OUT PALLOCATE_FREE_MEMORY_RESULT pResult );
//
// Memory allocation cleanup routines
//
void BBCleanupPhysMemEntry( IN PMEM_PHYS_ENTRY pEntry, BOOLEAN attached );
void BBCleanupProcessPhysEntry( IN PMEM_PHYS_PROCESS_ENTRY pEntry, BOOLEAN attached );
void BBCleanupProcessPhysList();