首页
社区
课程
招聘
[分享]枚举消息钩子
发表于: 2009-9-22 16:38 12547

[分享]枚举消息钩子

2009-9-22 16:38
12547

把炉子大侠的代码翻译了下,代码写的很乱,没整理,很多没必要的也没去掉,凑活看。
没有原创,如有雷同,纯属巧合。主要参考如下两篇:
http://bbs.pediy.com/showthread.php?t=55453
http://www.xfocus.net/articles/200408/721.html
其他的记不清了,再此一并谢过。

#include <stdio.h>
#include <conio.h>
#include <Windows.h>

typedef enum  _HOOK_TYPE{
        R_WH_MSGFILTER = -1,
        R_WH_JOURNALRECORD = 0,
        R_WH_JOURNALPLAYBACK = 1,
        R_WH_KEYBOARD = 2,
        R_WH_GETMESSAGE = 3,
        R_WH_CALLWNDPROC = 4,
        R_WH_CBT = 5,
        R_WH_SYSMSGFILTER = 6,
        R_WH_MOUSE = 7,
        R_WH_HARDWARE = 8,
        R_WH_DEBUG = 9,
        R_WH_SHELL = 10,
        R_WH_FOREGROUNDIDLE = 11,
        R_WH_CALLWNDPROCRET = 12,
        R_WH_KEYBOARD_LL = 13,
        R_WH_MOUSE_LL = 14
}HOOK_TYPE;

typedef enum _HANDLE_TYPE
{
        TYPE_FREE = 0,
        TYPE_WINDOW = 1,
        TYPE_MENU = 2,
        TYPE_CURSOR = 3,
        TYPE_SETWINDOWPOS = 4,
        TYPE_HOOK = 5,
        TYPE_CLIPDATA = 6,
        TYPE_CALLPROC = 7,
        TYPE_ACCELTABLE = 8,
        TYPE_DDEACCESS = 9,
        TYPE_DDECONV = 10,
        TYPE_DDEXACT = 11,
        TYPE_MONITOR = 12,
        TYPE_KBDLAYOUT = 13,
        TYPE_KBDFILE = 14,
        TYPE_WINEVENTHOOK = 15,
        TYPE_TIMER = 16,
        TYPE_INPUTCONTEXT = 17,
        TYPE_CTYPES = 18,
        TYPE_GENERIC = 255
}HANDLE_TYPE;

typedef struct _HANDLEENTRY{
        PVOID  phead;      
        ULONG  pOwner;      
        BYTE  bType;        
        BYTE  bFlags;      
        WORD  wUniq;      
}HANDLEENTRY,*PHE;

typedef struct _SERVERINFO{        
        WORD wRIPFlags ;      
        WORD wSRVIFlags ;      
        WORD wRIPPID ;         
        WORD wRIPError ;      
        ULONG cHandleEntries;   
}SERVERINFO,*PSERVERINFO;

typedef struct _SHAREDINFO{
        PSERVERINFO psi;      
        PHE aheList;         
        ULONG pDispInfo;      
        ULONG ulSharedDelta;  
}SHAREDINFO,*PSHAREDINFO;

typedef struct _HEAD
{
        HANDLE h;
        ULONG cLockObj;
}HEAD;

typedef struct _THROBJHEAD
{
        HEAD headinfo;
        PVOID pti;
}THROBJHEAD;

typedef  struct _DESKHEAD
{
        PVOID rpdesk;
        PBYTE pSelf ;
}DESKHEAD;

typedef struct _THRDESKHEAD
{
        THROBJHEAD ThreadObjHead ;
        DESKHEAD DesktopHead ;
}THRDESKHEAD;

typedef  struct _HOOK
{
        THRDESKHEAD tshead ;
        HOOK * phkNext ;
        HOOK_TYPE iHook;   
        ULONG offPfn;               
        UINT flags ;        
        DWORD  ihmod ;               
        PVOID ptiHooked;   
        PVOID rpdesk ;      
}HOOK,*PHOOK;

#define IN
#define OUT
#define OPTIONAL

#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)

typedef enum _SYSDBG_COMMAND {
        SysDbgQueryModuleInformation,
        SysDbgQueryTraceInformation,
        SysDbgSetTracepoint,
        SysDbgSetSpecialCall,
        SysDbgClearSpecialCalls,
        SysDbgQuerySpecialCalls,
        SysDbgCopyMemoryChunks_0 = 8
} SYSDBG_COMMAND, *PSYSDBG_COMMAND;

typedef struct _MEMORY_CHUNKS {
        ULONG Address;
        PVOID Data;
        ULONG Length;
}MEMORY_CHUNKS, *PMEMORY_CHUNKS;

typedef NTSTATUS (_stdcall *NTSYSTEMDEBUGCONTROL)(
                                          IN SYSDBG_COMMAND Command,
                                          IN PVOID InputBuffer,
                                          IN ULONG InputBufferLength,
                                          OUT PVOID OutputBuffer,
                                          IN ULONG OutputBufferLength,
                                          OUT PULONG ReturnLength
                                          );
