-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
237 lines (223 loc) · 10.2 KB
/
Program.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace WMIParserStr
{
class Program
{
public static Arguments CommandLine;
public static string entrada;
public static List<Details> LConsumers = new List<Details>();
public static List<Details> LEventFilters = new List<Details>();
public static List<Details> LBindings = new List<Details>();
static void Main(string[] args)
{
//----------------------------------------------------------
CommandLine = new Arguments(args);
if ((!string.IsNullOrEmpty(CommandLine["i"])) && (File.Exists(CommandLine["i"]))) entrada = CommandLine["i"];
else Ayuda();
string data = File.ReadAllText(entrada);
Regex getNames = new Regex(@"(.*).Name=""(.*)""", RegexOptions.IgnoreCase | RegexOptions.Compiled);
Regex strings = new Regex(@"[ -~]{3,}", RegexOptions.IgnoreCase | RegexOptions.Compiled);
var matchesStrings = strings.Matches(data);
int longObjects = matchesStrings.Count;
for (int i = 0; i < longObjects; i++)
{
Match m = matchesStrings[i];
if (m.Value.Contains(@"EventConsumer.Name="""))
{
var nameConsumer = getNames.Matches(m.Value);
i++;
m = matchesStrings[i];
var nameEventFilter = getNames.Matches(m.Value);
LBindings.Add(new Details("Binding", nameConsumer[0].Groups[1].Value, nameConsumer[0].Groups[2].Value, nameEventFilter[0].Groups[2].Value, false));
}
}
for (int i = 0; i < longObjects; i++)
{
Match m = matchesStrings[i];
string type = "";
string name = "";
string content = "";
string other = "";
if (m.Value == "ActiveScriptEventConsumer")
{
bool valid = false;
type = "ActiveScriptEventConsumer";
i++;
name = matchesStrings[i].Value;
i++;
if ((matchesStrings[i].Value.ToLower() == "vbscript") || (matchesStrings[i].Value.ToLower() == "jscript"))
{
valid = true;
other = matchesStrings[i].Value;
i++;
content = matchesStrings[i].Value;
}
else if ((matchesStrings[i + 1].Value.ToLower() == "vbscript") || (matchesStrings[i + 1].Value.ToLower() == "jscript"))
{
valid = true;
content = matchesStrings[i].Value;
i++;
other = matchesStrings[i].Value;
}
if (valid)
{
//check if it is a consumer without binding
var match = LBindings.FirstOrDefault(item => item.Content.Equals(name));
if (match != null) LConsumers.Add(new Details(type, name, content, other, false));
else LConsumers.Add(new Details(type, name, content, other, true));
}
else
{
i -= 2;
}
}
else if (m.Value == "CommandLineEventConsumer")
{
type = "CommandLineEventConsumer";
i++;
content = matchesStrings[i].Value;
i++;
string temp = matchesStrings[i].Value;
var match = LBindings.FirstOrDefault(item => item.Content.Equals(temp));
if (match != null)
{
name = temp;
LConsumers.Add(new Details(type, name, content, "", false));
}
else if (temp.ToLower().EndsWith(".exe"))
{
other = temp;
i++;
name = matchesStrings[i].Value;
match = LBindings.FirstOrDefault(item => item.Content.Equals(name));
if (match != null) LConsumers.Add(new Details(type, name, content, "", false));
else LConsumers.Add(new Details(type, name, content, "", true));
}
else
{
name = temp;
LConsumers.Add(new Details(type, name, content, "", true));
}
}
else if (m.Value == "__EventFilter")
{
type = "__EventFilter";
i++;
other = matchesStrings[i].Value;
i++;
name = matchesStrings[i].Value;
i++;
content = matchesStrings[i].Value;
for (int n = 0; n < 7; n++)
{
i++;
string temp = matchesStrings[i].Value;
if ((temp.ToLower() == "wql") || (n==6))
{
var match = LBindings.FirstOrDefault(item => item.Other.Equals(name));
if (match != null) LEventFilters.Add(new Details(type, name, content, other, false));
else LEventFilters.Add(new Details(type, name, content, other, true));
break;
}
else
{
content += temp;
}
}
}
}
OutputToConsole();
if ((!string.IsNullOrEmpty(CommandLine["s"])) && (Directory.Exists(CommandLine["s"])))
{
WriteStrings(matchesStrings);
}
if ((!string.IsNullOrEmpty(CommandLine["o"])) && (Directory.Exists(CommandLine["o"])))
{
OutputToFile();
}
}
private static void WriteStrings(MatchCollection matchesStrings)
{
using (FileStream fileStream = new FileStream(Path.Combine(CommandLine["s"], "WMIParserStr-OBJECTS_strings.txt"), FileMode.Create, FileAccess.Write))
using (StreamWriter writer = new StreamWriter(fileStream))
{
foreach (var line in matchesStrings)
{
writer.WriteLine(line.ToString());
}
}
}
private static void OutputToConsole()
{
Console.WriteLine("Total Bindings: {0}", LBindings.Count);
foreach (var b in LBindings)
{
Console.WriteLine("[{0}]-[{1}]-[{2}]-[{3}]-[{4}]", b.Type, b.Name, b.Content, b.Other, b.Orphan);
}
Console.WriteLine("Total Consumers: {0}", LConsumers.Count);
foreach (var b in LConsumers)
{
Console.WriteLine("[{0}]-[{1}]-[{2}]-[{3}]-[{4}]", b.Type, b.Name, b.Content, b.Other, b.Orphan);
}
Console.WriteLine("Total EventFilters: {0}", LEventFilters.Count);
foreach (var b in LEventFilters)
{
Console.WriteLine("[{0}]-[{1}]-[{2}]-[{3}]-[{4}]", b.Type, b.Name, b.Content, b.Other, b.Orphan);
}
}
private static void OutputToFile()
{
using (FileStream fileStream = new FileStream(Path.Combine(CommandLine["o"], "WMIParserStr-output.tsv"), FileMode.Create, FileAccess.Write))
using (StreamWriter writer = new StreamWriter(fileStream))
{
writer.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}", "Type", "Name", "Content", "Other", "Orphan");
foreach (var item in LBindings)
{
writer.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}", item.Type.ToString(), item.Name.ToString(), item.Content.ToString(), item.Other.ToString(), item.Orphan.ToString());
}
foreach (var item in LConsumers)
{
writer.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}", item.Type.ToString(), item.Name.ToString(), item.Content.ToString(), item.Other.ToString(), item.Orphan.ToString());
}
foreach (var item in LEventFilters)
{
writer.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}", item.Type.ToString(), item.Name.ToString(), item.Content.ToString(), item.Other.ToString(), item.Orphan.ToString());
}
}
}
private static void Ayuda()
{
var ayuda = $"Author: Ignacio J. Pérez Jiménez\r\ngithub.com/ignacioj\r\n\r\n-i Input file (OBJECTS.DATA)\r\n-o Output directory for analysis results\r\n-s Ouput directory to save the strings (not Unicode) of OBJECTS.DATA";
Console.WriteLine(ayuda);
System.Environment.Exit(0);
}
}
/*
Type Name Content Other Orphan
Bindings: Binding Type of Consumer Consumer name EventFilter name FALSE
Consumers: Type of consumer Consumer name CommandLineTemplate [ExecutablePath][VBScript/JSCript] False/True
EventFilter: __EventFilter EventFilter name Condition [root\cimv2][...]
*/
public class Details
{
public string Type { get; set; }
public string Name { get; set; }
public string Content { get; set; }
public string Other { get; set; }
public bool Orphan { get; set; }
public Details(string type, string name, string content, string other, bool orphan)
{
Type = type;
Name = name;
Content = content;
Other = other;
Orphan = orphan;
}
}
}