- UID
- 47764
注册时间2008-3-1
阅读权限20
最后登录1970-1-1
以武会友
该用户从未签到
|
最近研究屏幕扫描算法 要HOOK BitBlt函数测试效果。
采用远程注入DLL HOOK IAT实现,下面是主要代码,顺便拿出来分享下。高手飘过。
// APIHOOK.cpp : Defines the entry point for the DLL application.
//
#include "process.h"
#include "stdio.h"
#include "windows.h"
HWND hEdit;
FILE * pfile;
//IAT HOOK
// 定义MessageBoxA函数原型
typedef int (WINAPI *pBitBlt)(HDC, int, int, int, int, HDC, int, int, DWORD);
int WINAPI NewBitBlt(HDC, int, int, int, int, HDC, int, int, DWORD);
int * addr = (int *)BitBlt; //保存函数的入口地址
int * myaddr = (int *)NewBitBlt;
PDWORD pAddr=NULL;
//结束进程的函数
unsigned __stdcall ThreadProc(void *param)
{
//------------hook api----------------
HMODULE hMod = GetModuleHandle(NULL);
PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)hMod;
PIMAGE_NT_HEADERS pNTHeaders = (PIMAGE_NT_HEADERS)((BYTE *)hMod + pDosHeader->e_lfanew);
PIMAGE_OPTIONAL_HEADER pOptHeader = (PIMAGE_OPTIONAL_HEADER)&(pNTHeaders->OptionalHeader);
PIMAGE_IMPORT_DESCRIPTOR pImportDescriptor = (PIMAGE_IMPORT_DESCRIPTOR)((BYTE *)hMod + pOptHeader->DataDirectory[1].VirtualAddress);
while(pImportDescriptor->FirstThunk)
{
char * dllname = (char *)((BYTE *)hMod + pImportDescriptor->Name);
PIMAGE_THUNK_DATA pThunkData = (PIMAGE_THUNK_DATA)((BYTE *)hMod + pImportDescriptor->OriginalFirstThunk);
int no = 1;
while(pThunkData->u1.Function)
{
char * funname = (char *)((BYTE *)hMod + (DWORD)pThunkData->u1.AddressOfData + 2);
PDWORD lpAddr = (DWORD *)((BYTE *)hMod + (DWORD)pImportDescriptor->FirstThunk) +(no-1);
//修改内存的部分
if((*lpAddr) == (DWORD)addr)
{
//修改内存页的属性
DWORD dwOLD;
MEMORY_BASIC_INFORMATION mbi;
VirtualQuery(lpAddr,&mbi,sizeof(mbi));
VirtualProtect(lpAddr,sizeof(DWORD),PAGE_READWRITE,&dwOLD);
WriteProcessMemory(GetCurrentProcess(), lpAddr, &myaddr, sizeof(DWORD), NULL);
pAddr=lpAddr;
break;
}
no++;
pThunkData++;
}
pImportDescriptor++;
}
return 0;
}
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
if(ul_reason_for_call==DLL_PROCESS_ATTACH)
{
_beginthreadex(NULL,0,ThreadProc,NULL,NULL,NULL);
MessageBox(NULL,"注入成功","提示",MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
HWND hWnd = FindWindow("#32770","SharkServer 1.0");
hEdit = FindWindowEx(hWnd,NULL,"Edit",NULL);
pfile=fopen("BitBlt调用情况.txt","a");
}
if(ul_reason_for_call==DLL_PROCESS_DETACH)
{
WriteProcessMemory(GetCurrentProcess(), pAddr, &addr, sizeof(DWORD), NULL);
MessageBox(NULL,"卸载HOOK成功","提示",MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
fclose(pfile);
}
return TRUE;
}
int WINAPI NewBitBlt(HDC hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
HDC hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
DWORD dwRop)
{
char szbuffer[100]={0};
sprintf(szbuffer,"nXDest:%-4d nYDest:%-4d nWidth:%-4d nHeight:%-4d nXSrc:%-4d nYSrc:%-4d\r\n",
nXDest,nYDest,nWidth,nHeight,nXSrc,nYSrc);
fwrite(szbuffer,1,100,pfile);
SendMessage(hEdit,EM_REPLACESEL,0,(LPARAM)szbuffer);
BitBlt(hdcDest, nXDest,nYDest, nWidth, nHeight,hdcSrc,nXSrc, nYSrc,dwRop);
return 0;
}
测试代码
void CSharkServerDlg::OnStartHook()
{
// TODO: 在此添加控件通知处理程序代码
char process[20];
GetDlgItemText(IDC_PROCESSNAME,process,20);
HANDLE hToken;
if (!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken))
{
return ;
}
TOKEN_PRIVILEGES tp;
LUID Luid;
if (!LookupPrivilegeValue(NULL,SE_DEBUG_NAME,&Luid))
{
return ;
}
tp.PrivilegeCount = 1;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
tp.Privileges[0].Luid = Luid;
if (!AdjustTokenPrivileges(hToken,0,&tp,sizeof(TOKEN_PRIVILEGES),NULL,NULL))
{
return ;
}
PROCESSENTRY32 pe;
pe.dwSize = sizeof(pe);
HANDLE hSnap=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
BOOL bNext=Process32First(hSnap, &pe);
while(bNext)
{
if(!strcmpi(pe.szExeFile,process))
{
m_hTarget=OpenProcess(PROCESS_CREATE_THREAD|PROCESS_VM_WRITE|PROCESS_VM_OPERATION,1,pe.th32ProcessID);
break;
}
bNext=Process32Next(hSnap, &pe);
}
CloseHandle(hSnap);
//申请内存
CString sPath;
GetModuleFileName(NULL,sPath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
sPath.ReleaseBuffer();
int nPos;
nPos=sPath.ReverseFind('\\');
sPath=sPath.Left(nPos);
CString szDllName = sPath + "\\APIHOOK.dll";
strcpy((char*)m_DllName,szDllName); //DLL文件的路径
m_dwSize=strlen(m_DllName);
m_pAddr=VirtualAllocEx(m_hTarget,NULL,m_dwSize,MEM_COMMIT,PAGE_READWRITE);
WriteProcessMemory(m_hTarget,m_pAddr,(void*)m_DllName,m_dwSize,NULL);
HANDLE hThread = CreateRemoteThread(m_hTarget,NULL,0,(LPTHREAD_START_ROUTINE)LoadLibrary,(LPVOID)m_pAddr,NULL,NULL);
WaitForSingleObject(hThread,INFINITE);
GetExitCodeThread(hThread,&m_dwThreadExitCode);
CloseHandle(hThread);
return ;
}
void CSharkServerDlg::OnStopHook()
{
// TODO: 在此添加控件通知处理程序代码
VirtualFreeEx(m_hTarget,m_pAddr,m_dwSize,MEM_RELEASE);
HANDLE hThread = CreateRemoteThread(m_hTarget,NULL,0,(LPTHREAD_START_ROUTINE)FreeLibrary,(LPVOID)m_dwThreadExitCode,NULL,NULL);
CloseHandle(m_hTarget);
}
[ 本帖最后由 frozenrain 于 2009-5-12 22:57 编辑 ] |
|