1

我正在将代码从 C++ 转换为我的 VB.NET 应用程序。这是 C++ 代码:

typedef int (__stdcall  *init_t)(uint32_t value,uint32_t param1,uint32_t* param2);
static init_t fnInit;
...
...
memory = (uint8_t*)VirtualAlloc(NULL,5000,MEM_COMMIT,PAGE_EXECUTE_READWRITE);
memset(memory,5000,0);
memmove(memory,data,datalen);
fnInit = (init_t )&memory[0];

如您所见,它在堆栈中定义了一个函数。如何在 VB.NET 中模拟它?

注意:你可以写C#代码,没问题。

编辑:

我根据你的建议写了一堂课。

        <Flags> _
    Public Enum AllocationType As UInteger
        COMMIT = &H1000
        RESERVE = &H2000
        RESET = &H80000
        LARGE_PAGES = &H20000000
        PHYSICAL = &H400000
        TOP_DOWN = &H100000
        WRITE_WATCH = &H200000
    End Enum

    <Flags> _
    Public Enum MemoryProtection As UInteger
        EXECUTE = &H10
        EXECUTE_READ = &H20
        EXECUTE_READWRITE = &H40
        EXECUTE_WRITECOPY = &H80
        NOACCESS = &H1
        [READONLY] = &H2
        READWRITE = &H4
        WRITECOPY = &H8
        GUARD_Modifierflag = &H100
        NOCACHE_Modifierflag = &H200
        WRITECOMBINE_Modifierflag = &H400
    End Enum
    <DllImport("kernel32.dll", SetLastError:=True)> _
    Private Shared Function VirtualAlloc(lpAddress As IntPtr, dwSize As UIntPtr, flAllocationType As AllocationType, flProtect As MemoryProtection) As IntPtr
    End Function
    <DllImport("msvcrt.dll", EntryPoint:="memset", CallingConvention:=CallingConvention.Cdecl, SetLastError:=False)> _
    Public Shared Function MemSet(dest As IntPtr, c As Integer, count As IntPtr) As IntPtr
    End Function
    <DllImport("msvcrt.dll", SetLastError:=False)> _
    Private Shared Function memmove(dest As IntPtr, src As IntPtr, count As Integer) As IntPtr
    End Function

    <UnmanagedFunctionPointer(CallingConvention.StdCall)> _
    Public Delegate Sub Init(ByVal Value As UInt32, ByVal Param1 As UInt32, ByVal Param2 As IntPtr)

    Dim Func_Init As Init

   Sub New(ByRef Bytes() As Byte)
        Dim OffSetInit As Int32 = &H0 'This is correct
        Dim MemoryPtr As IntPtr
        MemoryPtr = VirtualAlloc(Nothing, 5000, AllocationType.COMMIT, MemoryProtection.EXECUTE_READWRITE)
        MemSet(MemoryPtr, 0, 5000)
        Marshal.Copy(Bytes, 0, MemoryPtr, Bytes.Length)
        Dim InitPtr As IntPtr = MemoryPtr + OffSetInit
        Func_Init = CType(Marshal.GetDelegateForFunctionPointer(InitPtr, GetType(Init)), Init)
        Func_Init.Invoke(0, 0, Nothing)
    End Sub

当我执行这个时,“Func_Init.Invoke(0, 0, Nothing)” 行给了我这个错误:

System.AccessViolationException
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

编辑2:

我在字节变体中添加了汇编代码的第一部分:

seg000:00000000     ;
seg000:00000000     ; +-------------------------------------------------------------------------+
seg000:00000000     ; |   This file has been generated by The Interactive Disassembler (IDA)    |
seg000:00000000     ; |           Copyright (c) 2011 Hex-Rays, <support@hex-rays.com>           |
seg000:00000000     ; |                      License info: B3-432E-F558-21                      |
seg000:00000000     ; |                             Ilfak Guilfanov                             |
seg000:00000000     ; +-------------------------------------------------------------------------+
seg000:00000000     ;
seg000:00000000     ; Input MD5   : 55D6B96FEF969A18F927BC5A7A21FAEE
seg000:00000000     ; Input CRC32 : 9A6D4B33
seg000:00000000
seg000:00000000     ; File Name   : C:\****************.dll
seg000:00000000     ; Format      : Binary file
seg000:00000000     ; Base Address: 0000h Range: 0000h - 4174h Loaded length: 4174h
seg000:00000000
seg000:00000000                     include uni.inc ; see unicode subdir of ida for info on unicode
seg000:00000000
seg000:00000000                     .686p
seg000:00000000                     .mmx
seg000:00000000                     .model flat
seg000:00000000
seg000:00000000     ; ===========================================================================
seg000:00000000
seg000:00000000     ; Segment type: Pure code
seg000:00000000     seg000          segment byte public 'CODE' use32
seg000:00000000                     assume cs:seg000
seg000:00000000                     assume es:nothing, ss:nothing, ds:nothing, fs:nothing, gs:nothing
seg000:00000000
seg000:00000000     ; =============== S U B R O U T I N E =======================================
seg000:00000000
seg000:00000000     ; Attributes: bp-based frame
seg000:00000000
seg000:00000000     sub_0           proc near
seg000:00000000
seg000:00000000     var_4           = dword ptr -4
seg000:00000000     arg_0           = dword ptr  8
seg000:00000000     arg_4           = dword ptr  0Ch
seg000:00000000     arg_8           = dword ptr  10h
seg000:00000000
...
4

1 回答 1

5

该代码正在分配内存,将数据复制到分配的内存,然后创建一个引用它的函数指针。大概下一步是执行该代码。

虽然您可以在 .NET 中分配内存并将数据复制到其中,但您将无法将控制权转移到该内存块。

有关VirtualAlloc从 .NET 调用的信息,请参阅pinvoke.net:VirtualAlloc

要将数据复制到该分配的块,您需要调用某种形式的Marshal.Copy

如果出于某种未知原因,您确实想在该内存块中执行代码,则必须调用非托管函数(例如,用 C 或 C++ 编写),将返回的地址传递给它VirtualAlloc,然后让它执行代码。

(来自评论)

如果出于某种原因您真的想在该内存块中执行代码,您可以调用Marshal.GetDelegateForFunctionPointer,并将该内存块的地址传递给它。然后,您可以通过委托调用该函数。

顺便说一下,委托类型类似于:

delegate int test_t (uint32 type, IntPtr data, uint32 length);

我不确定您将如何指定调用约定,并且有几种方法可以编组数据。您可以固定一个数组并传递指针,使用不安全的指针并传递 a byte*,或者可能将参数声明为 abyte[]并编组数据(尽管我不确定您将如何指定编组)。也许其他人对此有答案。

更新

您将委托定义为:

Public Delegate Sub Init(
    ByVal Value As UInt32,
    ByVal Param1 As UInt32,
    ByVal Param2 As IntPtr)

这与 C 定义不匹配。我认为Param1应该是IntPtr而且Param2应该是UInt32

此外,您不会说这是 32 位还是 64 位代码。如果您正在运行 64 位应用程序并且您正在执行的代码(无论Bytes.

我不知道 中的内容Bytes,所以我什至无法猜测可能导致您的访问冲突的原因。但是,您正在向它传递一个空指针,所以这可能是一个问题。如果要调试,则必须使用程序集级调试器。

于 2013-07-31T20:44:49.863 回答