2014-04-06 18:31:53 +00:00
|
|
|
/* Benjamin DELPY `gentilkiwi`
|
2020-09-17 01:17:11 +00:00
|
|
|
https://blog.gentilkiwi.com
|
2014-04-06 18:31:53 +00:00
|
|
|
benjamin@gentilkiwi.com
|
2015-08-25 09:19:01 +00:00
|
|
|
Licence : https://creativecommons.org/licenses/by/4.0/
|
2014-04-06 18:31:53 +00:00
|
|
|
*/
|
|
|
|
#include "kkll_m_memory.h"
|
|
|
|
|
2016-10-25 00:25:34 +00:00
|
|
|
NTSTATUS kkll_m_memory_search(const PUCHAR adresseBase, const PUCHAR adresseMaxMin, const UCHAR *pattern, PUCHAR *addressePattern, SIZE_T longueur)
|
2014-04-06 18:31:53 +00:00
|
|
|
{
|
|
|
|
for(*addressePattern = adresseBase; (adresseMaxMin > adresseBase) ? (*addressePattern <= adresseMaxMin) : (*addressePattern >= adresseMaxMin); *addressePattern += (adresseMaxMin > adresseBase) ? 1 : -1)
|
|
|
|
if(RtlEqualMemory(pattern, *addressePattern, longueur))
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
*addressePattern = NULL;
|
|
|
|
return STATUS_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2016-10-25 00:25:34 +00:00
|
|
|
NTSTATUS kkll_m_memory_genericPointerSearch(PUCHAR *addressePointeur, const PUCHAR adresseBase, const PUCHAR adresseMaxMin, const UCHAR *pattern, SIZE_T longueur, LONG offsetTo)
|
2014-04-06 18:31:53 +00:00
|
|
|
{
|
|
|
|
NTSTATUS status = kkll_m_memory_search(adresseBase, adresseMaxMin, pattern, addressePointeur, longueur);
|
|
|
|
if(NT_SUCCESS(status))
|
|
|
|
{
|
|
|
|
*addressePointeur += offsetTo;
|
2019-03-25 00:57:56 +00:00
|
|
|
#if defined(_M_X64)
|
2014-04-06 18:31:53 +00:00
|
|
|
*addressePointeur += sizeof(LONG) + *(PLONG)(*addressePointeur);
|
2019-03-25 00:57:56 +00:00
|
|
|
#elif defined(_M_IX86)
|
2014-04-06 18:31:53 +00:00
|
|
|
*addressePointeur = *(PUCHAR *)(*addressePointeur);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if(!*addressePointeur)
|
|
|
|
status = STATUS_INVALID_HANDLE;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
PKKLL_M_MEMORY_GENERIC kkll_m_memory_getGenericFromBuild(PKKLL_M_MEMORY_GENERIC generics, SIZE_T cbGenerics)
|
|
|
|
{
|
|
|
|
SIZE_T i;
|
|
|
|
for(i = 0; i < cbGenerics; i++)
|
|
|
|
if(generics[i].OsIndex == KiwiOsIndex)
|
|
|
|
return generics + i;
|
|
|
|
return NULL;
|
2014-06-07 19:24:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS kkll_m_memory_vm_read(PVOID Dest, PVOID From, DWORD Size)
|
|
|
|
{
|
|
|
|
NTSTATUS status = STATUS_MEMORY_NOT_ALLOCATED;
|
|
|
|
PMDL pMdl;
|
|
|
|
if(pMdl = IoAllocateMdl(From, Size, FALSE, FALSE, NULL))
|
|
|
|
{
|
|
|
|
__try
|
|
|
|
{
|
|
|
|
MmProbeAndLockPages(pMdl, KernelMode, IoReadAccess);
|
|
|
|
RtlCopyMemory(Dest, From, Size);
|
|
|
|
status = STATUS_SUCCESS;
|
|
|
|
MmUnlockPages(pMdl);
|
|
|
|
}
|
|
|
|
__except(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
status = GetExceptionCode();
|
|
|
|
}
|
|
|
|
IoFreeMdl(pMdl);
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS kkll_m_memory_vm_write(PVOID Dest, PVOID From, DWORD Size)
|
|
|
|
{
|
|
|
|
NTSTATUS status = STATUS_MEMORY_NOT_ALLOCATED;
|
|
|
|
PMDL pMdl;
|
|
|
|
if(pMdl = IoAllocateMdl(Dest, Size, FALSE, FALSE, NULL))
|
|
|
|
{
|
|
|
|
__try
|
|
|
|
{
|
|
|
|
MmProbeAndLockPages(pMdl, KernelMode, IoWriteAccess);
|
|
|
|
RtlCopyMemory(Dest, From, Size);
|
|
|
|
status = STATUS_SUCCESS;
|
|
|
|
MmUnlockPages(pMdl);
|
|
|
|
}
|
|
|
|
__except(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
status = GetExceptionCode();
|
|
|
|
}
|
|
|
|
IoFreeMdl(pMdl);
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS kkll_m_memory_vm_alloc(DWORD Size, PVOID *Addr)
|
|
|
|
{
|
|
|
|
NTSTATUS status = STATUS_DATA_NOT_ACCEPTED;
|
|
|
|
if(Addr)
|
|
|
|
{
|
|
|
|
if(*Addr = ExAllocatePoolWithTag(NonPagedPool, Size, POOL_TAG))
|
|
|
|
status = STATUS_SUCCESS;
|
|
|
|
else
|
|
|
|
status = STATUS_MEMORY_NOT_ALLOCATED;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS kkll_m_memory_vm_free(PVOID Addr)
|
|
|
|
{
|
|
|
|
NTSTATUS status = STATUS_DATA_NOT_ACCEPTED;
|
|
|
|
if(Addr)
|
|
|
|
{
|
|
|
|
ExFreePoolWithTag(Addr, POOL_TAG);
|
|
|
|
status = STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
return status;
|
2014-04-06 18:31:53 +00:00
|
|
|
}
|