LoginEvent.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace DecryptPwd.Utils
  8. {
  9. public class LoginEvent
  10. {
  11. /// <summary>
  12. /// 查询登录事件,需要管理员权限,从注册表中查询。
  13. /// </summary>
  14. public static void EventLog_4624()
  15. {
  16. EventLog log = new EventLog("Security");
  17. Console.WriteLine("\r\n========== SharpEventLog -> 4624 ==========\r\n");
  18. var entries = log.Entries.Cast<EventLogEntry>().Where(x => x.InstanceId == 4624);
  19. entries.Select(x => new
  20. {
  21. x.MachineName,
  22. x.Site,
  23. x.Source,
  24. x.Message,
  25. x.TimeGenerated
  26. }).ToList();
  27. foreach (EventLogEntry log1 in entries)
  28. {
  29. string text = log1.Message;
  30. string ipaddress = MidStrEx(text, " 源网络地址: ", " 源端口:");
  31. string username = MidStrEx(text, "新登录:", "进程信息:");
  32. username = MidStrEx(username, " 帐户名: ", " 帐户域: ");
  33. DateTime Time = log1.TimeGenerated;
  34. if (ipaddress.Length >= 7)
  35. {
  36. Console.WriteLine("\r\n-----------------------------------");
  37. Console.WriteLine("Time: " + Time);
  38. Console.WriteLine("Status: True");
  39. Console.WriteLine("Username: " + username.Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", ""));
  40. Console.WriteLine("Remote ip: " + ipaddress.Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", ""));
  41. }
  42. }
  43. }
  44. public static void EventLog_4625()
  45. {
  46. EventLog log = new EventLog("Security");
  47. Console.WriteLine("\r\n========== SharpEventLog -> 4625 ==========\r\n");
  48. var entries = log.Entries.Cast<EventLogEntry>().Where(x => x.InstanceId == 4625);
  49. entries.Select(x => new
  50. {
  51. x.MachineName,
  52. x.Site,
  53. x.Source,
  54. x.Message,
  55. x.TimeGenerated
  56. }).ToList();
  57. foreach (EventLogEntry log1 in entries)
  58. {
  59. string text = log1.Message;
  60. string ipaddress = MidStrEx(text, " 源网络地址: ", " 源端口:");
  61. string username = MidStrEx(text, "新登录:", "进程信息:");
  62. username = MidStrEx(username, " 帐户名: ", " 帐户域: ");
  63. DateTime Time = log1.TimeGenerated;
  64. if (ipaddress.Length >= 7)
  65. {
  66. Console.WriteLine("\r\n-----------------------------------");
  67. Console.WriteLine("Time: " + Time);
  68. Console.WriteLine("Status: Flase");
  69. Console.WriteLine("Username: " + username.Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", ""));
  70. Console.WriteLine("Remote ip: " + ipaddress.Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", ""));
  71. }
  72. }
  73. }
  74. public static string MidStrEx(string sourse, string startstr, string endstr)
  75. {
  76. string result = string.Empty;
  77. int startindex, endindex;
  78. startindex = sourse.IndexOf(startstr);
  79. if (startindex == -1)
  80. return result;
  81. string tmpstr = sourse.Substring(startindex + startstr.Length);
  82. endindex = tmpstr.IndexOf(endstr);
  83. if (endindex == -1)
  84. return result;
  85. result = tmpstr.Remove(endindex);
  86. return result;
  87. }
  88. }
  89. }