gpt4 book ai didi

windows - 具有 Virtualprotect 问题的自修改算法

转载 作者:可可西里 更新时间:2023-11-01 10:09:09 28 4
gpt4 key购买 nike

我在使用 Windows 的 Virtualprotect() api 时遇到问题。我从学校得到了一个作业,我的老师告诉我们,在过去内存力稀缺且成本高昂的时候。程序员必须创建可以动态修改自身以节省内存的高级算法。好了,我们现在必须编写这样一个算法,它不一定是有效的,但它必须 self 修改。

所以我开始这样做,而且我认为在寻求任何帮助之前我已经做得很好了。

我的程序是这样运行的:

我有一个函数和一个带有内置堆栈溢出的循环。堆栈溢出了在循环期间构造的代码所在的内存位置的地址。控制被传递给内存中的代码。代码加载一个 dll 然后退出,但在退出之前它必须修复循环。这是我们分配的条件之一,必须恢复原始循环中更改的所有内容。

问题是我没有对循环的写访问权限,只有 READ_EXECUTE,所以我认为要更改我的访问权限,我使用了 virtualprotect。但是那个函数返回了一个错误:

ERROR_NOACCESS,关于这个错误的文档非常少,windows只说:Invailid access to memory address。哪些数字是因为我首先想更改访问权限。那怎么了?这是在内存中构建的代码:我的代码中所有数据的名称有点模糊,所以我提供了一些注释

Size1: 
TrapData proc

jmp pLocals
LocalDllName db 100 dup(?) ; name of the dll to be called ebx-82h
RestoreBuffer db 5 dup(?) ; previous bytes at the overflow location
LoadAddress dd 0h ; ebx - 19h ; address to kernel32.loadlibrary
RestoreAddress dd 0h ; ebx - 15h ; address to restore (with the restore buffer)
AddressToRestoreBuffer dd 0h ; ebx - 11h ; obsolete, I don't use this one
AddressToLea dd 0h ; ebx - 0Dh Changed, address to kernel32.virutalprotect
AddressToReturnTo dd 0h ; ebx - 9h address to return execution to(the same as RestoreAddress
pLocals:


call Refpnt
Refpnt: pop ebx ; get current address in ebx

push ebx
mov eax, ebx

sub ebx, 82h
push ebx ; dll name

sub eax, 19h ; load lib address
mov eax, [eax]
call eax


pop ebx ; Current address
push ebx


;BOOL WINAPI VirtualProtect(
; __in LPVOID lpAddress,
; __in SIZE_T dwSize,
; __in DWORD flNewProtect,
; __out PDWORD lpflOldProtect
;);

mov eax, ebx
mov esi, ebx

sub eax, 82h
push eax ; overwrite the buffer containing the dll name, we don't need it anymore
push PAGE_EXECUTE_READWRITE
push 5h
sub esi, 15h
mov esi, [esi]
push esi
sub ebx, 0Dh
mov ebx, [ebx]
call ebx ; Returns error 998 ERROR_NOACCESS (to what?)

pop ebx
push ebx


sub ebx, 1Eh
mov eax, ebx ; restore address buffer pointer

pop ebx
push ebx

sub ebx, 15h ; Restore Address
mov ebx, [ebx]
xor esi, esi ; counter to 0

@0:

push eax

mov al, byte ptr[eax+esi]
mov byte ptr[ebx+esi], al

pop eax

inc esi
cmp esi, 5
jne @0

pop ebx
sub ebx, 9h
mov ebx, [ebx]
push ebx ; address to return to
ret

Size2:

那怎么了?你们能帮帮我吗?

编辑,工作代码:

Size1: 


jmp pLocals
LocalDllName db 100 dup(?)
RestoreBuffer db 5 dup(?)
LoadAddress dd 0h ; ebx - 19h
RestoreAddress dd 0h ; ebx - 15h
AddressToRestoreBuffer dd 0h ; ebx - 11h
AddressToLea dd 0h ; ebx - 0Dh
AddressToReturnTo dd 0h ; ebx - 9h
pLocals:


call Refpnt
Refpnt: pop ebx ; get current address in ebx

push ebx
mov eax, ebx

sub ebx, 82h
push ebx ; dll name

sub eax, 19h ; load lib address
mov eax, [eax]
call eax


pop ebx ; Current address
push ebx


;BOOL WINAPI VirtualProtect(
; __in LPVOID lpAddress,
; __in SIZE_T dwSize,
; __in DWORD flNewProtect,
; __out PDWORD lpflOldProtect
;);

mov esi, ebx

push 0
push esp
push PAGE_EXECUTE_READWRITE
push 5h
sub esi, 15h
mov esi, [esi]
push esi
sub ebx, 0Dh
mov ebx, [ebx]
call ebx

pop ebx
pop ebx
push ebx


sub ebx, 1Eh
mov eax, ebx ; restore address buffer pointer

pop ebx
push ebx

sub ebx, 15h ; Restore Address
mov ebx, [ebx]
xor esi, esi ; counter to 0

@0:

push eax

mov al, byte ptr[eax+esi]
mov byte ptr[ebx+esi], al

pop eax

inc esi
cmp esi, 5
jne @0

pop ebx
sub ebx, 9h
mov ebx, [ebx]
push ebx ; address to return to
ret


Size2:

也许有点草率,但我不在乎 ;)

最佳答案

您正在尝试让 VirtualProtectlpflOldProtect 写入只读内存位置,即您当前的代码部分,这是您首先要取消保护的部分地方!我猜这就是给您 ERROR_NO_ACCESS 的原因。由于无论如何您都在使用堆栈,因此请将 lpflOldProtect 写入堆栈位置。

关于windows - 具有 Virtualprotect 问题的自修改算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4842539/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com