Пример #1
0
        public void TestInvalidOpCodeMap()
        {
            var random = new Random();

            var mod  = new ModuleDefUser("test");
            var type = new TypeDefUser("Constants");

            for (var i = 0; i < random.Next(0, 100); i++)
            {
                type.Fields.Add(new FieldDefUser("randomName" + random.Next(), new FieldSig(mod.CorLibTypes.Byte)));
            }

            mod.Types.Add(type);

            var ms = new MemoryStream();

            mod.Write(ms);

            var ctx = new RhydonContext {
                Module = ModuleDefMD.Load(ms), Logger = new DummyLogger()
            };

            OpCodeMap.Parse(ctx);
            Assert.IsNull(ctx.Constants);
        }
Пример #2
0
        static void Main()
        {
            Console.WriteAscii("Rhydon v1.0.0", Color.Coral);
            Console.WriteLine(new string(' ', 44) + "by xsilent007 & TobitoFatito\n", Color.White);

            var ctx = new RhydonContext {
                Module = ModuleDefMD.Load("Test.exe"),
                Logger = new Logger()
            };

            Resolver.ResolveAssemblies(ctx);
            KoiHeader.Parse(ctx);
            OpCodeMap.Parse(ctx);
            VirtualizedMethods.Parse(ctx);

            var emu = new KoiEmulator(ctx, ctx.Header.Methods[2]);

            emu.EmulateNext();
            emu.EmulateNext();
            emu.EmulateNext();
            emu.EmulateNext();
            emu.EmulateNext();
            emu.EmulateNext();
            emu.EmulateNext();
            emu.EmulateNext();
            emu.EmulateNext();
            emu.EmulateNext();
            emu.EmulateNext();
            emu.EmulateNext();
            emu.EmulateNext();
            emu.EmulateNext();
            emu.EmulateNext();

            Console.ReadLine();
        }
Пример #3
0
            public static bool TryParseOpCode(string str, out OpCode dst)
            {
                OpCodeDescriptor dsc;

                if (OpCodeMap.TryGetValue(str, out dsc))
                {
                    dst = dsc.OpCode;
                    return(true);
                }
                dst = OpCode.__UNKNOWN__;
                return(false);
            }
Пример #4
0
        public void TestValidOpCodeMap()
        {
            var random = new Random();

            var mod  = new ModuleDefUser("test");
            var type = new TypeDefUser("Constants");

            for (var i = 0; i < 119; i++)
            {
                type.Fields.Add(new FieldDefUser("randomName" + random.Next(), new FieldSig(mod.CorLibTypes.Byte)));
            }

            mod.Types.Add(type);

            var ctor = type.FindOrCreateStaticConstructor();
            var body = new CilBody();

            for (var i = 1; i < 119; i++)
            {
                body.Instructions.Insert(0, Instruction.Create(OpCodes.Stfld, type.Fields[i]));
                body.Instructions.Insert(0, Instruction.Create(OpCodes.Ldc_I4, random.Next(0, 0xFF)));
                body.Instructions.Insert(0, Instruction.Create(OpCodes.Ldnull));
            }

            body.Instructions.Add(Instruction.Create(OpCodes.Ldnull));
            body.Instructions.Add(Instruction.Create(OpCodes.Ldc_I4, 112));
            body.Instructions.Add(Instruction.Create(OpCodes.Stfld, type.Fields[0]));
            body.Instructions.Add(Instruction.Create(OpCodes.Ret));
            ctor.Body = body;

            var ms = new MemoryStream();

            mod.Write(ms);

            var ctx = new RhydonContext {
                Module = ModuleDefMD.Load(ms), Logger = new DummyLogger()
            };

            OpCodeMap.Parse(ctx);
            Assert.IsNotNull(ctx.Constants);
            Assert.IsTrue(ctx.Constants.REG_R0 == 112);

            ms.Close();
        }
Пример #5
0
        private static ExecutionState DarkInternal(DarksVMContext ctx)
        {
            ExecutionState state;

            while (true)
            {
                var op = ctx.ReadByte();
                var p  = ctx.ReadByte(); // For key fixup
                OpCodeMap.Lookup(op).Load(ctx, out state);

                if (ctx.Registers[DarksVMConstants.REG_IP].U8 == 1)
                {
                    state = ExecutionState.Exit;
                }

                if (state != ExecutionState.Next)
                {
                    return(state);
                }
            }
        }
