DumpedMethods decryptMethods() { var dumpedMethods = new DumpedMethods(); var peImage = decrypterInfo.peImage; var methodInfos = new MethodInfos(decrypterInfo.mainType, peImage, decrypterInfo.peHeader, decrypterInfo.mcKey); methodInfos.initializeInfos(); var methodDef = peImage.DotNetFile.MetaData.TablesStream.MethodTable; for (uint rid = 1; rid <= methodDef.Rows; rid++) { var dm = new DumpedMethod(); peImage.readMethodTableRowTo(dm, rid); var info = methodInfos.lookup(dm.mdRVA); if (info == null) continue; ushort magic = peImage.readUInt16(dm.mdRVA); if (magic != 0xFFF3) continue; var mbHeader = MethodBodyParser.parseMethodBody(MemoryImageStream.Create(info.body), out dm.code, out dm.extraSections); peImage.updateMethodHeaderInfo(dm, mbHeader); dumpedMethods.add(dm); } return dumpedMethods; }
public bool decrypt(MyPEImage peImage, ref DumpedMethods dumpedMethods) { dumpedMethods = new DumpedMethods(); bool decrypted = false; var methodDef = peImage.DotNetFile.MetaData.TablesStream.MethodTable; for (uint rid = 1; rid <= methodDef.Rows; rid++) { var dm = new DumpedMethod(); peImage.readMethodTableRowTo(dm, rid); if (dm.mdRVA == 0) continue; uint bodyOffset = peImage.rvaToOffset(dm.mdRVA); peImage.Reader.Position = bodyOffset; var mbHeader = MethodBodyParser.parseMethodBody(peImage.Reader, out dm.code, out dm.extraSections); peImage.updateMethodHeaderInfo(dm, mbHeader); if (dm.code.Length < 6 || dm.code[0] != 0x2A || dm.code[1] != 0x2A) continue; int seed = BitConverter.ToInt32(dm.code, 2); Array.Copy(newCodeHeader, dm.code, newCodeHeader.Length); if (seed == 0) decrypt(dm.code); else decrypt(dm.code, seed); dumpedMethods.add(dm); decrypted = true; } return decrypted; }
DumpedMethods createDumpedMethods(MyPEImage peImage, byte[] fileData, byte[] methodsData) { var dumpedMethods = new DumpedMethods(); var methodsDataReader = MemoryImageStream.Create(methodsData); var fileDataReader = MemoryImageStream.Create(fileData); var methodDef = peImage.DotNetFile.MetaData.TablesStream.MethodTable; for (uint rid = 1; rid <= methodDef.Rows; rid++) { var dm = new DumpedMethod(); peImage.readMethodTableRowTo(dm, rid); if (dm.mdRVA == 0) continue; uint bodyOffset = peImage.rvaToOffset(dm.mdRVA); 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.Position = codeOffset; if (!decrypter.decrypt(fileDataReader, dm)) continue; dumpedMethods.add(dm); } return dumpedMethods; }
public bool decrypt(MyPEImage peImage, ISimpleDeobfuscator simpleDeobfuscator, ref DumpedMethods dumpedMethods, Dictionary<uint, byte[]> tokenToNativeCode, bool unpackedNativeFile) { if (encryptedResource.Method == null) return false; encryptedResource.init(simpleDeobfuscator); if (!encryptedResource.FoundResource) return false; var methodsData = encryptedResource.decrypt(); bool hooksJitter = findDnrCompileMethod(encryptedResource.Method.DeclaringType) != null; xorKey = getXorKey(); xorEncrypt(methodsData); var methodsDataReader = MemoryImageStream.Create(methodsData); int patchCount = methodsDataReader.ReadInt32(); int mode = methodsDataReader.ReadInt32(); int tmp = methodsDataReader.ReadInt32(); methodsDataReader.Position -= 4; if ((tmp & 0xFF000000) == 0x06000000) { // It's method token + rva. DNR 3.7.0.3 (and earlier?) - 3.9.0.1 methodsDataReader.Position += 8L * patchCount; patchCount = methodsDataReader.ReadInt32(); mode = methodsDataReader.ReadInt32(); patchDwords(peImage, methodsDataReader, patchCount); while (methodsDataReader.Position < methodsData.Length - 1) { uint token = methodsDataReader.ReadUInt32(); int numDwords = methodsDataReader.ReadInt32(); patchDwords(peImage, methodsDataReader, numDwords / 2); } } else if (!hooksJitter || mode == 1) { // DNR 3.9.8.0, 4.0, 4.1, 4.2, 4.3, 4.4 // If it's .NET 1.x, then offsets are used, not RVAs. bool useOffsets = unpackedNativeFile && module.IsClr1x; patchDwords(peImage, methodsDataReader, patchCount); while (methodsDataReader.Position < methodsData.Length - 1) { uint rva = methodsDataReader.ReadUInt32(); uint token = methodsDataReader.ReadUInt32(); // token, unknown, or index int size = methodsDataReader.ReadInt32(); if (size > 0) { var newData = methodsDataReader.ReadBytes(size); if (useOffsets) peImage.dotNetSafeWriteOffset(rva, newData); else peImage.dotNetSafeWrite(rva, newData); } } } else { // DNR 4.0 - 4.5 (jitter is hooked) var methodDef = peImage.DotNetFile.MetaData.TablesStream.MethodTable; var rvaToIndex = new Dictionary<uint, int>((int)methodDef.Rows); uint offset = (uint)methodDef.StartOffset; for (int i = 0; i < methodDef.Rows; i++) { uint rva = peImage.offsetReadUInt32(offset); offset += methodDef.RowSize; if (rva == 0) continue; if ((peImage.readByte(rva) & 3) == 2) rva++; else rva += (uint)(4 * (peImage.readByte(rva + 1) >> 4)); rvaToIndex[rva] = i; } patchDwords(peImage, methodsDataReader, patchCount); int count = methodsDataReader.ReadInt32(); dumpedMethods = new DumpedMethods(); while (methodsDataReader.Position < methodsData.Length - 1) { uint rva = methodsDataReader.ReadUInt32(); uint index = methodsDataReader.ReadUInt32(); bool isNativeCode = index >= 0x70000000; int size = methodsDataReader.ReadInt32(); var methodData = methodsDataReader.ReadBytes(size); int methodIndex; if (!rvaToIndex.TryGetValue(rva, out methodIndex)) { Logger.w("Could not find method having code RVA {0:X8}", rva); continue; } uint methodToken = 0x06000001 + (uint)methodIndex; if (isNativeCode) { totalEncryptedNativeMethods++; if (tokenToNativeCode != null) tokenToNativeCode[methodToken] = methodData; // Convert return true / false methods. The others are converted to // throw 0xDEADCODE. if (DeobUtils.isCode(nativeLdci4, methodData)) { uint val = BitConverter.ToUInt32(methodData, 4); // ldc.i4 XXXXXXXXh / ret methodData = new byte[] { 0x20, 0, 0, 0, 0, 0x2A }; methodData[1] = (byte)val; methodData[2] = (byte)(val >> 8); methodData[3] = (byte)(val >> 16); methodData[4] = (byte)(val >> 24); } else if (DeobUtils.isCode(nativeLdci4_0, methodData)) { // ldc.i4.0 / ret methodData = new byte[] { 0x16, 0x2A }; } else { tokenToNativeMethod[methodToken] = methodData; // ldc.i4 0xDEADCODE / conv.u4 / throw methodData = new byte[] { 0x20, 0xDE, 0xC0, 0xAD, 0xDE, 0x6D, 0x7A }; } } var dm = new DumpedMethod(); peImage.readMethodTableRowTo(dm, MDToken.ToRID(methodToken)); dm.code = methodData; var codeReader = peImage.Reader; codeReader.Position = peImage.rvaToOffset(dm.mdRVA); byte[] code; var mbHeader = MethodBodyParser.parseMethodBody(codeReader, out code, out dm.extraSections); peImage.updateMethodHeaderInfo(dm, mbHeader); dumpedMethods.add(dm); } } return true; }
void decryptMethodsOld(MDTable methodDefTable, ref DumpedMethods dumpedMethods) { dumpedMethods = new DumpedMethods(); var decrypter = new Decrypter10(peImage, codeHeader.decryptionKey); for (uint rid = 1; rid <= methodDefTable.Rows; rid++) { var dm = new DumpedMethod(); var method = (MethodDef)module.ResolveMethod(rid); if (method == null || method.DeclaringType == module.GlobalType) continue; peImage.readMethodTableRowTo(dm, rid); if (dm.mdRVA == 0) continue; uint bodyOffset = peImage.rvaToOffset(dm.mdRVA); var mbHeader = decrypter.decrypt(bodyOffset, out dm.code, out dm.extraSections); peImage.updateMethodHeaderInfo(dm, mbHeader); dumpedMethods.add(dm); } }
void decryptMethods(uint codeHeaderOffset, MDTable methodDefTable, ICsHeader csHeader, ref DumpedMethods dumpedMethods) { var methodInfos = csHeader.getMethodInfos(codeHeaderOffset); csHeader.patchMethodTable(methodDefTable, methodInfos); dumpedMethods = new DumpedMethods(); decrypter = csHeader.createDecrypter(); for (uint rid = 1; rid <= (uint)methodInfos.Count; rid++) { var methodInfo = methodInfos[(int)rid - 1]; if (methodInfo.codeOffs == 0) continue; var dm = new DumpedMethod(); peImage.readMethodTableRowTo(dm, rid); var mbHeader = decrypter.decrypt(methodInfo, out dm.code, out dm.extraSections); peImage.updateMethodHeaderInfo(dm, mbHeader); dumpedMethods.add(dm); } }