Example #1
0
        /// <summary>
        /// Build an obfuscated XLS Macro Document, or Deobfuscate an existing malicious XLS Macro Document.
        /// </summary>
        /// <param name="args"></param>
        public static void Main(string[] args)
        {
            try
            {
                Command buildCommand = new Command("b", null);
                buildCommand.AddAlias("build");
                MethodInfo buildMethodInfo = typeof(Program).GetMethod(nameof(Build));
                buildCommand.ConfigureFromMethod(buildMethodInfo);

                Command deobfuscateCommand = new Command("d", null);
                deobfuscateCommand.AddAlias("deobfuscate");
                MethodInfo deobfuscateMethodInfo = typeof(Program).GetMethod(nameof(Deobfuscate));
                deobfuscateCommand.ConfigureFromMethod(deobfuscateMethodInfo);

                Command    dumpCommand    = new Command("dump", null);
                MethodInfo dumpMethodInfo = typeof(Program).GetMethod(nameof(Dump));
                dumpCommand.ConfigureFromMethod(dumpMethodInfo);


                RootCommand rootCommand = new RootCommand("Build an obfuscated XLS Macro Document, or Deobfuscate an existing malicious XLS Macro Document.")
                {
                    deobfuscateCommand,
                    buildCommand,
                    dumpCommand
                };

                CommandLineBuilder builder = new CommandLineBuilder(rootCommand);
                builder.ConfigureHelpFromXmlComments(buildMethodInfo, null);

                //Manually set this after reading the XML for descriptions
                builder.Command.Description =
                    "Build an obfuscated XLS Macro Document or Deobfuscate an existing malicious XLS Macro Document.";

                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

                builder
                .UseDefaults()
                .Build()
                .Invoke(args);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unexpected Exception Occurred:\n");
                Console.WriteLine(e);
            }
        }
        public CommandLineBuilder CreateCommandLineBuilder()
        {
            // Check for the original EntryPoint
            var exeAsm     = Assembly.GetEntryAssembly();
            var entryPoint = exeAsm.EntryPoint;

            if (entryPoint == null)
            {
                throw new InvalidOperationException("Failed to locate assembly entry point!");
            }
            var programType = entryPoint.DeclaringType;

            var executeMethods = programType
                                 .GetMethods()
                                 .Where(m => m.IsPublic && m.Name.Equals("ExecuteAsync", StringComparison.OrdinalIgnoreCase))
                                 .ToList();

            if (executeMethods.Count == 0)
            {
                throw new InvalidOperationException($"Unable to find a '{programType.FullName}.ExecuteAsync' method!");
            }
            else if (executeMethods.Count > 1)
            {
                throw new AmbiguousMatchException($"Multiple '{programType.FullName}.ExecuteAsync' methods were found!");
            }

            var executeMethod = executeMethods[0];

            var builder = new CommandLineBuilder();

            // The 'target' argument isn't even used...
            builder.Command.ConfigureFromMethod(executeMethod, target: () => null);

            // Try to add docs from XML doc comments
            builder.ConfigureHelpFromXmlComments(executeMethod, Path.ChangeExtension(exeAsm.Location, ".xml"));

            // Activate the target via DI
            builder.UseMiddleware(c => c.BindingContext.AddService(executeMethod.DeclaringType, () => ActivatorUtilities.CreateInstance(_services, executeMethod.DeclaringType, Array.Empty <object>())));

            return(builder);
        }