我试图通过C#运行一些ASM代码(它只是递减一个数字)。我有一个链接文件(.exe)和一个(.o)win32 ASM编译文件。我试图用字节读取它们并通过在托管环境中执行ASM的标准方式执行它,但是它一直给我一个System.AccessViolationException。
是什么给出的?我试图对内存分配进行一些研究,但我不知道我的问题在哪里。
代码:
Const PAGE_EXECUTE_READWRITE As UInteger = &H40
Const MEM_COMMIT As UInteger = &H1000
<DllImport("kernel32.dll", SetLastError:=True)>
Private Shared Function VirtualAlloc(lpAddress As IntPtr, dwSize As IntPtr, flAllocationType As UInteger, flProtect As UInteger) As IntPtr
End Function
Private Delegate Function IntReturner() As Integer
Public Sub Run()
Dim body As Byte() = IO.File.ReadAllBytes("C:\MinGW\bin\test.exe")
Dim buf As IntPtr = VirtualAlloc(IntPtr.Zero, CUInt(body.Length), MEM_COMMIT, PAGE_EXECUTE_READWRITE)
Marshal.Copy(body, 0, buf, body.Length)
Dim ptr As IntReturner = DirectCast(Marshal.GetDelegateForFunctionPointer(buf, GetType(IntReturner)), IntReturner)
Console.WriteLine(ptr())
End Sub
编辑:如果有人知道如何将可执行文件转换为shellcode。我可以简单地将字节直接附加到body。我认为这是出错的地方?
section .text
global _WinMain@16
_WinMain@16:
mov ecx,4294967295
myloop:
dec ecx
jnz myloop
; Terminate program
mov eax, 0
ret 16