客户端监控键盘记录问题
首先,我正在开发的键盘记录器根本不是用于攻击性和破坏性目的。 :)
我正在 C#.NET 中开发客户端监控应用程序。 键盘记录是我的应用程序中的功能之一。 尽管我已经开发了键盘记录器的代码,但我无法在我的应用程序中正确实现它。
我的解决方案中有两个项目。 用户界面 - 用于服务器端。 Tracker - 用于客户端 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 ) { 消息缓冲区=新字节[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() 就停止被调用; 密钥
不再处理
,就好像从未安装过钩子一样。 另一个问题是,安装钩子后,当我按任意键时,系统变得相当慢。
我的假设是这两件事都与这里发生的线程有关。 但是,我无法得到确切的问题。 我已尽力解释情况。不过,欢迎任何问题。 有人可以给我提供解决方案吗?
First of all, the keylogger that i am developing is not at all for offensive and destructive purposes. :)
I am developing a client monitoring application in C#.NET.
Keylogging is one of the features in my application.
Though i have developed the code for the keylogger, i have not been able to implement it properly in my application.
There are two projects in my solution.
The UserInterface - for server side.
The Tracker - for client side PCs.
The keylogging module Keylogger is in the Tracker project.
I have used the helper classes for socket programming - TcpClient, TcpListener and NetworkStream to help them out.
Also, i am using asynchronous mode for communication.
I am posting the part of code with which i am facing the problem :
//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 );
}
}
}
Now, the client-side code :
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.
}
I am trying to explain the situation here.
To start keylogging, the server UI would send a message "StartKeyLog" to the client.
On receiving the message, the client will process it in the callback function "onDataReceived".In this function, the message is processed and the
installHook() method is called, which would install the hook.
When i ran the application, the hook got installed; also, the KeyboardHookProc() callback got called properly, and the keystrokes were processed. But this
was the case only till the onDataReceived callback method was alive. As soon as the that method ended, the KeyboardHookProc() stopped getting called; keys
were no longer processed, as if the hook was never installed.
Another problem was that after the hook got installed, the system got considerably slow when i hit any key.
My assumption is that both the things have something to do with the threading that happens here. But, i am not able to get the exact problem.
I have tried my best to explain the situation.Still, any questions are welcome.
Could anyone provide me with the solution??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该添加 KeyboardHook 的代码。
You should add the code of your KeyboardHook.
好吧,我已经成功解决了我的问题。 问题是我正在应用程序的主线程调用的线程上设置挂钩。 事实上,我启动该线程只是为了安装挂钩。 我刚刚重组了我的代码,以便将钩子安装在主线程上。 它解决了整个问题。 :)
well, i have managed to solve my problem. The problem was that i was setting the hook on a thread that the main thread of my application had invoked. Infact, i had started that thread just to install the hook. I just restructured my code so that the hook was installed on the main thread. It solved out the whole problem. :)