-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathSharpGen.cs
More file actions
446 lines (408 loc) · 18.3 KB
/
SharpGen.cs
File metadata and controls
446 lines (408 loc) · 18.3 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Author: Ryan Cobb (@cobbr_io)
// Project: SharpGen (https://github.com/cobbr/SharpGen)
// License: BSD 3-Clause
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis;
using McMaster.Extensions.CommandLineUtils;
using McMaster.Extensions.CommandLineUtils.Validation;
using System.ComponentModel.DataAnnotations;
using Confuser.Core;
using Confuser.Core.Project;
using YamlDotNet.Serialization;
namespace SharpGen
{
class SharpGen
{
static void Main(string[] args)
{
CommandLineApplication app = new CommandLineApplication();
app.HelpOption("-? | -h | --help");
app.ThrowOnUnexpectedArgument = false;
// Required arguments
CommandOption OutputFileOption = app.Option(
"-f | --file <OUTPUT_FILE>",
"The output file to write to.",
CommandOptionType.SingleValue
).IsRequired();
// Compilation-related arguments
CommandOption<Compiler.DotNetVersion> DotNetVersionOption = app.Option<Compiler.DotNetVersion>(
"-d | --dotnet | --dotnet-framework <DOTNET_VERSION>",
"The Dotnet Framework version to target (net35 or net40).",
CommandOptionType.SingleValue
);
DotNetVersionOption.Validators.Add(new MustBeDotNetVersionValidator());
CommandOption OutputKindOption = app.Option(
"-o | --output-kind <OUTPUT_KIND>",
"The OutputKind to use (dll or console).",
CommandOptionType.SingleValue
);
OutputKindOption.Validators.Add(new MustBeOutputKindValidator());
CommandOption PlatformOption = app.Option(
"-p | --platform <PLATFORM>",
"The Platform to use (AnyCpy, x86, or x64).",
CommandOptionType.SingleValue
);
PlatformOption.Validators.Add(new MustBePlatformValidator());
CommandOption NoOptimizationOption = app.Option(
"-n | --no-optimization",
"Don't use source code optimization.",
CommandOptionType.NoValue
);
CommandOption AssemblyNameOption = app.Option(
"-a | --assembly-name <ASSEMBLY_NAME>",
"The name of the assembly to be generated.",
CommandOptionType.SingleValue
);
AssemblyNameOption.Validators.Add(new MustBeIdentifierValidator());
// Source-related arguments
CommandOption SourceFileOption = app.Option(
"-s | --source-file <SOURCE_FILE>",
"The source code to compile.",
CommandOptionType.SingleValue
).Accepts(v => v.ExistingFile());
CommandOption ClassNameOption = app.Option(
"-c | --class-name <CLASS_NAME>",
"The name of the class to be generated.",
CommandOptionType.SingleValue
);
ClassNameOption.Validators.Add(new MustBeIdentifierValidator());
CommandOption ConfuseOption = app.Option(
"--confuse <CONFUSEREX_PROJECT_FILE>",
"The ConfuserEx ProjectFile configuration.",
CommandOptionType.SingleValue
).Accepts(v => v.ExistingFile());
app.OnExecute(() =>
{
Compiler.CompilationRequest request = new Compiler.CompilationRequest();
// Compilation options
if (!SetRequestDirectories(ref request))
{
SharpGenConsole.PrintFormattedErrorLine("Unable to specify CompilationRequest directories");
app.ShowHelp();
return -1;
}
if (!SetRequestDotNetVersion(DotNetVersionOption, ref request))
{
SharpGenConsole.PrintFormattedErrorLine("Invalid DotNetVersion specified.");
app.ShowHelp();
return -2;
}
if (!SetRequestOutputKind(OutputKindOption, OutputFileOption, ref request))
{
SharpGenConsole.PrintFormattedErrorLine("Invalid OutputKind specified.");
app.ShowHelp();
return -3;
}
if (!SetRequestPlatform(PlatformOption, ref request))
{
SharpGenConsole.PrintFormattedErrorLine("Invalid Platform specified.");
app.ShowHelp();
return -4;
}
if (!SetRequestOptimization(NoOptimizationOption, ref request))
{
SharpGenConsole.PrintFormattedErrorLine("Invalid NoOptimization specified.");
app.ShowHelp();
return -5;
}
if (!SetRequestAssemblyName(AssemblyNameOption, ref request))
{
SharpGenConsole.PrintFormattedErrorLine("Invalid AssemblyName specified.");
app.ShowHelp();
return -6;
}
if (!SetRequestReferences(ref request))
{
SharpGenConsole.PrintFormattedErrorLine("Unable to set CompilationRequest references.");
app.ShowHelp();
return -7;
}
if (!SetRequestEmbeddedResources(ref request))
{
SharpGenConsole.PrintFormattedErrorLine("Unable to set CompilationRequest resources.");
app.ShowHelp();
return -8;
}
// Source options
if (!SetRequestSource(SourceFileOption, ClassNameOption, app.RemainingArguments, ref request))
{
SharpGenConsole.PrintFormattedErrorLine("Unable to create source code for request.");
app.ShowHelp();
return -9;
}
// Compile
SharpGenConsole.PrintFormattedProgressLine("Compiling source: ");
SharpGenConsole.PrintInfoLine(request.Source);
try
{
byte[] compiled = Compiler.Compile(request);
// Write to file
string path = Path.Combine(Common.SharpGenOutputDirectory, OutputFileOption.Value());
File.WriteAllBytes(path, compiled);
if (ConfuseOption.HasValue())
{
ConfuserProject project = new ConfuserProject();
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
string ProjectFile = String.Format(
File.ReadAllText(ConfuseOption.Value()),
Common.SharpGenOutputDirectory,
Common.SharpGenOutputDirectory,
OutputFileOption.Value()
);
doc.Load(new StringReader(ProjectFile));
project.Load(doc);
project.ProbePaths.Add(Common.Net35Directory);
project.ProbePaths.Add(Common.Net40Directory);
SharpGenConsole.PrintFormattedProgressLine("Confusing assembly...");
ConfuserParameters parameters = new ConfuserParameters();
parameters.Project = project;
parameters.Logger = new ConfuserConsoleLogger();
Directory.SetCurrentDirectory(Common.SharpGenRefsDirectory);
ConfuserEngine.Run(parameters).Wait();
}
SharpGenConsole.PrintFormattedHighlightLine("Compiled assembly written to: " + path);
}
catch (CompilerException e)
{
SharpGenConsole.PrintFormattedErrorLine(e.Message);
return -10;
}
catch (ConfuserException e)
{
SharpGenConsole.PrintFormattedErrorLine("Confuser Exception: " + e.Message);
return -11;
}
return 0;
});
app.Execute(args);
}
private static bool SetRequestDirectories(ref Compiler.CompilationRequest request)
{
request.SourceDirectory = Common.SharpGenDirectory;
request.ReferenceDirectory = Common.SharpGenReferencesDirectory;
request.ResourceDirectory = Common.SharpGenResourcesDirectory;
return true;
}
private static bool SetRequestDotNetVersion(CommandOption<Compiler.DotNetVersion> DotNetVersionOption, ref Compiler.CompilationRequest request)
{
request.TargetDotNetVersion = DotNetVersionOption.HasValue() ? DotNetVersionOption.ParsedValue : Compiler.DotNetVersion.Net35;
return true;
}
private static bool SetRequestOutputKind(CommandOption OutputKindOption, CommandOption OutputFileOption, ref Compiler.CompilationRequest request)
{
if (OutputKindOption.HasValue())
{
if (OutputKindOption.Value().Contains("console", StringComparison.OrdinalIgnoreCase) || OutputKindOption.Value().Contains("exe", StringComparison.OrdinalIgnoreCase))
{
request.OutputKind = OutputKind.ConsoleApplication;
}
else if (OutputKindOption.Value().Contains("dll", StringComparison.OrdinalIgnoreCase) || OutputKindOption.Value().Contains("dynamicallylinkedlibrary", StringComparison.OrdinalIgnoreCase))
{
request.OutputKind = OutputKind.DynamicallyLinkedLibrary;
}
else
{
return false;
}
}
else if (OutputFileOption.HasValue())
{
if (OutputFileOption.Value().EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
{
request.OutputKind = OutputKind.ConsoleApplication;
}
else if (OutputFileOption.Value().EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
{
request.OutputKind = OutputKind.DynamicallyLinkedLibrary;
}
}
return true;
}
private static bool SetRequestPlatform(CommandOption PlatformOption, ref Compiler.CompilationRequest request)
{
if (PlatformOption.HasValue())
{
if (PlatformOption.Value().Equals("x86", StringComparison.OrdinalIgnoreCase))
{
request.Platform = Platform.X86;
}
else if (PlatformOption.Value().Equals("x64", StringComparison.OrdinalIgnoreCase))
{
request.Platform = Platform.X64;
}
else if (PlatformOption.Value().Equals("AnyCpu", StringComparison.OrdinalIgnoreCase))
{
request.Platform = Platform.AnyCpu;
}
else
{
return false;
}
}
else
{
request.Platform = Platform.AnyCpu;
}
return true;
}
private static bool SetRequestOptimization(CommandOption NoOptimizationOption, ref Compiler.CompilationRequest request)
{
request.Optimize = !NoOptimizationOption.HasValue();
return true;
}
private static bool SetRequestAssemblyName(CommandOption AssemblyNameOption, ref Compiler.CompilationRequest request)
{
if (AssemblyNameOption.HasValue()) { request.AssemblyName = AssemblyNameOption.Value(); }
return true;
}
private static bool SetRequestReferences(ref Compiler.CompilationRequest request)
{
using (TextReader reader = File.OpenText(Common.SharpGenReferencesConfig))
{
var deserializer = new DeserializerBuilder().Build();
request.References = deserializer.Deserialize<List<Compiler.Reference>>(reader).ToList();
}
return true;
}
private static bool SetRequestEmbeddedResources(ref Compiler.CompilationRequest request)
{
using (TextReader reader = File.OpenText(Common.SharpGenResourcesConfig))
{
var deserializer = new DeserializerBuilder().Build();
request.EmbeddedResources = deserializer.Deserialize<List<Compiler.EmbeddedResource>>(reader);
}
return true;
}
private static bool SetRequestSource(CommandOption SourceFileOption, CommandOption ClassNameOption, List<string> RemainingArguments, ref Compiler.CompilationRequest request)
{
string className = RandomString();
string returnType = "";
string functionName = "";
string code = "";
if (ClassNameOption.HasValue())
{
className = ClassNameOption.Value();
}
if (SourceFileOption.HasValue())
{
code = File.ReadAllText(SourceFileOption.Value());
}
else
{
code = string.Join(" ", RemainingArguments);
}
if (request.OutputKind == OutputKind.ConsoleApplication)
{
returnType = "void";
functionName = "Main";
if (!code.Contains("return;"))
{
code = code + "\r\n\t" + "return;";
}
}
else if (request.OutputKind == OutputKind.DynamicallyLinkedLibrary)
{
returnType = "object";
functionName = "Execute";
if (!code.Contains("return;"))
{
code = "return " + code;
}
}
if (code.Contains(" class ") || code.Contains("\nclass "))
{
request.Source = code;
}
else
{
request.Source = String.Format(WrapperFunctionFormat, className, returnType, functionName, code);
}
return true;
}
private class MustBeOutputKindValidator : IOptionValidator
{
public ValidationResult GetValidationResult(CommandOption option, ValidationContext context)
{
if (!option.HasValue()) { return ValidationResult.Success; }
string val = option.Value().ToLower();
if (val != "console" && val != "consoleapp" && val != "consoleapplication" &&
val != "dll" && val != "dynamicallylinkedlibrary" && val != "exe")
{
return new ValidationResult($"Invalid --{option.LongName} specified.");
}
return ValidationResult.Success;
}
}
private class MustBeDotNetVersionValidator : IOptionValidator
{
public ValidationResult GetValidationResult(CommandOption option, ValidationContext context)
{
if (!option.HasValue()) { return ValidationResult.Success; }
string val = option.Value();
if (!val.Contains("35") && !val.Contains("40"))
{
return new ValidationResult($"Invalid --{option.LongName} specified.");
}
return ValidationResult.Success;
}
}
private class MustBeIdentifierValidator : IOptionValidator
{
private static Regex identifierRegex = new Regex("^[a-zA-Z_][a-zA-Z0-9]*$");
public ValidationResult GetValidationResult(CommandOption option, ValidationContext context)
{
if (!option.HasValue()) { return ValidationResult.Success; }
string val = option.Value();
if (!identifierRegex.IsMatch(val))
{
return new ValidationResult($"Invalid --{option.LongName} specified.");
}
return ValidationResult.Success;
}
}
private class MustBePlatformValidator : IOptionValidator
{
public ValidationResult GetValidationResult(CommandOption option, ValidationContext context)
{
if (!option.HasValue()) { return ValidationResult.Success; }
string val = option.Value().ToLower();
if (val != "x86" && val != "x64" && val != "anycpu")
{
return new ValidationResult($"Invalid --{option.LongName} specified.");
}
return ValidationResult.Success;
}
}
private static Random random = new Random();
private static string RandomString()
{
const string alphachars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
return alphachars[random.Next(alphachars.Length)] + new string(Enumerable.Repeat(chars, random.Next(10, 30)).Select(s => s[random.Next(s.Length)]).ToArray());
}
private static string WrapperFunctionFormat =
@"using System;
using System.IO;
using System.Text;
using System.Linq;
using System.Security.Principal;
using System.Collections.Generic;
using SharpSploit.Credentials;
using SharpSploit.Enumeration;
using SharpSploit.Execution;
using SharpSploit.LateralMovement;
using SharpSploit.Generic;
using SharpSploit.Misc;
public static class {0}
{{
static {1} {2}(string[] args)
{{
{3}
}}
}}";
}
}