forked from ignacioj/WMIParserStr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
266 lines (246 loc) · 10.7 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
namespace WMIParserStr
{
class Program
{
public static Arguments CommandLine;
public static string entry;
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["input"])) && (File.Exists(CommandLine["input"])))
entry = CommandLine["input"];
else
DisplayHelp();
string data = File.ReadAllText(entry);
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;
ProcessBindings(getNames, matchesStrings, longObjects);
ProcessConsumers(getNames, matchesStrings, longObjects);
ProcessEventFilters(getNames, matchesStrings, longObjects);
OutputToConsole();
if ((!string.IsNullOrEmpty(CommandLine["strings"])) && (Directory.Exists(CommandLine["strings"])))
{
WriteStrings(matchesStrings);
}
if ((!string.IsNullOrEmpty(CommandLine["output"])) && (Directory.Exists(CommandLine["output"])))
{
OutputToFile();
}
}
private static void ProcessBindings(Regex getNames, MatchCollection matchesStrings, int longObjects)
{
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));
}
}
}
private static void ProcessConsumers(Regex getNames, MatchCollection matchesStrings, int longObjects)
{
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)
{
var match = LBindings.Find(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.Find(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.Find(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));
}
}
}
}
private static void ProcessEventFilters(Regex getNames, MatchCollection matchesStrings, int longObjects)
{
for (int i = 0; i < longObjects; i++)
{
Match m = matchesStrings[i];
string type = "";
string name = "";
string content = "";
string other = "";
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.Find(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;
}
}
}
}
}
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 WriteStrings(MatchCollection matchesStrings)
{
string outputPath = Path.Combine(CommandLine["strings"], "WMIParserStr-OBJECTS_strings.txt");
using (FileStream fileStream = new FileStream(outputPath, FileMode.Create, FileAccess.Write))
using (StreamWriter writer = new StreamWriter(fileStream))
{
foreach (var line in matchesStrings)
{
writer.WriteLine(line.ToString());
}
}
}
private static void OutputToFile()
{
string outputPath = Path.Combine(CommandLine["output"], "WMIParserStr-output.tsv");
using (FileStream fileStream = new FileStream(outputPath, 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 DisplayHelp()
{
var help = "Author: Ignacio J. Pérez Jiménez\r\n" +
"github.com/ignacioj\r\n\r\n" +
"-input: Input file (OBJECTS.DATA)\r\n" +
"-output: Output directory for analysis results\r\n" +
"-strings: Output directory to save the strings (not Unicode) of OBJECTS.DATA";
Console.WriteLine(help);
Environment.Exit(0);
}
}
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;
}
}
}