![]() |
Tutorial [C#]Simple keylogger with Keyboardhook - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Visual Basic & .NET Framework (https://sinister.ly/Forum-Visual-Basic-NET-Framework) +--- Thread: Tutorial [C#]Simple keylogger with Keyboardhook (/Thread-Tutorial-C-Simple-keylogger-with-Keyboardhook) |
[C#]Simple keylogger with Keyboardhook - Prestige - 11-25-2012 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 Spoiler: Preview: (warning big) Step 4: add the following line of code to the top of your class (above the namespace): Code: using System; 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); 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; 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() 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 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); 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) 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; 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; PS: I know I suck at explaining but I hope it was clear for you guys RE: [C#]Simple keylogger with Keyboardhook - Daaksin - 11-26-2012 Are you going to credit where this code came from or..? RE: [C#]Simple keylogger with Keyboardhook - Prestige - 11-26-2012 (11-26-2012, 05:44 AM)Daaksin Wrote: Are you going to credit where this code came from or..? Sure: credits to prestige for the code and for microsoft for the dll files for keyboardhook RE: [C#]Simple keylogger with Keyboardhook - Prestige - 12-04-2012 (12-04-2012, 08:13 AM)Scophie Wrote: Hey, you have done a good jod, so you made it yourself? How can I know the keylogger is safe? 1. It's a tutorial 2. This needs lot's of editing if you want to use it to hack someone it's an example of keyhook like spam text if someone presses f1 RE: [C#]Simple keylogger with Keyboardhook - Ultimatum - 12-04-2012 Heh, Windows 8, nice. How did you get the start bar there? RE: [C#]Simple keylogger with Keyboardhook - Prestige - 12-04-2012 (12-04-2012, 08:55 AM)Ultimatum Wrote: Heh, Windows 8, nice. How did you get the start bar there? Start 8 cracked xD RE: [C#]Simple keylogger with Keyboardhook - YP. - 12-07-2012 Thank you for the share! Good job! Hopefully I can learn something from this ![]() RE: [C#]Simple keylogger with Keyboardhook - SQLi - 07-23-2013 Good job, here's a Visual Basic .NET port: Keyboard.vb Code: Public Class Keyboard Example usage Code: Public Class Program |