要像系统注册一个全局热键,需要用到RegisterHotKey,函数用法如下(MSDN):
BOOL RegisterHotKey(
HWND hWnd,
int id,
UINT fsModifiers,
UINT vk
);
函数功能:该函数定义一个系统范围的热键。
函数原型:BOOL RegisterHotKey(HWND hWnd,int id,UINT fsModifiers,UINT vk);
参数:
hWnd:接收热键产生WM_HOTKEY消息的窗口句柄。若该参数NULL,传递给调用线程的WM_HOTKEY消息必须在消息循环中中进行处理。
id:定义热键的标识符。调用线程中的其他热键不能使用同样的标识符。应用功能程序必须定义一个0X0000-0xBFFF范围的值。一个共享的动态链接库(DLL)必须
定义一个0xC000-0xFFFF范围的值伯GlobalAddAtom函数返回该范围)。为了避免与其他动态链接库定义的热键冲突,一个DLL必须使用GlobalAddAtom函数获得热键的标
识符。
fsModifoers:定义为了产生WM_HOTKEY消息而必须与由nVirtKey参数定义的键一起按下的键。该参数可以是如下值的组合:
MOD_ALT:按下的可以是任一Alt键。MOD_CONTROL:按下的可以是任一Ctrl键。
MOD_SHIFT:按下的可以是任一Shift键。
MOD_WIN:按下的可以是任一Windows按键。这些键可以用Microsoft Windows日志记录下来。
MOD_NOREPEAT:Windows 7或者后续版本: 更改热键行为,以便键盘自动重复不会产生多个热键通知。
vk:定义热键的虚拟键码。
返回值:若函数调用成功,返回一个非O值。若函数调用失败,则返回值为0。若要获得更多的错误信息,可以调用GetLastError函数。
备注:当某键被接下时,系统在所有的热键中寻找匹配者。一旦找到一个匹配的热键,系统将把WM_HOTKEY消息传递给登记了该热键的线程的消息队列。该消息被传
送到队列头部,因此它将在下一轮消息循环中被移去。该函数不能将热键同其他线程创建的窗口关联起来。
若为一热键定义的击键己被其他热键所定义,则RegisterHotKey函数调用失败。
若hWnd参数标识的窗口已用与id参数定义的相同的标识符登记了一个热键,则参数fsModifiers和vk的新值将替代这些参数先前定义的值。
Windows CE:Windows CE 2.0以上版本对于参数fsModifiers支持一个附加的标志位。叫做MOD_KEYUP。
若设置MOD_KEYUP位,则当发生键被按下或被弹起的事件时,窗口将发送WM_HOTKEY消息。
RegisterHotKey可以被用来在线程之间登记热键。
速查:Windows NT:3.1及以上版本;Windows:95及以上版本;Windows CE:不支持;头文件:winuser.h;库文件:Hotkey.lib。
F12键是调试器所使用的保留,所以不应将其注册为热键
#define MOD_ALT 0x0001 = 1 #define MOD_CONTROL 0x0002 = 10 #define MOD_SHIFT 0x0004 = 100 #define MOD_WIN 0x0008 = 1000
在IDA中反汇编RegisterHotKey
.text:77D1EBB3 mov eax, 11EAh //系统服务号 .text:77D1EBB8 mov edx, 7FFE0300h .text:77D1EBBD call dword ptr [edx] .text:77D1EBBF retn 10h .text:77D1EBBF _NtUserRegisterHotKey@16 endp
OD查看得到7FFE0300
dd 7FFE0300 7FFE0300 7C92E510 ntdll.KiFastSystemCall 7FFE0304 7C92E514 ntdll.KiFastSystemCallRet
lkd> dd ffdf0300 l2 ffdf0300 7c92e510 7c92e514 lkd> u 7c92e510 7c92e510 8bd4 mov edx,esp 7c92e512 0f34 sysenter
余下的大约有3K
USER:0x7FFE0000
KERNEL:0x0FFDF0000
在Windbg可用dt nt!_KUSER_SHARED_DATA命令查看该共享区域
lkd> dt nt!_KUSER_SHARED_DATA +0x000 TickCountLow : Uint4B +0x004 TickCountMultiplier : Uint4B +0x008 InterruptTime : _KSYSTEM_TIME +0x014 SystemTime : _KSYSTEM_TIME +0x020 TimeZoneBias : _KSYSTEM_TIME +0x02c ImageNumberLow : Uint2B +0x02e ImageNumberHigh : Uint2B +0x030 NtSystemRoot : [260] Uint2B +0x238 MaxStackTraceDepth : Uint4B +0x23c CryptoExponent : Uint4B +0x240 TimeZoneId : Uint4B +0x244 Reserved2 : [8] Uint4B +0x264 NtProductType : _NT_PRODUCT_TYPE +0x268 ProductTypeIsValid : UChar +0x26c NtMajorVersion : Uint4B +0x270 NtMinorVersion : Uint4B +0x274 ProcessorFeatures : [64] UChar +0x2b4 Reserved1 : Uint4B +0x2b8 Reserved3 : Uint4B +0x2bc TimeSlip : Uint4B +0x2c0 AlternativeArchitecture : _ALTERNATIVE_ARCHITECTURE_TYPE +0x2c8 SystemExpirationDate : _LARGE_INTEGER +0x2d0 SuiteMask : Uint4B +0x2d4 KdDebuggerEnabled : UChar +0x2d5 NXSupportPolicy : UChar +0x2d8 ActiveConsoleId : Uint4B +0x2dc DismountCount : Uint4B +0x2e0 ComPlusPackage : Uint4B +0x2e4 LastSystemRITEventTickCount : Uint4B +0x2e8 NumberOfPhysicalPages : Uint4B +0x2ec SafeBootMode : UChar +0x2f0 TraceLogging : Uint4B +0x2f8 TestRetInstruction : Uint8B +0x300 SystemCall : Uint4B +0x304 SystemCallReturn : Uint4B +0x308 SystemCallPad : [3] Uint8B +0x320 TickCount : _KSYSTEM_TIME +0x320 TickCountQuad : Uint8B +0x330 Cookie : Uint4B
并导出KeServiceDescriptorTable指针
可见该函数没做任何处理直接进入内核(win32k.sys)中,在Windbg反汇编:
lkd> uf win32k!NtUserRegisterHotKey win32k!NtUserRegisterHotKey+0x34: bf899720 33c0 xor eax,eax //eax = NULL bf899722 eb29 jmp win32k!NtUserRegisterHotKey+0x36 (bf89974d) win32k!NtUserRegisterHotKey: bf899729 8bff mov edi,edi bf89972b 55 push ebp bf89972c 8bec mov ebp,esp bf89972e 56 push esi bf89972f e8b673f6ff call win32k!EnterCrit (bf800aea) bf899734 f74510f07fffff test dword ptr [ebp+10h],0FFFF7FF0h //fsModifiers是否有效,是否大于1000b 11111111111111110111111111110000 bf89973b 752d jne win32k!NtUserRegisterHotKey+0x14 (bf89976a)//fsModifiers无效则跳转 win32k!NtUserRegisterHotKey+0x20: bf89973d 8b4d08 mov ecx,dword ptr [ebp+8] //hWnd bf899740 85c9 test ecx,ecx bf899742 74dc je win32k!NtUserRegisterHotKey+0x34 (bf899720)//hWnd == NULL win32k!NtUserRegisterHotKey+0x27: bf899744 e86a7ef6ff call win32k!ValidateHwnd (bf8015b3)//则验证句柄 bf899749 85c0 test eax,eax bf89974b 7427 je win32k!NtUserRegisterHotKey+0x30 (bf899774) //返回NULL win32k!NtUserRegisterHotKey+0x36: bf89974d ff7514 push dword ptr [ebp+14h] //vk bf899750 ff7510 push dword ptr [ebp+10h] //fsModifiers bf899753 ff750c push dword ptr [ebp+0Ch] //id bf899756 50 push eax //pWnd bf899757 e8aefeffff call win32k!_RegisterHotKey (bf89960a) bf89975c 8bf0 mov esi,eax win32k!NtUserRegisterHotKey+0x47: bf89975e e8b373f6ff call win32k!LeaveCrit (bf800b16) bf899763 8bc6 mov eax,esi bf899765 5e pop esi bf899766 5d pop ebp bf899767 c21000 ret 10h win32k!NtUserRegisterHotKey+0x14: bf89976a 68ec030000 push 3ECh //错误码:1004,参数无效 bf89976f e83da0f6ff call win32k!UserSetLastError (bf8037b1) win32k!NtUserRegisterHotKey+0x30: bf899774 33f6 xor esi,esi bf899776 ebe6 jmp win32k!NtUserRegisterHotKey+0x47 (bf89975e) /***************************************/ PWND FASTCALL ValidateHwnd( HWND hwnd);
BOOLEN APIENTRY
NtUserRegisterHotKey(HWND hWnd,
int id,
UINT fsModifiers,
UINT vk)
{
BOOLEN bRet;
PWND pWnd = NULL;
EnterCrit();
if(!(fsModifiers & 0x0FFFF7FF0h))
{
if(hWnd)
{
pWnd = ValidateHwnd(hWnd);
}
bRet = _RegisterHotKey(pWnd,id,fsModifiers,vk);
}
else
{
UserSetLastError(1004);//1004无效标志
bRet = FALSE;
}
LeaveCrit();
return bRet;
}
typedef struct _HOT_KEY_ITEM
{
PETHREAD Thread;
HWND spwnd;
UINT fsModifiers;
UINT vk;
int id;
struct _HOT_KEY_ITEM phkNext;
} HOT_KEY_ITEM, *PHOT_KEY_ITEM;
BOOL _RegisterHotKey(
PWND pwnd,
int id,
UINT fsModifiers,
UINT vk)
{
PHOT_KEY_ITEM phk;
BOOL fKeysExist = FALSE;
PTHREADINFO ptiCurrent;
PWINDOWSTATION pwinsta = _GetProcessWindowStation(NULL);
DWORD ErrorCode;
ptiCurrent = gptiCurrent;
//如果调用者不是WindowStation初始化的线程和不适当的权限
if(grpwinstaList && !CheckWinstaWriteAttributesAccess())
{
return FALSE;
}
//不能为其他线程的窗口注册热键
if ((pwnd != PWND_FOCUS) && (pwnd != PWND_INPUTOWNER))
{
if (GETPTI(pwnd) != ptiCurrent)
{
UserSetLastError(1408); //1408错误码:无效窗口;它属于另一线程。
return FALSE;
}
}
phk = FindHotKey(ptiCurrent, pwnd, id, fsModifiers, vk, FALSE, &fKeysExist);
//如果其他线程已经注册过该热键,返回FALSE
if (fKeysExist)
{
UserSetLastError(1409); //1409错误码:热键已被注册
return FALSE;
}
if (phk == NULL)
{
//热键并未被注册
phk = (PHOT_KEY_ITEM)HeavyAllocPool(sizeof(HOT_KEY_ITEM), TAG_HOTKEY);
//分配失败,返回FALSE
if (phk == NULL)
{
return FALSE;
}
phk->pti = ptiCurrent;
if ((pwnd != PWND_FOCUS) && (pwnd != PWND_INPUTOWNER))
{
phk->spwnd = NULL;
HMAssignmentLock(&phk->spwnd, pwnd);
}
else
{
phk->spwnd = pwnd;
}
phk->fsModifiers = fsModifiers;
phk->vk = vk;
phk->id = id;
//插入到系统热键链表中
//gphkFirst - 这是不导出变量存储了系统结构热键(phkNext指向下一个热键结构域)地址
phk->phkNext = gphkFirst;
gphkFirst = phk;
}
else
{
//如果本线程已注册过该热键,则重新覆盖
phk->fsModifiers = fsModifiers;
phk->vk = vk;
}
return TRUE;
}
lkd> dd gphkFirst L1 bf9af814 e2ce10d8
e2ce10d8就是最近一次软件向系统注册的全局热键,继续
lkd> dd e2ce10d8 l6 e2ce10d8 e2265008 bbe35a28 00000003 00000054 e2ce10e8 0000c024 e2291a68
bbe35a28 是窗口句柄
00000003 是功能键11,说明有Ctrl+Alt键
00000054 是VK_?,0x54对应ASCI码的大写T,Ctrl+ATL+T(QQ上:发送腾讯微博的)
0000c024 是热键的ID
e2291a68 是下一个热键结构
PHOT_KEY_ITEM FindHotKey(
PTHREADINFO ptiCurrent,
PWND pwnd,
int id,
UINT fsModifiers,
UINT vk,
BOOL fUnregister,
PBOOL pfKeysExist)
{
PHOT_KEY_ITEM phk, phkRet, phkPrev;
//初始化返回值
*pfKeysExist = FALSE;
phkRet = NULL;
phk = gphkFirst;
while (phk)
{
if ((phk->pti == ptiCurrent) && (phk->spwnd == pwnd) && (phk->id == id))
{
if (fUnregister)
{
//摘掉热键
if (phk == gphkFirst)
{
gphkFirst = phk->phkNext;
}
else
{
phkPrev->phkNext = phk->phkNext;
}
if ((pwnd != PWND_FOCUS) && (pwnd != PWND_INPUTOWNER))
{
Unlock(&phk->spwnd);
}
UserFreePool((PVOID)phk);
return((PHOT_KEY_ITEM)1);
}
phkRet = phk;
}
//如果热键已经注册过,设置已存在标志
if ((phk->fsModifiers == fsModifiers) && (phk->vk == vk))
{
if (phk->spwnd == PWND_FOCUS)
{
if (phk->pti == ptiCurrent)
{
*pfKeysExist = TRUE;
}
}
else
{
*pfKeysExist = TRUE;
}
}
phkPrev = phk;
phk = phk->phkNext;
}
return phkRet;
}
VOID DumpHotKeys()
{
ULONG dwAddr;
KAPC_STATE ApcState;
PETHREAD pThread;
PEPROCESS pProc;
PHOTKEY phk;
//必须在GUI线程中遍历
KeStackAttachProcess( pExpEprocess , &ApcState );
dwAddr = *(PULONG)gphkFirst;
KeUnstackDetachProcess(&ApcState);
phk = (PHOTKEY)dwAddr;
//解析系统所有热键
while( phk != NULL )
{
pThread = *(PULONG)phk->pti;
//0x220位置指向当前线程的EPROCESS
pProc = *(PULONG)( (ULONG)pThread + 0x220 );
//EPROCESS + 0x174指向进程名字
KdPrint(("Process Name : %s\n" , (ULONG)pProc + 0x174 ));
KdPrint(("id : %d\n" , phk->id ));
KdPrint(("Combination : %s + %X\n" , GetButton( phk->fsModifiers ) , phk->vk ));
KdPrint(("------------------------------------------\n"));
phk = phk->phkNext;
}
}