调用代码的远程的步骤
代码的远程调用涉及到以下技术难点
1.代码的拷贝:
DWORDCodeSize=0;
DWORDCodeStart=0;
_asm 
...{
moveax,_end
subeax,_begin
movCodeSize,eaxmoveax,_begin
movCodeStart,eax
jmp_end
_begin:
//此处是具体的功能代码
_end:
}
//计算要分配的空间
DWORDToAllocateSize=CodeSize+sizeof(int);
//分配空间
LPVOIDRemotePointer=VirtualAllocEx(RemoteProcess,NULL,ToAllocateSize,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
//拷贝代码
WriteProcessMemory(RemoteProcess,RemotePointer,reinterpret_cast<char*>(CodeStart),CodeSize,NULL);
//释放分配的空间
VirtualFreeEx(RemoteProcess,RemotePointer,CodeSize,MEM_DECOMMIT);
VirtualFreeEx(RemoteProcess,RemotePointer,0,MEM_RELEASE);
2.参数的传递:参数传递的难点在于定位,在上面的基础上面继续完成参数的传递
intParam1=GetTickCount();//用于测试的参数,此处用GetTickCount来赋值是表明此值不是常量,所以需要传递给调用的代码
DWORDCodeSize=0;
DWORDCodeStart=0;
_asm 
...{
moveax,_end
subeax,_begin
movCodeSize,eaxmoveax,_begin
movCodeStart,eax
jmp_end
_begin:
//注意,下面是使用传递过来的数据
//获取重定位信息
calldelta
delta:popebp
moveax,offsetdelta
subebp,eax
//完成重定位,得到_end的真实地址,_end之后跟的是数据
movebx,_end
addebx,ebp
//上面的步骤执行完了之后,ebx里面就是_end的实际位置了,在这里也就是指向Param1了
_end:
}
//计算要分配的空间
DWORDToAllocateSize=CodeSize+sizeof(int);
//分配空间
LPVOIDRemotePointer=VirtualAllocEx(RemoteProcess,NULL,ToAllocateSize,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
//拷贝代码
WriteProcessMemory(RemoteProcess,RemotePointer,reinterpret_cast<char*>(CodeStart),CodeSize,NULL);
//接着前面的代码拷贝数据
WriteProcessMemory(RemoteProcess,RemotePointer+CodeSize,reinterpret_cast<char*>(&Param1),sizeof(int),NULL);
//释放分配的空间
VirtualFreeEx(RemoteProcess,RemotePointer,CodeSize,MEM_DECOMMIT);
VirtualFreeEx(RemoteProcess,RemotePointer,0,MEM_RELEASE);
3.调用:调用实在是再简单不过的事情了,上面的代码完成之后,可以使用你可以想到的调用方式,目前有实际意思的是远程线程以及网络调用
远程线程的方法:
intParam1=GetTickCount();//用于测试的参数,此处用GetTickCount来赋值是表明此值不是常量,所以需要传递给调用的代码
DWORDCodeSize=0;
DWORDCodeStart=0;
_asm 
...{
moveax,_end
subeax,_begin
movCodeSize,eaxmoveax,_begin
movCodeStart,eax
jmp_end
_begin:
//注意,下面是使用传递过来的数据
//获取重定位信息
calldelta
delta:popebp
moveax,offsetdelta
subebp,eax
//完成重定位,得到_end的真实地址,_end之后跟的是数据
movebx,_end
addebx,ebp
//上面的步骤执行完了之后,ebx里面就是_end的实际位置了,在这里也就是指向Param1了
_end:
}
//计算要分配的空间
DWORDToAllocateSize=CodeSize+sizeof(int);
//分配空间
LPVOIDRemotePointer=VirtualAllocEx(RemoteProcess,NULL,ToAllocateSize,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
//拷贝代码
WriteProcessMemory(RemoteProcess,RemotePointer,reinterpret_cast<char*>(CodeStart),CodeSize,NULL);
//接着前面的代码拷贝数据
WriteProcessMemory(RemoteProcess,RemotePointer+CodeSize,reinterpret_cast<char*>(&Param1),sizeof(int),NULL);
//远程调用
HANDLERemoteThread=CreateRemoteThread(RemoteProcess,NULL,0,reinterpret_cast<PTHREAD_START_ROUTINE>(RemotePointer),0,0,NULL);
WaitForSingleObject(RemoteThread,INFINITE);
//释放分配的空间
VirtualFreeEx(RemoteProcess,RemotePointer,CodeSize,MEM_DECOMMIT);
VirtualFreeEx(RemoteProcess,RemotePointer,0,MEM_RELEASE);
网络调用可以把上述的代码进行修改之后,通过网络传输之后,由接受放负责调用
本文地址:
http://www.45fan.com/a/question/70372.html