I have tried the following two methods,both of them can write read only memory.If i don’t not use the following methods, and try to write the memory(assume the system space memory like nt or hal data structure and so on), i got BSOD:
Bug Check 0xBE: ATTEMPTED_WRITE_TO_READONLY_MEMORY
Methord one:
function WriteReadOnlyMemoryMark(lpDest, lpSource: Pointer; Length: Integer):
NTSTATUS;
var
tempSpinLock: KSPIN_LOCK;
oldirql: KIRQL;
mdl: PMDL;
writableAddress: Pointer;
begin
Result := STATUS_UNSUCCESSFUL;
mdl := IoAllocateMdl(lpDest, Length, False, False, nil);
if (mdl <> nil) then
begin
MmBuildMdlForNonPagedPool(mdl);
MmProbeAndLockPages(mdl, KernelMode, IoWriteAccess);
writableAddress := MmMapLockedPages(mdl, KernelMode);
if (writableAddress <> nil) then
begin
oldirql := 0;
KeInitializeSpinLock(@tempSpinLock);
fast_KfAcquireSpinLock(@tempSpinLock);
memcpy(writableAddress, lpSource, Length);
fast_KfReleaseSpinLock(@tempSpinLock, oldirql);
MmUnmapLockedPages(writableAddress, mdl);
Result := STATUS_SUCCESS;
end;
MmUnlockPages(mdl);
IoFreeMdl(mdl);
end;
end;
Methord two:
function WriteReadOnlyMemoryGates(lpDest, lpSource: Pointer; Length: Integer):
NTSTATUS;
var
tempSpinLock: KSPIN_LOCK;
oldirql: KIRQL;
mdl: PMDL;
writableAddress: Pointer;
begin
Result := STATUS_UNSUCCESSFUL;
mdl := MmCreateMdl(nil, lpDest, Length);
if (mdl <> nil) then
begin
MmBuildMdlForNonPagedPool(mdl);
mdl^.MdlFlags := mdl^.MdlFlags or MDL_MAPPED_TO_SYSTEM_VA;
writableAddress := MmMapLockedPages(mdl, KernelMode);
if (writableAddress <> nil) then
begin
oldirql := 0;
KeInitializeSpinLock(@tempSpinLock);
fast_KfAcquireSpinLock(@tempSpinLock);
memcpy(writableAddress, lpSource, Length);
fast_KfReleaseSpinLock(@tempSpinLock, oldirql);
MmUnmapLockedPages(writableAddress, mdl);
Result := STATUS_SUCCESS;
end;
MmUnlockPages(mdl);
IoFreeMdl(mdl);
end;
end;
Now my question is:
1 why the methords mentioned above can let the memory be writable?
Does the functions change the propertries(like x86 CPU defined) of the physical page of the memory? This refers to memory management and protection mechanism of windows, one word can not classifies clearly.
2 Will changed memory be writen to the disk image(not page file on the disk)? If this is truth,then the disk image of the system key files(like hal.dll and so on) may be destroyed(I assume the WFP is disabled)?
Just for research purpose, so the more details supplied(don’t say the OS do it, i do know cpu and os do it:)), the better i think. Any articles about those aspect will be highly appreciated too.
Thanks very much.
Ma