コード例 #1
0
        public void convert(MethodDefinition cilMethod, CsvmMethodData csvmMethod)
        {
            var newInstructions = readInstructions(cilMethod, csvmMethod);
            var newLocals = readLocals(cilMethod, csvmMethod);
            var newExceptions = readExceptions(cilMethod, csvmMethod, newInstructions);

            fixInstructionOperands(newInstructions);
            fixLocals(newInstructions, cilMethod.Body.Variables);
            fixArgs(newInstructions, cilMethod);

            DotNetUtils.restoreBody(cilMethod, newInstructions, newExceptions);

            if (!operandRestorer.restore(cilMethod))
                Log.w("Failed to restore one or more instruction operands in CSVM method {0:X8}", cilMethod.MetadataToken.ToInt32());
            restoreConstrainedPrefix(cilMethod);
        }
コード例 #2
0
ファイル: CsvmDataReader.cs プロジェクト: Joelone/de4dot
        public List<CsvmMethodData> read()
        {
            int numMethods = reader.ReadInt32();
            if (numMethods < 0)
                throw new ApplicationException("Invalid number of methods");
            var methods = new List<CsvmMethodData>(numMethods);

            for (int i = 0; i < numMethods; i++) {
                var csvmMethod = new CsvmMethodData();
                csvmMethod.Guid = new Guid(reader.ReadBytes(16));
                csvmMethod.Token = reader.ReadInt32();
                csvmMethod.Locals = reader.ReadBytes(reader.ReadInt32());
                csvmMethod.Instructions = reader.ReadBytes(reader.ReadInt32());
                csvmMethod.Exceptions = reader.ReadBytes(reader.ReadInt32());
                methods.Add(csvmMethod);
            }

            return methods;
        }
コード例 #3
0
        List <ExceptionHandler> readExceptions(MethodDefinition cilMethod, CsvmMethodData csvmMethod, List <Instruction> cilInstructions)
        {
            var reader = new BinaryReader(new MemoryStream(csvmMethod.Exceptions));
            var ehs    = new List <ExceptionHandler>();

            if (reader.BaseStream.Length == 0)
            {
                return(ehs);
            }

            int numExceptions = reader.ReadInt32();

            if (numExceptions < 0)
            {
                throw new ApplicationException("Invalid number of exception handlers");
            }

            for (int i = 0; i < numExceptions; i++)
            {
                var eh = new ExceptionHandler((ExceptionHandlerType)reader.ReadInt32());
                eh.TryStart     = getInstruction(cilInstructions, reader.ReadInt32());
                eh.TryEnd       = getInstructionEnd(cilInstructions, reader.ReadInt32());
                eh.HandlerStart = getInstruction(cilInstructions, reader.ReadInt32());
                eh.HandlerEnd   = getInstructionEnd(cilInstructions, reader.ReadInt32());
                if (eh.HandlerType == ExceptionHandlerType.Catch)
                {
                    eh.CatchType = (TypeReference)module.LookupToken(reader.ReadInt32());
                }
                else if (eh.HandlerType == ExceptionHandlerType.Filter)
                {
                    eh.FilterStart = getInstruction(cilInstructions, reader.ReadInt32());
                }

                ehs.Add(eh);
            }

            return(ehs);
        }
コード例 #4
0
        public List <CsvmMethodData> read()
        {
            int numMethods = reader.ReadInt32();

            if (numMethods < 0)
            {
                throw new ApplicationException("Invalid number of methods");
            }
            var methods = new List <CsvmMethodData>(numMethods);

            for (int i = 0; i < numMethods; i++)
            {
                var csvmMethod = new CsvmMethodData();
                csvmMethod.Guid         = new Guid(reader.ReadBytes(16));
                csvmMethod.Token        = reader.ReadInt32();
                csvmMethod.Locals       = reader.ReadBytes(reader.ReadInt32());
                csvmMethod.Instructions = reader.ReadBytes(reader.ReadInt32());
                csvmMethod.Exceptions   = reader.ReadBytes(reader.ReadInt32());
                methods.Add(csvmMethod);
            }

            return(methods);
        }
コード例 #5
0
        List <VariableDefinition> readLocals(MethodDefinition cilMethod, CsvmMethodData csvmMethod)
        {
            var locals = new List <VariableDefinition>();
            var reader = new BinaryReader(new MemoryStream(csvmMethod.Locals));

            if (csvmMethod.Locals.Length == 0)
            {
                return(locals);
            }

            int numLocals = reader.ReadInt32();

            if (numLocals < 0 || numLocals != cilMethod.Body.Variables.Count)
            {
                throw new ApplicationException("Invalid number of locals");
            }

            for (int i = 0; i < numLocals; i++)
            {
                locals.Add(new VariableDefinition(readTypeReference(reader)));
            }

            return(locals);
        }
コード例 #6
0
        List<VariableDefinition> readLocals(MethodDefinition cilMethod, CsvmMethodData csvmMethod)
        {
            var locals = new List<VariableDefinition>();
            var reader = new BinaryReader(new MemoryStream(csvmMethod.Locals));

            if (csvmMethod.Locals.Length == 0)
                return locals;

            // v6.0.0.5 sometimes duplicates the last two locals so only check for a negative value.
            int numLocals = reader.ReadInt32();
            if (numLocals < 0)
                throw new ApplicationException("Invalid number of locals");

            for (int i = 0; i < numLocals; i++)
                locals.Add(new VariableDefinition(readTypeReference(reader)));

            return locals;
        }
コード例 #7
0
 List<Instruction> readInstructions(MethodDefinition cilMethod, CsvmMethodData csvmMethod)
 {
     var reader = new BinaryReader(new MemoryStream(csvmMethod.Instructions));
     var instrs = new List<Instruction>();
     int offset = 0;
     while (reader.BaseStream.Position < reader.BaseStream.Length) {
         int vmOpCode = reader.ReadUInt16();
         var instr = opCodeDetector.Handlers[vmOpCode].read(reader);
         instr.Offset = offset;
         offset += getInstructionSize(instr);
         instrs.Add(instr);
     }
     return instrs;
 }
コード例 #8
0
        List<ExceptionHandler> readExceptions(MethodDefinition cilMethod, CsvmMethodData csvmMethod, List<Instruction> cilInstructions)
        {
            var reader = new BinaryReader(new MemoryStream(csvmMethod.Exceptions));
            var ehs = new List<ExceptionHandler>();

            if (reader.BaseStream.Length == 0)
                return ehs;

            int numExceptions = reader.ReadInt32();
            if (numExceptions < 0)
                throw new ApplicationException("Invalid number of exception handlers");

            for (int i = 0; i < numExceptions; i++) {
                var eh = new ExceptionHandler((ExceptionHandlerType)reader.ReadInt32());
                eh.TryStart = getInstruction(cilInstructions, reader.ReadInt32());
                eh.TryEnd = getInstructionEnd(cilInstructions, reader.ReadInt32());
                eh.HandlerStart = getInstruction(cilInstructions, reader.ReadInt32());
                eh.HandlerEnd = getInstructionEnd(cilInstructions, reader.ReadInt32());
                if (eh.HandlerType == ExceptionHandlerType.Catch)
                    eh.CatchType = (TypeReference)module.LookupToken(reader.ReadInt32());
                else if (eh.HandlerType == ExceptionHandlerType.Filter)
                    eh.FilterStart = getInstruction(cilInstructions, reader.ReadInt32());

                ehs.Add(eh);
            }

            return ehs;
        }