Windows 具有虚拟保护问题的自修改算法

Windows 具有虚拟保护问题的自修改算法,windows,winapi,assembly,memory-management,Windows,Winapi,Assembly,Memory Management,windows的Virtualprotect()api有问题。 我从学校得到一份作业,老师告诉我们,在过去,当记忆稀缺且昂贵的时候。程序员必须创建先进的算法,这些算法可以动态修改自身以节省内存。我们现在必须编写这样一个算法,它不一定要有效,但它必须自我修改 所以我就开始这么做了,我想在寻求任何帮助之前我已经做了很多 我的程序是这样工作的: 我有一个函数和一个内置堆栈溢出的循环。堆栈会溢出循环期间构造的代码所在的内存位置的地址。控件传递给内存中的代码。代码加载dll然后退出,但在退出之前必须修复循

windows的Virtualprotect()api有问题。 我从学校得到一份作业,老师告诉我们,在过去,当记忆稀缺且昂贵的时候。程序员必须创建先进的算法,这些算法可以动态修改自身以节省内存。我们现在必须编写这样一个算法,它不一定要有效,但它必须自我修改

所以我就开始这么做了,我想在寻求任何帮助之前我已经做了很多

我的程序是这样工作的:

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

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

错误\u NOACCESS,关于此错误的文档非常少,windows仅说明:Invailid访问内存地址。因为我一开始就想更改访问权限。那怎么了?以下是在内存中构造的代码: 代码中所有数据的名称都有点模糊,所以我提供了一些注释

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: 

也许有点邋遢,但我不在乎;)

这已经不像过去那么容易了;读访问过去意味着执行访问,许多内存映射映射都是可写的

现在,如果有许多(任何?)内存映射既可写又可执行,我会感到惊讶。(即使是32位内核,支持PAE的现代CPU也足以提供不可执行但可读的映射。)

我想说,首先,找到一个旧的Windows系统,Win2k或更早,然后开始尝试解决这个问题。:)

编辑:哦!我以为加载DLL失败了。干得好。:)


“恢复循环”是什么意思?由于您破坏了堆栈以跳转到代码,因此并没有真正破坏循环的文本段,您只是在堆栈上涂鸦。可以在循环之前插入另一个函数,然后从dll返回调用循环的函数。(您从循环中“返回”到注入的代码中,因此如果不为其构建假堆栈帧,就无法返回到循环中;返回到上一个函数似乎比构建假堆栈帧更容易。)

您正在尝试将
VirtualProtect
写入
lpflOldProtect
到只读内存位置,也就是说,您当前的代码部分就是您首先要取消保护的部分!我猜这就是为什么您会出现
错误\u无法访问
。由于您仍在使用堆栈,请让它将
lpflOldProtect
写入堆栈位置。

除了这无法解决我的问题外,剩下的唯一问题是无法恢复循环。其余的都很好用。既然你提到了。这很可能是我的问题,我将尝试一下,我只需要传递一个指向堆栈的指针,对吗?一个编写“sploit代码”的类。美好的