key.cpp

来自「简单的远程控制工具,分为服务器与客户斋,让你了解socket编程的知识.」· C++ 代码 · 共 209 行

CPP
209
字号
/*  Back Orifice 2000 - Remote Administration Suite
    Copyright (C) 1999, Cult Of The Dead Cow

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

	The author of this program may be contacted at dildog@l0pht.com. */

#include "stdafx.h"
#include "Ctrl_Clnt9.h"
#include "windowsx.h"
#include "key.h"


BOOL	g_bLogging = FALSE;
//HANDLE	g_hCapFile = NULL;
DWORD	g_dwKeyCapTID = 0;
HANDLE	g_hKeyCapThread = NULL;
//HWND	g_hLastFocus=NULL;


HHOOK	g_hLogHook=NULL;
LRESULT CALLBACK JournalLogProc(int code, WPARAM wParam, LPARAM lParam)
{
/*
键盘钩:
    将WM_KEYDOWN和WM_KEYUP转换为WM_SENDKEY_DOWN、WM_SENDKEY_UP

*/
//	DWORD	dwCount,dwBytes;
//	char	svBuffer[256];
	int  	vKey,nScan;


	if (code < 0) 
		return CallNextHookEx(g_hLogHook,code,wParam,lParam);

	if(code == HC_ACTION) 
	{
		EVENTMSG *pEvt = (EVENTMSG *)lParam;
		HWND hFocus = GetActiveWindow();
		switch(pEvt->message)
		{
		case WM_KEYDOWN:
			vKey = LOBYTE(pEvt->paramL);
			nScan = HIBYTE(pEvt->paramL);
			nScan <<= 16;
			
			// Check to see if focus has changed
			PostMessage(hFocus, WM_SENDKEY_DOWN, (WPARAM)vKey, (LPARAM)nScan);
			break;
		case WM_KEYUP:
			vKey = LOBYTE(pEvt->paramL);
			nScan = HIBYTE(pEvt->paramL);
			nScan <<= 16;
			PostMessage(hFocus, WM_SENDKEY_UP, (WPARAM)vKey, (LPARAM)nScan);
			break;
	}	}
	return CallNextHookEx(g_hLogHook,code,wParam,lParam);



}

/***************** No use ***************************/

DWORD WINAPI KeyCapThread(LPVOID param)
{
	MSG	msg;
/*	BYTE	keytbl[256];
	int	i;

	for (i = 0; i < 256; i++) 
		keytbl[i] = 0;
					
	g_bLogging = TRUE;
	g_hLastFocus = NULL;

	g_hCapFile = CreateFile((char *)param,
				GENERIC_WRITE,
				0,
				NULL,
				OPEN_ALWAYS,
				FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM,
				NULL);

	if(g_hCapFile == INVALID_HANDLE_VALUE) 
		return -1;

	SetFilePointer (g_hCapFile,0,NULL,FILE_END);
*/
	g_hLogHook = SetWindowsHookEx (WH_JOURNALRECORD,JournalLogProc,g_module,0);
	if(g_hLogHook==NULL) return -1;

	g_bLogging=TRUE;

	while(g_bLogging) 
	{
		while(PeekMessage(&msg,NULL,0,0,PM_NOREMOVE)) 
		{
			GetMessage(&msg,NULL,0,0);
			DispatchMessage(&msg);
			if(msg.message==WM_CANCELJOURNAL) 
			{
				//SetKeyboardState(keytbl);
				g_hLogHook=SetWindowsHookEx(WH_JOURNALRECORD,
							JournalLogProc,g_module,0);
				
				if(g_hLogHook==NULL) 
				{
					return -1;
				}
			} 
			else 
			{
				DispatchMessage(&msg);
			}
		}
		Sleep(2);
	}

	UnhookWindowsHookEx (g_hLogHook);
	
	g_hKeyCapThread = NULL;

	return 0;
}



/*
int CmdProc_SysLogKeys(char * strFile_Name)
{
	if(g_bLogging == TRUE)
		return -1;
	
	g_hKeyCapThread = CreateThread(NULL,0,KeyCapThread,
					(LPVOID)strFile_Name,0,
					&g_dwKeyCapTID);
	if(g_hKeyCapThread==NULL) 
		return -1;

	return 0;
}

int CmdProc_SysEndKeyLog(void)
{
	if(g_bLogging==FALSE)
		return 0;

	g_bLogging=FALSE;
	if(WaitForSingleObject(g_hKeyCapThread,5000)!=WAIT_OBJECT_0)
		return -1;

	return 0;
}
*/
BOOL CmdProc_LogHook(void)
{
	if (g_bLogging) return true;
	g_hKeyCapThread = CreateThread(NULL,0,KeyCapThread,
					0,0,
					&g_dwKeyCapTID);
	if(g_hKeyCapThread==NULL) 
		return false;

	return true;

/*
	if(g_hLogHook) return TRUE;
//	g_hLogHook = SetWindowsHookEx (WH_JOURNALRECORD,JournalLogProc,g_module,0);
	g_hLogHook = SetWindowsHookEx (WH_KEYBOARD,JournalLogProc,g_module,0);
	if(g_hLogHook==NULL) 
		return FALSE;
    else
	    return TRUE;
*/
}
BOOL CmdProc_EndHook(void)
{
	if(g_bLogging==FALSE)
		return true;

	g_bLogging=FALSE;
	if(WaitForSingleObject(g_hKeyCapThread,5000)!=WAIT_OBJECT_0)
		return false;

	return true;
/* 
    if(g_hLogHook==NULL) return TRUE;
	UnhookWindowsHookEx (g_hLogHook);
	g_hLogHook = NULL;
	return TRUE;
	*/

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?