Пример #6
0
    private void ProcessIlEmitMethodCall(Instruction emitCallInstruction, out Instruction?nextInstruction)
    {
        var emittedInstruction = CreateInstructionToEmit();

        _il.Replace(emitCallInstruction, emittedInstruction);

        if (emittedInstruction.OpCode.OpCodeType == OpCodeType.Prefix)
        {
            _il.RemoveNopsAfter(emittedInstruction);
        }

        var sequencePoint = _sequencePoints.MapSequencePoint(emitCallInstruction, emittedInstruction);

        if (emittedInstruction.Previous?.OpCode.OpCodeType == OpCodeType.Prefix)
        {
            _sequencePoints.MergeWithPreviousSequencePoint(sequencePoint);
        }

        nextInstruction = emittedInstruction.Next;
        RemoveNopInDebugBuild(ref nextInstruction);
        nextInstruction = emittedInstruction.NextSkipNops();

        switch (emittedInstruction.OpCode.Code)
        {
        case Code.Ret:
        case Code.Endfinally:
        case Code.Endfilter:
        {
            if (nextInstruction?.OpCode == emittedInstruction.OpCode)
            {
                _il.Remove(emittedInstruction);
                _log.Debug($"Removed duplicate {emittedInstruction.OpCode}");
            }

            break;
        }

        case Code.Leave:
        case Code.Leave_S:
        case Code.Throw:
        case Code.Rethrow:
        {
            if (nextInstruction?.OpCode == OpCodes.Leave || nextInstruction?.OpCode == OpCodes.Leave_S)
            {
                _il.RemoveNopsAfter(emittedInstruction);
                _il.Remove(emittedInstruction);
                _il.Replace(nextInstruction, emittedInstruction);
                _log.Debug($"Replaced {nextInstruction.OpCode} with emitted {emittedInstruction.OpCode}");
                nextInstruction = emittedInstruction.NextSkipNops();
            }

            break;
        }
        }

        Instruction CreateInstructionToEmit()
        {
            var method = (MethodReference)emitCallInstruction.Operand;
            var opCode = OpCodeMap.FromCecilFieldName(method.Name);
            var args   = _il.GetArgumentPushInstructionsInSameBasicBlock(emitCallInstruction);

            switch (opCode.OperandType)
            {
            case OperandType.InlineNone:
            {
                if (args.Length != 0)
                {
                    throw new InstructionWeavingException(emitCallInstruction, "Unexpected operand argument");
                }

                return(_il.Create(opCode));
            }

            case OperandType.InlineI:
            case OperandType.ShortInlineI:
            case OperandType.InlineI8:
            case OperandType.InlineR:
            case OperandType.ShortInlineR:
                return(_il.CreateConst(opCode, _consumer.ConsumeArgConst(args.Single())));

            case OperandType.InlineString:
                return(_il.CreateConst(opCode, _consumer.ConsumeArgString(args.Single())));

            case OperandType.InlineType:
            {
                if (method.IsGenericInstance)
                {
                    return(_il.Create(opCode, ((GenericInstanceMethod)method).GenericArguments[0]));
                }

                return(_il.Create(opCode, _consumer.ConsumeArgTypeRef(args.Single())));
            }

            case OperandType.InlineMethod:
                return(_il.Create(opCode, _consumer.ConsumeArgMethodRef(args.Single())));

            case OperandType.InlineField:
                return(_il.Create(opCode, _consumer.ConsumeArgFieldRef(args.Single())));

            case OperandType.InlineTok:
            {
                if (method.IsGenericInstance)
                {
                    return(_il.Create(opCode, ((GenericInstanceMethod)method).GenericArguments[0]));
                }

                return(method.Parameters[0].ParameterType.FullName switch
                    {
                        KnownNames.Full.TypeRefType => _il.Create(opCode, _consumer.ConsumeArgTypeRef(args.Single())),
                        KnownNames.Full.MethodRefType => _il.Create(opCode, _consumer.ConsumeArgMethodRef(args.Single())),
                        KnownNames.Full.FieldRefType => _il.Create(opCode, _consumer.ConsumeArgFieldRef(args.Single())),
                        _ => throw new InstructionWeavingException(emitCallInstruction, $"Unexpected argument type: {method.Parameters[0].ParameterType.FullName}")
                    });
            }