NTSYSTEMDEBUGCONTROL NtSystemDebugControl;
typedef NTSTATUS (_stdcall *NTREADVIRTUALMEMORY)(IN HANDLE ProcessHandle, IN PVOID BaseAddress, OUT PVOID Buffer, IN ULONG NumberOfBytesToRead, OUT PULONG NumberOfBytesRead OPTIONAL);
NTREADVIRTUALMEMORY NtReadVirtualMemory;

int _tmain(int argc, _TCHAR* argv[])
{
        NTSTATUS status;
        SHAREDINFO SharedInfo;
        PSHAREDINFO pSharedInfo;
        DWORD NumberOfBytesRead;
        SERVERINFO ServerInfo;
        PHE pHandleEntry;
        PHE pHandleEntryR;
        PHOOK pHook;
        MEMORY_CHUNKS mc;

        ULONG uWowHandler =(ULONG)GetProcAddress(LoadLibraryW(L"user32.dll"), "UserRegisterWowHandlers");
        for (ULONG uAddr=uWowHandler; uAddr<=uWowHandler+0x250; uAddr++)
        {
                if (0x40c7 ==*(WORD*)uAddr && 0xb8 ==*(BYTE*)(uAddr+7))
                {
                        pSharedInfo=(PSHAREDINFO)(*(DWORD*)(uAddr+8));
                        printf("%x\n", pSharedInfo);
                        break;
                }
        }
        //
        NtSystemDebugControl =(NTSYSTEMDEBUGCONTROL)GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "NtSystemDebugControl");
        //
        NtReadVirtualMemory =(NTREADVIRTUALMEMORY)GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "NtReadVirtualMemory");
        status =NtReadVirtualMemory(GetCurrentProcess(),pSharedInfo, &SharedInfo, sizeof(SHAREDINFO), &NumberOfBytesRead);

        if (!NT_SUCCESS(status))
        {
                return 1;
        }
        status =NtReadVirtualMemory(GetCurrentProcess(), SharedInfo.psi, &ServerInfo, sizeof(SERVERINFO), &NumberOfBytesRead);

        if (!NT_SUCCESS(status))
        {
                return 1;
        }
   
    pHandleEntryR =(PHE)VirtualAlloc(NULL, sizeof(HANDLEENTRY) *ServerInfo.cHandleEntries, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
        pHandleEntry =pHandleEntryR;

        NtReadVirtualMemory(GetCurrentProcess(),SharedInfo.aheList, pHandleEntry, sizeof(HANDLEENTRY) *ServerInfo.cHandleEntries, &NumberOfBytesRead);
        if (!NT_SUCCESS(status))
        {
                return 1;
        }

        pHook =(PHOOK)VirtualAlloc(NULL, sizeof(HOOK), MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
        for ( ; pHandleEntry< pHandleEntryR +ServerInfo.cHandleEntries; pHandleEntry++)
        {

                if (TYPE_HOOK ==pHandleEntry->bType)
                {
                        mc.Address =(ULONG)pHandleEntry->phead;
                        mc.Length =sizeof(HOOK);
                        mc.Data =pHook;

                        status =NtSystemDebugControl(SysDbgCopyMemoryChunks_0, &mc, sizeof(mc), NULL, 0, &NumberOfBytesRead);
                        if (!NT_SUCCESS(status))
                        {
                                VirtualFree(pHook, sizeof(HOOK), MEM_DECOMMIT);
                                VirtualFree(pHandleEntryR, sizeof(HANDLEENTRY) *ServerInfo.cHandleEntries, MEM_DECOMMIT);
                                return 1;
                        }
                        switch(pHook->iHook)
                        {
                        case         R_WH_MSGFILTER: printf("Handle:%x; Type:WH_MSGFILTER, FunAddr:%x\n", pHook->tshead.ThreadObjHead.headinfo.h, pHook->offPfn);break;
                        case         R_WH_JOURNALRECORD: printf("Handle:%x; Type:WH_JOURNALRECORD, FunAddr:%x\n", pHook->tshead.ThreadObjHead.headinfo.h, pHook->offPfn);break;
                        case         R_WH_JOURNALPLAYBACK: printf("Handle:%x; Type:WH_JOURNALPLAYBACK, FunAddr:%x\n", pHook->tshead.ThreadObjHead.headinfo.h, pHook->offPfn);break;
                        case         R_WH_KEYBOARD: printf("Handle:%x; Type:WH_KEYBOARD, FunAddr:%x\n", pHook->tshead.ThreadObjHead.headinfo.h, pHook->offPfn);break;
                        case         R_WH_GETMESSAGE: printf("Handle:%x; Type:WH_GETMESSAGE, FunAddr:%x\n", pHook->tshead.ThreadObjHead.headinfo.h, pHook->offPfn);break;
                        case         R_WH_CALLWNDPROC: printf("Handle:%x; Type:WH_CALLWNDPROC, FunAddr:%x\n", pHook->tshead.ThreadObjHead.headinfo.h, pHook->offPfn);break;
                        case         R_WH_CBT: printf("Handle:%x; Type:WH_CBT, FunAddr:%x\n", pHook->tshead.ThreadObjHead.headinfo.h, pHook->offPfn);break;
                        case         R_WH_SYSMSGFILTER: printf("Handle:%x; Type:WH_SYSMSGFILTER, FunAddr:%x\n", pHook->tshead.ThreadObjHead.headinfo.h, pHook->offPfn);break;
                        case         R_WH_MOUSE: printf("Handle:%x; Type:WH_MOUSE, FunAddr:%x\n", pHook->tshead.ThreadObjHead.headinfo.h, pHook->offPfn);break;
                        case         R_WH_HARDWARE: printf("Handle:%x; Type:WH_HARDWARE, FunAddr:%x\n", pHook->tshead.ThreadObjHead.headinfo.h, pHook->offPfn);break;
                        case         R_WH_DEBUG: printf("Handle:%x; Type:WH_DEBUG, FunAddr:%x\n", pHook->tshead.ThreadObjHead.headinfo.h, pHook->offPfn);break;
                        case         R_WH_SHELL: printf("Handle:%x; Type:WH_SHELL, FunAddr:%x\n", pHook->tshead.ThreadObjHead.headinfo.h, pHook->offPfn);break;
                        case         R_WH_FOREGROUNDIDLE: printf("Handle:%x; Type:WH_FOREGROUNDIDLE, FunAddr:%x\n", pHook->tshead.ThreadObjHead.headinfo.h, pHook->offPfn);break;
                        case         R_WH_CALLWNDPROCRET: printf("Handle:%x; Type:WH_KEYBOARD_LL, FunAddr:%x\n", pHook->tshead.ThreadObjHead.headinfo.h, pHook->offPfn);break;
                        case         R_WH_KEYBOARD_LL: printf("Handle:%x; Type:WH_MSGFILTER, FunAddr:%x\n", pHook->tshead.ThreadObjHead.headinfo.h, pHook->offPfn);break;
                        case         R_WH_MOUSE_LL: printf("Handle:%x; Type:WH_MOUSE_LL, FunAddr:%x\n", pHook->tshead.ThreadObjHead.headinfo.h, pHook->offPfn);break;
                        default: printf("Unknown\n");
                        }
                }//if
        }

        //////////////////////////////////////////////////////////////////////////
        VirtualFree(pHook, sizeof(HOOK), MEM_DECOMMIT);
        VirtualFree(pHandleEntryR, sizeof(HANDLEENTRY) *ServerInfo.cHandleEntries, MEM_DECOMMIT);
        _getch();
        return 0;
}


[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 7
支持
分享
最新回复 (5)
雪    币: 364
活跃值: (1736)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
2
不错,好东西,好好学习一下
2009-9-22 23:25
0
雪    币: 364
活跃值: (1736)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
3
能列出消息钩子对应进程路径更好了
2009-9-22 23:33
0
雪    币: 208
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
编译不能通过。
C:\1.cpp(98) : error C2143: syntax error : missing ';' before '*'
C:\1.cpp(98) : error C2501: 'HOOK' : missing storage-class or type specifiers
C:\1.cpp(98) : error C2501: 'phkNext' : missing storage-class or type specifiers
C:\1.cpp(129) : error C2059: syntax error : '__stdcall'
C:\1.cpp(136) : error C2091: function returns function
C:\1.cpp(137) : error C2146: syntax error : missing ';' before identifier 'NtSystemDebugControl'
C:\1.cpp(137) : error C2501: 'NTSYSTEMDEBUGCONTROL' : missing storage-class or type specifiers
C:\1.cpp(137) : fatal error C1004: unexpected end of file found
2010-4-5 19:25
0
雪    币: 33
活跃值: (11)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
ULONG uWowHandler =(ULONG)GetProcAddress(LoadLibraryW(L"user32.dll"), "UserRegisterWowHandlers");
        for (ULONG uAddr=uWowHandler; uAddr<=uWowHandler+0x250; uAddr++)
        {
                if (0x40c7 ==*(WORD*)uAddr && 0xb8 ==*(BYTE*)(uAddr+7))
                {
                        pSharedInfo=(PSHAREDINFO)(*(DWORD*)(uAddr+8));
                        printf("%x\n", pSharedInfo);
                        break;
                }
        }


这个编码是要搜索谁?
2010-4-5 23:00
0
雪    币: 230
活跃值: (10)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
6
[QUOTE=PEBOSS;786502]这个编码是要搜索谁?

[/QUOTE]

获取SHAREDINFO的指针。
2010-5-11 13:59
0
游客
登录 | 注册 方可回帖
返回
//