private Tuple <Compilation, int> CreateCompilation()
        {
            List <SyntaxTree> sourceTrees = new List <SyntaxTree>();
            SharedContainer   instance    = SharedContainer.Instance;

            if (instance.sourceTrees == null)
            {
                instance.sourceTrees = CreateSyntaxTrees();
            }
            else
            {
                instance.sourceTrees = ReCreateSyntaxTrees();
            }
            sourceTrees = instance.sourceTrees;
            MetadataReference mscorlib =
                MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
            MetadataReference codeAnalysis =
                MetadataReference.CreateFromFile(typeof(SyntaxTree).Assembly.Location);
            MetadataReference csharpCodeAnalysis =
                MetadataReference.CreateFromFile(typeof(CSharpSyntaxTree).Assembly.Location);

            MetadataReference[] references = { mscorlib, codeAnalysis, csharpCodeAnalysis };

            return(Tuple.Create(CSharpCompilation.Create("CodeObfuscation",
                                                         sourceTrees.ToArray(),
                                                         references,
                                                         new CSharpCompilationOptions(
                                                             OutputKind.ConsoleApplication)) as Compilation, sourceTrees.Count()));
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            string path        = null;
            string mode        = null;
            bool   randomNames = false;
            string outputPath  = null;
            int    iMode       = 1;

            Console.WriteLine("Please provide project path:");
            do
            {
                path = Console.ReadLine();
            } while (path == null);

            Console.WriteLine("Please provide obfuscation mode path:");
            Console.WriteLine("1) In-place rewrite (default)");
            Console.WriteLine("2) Random names of output files");
            Console.WriteLine("3) Different output directory");
            mode = Console.ReadLine();

            switch (mode)
            {
            case "2":
                iMode       = 2;
                randomNames = true;
                Console.WriteLine("Please provide output directory path:");
                do
                {
                    outputPath = Console.ReadLine();
                } while (outputPath == null);
                break;

            case "3":
                iMode = 3;
                Console.WriteLine("Please provide output directory path:");
                do
                {
                    outputPath = Console.ReadLine();
                } while (outputPath == null);
                break;

            default:
                outputPath = path;
                break;
            }

            SharedContainer instance = SharedContainer.Instance;

            instance.path            = path;
            instance.outputPath      = outputPath;
            instance.generalizeNames = randomNames;
            instance.mode            = iMode;

            CompilationManager compilationManager = new CompilationManager();

            Priority.En_Priority[] priorityTable = { Priority.En_Priority.WHITESPACE, Priority.En_Priority.COMMENT, Priority.En_Priority.CLASS, Priority.En_Priority.CONSTRUCTOR, Priority.En_Priority.METHOD, Priority.En_Priority.VARIABLE, Priority.En_Priority.OCCURENCE, Priority.En_Priority.TYPE_INFERENCE, Priority.En_Priority.ENUM, Priority.En_Priority.PARAMETER };
            compilationManager.parseCompilationWithProvidedPriorities(priorityTable);
        }
        private List <SyntaxTree> CreateSyntaxTrees()
        {
            List <SyntaxTree> sourceTrees = new List <SyntaxTree>();
            SharedContainer   instance    = SharedContainer.Instance;

            System.IO.Directory.CreateDirectory(instance.outputPath);
            string[] allfiles = System.IO.Directory.GetFiles(instance.path, "*.*", System.IO.SearchOption.AllDirectories);
            foreach (var file in allfiles)
            {
                FileInfo info = new FileInfo(file);
                // ACHTUNG - Hacky "if" ahead!
                if (info.Extension == ".cs" && !info.Name.Contains("TemporaryGeneratedFile_") && !info.Name.Contains("AssemblyInfo"))
                {
                    string programPath            = null;
                    string destinationProgramPath = null;
                    string path;
                    switch (instance.mode)
                    {
                    case 1:     // In-place
                        programPath            = info.FullName;
                        destinationProgramPath = info.FullName;
                        instance.filePaths.Add(info.FullName);
                        break;

                    case 2:     // Random names + different dir
                        path = Path.Combine(instance.outputPath, instance.RandomFileName() + ".cs");
                        File.Copy(info.FullName, path);
                        programPath            = path;
                        destinationProgramPath = path;
                        instance.filePaths.Add(path);
                        break;

                    case 3:     // Different dir only
                        path = Path.Combine(instance.outputPath, info.Name);
                        File.Copy(info.FullName, path);
                        programPath            = path;
                        destinationProgramPath = path;
                        instance.filePaths.Add(path);
                        break;
                    }
                    string     programText = File.ReadAllText(programPath);
                    SyntaxTree programTree =
                        CSharpSyntaxTree.ParseText(programText)
                        .WithFilePath(destinationProgramPath);
                    sourceTrees.Add(programTree);
                }
            }
            return(sourceTrees);
        }
        private List <SyntaxTree> ReCreateSyntaxTrees()
        {
            List <SyntaxTree> sourceTrees = new List <SyntaxTree>();
            SharedContainer   instance    = SharedContainer.Instance;

            foreach (string file in instance.filePaths)
            {
                string     programText = File.ReadAllText(file);
                SyntaxTree programTree =
                    CSharpSyntaxTree.ParseText(programText)
                    .WithFilePath(file);
                sourceTrees.Add(programTree);
            }
            return(sourceTrees);
        }