- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想在另一个进程中调用一个函数,并通过 createremotethread 发送超过 1 个参数。
现在,我可以通过发送内联 asm 来做到这一点,但我对汇编的了解不够多,无法那样做。此外,我无权访问远程进程源代码。
我在考虑使用:
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct RemoteThreadParams
{
[MarshalAs(UnmanagedType.I4)]
public int Param1;
[MarshalAs(UnmanagedType.I4)]
public int Param2;
}
但据我了解,远程进程必须知道如何处理它。
有没有一种简单的方法可以向不包含任何程序集的远程进程发送超过 1 个参数?
编辑:
这就是我目前尝试解决它的方式,但我遇到内存不足异常,我真的不明白我做错了什么。
我们在远程进程中的 0x64D480 处有我们的函数 ptr,这是从 IDA pro 中获取的程序集。
// FUNCTION PTR IS 0x64D480
.text:0064D480 sub_64D480 proc near ; CODE XREF: sub_4DA7F0+3Ap
.text:0064D480 ; sub_64D550+Bp ...
.text:0064D480
.text:0064D480 var_C = dword ptr -0Ch // arg1
.text:0064D480 arg_0 = dword ptr 4 // arg2
.text:0064D480
.text:0064D480 push esi
.text:0064D481 push edi
.text:0064D482 mov edi, [esp+8+arg_0]
.text:0064D486 push edi
.text:0064D487 mov esi, ecx
.text:0064D489 call sub_64D330
.text:0064D48E test al, al
.text:0064D490 jnz short loc_64D497
.text:0064D492 pop edi
.text:0064D493 pop esi
.text:0064D494 retn 4
是不是应该可以这样调用函数:
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct RemoteThreadParams
{
[MarshalAs(UnmanagedType.I4)]
public int Param1;
[MarshalAs(UnmanagedType.I4)]
public int Param2;
}
void CallFunction(IntPtr _functionPtr, RemoteThreadParams _parameters)
{
// Allocate some native heap memory in your process big enough to store the parameter data
IntPtr iptrtoparams = Marshal.AllocHGlobal(Marshal.SizeOf(_parameters));
// Copies the data in your structure into the native heap memory just allocated
Marshal.StructureToPtr(_parameters, iptrtoparams, false);
// Use to alloc "committed" memory that is addressable by other process
IntPtr iptrremoteallocatedmemory = VirtualAllocEx(this.handle, IntPtr.Zero, (uint)Marshal.SizeOf(_parameters), AllocationType.Commit, MemoryProtection.ExecuteReadWrite);
UIntPtr bytesWritten = UIntPtr.Zero;
// Copy from local process memory to the memory of the remote process
WriteProcessMemory(this.handle, iptrremoteallocatedmemory, iptrtoparams, (uint)Marshal.SizeOf(_parameters), out bytesWritten);
//Free up memory
Marshal.FreeHGlobal(iptrtoparams);
//thread id and return value in case we need it for later
uint iThreadId;
uint returnValue = 0;
IntPtr hThread = CreateRemoteThread(this.handle, IntPtr.Zero, 0, _functionPtr, iptrtoparams, 0, out iThreadId);
WaitForSingleObject(hThread, 0xFFFFFFFF);
GetExitCodeThread(hThread, out returnValue);
CloseHandle(hThread);
CloseHandle(this.handle);
}
最佳答案
没有简单的方法。最简单的方法是使用 LoadLibrary
和非托管代码来执行此操作。您需要两个版本:32 位 dll 和 64 位 dll,您根据目标进程的“位数”选择注入(inject)哪个版本。
CreateRemoteThread(....., Address of LoadLibrary, "location of your unmanaged dll");
当您用谷歌搜索 C# CreateRemoteThread LoadLibrary 时,您会在 Internet 上找到引用资料。
之后,您希望自定义 dll 进行函数调用:
#include "stdafx.h"
typedef void (__stdcall *MyCustomFunc)(int param1, int param2);
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
// this code is executed arbitrarily.
// this is just a test,
// you're better off writing a separate function in a DLL
// and export it, so you can later call it from C#, once DLL is injected.
MyCustomFunc test = 0x64D480;
test(2, 4);
}
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
ps,如果需要的话,你还需要FreeLibrary
。正确调用约定也很重要。我在这里有点冒险,说它看起来像 __stdcall,但它可能是 __cdecl、__fastcall 或其他任何东西。请参阅:http://www.codeproject.com/Articles/1388/Calling-Conventions-Demystified了解更多信息。
关于c# - 在不是用 .NET 编写的另一个进程中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26264910/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!