-
Notifications
You must be signed in to change notification settings - Fork 38
/
ADHuntUser.cs
101 lines (90 loc) · 3.4 KB
/
ADHuntUser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using System.Xml;
using System.Net;
using System.Reflection;
using System.Globalization;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.Eventing.Reader;
using System.Security.Principal;
using System.Resources;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.Security;
namespace ADHuntUserEventParser
{
public enum Options
{
username = 0,
domain = 1,
ip = 2
}
class Program
{
static void Main(string[] args)
{
string[] queryString = new string[]
{
"*[System[(EventID=4624)] and EventData[Data[@Name=\"TargetUserName\"]=\"{0}\"]]",
"*[System[(EventID=4624)] and EventData[Data[@Name=\"TargetDomainName\"]=\"{0}\"]]",
"*[EventData[Data[@Name=\"IpAddress\"] and(Data=\"{0}\")]]"
};
string search = args[1];
if (!Enum.IsDefined(typeof(Options), args[0]))
{
Console.WriteLine("Invalid Option: username, domain, ip");
return;
}
Console.WriteLine("Searching for '{0}'", search);
int index = (int)Enum.Parse(typeof(Options), args[0]);
string query = String.Format(queryString[index], search);
Console.WriteLine("Querying: {0}", query);
foreach (DomainController target in Domain.GetCurrentDomain().DomainControllers)
{
try
{
Console.WriteLine("Parsing {0} ({1}) logs", target.IPAddress, target.Name);
EventLogSession els;
if (args.Length == 5)
{
Console.WriteLine("Using user provided credentials to authenticate as {0}\\{1}", args[2], args[3]);
SecureString ss = new SecureString();
foreach(char c in args[4])
{
ss.AppendChar(c);
}
ss.MakeReadOnly();
els = new EventLogSession(target.Name, args[2], args[3], ss, SessionAuthentication.Default);
} else
{
els = new EventLogSession(target.Name);
}
EventLogQuery logQuery = new EventLogQuery("Security", PathType.LogName, query);
logQuery.Session = els;
EventLogReader elr = new EventLogReader(logQuery);
while (true)
{
EventRecord er = elr.ReadEvent();
if (er == null)
{
break;
}
Console.WriteLine(er.FormatDescription() + "\r\n-----------------------------------\r\n");
if (er != null)
{
er.Dispose();
}
}
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e.Message);
}
}
}
}
}