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

标签 windows winapi assembly memory-management

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

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

我的程序是这样工作的:

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

问题是我没有对循环的写访问权限,只有 READ_EXECUTE,因此要更改我的访问权限,我想,我使用 virtualprotect。但该函数返回了一个错误:

ERROR_NOACCESS,关于这个错误的文档非常少,windows只说:无效访问内存地址。因为我首先想更改访问权限,所以数字是这样的。那么出了什么问题呢?这是在内存中构造的代码: 我的代码中所有数据的名称有点模糊,所以我提供了一些注释

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/

相关文章:

更改 Win32 Windows 项目中的默认窗口字体

c - 在 Win32 SDK 中使用 PlaySound 时,如何检测声音何时播放完毕?

assembly - 向量地址从程序存储器的中间开始

assembly - ARM汇编中有小寄存器吗?

c++ - Listview 中的第一次机会异常设置检查状态

c++ - realloc 在以前稳定的功能中崩溃

windows - 如何在 Windows 上对多个 repo 进行 git pull?

c++ - LZ函数的用法不清楚

windows - 升级 Fedora 后,为什么我无法再更改通过 SMB 挂载的文件的权限

c++ - 高级和低级编程语言之间的关系