Ejemplo n.º 1
0
        private void ProcessRuntimeModule()
        {
            logger.Info("Processing Runtime Module...");

            var module = ModuleDefMD.Load(Path.Combine(@"C:\Users\Nybher\Desktop\koiVM\Debug\bin", "KoiVM.Runtime.dll"));

            module.Assembly.Name += "." + User.LongID;
            module.Name           = string.Format("KoiVM.Runtime.{0}.dll", User.LongID);

            var renamer = new Renamer(random.Value.Next(), true, '#');

            renamer.Process(module);

            ctx = new RenameContext();

            ctx.VMEntry = renamer.GetNewName(ctx.VMEntry);
            ctx.VMRun   = renamer.GetNewName(ctx.VMRun);

            ctx.VMDispatcher          = renamer.GetNewName(ctx.VMDispatcher);
            ctx.VMDispatcherDothrow   = renamer.GetNewName(ctx.VMDispatcherDothrow);
            ctx.VMDispatcherThrow     = renamer.GetNewName(ctx.VMDispatcherThrow);
            ctx.VMDispatcherGetIP     = renamer.GetNewName(ctx.VMDispatcherGetIP);
            ctx.VMDispatcherStackwalk = renamer.GetNewName(ctx.VMDispatcherStackwalk);

            ctx.VMConstants = renamer.GetNewName(ctx.VMConstants);

            using (var reader = new StringReader(ctx.VMConstMapText))
            {
                while (reader.Peek() > 0)
                {
                    var line = reader.ReadLine().Trim();
                    if (string.IsNullOrEmpty(line))
                    {
                        continue;
                    }
                    var entry = line.Split(new[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
                    var key   = renamer.GetNewName(entry[0]);
                    ctx.VMConstMap.Add(Tuple.Create(key, entry[1]));
                }
            }

            random.Value.Shuffle(ctx.VMConstMap);

            using (var stream = new MemoryStream())
            {
                module.Write(stream);
                rtModule = stream.ToArray();
            }
            logger.Info("Finished Runtime Module.");
        }
Ejemplo n.º 2
0
        private void ProcessVMModule()
        {
            logger.Info("Processing VM Module...");

            var module = ModuleDefMD.Load(Path.Combine(@"C:\Users\Nybher\Desktop\koiVM\Debug\bin", "KoiVM.dll"));

            module.LoadPdb();

            var ca = module.Assembly.CustomAttributes.Find("System.Reflection.AssemblyInformationalVersionAttribute");

            version = (UTF8String)ca.ConstructorArguments[0].Value;

            foreach (var instr in module.Find("KoiVM.Watermark", true).FindMethod("GenerateWatermark").Body.Instructions)
            {
                if (instr.OpCode.Code == Code.Ldc_I4 && (int)instr.Operand == 0x10000)
                {
                    instr.Operand = (int)(User.Watermark * 0xaeaf10f7);  // mod-inverse = 0xa7c0b0c7
                }
            }
            var initCctor = module.Find("KoiVM.RT.Mutation.RTMap", true).FindStaticConstructor();
            var renamer   = new Renamer(random.Value.Next(), false, '$');
            renamer.Process(module);

            var sb = new StringBuilder();
            foreach (var entry in ctx.VMConstMap)
            {
                sb.AppendLine(string.Format("{0}\t{1}", entry.Item1, renamer.GetNewName(entry.Item2)));
            }
            ctx.VMConstMapText = sb.ToString();

            foreach (var instr in initCctor.Body.Instructions)
            {
                var value = instr.Operand as string;
                if (value == null)
                {
                    continue;
                }
                switch (value)
                {
                case "KoiVM.Runtime.VMEntry":
                    instr.Operand = ctx.VMEntry;
                    break;

                case "Run":
                    instr.Operand = ctx.VMRun;
                    break;

                case "KoiVM.Runtime.Execution.VMDispatcher":
                    instr.Operand = ctx.VMDispatcher;
                    break;

                case "DoThrow":
                    instr.Operand = ctx.VMDispatcherDothrow;
                    break;

                case "Throw":
                    instr.Operand = ctx.VMDispatcherThrow;
                    break;

                case "GetIP":
                    instr.Operand = ctx.VMDispatcherGetIP;
                    break;

                case "StackWalk":
                    instr.Operand = ctx.VMDispatcherStackwalk;
                    break;

                case "KoiVM.Runtime.Dynamic.Constants":
                    instr.Operand = ctx.VMConstants;
                    break;

                default:
                    instr.Operand = ctx.VMConstMapText;
                    break;
                }
            }

            module.Resources.Add(new EmbeddedResource("KoiVM.Runtime.dll", rtModule));
            module.Write(Path.Combine(privPath, "KoiVM.dll"), new ModuleWriterOptions(module)
            {
                WritePdb = true
            });
            logger.Info("Finished VM Module.");
        }