#include <malloc.h>
#include <TlHelp32.h>
#ifdef UNICODE
#define EjectLib EjectLibW
#else
#define EjectLib EjectLibA
#endif // !UNICODE
//dwProcessId 进程id
//pszLibFile 库的绝对路径
BOOL WINAPI EjectLibW(DWORD dwProcessId,PCWSTR pszLibFile)
{
BOOL fOk=FALSE;//赋初值,假定函数失败
HANDLE hthSnapshot=NULL;
HANDLE hProcess=NULL,hThread=NULL;
__try
{
//获取进程快照
hthSnapshot=CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,dwProcessId);
if (hthSnapshot==NULL) __leave;
//获取注入dll的句柄
MODULEENTRY32W me={sizeof(me)};
BOOL fFound=FALSE;
BOOL fMoreMods=Module32FirstW(hthSnapshot,&me);
for(;fMoreMods;fMoreMods=Module32NextW(hthSnapshot,&me))//枚举进程模块判断是否为注入模块
{
fFound=(lstrcmpiW(me.szModule,pszLibFile)==0)||(lstrcmpiW(me.szExePath,pszLibFile)==0);
if(fFound)break;
}
if(!fFound)__leave;
//获得进程句柄
hProcess = OpenProcess(
PROCESS_QUERY_INFORMATION | // Required by Alpha
PROCESS_CREATE_THREAD |
PROCESS_VM_OPERATION, //可以使用CreateRemoteThread
FALSE,dwProcessId);
if(hProcess==NULL) __leave;
//获取Kernel32.dll中LoadLibraryW的真实地址
PTHREAD_START_ROUTINE pfnThreadRtn=(PTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandle(TEXT("Kernel32")),"FreeLibrary");
if(pfnThreadRtn == NULL) __leave;
建立远程线程调用LoadLibraryW
hThread=CreateRemoteThread(hProcess,NULL,0,pfnThreadRtn,me.modBaseAddr,0,NULL);
if (hThread==NULL) __leave;
//等待线程结束
WaitForSingleObject(hThread, INFINITE);
fOk=TRUE; //释放成功
}
__finally
{
//异常处理
if(hthSnapshot != NULL)
CloseHandle(hthSnapshot);
if(hThread != NULL)
CloseHandle(hThread);
if(hProcess != NULL)
CloseHandle(hProcess);
}
return(fOk);
}
///
BOOL WINAPI EjectLibA(DWORD dwProcessId,PCSTR pszLibFile)
{
//分配Unicode格式下路径存储空间
PWSTR pszLibFileW=(PWSTR)_alloca((lstrlenA(pszLibFile) + 1) * sizeof(WCHAR));
//把ANSI路径名转换为Unicode格式
wsprintfW(pszLibFileW, L"%S", pszLibFile);
//调用Unicode格式的函数
return(EjectLibW(dwProcessId, pszLibFileW));
}
#include <TlHelp32.h>
#ifdef UNICODE
#define EjectLib EjectLibW
#else
#define EjectLib EjectLibA
#endif // !UNICODE
//dwProcessId 进程id
//pszLibFile 库的绝对路径
BOOL WINAPI EjectLibW(DWORD dwProcessId,PCWSTR pszLibFile)
{
BOOL fOk=FALSE;//赋初值,假定函数失败
HANDLE hthSnapshot=NULL;
HANDLE hProcess=NULL,hThread=NULL;
__try
{
//获取进程快照
hthSnapshot=CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,dwProcessId);
if (hthSnapshot==NULL) __leave;
//获取注入dll的句柄
MODULEENTRY32W me={sizeof(me)};
BOOL fFound=FALSE;
BOOL fMoreMods=Module32FirstW(hthSnapshot,&me);
for(;fMoreMods;fMoreMods=Module32NextW(hthSnapshot,&me))//枚举进程模块判断是否为注入模块
{
fFound=(lstrcmpiW(me.szModule,pszLibFile)==0)||(lstrcmpiW(me.szExePath,pszLibFile)==0);
if(fFound)break;
}
if(!fFound)__leave;
//获得进程句柄
hProcess = OpenProcess(
PROCESS_QUERY_INFORMATION | // Required by Alpha
PROCESS_CREATE_THREAD |
PROCESS_VM_OPERATION, //可以使用CreateRemoteThread
FALSE,dwProcessId);
if(hProcess==NULL) __leave;
//获取Kernel32.dll中LoadLibraryW的真实地址
PTHREAD_START_ROUTINE pfnThreadRtn=(PTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandle(TEXT("Kernel32")),"FreeLibrary");
if(pfnThreadRtn == NULL) __leave;
建立远程线程调用LoadLibraryW
hThread=CreateRemoteThread(hProcess,NULL,0,pfnThreadRtn,me.modBaseAddr,0,NULL);
if (hThread==NULL) __leave;
//等待线程结束
WaitForSingleObject(hThread, INFINITE);
fOk=TRUE; //释放成功
}
__finally
{
//异常处理
if(hthSnapshot != NULL)
CloseHandle(hthSnapshot);
if(hThread != NULL)
CloseHandle(hThread);
if(hProcess != NULL)
CloseHandle(hProcess);
}
return(fOk);
}
///
BOOL WINAPI EjectLibA(DWORD dwProcessId,PCSTR pszLibFile)
{
//分配Unicode格式下路径存储空间
PWSTR pszLibFileW=(PWSTR)_alloca((lstrlenA(pszLibFile) + 1) * sizeof(WCHAR));
//把ANSI路径名转换为Unicode格式
wsprintfW(pszLibFileW, L"%S", pszLibFile);
//调用Unicode格式的函数
return(EjectLibW(dwProcessId, pszLibFileW));
}