远程注入DLL
至于为什么要使用远程DLL注入我就不了多说了,既然大家要用,肯定有各种各样的目的。
简单来说,就是利用API在宿主程序里运行我们的程序(代码等),以达到我们自己想要的效果和目的。
在阅读本文之前,想必以下几个API函数大家应该都有所了解我就不一一介绍了,关于函数的详细功能和介绍请参阅MSDN吧!
OpenProcess-用于打开宿主的目标进程。
VirtualAllocEx/VirtualFreeEx-用于在宿主进程中分配/释放内存空间。
WriteProcessMemory-用于在宿主进程中写入要加载的DLL名称。
CreateRemoteThread-远程加载DLL的核心内容,用于控制目标进程调用API函数。
LoadLibrary-目标进程通过调用此函数来加载我们的DLL或者我们要执行的代码
下面是远程注入DLL的具体代码,慢慢看看吧!
#include "stdafx.h"
#include <string>
#include "stdio.h"
#include <iostream>
using namespace std; #define DEF_BUF_SIZE 1024 // 用于存储注入模块DLL的路径全名
char szDllPath[DEF_BUF_SIZE] = {0} ; // 使用远程线程向指定ID的进程注入模块
BOOL InjectModuleToProcessById ( DWORD dwProcessId )
{
if ( dwProcessId == 0 )
return FALSE ;
HANDLE hProcess = OpenProcess ( PROCESS_ALL_ACCESS, FALSE, dwProcessId ) ;
if ( hProcess == NULL )
return FALSE ;
UINT nLen = (UINT)strlen ( szDllPath ) + 1;
LPVOID lpRemoteDllName = VirtualAllocEx ( hProcess, NULL, nLen, MEM_COMMIT, PAGE_READWRITE ) ;
if ( lpRemoteDllName == NULL )
{
printf ( "[ERROR]VirtualAllocEx(%d)n", GetLastError() );
return FALSE ;
}
if ( WriteProcessMemory ( hProcess, lpRemoteDllName, szDllPath, nLen, NULL) == FALSE )
{
printf ( "[ERROR]WriteProcessMemory(%d)n", GetLastError() );
return FALSE ;
}
HMODULE hModule = GetModuleHandle ( L"kernel32.dll" ) ;
LPTHREAD_START_ROUTINE fnStartAddr = ( LPTHREAD_START_ROUTINE )GetProcAddress(hModule,"LoadLibraryA") ;
if ( (DWORD)fnStartAddr == 0 )
{
printf ( "[ERROR]GetProcAddress(%d)n", GetLastError() );
return FALSE ;
}
HANDLE hRemoteThread = CreateRemoteThread ( hProcess, NULL, 0,fnStartAddr, lpRemoteDllName, 0, NULL ) ;
if ( hRemoteThread == NULL )
{
printf ( "[ERROR]CreateRemoteThread(%d)n", GetLastError() );
return FALSE ;
}
if ( WaitForSingleObject ( hRemoteThread, INFINITE ) != WAIT_OBJECT_0 )
{
printf ( "[ERROR]WaitForSingleObject(%d)n", GetLastError() );
return FALSE ;
}
}
#include
<windows.h>#include <string>
#include "stdio.h"
#include <iostream>
using namespace std; #define DEF_BUF_SIZE 1024 // 用于存储注入模块DLL的路径全名
char szDllPath[DEF_BUF_SIZE] = {0} ; // 使用远程线程向指定ID的进程注入模块
BOOL InjectModuleToProcessById ( DWORD dwProcessId )
{
if ( dwProcessId == 0 )
return FALSE ;
// 打开进程
HANDLE hProcess = OpenProcess ( PROCESS_ALL_ACCESS, FALSE, dwProcessId ) ;
if ( hProcess == NULL )
return FALSE ;
//申请存放文件名的空间
UINT nLen = (UINT)strlen ( szDllPath ) + 1;
LPVOID lpRemoteDllName = VirtualAllocEx ( hProcess, NULL, nLen, MEM_COMMIT, PAGE_READWRITE ) ;
if ( lpRemoteDllName == NULL )
{
printf ( "[ERROR]VirtualAllocEx(%d)n", GetLastError() );
return FALSE ;
}
//把dll文件名写入申请的空间
if ( WriteProcessMemory ( hProcess, lpRemoteDllName, szDllPath, nLen, NULL) == FALSE )
{
printf ( "[ERROR]WriteProcessMemory(%d)n", GetLastError() );
return FALSE ;
}
//获取动态链接库函数地址
HMODULE hModule = GetModuleHandle ( L"kernel32.dll" ) ;
LPTHREAD_START_ROUTINE fnStartAddr = ( LPTHREAD_START_ROUTINE )GetProcAddress(hModule,"LoadLibraryA") ;
if ( (DWORD)fnStartAddr == 0 )
{
printf ( "[ERROR]GetProcAddress(%d)n", GetLastError() );
return FALSE ;
}
//创建远程线程
HANDLE hRemoteThread = CreateRemoteThread ( hProcess, NULL, 0,fnStartAddr, lpRemoteDllName, 0, NULL ) ;
if ( hRemoteThread == NULL )
{
printf ( "[ERROR]CreateRemoteThread(%d)n", GetLastError() );
return FALSE ;
}
// 等待远程线程结束
if ( WaitForSingleObject ( hRemoteThread, INFINITE ) != WAIT_OBJECT_0 )
{
printf ( "[ERROR]WaitForSingleObject(%d)n", GetLastError() );
return FALSE ;
}
CloseHandle ( hRemoteThread ) ;
CloseHandle ( hModule ) ;
CloseHandle ( hProcess ) ;
}
看过上面的代码,思路应该就很清晰了,大家快去试试吧!
下次,我将写一个C#或者JAVA版的远程注入DLL的DEMO,有兴趣的朋友就关注一下啦,嘿嘿!
本文章来自【龙歌网络】博客园(http://www.cnblogs.com/longle/archive/2011/06/04/2072934.html),转载就注明出处哦!