客户端监控键盘记录器问题

时间:2009-05-11 06:10:03

标签: c# multithreading asynchronous threadpool

首先,我正在开发的键盘记录器完全不是出于攻击性和破坏性目的。 :)

我正在使用C#.NET开发一个客户端监控应用程序。 键盘记录是我的应用程序中的功能之一。 虽然我已经开发了键盘记录器的代码,但我还是无法在我的应用程序中正确实现它。

我的解决方案中有两个项目。 UserInterface - 用于服务器端。 跟踪器 - 用于客户端PC。 键盘记录模块Keylogger位于Tracker项目中。

我已经使用辅助类进行套接字编程--TcpClient,TcpListener和NetworkStream来帮助它们。

另外,我正在使用异步模式进行通信。

我发布了我遇到问题的代码部分:

//This code resides on the server-side monitoring interface.When //the administrator hits a btnKeyLog button, a 

message //"StartKeyLog" is sent to the respective client, and the keylogging //is handled on the client.

private void btnKeyLog_Click(object sender,EventArgs e) {         messageBuffer = new byte [100];

    if ( btnKeyLog1.Text == "Start Keylogging" )
    {
        btnKeyLog1.Text = "Stop Keylogging";
        message = "StartKeyLog";

        messageBuffer = Encoding.ASCII.GetBytes ( message );
        try
        {
                //begin writing on the stream.
        clientConnections[0].networkStream.BeginWrite (messageBuffer, 0, messageBuffer.Length, new 

AsyncCallback ( onDataWrite ), null );
        }
        catch ( Exception exc )
        {
            MessageBox.Show ( exc.Message + exc.StackTrace );
        }
    }
    else
    {
        btnKeyLog1.Text = "Start Keylogging";
        message = "StopKeyLog";

        messageBuffer = Encoding.ASCII.GetBytes ( message );
        try
        {
        clientConnections[0].networkStream.BeginWrite ( messageBuffer, 0, messageBuffer.Length, new 

AsyncCallback ( onDataWrite ), null );
        }
        catch ( Exception exc )
        {
            MessageBox.Show ( exc.Message + exc.StackTrace );
        }
    }
}

现在,客户端代码:

public void onDataReceived ( IAsyncResult ar )
{
    int nBytesRead = 0;
    try
    {
        nBytesRead = clientConnection.networkStream.EndRead ( ar );
    }
    catch ( Exception exc )
    {
        MessageBox.Show ( exc.Message + exc.StackTrace );
    }
    message = Encoding.ASCII.GetString ( messageBuffer,0, nBytesRead);
    switch (message)
    {
        case "StartKeyLog" :
            MessageBox.Show ( "Keylogger started." );
                       //the following static method wraps the Win32 //implementation of SetWindowsHookEx - all given in Keylogger //module
            KeyboardHook.installHook ( );
                       //after this method is called, the hook is //actually installed; the callback function KeyboardHookProc is also //called.        

    Here, keylogger seems to be working fine, except that the //system slows down considerably when i type keystrokes.
        break;

        case "StopKeyLog":
            MessageBox.Show ( "Keylogger stopped." );
                        // the following method releases the hook
            KeyboardHook.releaseHook ( );
        break;
    }

    try
    {
        messageBuffer = new byte[100];
        clientConnection.networkStream.BeginRead ( messageBuffer, 0, messageBuffer.Length, new AsyncCallback ( onDataReceived ), null );
    }
    catch ( Exception exc )
    {
        MessageBox.Show ( exc.Message + exc.StackTrace );
    }
    //MessageBox.Show ( "Stop" );
//as soon as this function ends, however, the callback function of //the keyboard hook stops being called; keystrokes are not //processed.
//the keystrokes are caught until this function the control is in this //function. i assume that it has to do something with the thread.
}

我试图解释这里的情况。 要开始键盘记录,服务器UI将向客户端发送消息“StartKeyLog”。 收到消息后,客户端将在回调函数“onDataReceived”中处理它。在此函数中,消息被处理并且

调用

installHook()方法,它将安装钩子。

当我运行应用程序时,挂钩已安装;此外,正确调用了KeyboardHookProc()回调,并处理了击键。但是这个

只有在onDataReceived回调方法处于活动状态时才会出现这种情况。一旦该方法结束,KeyboardHookProc()就会停止调用;钥匙

不再处理,好像挂钩从未安装过。

另一个问题是,在安装了钩子后,当我按下任何键时系统变得相当慢。

我的假设是这两件事都与这里发生的线程有关。但是,我无法得到确切的问题。 我尽力解释这种情况。但是,欢迎任何问题。 谁能为我提供解决方案?

2 个答案:

答案 0 :(得分:0)

您应该添加KeyboardHook的代码。

答案 1 :(得分:0)

好吧,我已经设法解决了我的问题。问题是我在我的应用程序的主线程调用的线程上设置了钩子。事实上,我已经启动该线程只是为了安装钩子。我只是重构了我的代码,以便钩子安装在主线程上。它解决了整个问题。 :)