【问题标题】:Cast LPVOID to struct将 LPVOID 转换为结构
【发布时间】:2014-12-07 08:45:10
【问题描述】:

我想读取通过 CreateRemoteThread 发送到另一个进程中注入的 DLL 的参数。

我可以毫无问题地调用该函数,我只是不知道如何将 LPVOID 转换为结构。

这是一个例子:

#pragma pack(push,1)
struct tagRemoteThreadParams
{
    int Param1;
    int Param2;
} RemoteThreadParams, *PRemoteThreadParams;
#pragma pack(pop)

DWORD WINAPI testfunction(LPVOID param)
{
    // cast LPVOID to tagRemoteThreadParams (param)
    WriteToLog("YES YOU CALLED THE FUNCTION WITH PARAM: ");
    return 0;
}

这是我的结构以及我如何在进程中分配内存:

[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct RemoteThreadParams
{
    [MarshalAs(UnmanagedType.I4)]
    public int Param1;

    [MarshalAs(UnmanagedType.I4)]
    public int Param2;
}

public uint CallFunction(int _arg1)
{
    RemoteThreadParams arguments = new RemoteThreadParams();
    arguments.Param1 = 1;
    arguments.Param2 = 2;

    //pointer to the function im trying to call
    IntPtr _functionPtr = IntPtr.Add(this.modulePtr, 69772);

    // Allocate some native heap memory in your process big enough to store the
    // parameter data
    IntPtr iptrtoparams = Marshal.AllocHGlobal(Marshal.SizeOf(arguments));

    // Copies the data in your structure into the native heap memory just allocated
    Marshal.StructureToPtr(arguments, iptrtoparams, false);

    //allocate som mem in remote process
    IntPtr lpAddress = VirtualAllocEx(this.processHandle, IntPtr.Zero, (IntPtr)Marshal.SizeOf(arguments), AllocationType.Commit | AllocationType.Reserve, MemoryProtection.ExecuteReadWrite);

    if (lpAddress == IntPtr.Zero)
    {
        return 0;
    }

    if (WriteProcessMemory(this.processHandle, lpAddress, iptrtoparams, (uint)Marshal.SizeOf(arguments), 0) == 0)
    {
        return 0;
    }
    //Free up memory
    Marshal.FreeHGlobal(iptrtoparams);

    uint threadID = 0;
    IntPtr hThread = CreateRemoteThread(this.processHandle, IntPtr.Zero, 0, _functionPtr, lpAddress, 0, out threadID);
    if (hThread == IntPtr.Zero)
    {
        //throw new ApplicationException(Marshal.GetLastWin32Error().ToString());
        throw new Win32Exception();
    }
    WaitForSingleObject(hThread, 0xFFFFFFFF);
    // wait for thread to exit


    // get the thread exit code
    uint exitCode = 0;
    GetExitCodeThread(hThread, out exitCode);

    // close thread handle
    CloseHandle(hThread);

    return exitCode;
}

【问题讨论】:

  • 您正在尝试将指针值强制转换为非指针值。将参数作为指针传递(在这种情况下可能是指向 char 数组的指针),然后使用 static_cast
  • 也试试这个字符串 *args = reinterpret_cast(param);
  • 我重写了代码来传递一个结构体,请看一下代码并帮助我在我的代码中将 LPVOID 转换为结构体。在这里发疯。

标签: c# c++ casting


【解决方案1】:

演员问题的答案是:

struct tagRemoteThreadParams *tData = (struct tagRemoteThreadParams *)param;

感谢大家的帮助

【讨论】:

    【解决方案2】:

    如果我正确理解你的代码,你将 UT8 编码的字符串注入到对方的进程内存中(我有点惊讶它可以工作)。

    假设它确实有效,在您的 C++ 代码中,您需要将 param 指向的 UTF8 编码字节数组转换为 C++ 可以理解的某种字符串。

    一种方法是使用MultiByteToWideChar

    另一种方法是使用 STL。我发现了一个关于它的问题here

    【讨论】:

    • 我重写了一些代码,我只想将 LPVOID 强制转换为我代码中的结构。我已经在这里坐了好几个小时了,没有弄明白。你能看看吗? :)
    猜你喜欢
    • 1970-01-01
    • 2012-04-03
    • 1970-01-01
    • 2015-07-01
    • 2020-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    相关资源
    最近更新 更多