Ejemplo n.º 1
0
        DumpedMethods createDumpedMethods(PeImage peImage, byte[] fileData, byte[] methodsData)
        {
            var dumpedMethods = new DumpedMethods();

            var methodsDataReader = new BinaryReader(new MemoryStream(methodsData));
            var fileDataReader = new BinaryReader(new MemoryStream(fileData));

            var metadataTables = peImage.Cor20Header.createMetadataTables();
            var methodDef = metadataTables.getMetadataType(MetadataIndex.iMethodDef);
            uint methodDefOffset = methodDef.fileOffset;
            for (int i = 0; i < methodDef.rows; i++, methodDefOffset += methodDef.totalSize) {
                uint bodyRva = peImage.offsetReadUInt32(methodDefOffset);
                if (bodyRva == 0)
                    continue;
                uint bodyOffset = peImage.rvaToOffset(bodyRva);

                var dm = new DumpedMethod();
                dm.token = (uint)(0x06000001 + i);
                dm.mdImplFlags = peImage.offsetReadUInt16(methodDefOffset + (uint)methodDef.fields[1].offset);
                dm.mdFlags = peImage.offsetReadUInt16(methodDefOffset + (uint)methodDef.fields[2].offset);
                dm.mdName = peImage.offsetRead(methodDefOffset + (uint)methodDef.fields[3].offset, methodDef.fields[3].size);
                dm.mdSignature = peImage.offsetRead(methodDefOffset + (uint)methodDef.fields[4].offset, methodDef.fields[4].size);
                dm.mdParamList = peImage.offsetRead(methodDefOffset + (uint)methodDef.fields[5].offset, methodDef.fields[5].size);

                byte b = peImage.offsetReadByte(bodyOffset);
                uint codeOffset;
                if ((b & 3) == 2) {
                    if (b != 2)
                        continue;	// not zero byte code size

                    dm.mhFlags = 2;
                    dm.mhMaxStack = 8;
                    dm.mhLocalVarSigTok = 0;
                    codeOffset = bodyOffset + 1;
                }
                else {
                    if (peImage.offsetReadUInt32(bodyOffset + 4) != 0)
                        continue;	// not zero byte code size

                    dm.mhFlags = peImage.offsetReadUInt16(bodyOffset);
                    dm.mhMaxStack = peImage.offsetReadUInt16(bodyOffset + 2);
                    dm.mhLocalVarSigTok = peImage.offsetReadUInt32(bodyOffset + 8);
                    codeOffset = bodyOffset + (uint)(dm.mhFlags >> 12) * 4;
                }
                fileDataReader.BaseStream.Position = codeOffset;

                if (!decrypter.decrypt(fileDataReader, dm))
                    continue;

                dumpedMethods.add(dm);
            }

            return dumpedMethods;
        }