2011-10-25 14:27:36 +08:00
|
|
|
|
/*
|
2014-03-12 05:15:43 +08:00
|
|
|
|
Copyright (C) 2011-2014 de4dot@gmail.com
|
2011-10-25 14:27:36 +08:00
|
|
|
|
|
|
|
|
|
This file is part of de4dot.
|
|
|
|
|
|
|
|
|
|
de4dot is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
de4dot is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with de4dot. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2011-10-26 20:40:55 +08:00
|
|
|
|
using System.IO;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.IO;
|
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using dnlib.DotNet.MD;
|
|
|
|
|
using dnlib.DotNet.Emit;
|
|
|
|
|
using dnlib.DotNet.Writer;
|
2011-10-25 14:27:36 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
2011-12-22 01:55:36 +08:00
|
|
|
|
namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 {
|
2011-10-25 14:27:36 +08:00
|
|
|
|
class MethodsDecrypter {
|
2012-11-18 01:57:36 +08:00
|
|
|
|
ModuleDefMD module;
|
2011-10-26 20:40:55 +08:00
|
|
|
|
EncryptedResource encryptedResource;
|
2011-11-25 22:24:12 +08:00
|
|
|
|
Dictionary<uint, byte[]> tokenToNativeMethod = new Dictionary<uint, byte[]>();
|
2012-11-02 22:57:11 +08:00
|
|
|
|
Dictionary<MethodDef, byte[]> methodToNativeMethod = new Dictionary<MethodDef, byte[]>();
|
2012-11-18 06:49:19 +08:00
|
|
|
|
List<MethodDef> validNativeMethods;
|
2011-12-01 01:28:48 +08:00
|
|
|
|
int totalEncryptedNativeMethods = 0;
|
2011-11-25 22:24:12 +08:00
|
|
|
|
long xorKey;
|
2011-10-25 14:27:36 +08:00
|
|
|
|
|
|
|
|
|
public bool Detected {
|
2011-11-06 22:24:30 +08:00
|
|
|
|
get { return encryptedResource.Method != null; }
|
2011-10-25 14:27:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-25 22:24:12 +08:00
|
|
|
|
public bool HasNativeMethods {
|
|
|
|
|
get { return methodToNativeMethod.Count > 0; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public TypeDef DecrypterType {
|
2011-11-09 04:27:03 +08:00
|
|
|
|
get { return encryptedResource.Type; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef Method {
|
2011-11-06 22:24:30 +08:00
|
|
|
|
get { return encryptedResource.Method; }
|
2011-10-27 05:00:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-06 22:24:30 +08:00
|
|
|
|
public EmbeddedResource Resource {
|
|
|
|
|
get { return encryptedResource.Resource; }
|
2011-10-27 05:00:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-23 00:04:50 +08:00
|
|
|
|
public DnrDecrypterType DecrypterTypeVersion {
|
|
|
|
|
get { return encryptedResource.GuessDecrypterType(); }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-18 01:57:36 +08:00
|
|
|
|
public MethodsDecrypter(ModuleDefMD module) {
|
2011-10-25 14:27:36 +08:00
|
|
|
|
this.module = module;
|
2011-10-26 20:40:55 +08:00
|
|
|
|
this.encryptedResource = new EncryptedResource(module);
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-18 01:57:36 +08:00
|
|
|
|
public MethodsDecrypter(ModuleDefMD module, MethodsDecrypter oldOne) {
|
2011-10-26 20:40:55 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
this.encryptedResource = new EncryptedResource(module, oldOne.encryptedResource);
|
2011-11-25 22:24:12 +08:00
|
|
|
|
this.tokenToNativeMethod = oldOne.tokenToNativeMethod;
|
2011-12-01 01:28:48 +08:00
|
|
|
|
this.totalEncryptedNativeMethods = oldOne.totalEncryptedNativeMethods;
|
2011-11-25 22:24:12 +08:00
|
|
|
|
this.xorKey = oldOne.xorKey;
|
2011-10-25 14:27:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Find() {
|
2011-10-27 01:49:25 +08:00
|
|
|
|
var additionalTypes = new string[] {
|
|
|
|
|
"System.IntPtr",
|
2011-10-29 09:39:32 +08:00
|
|
|
|
// "System.Reflection.Assembly", //TODO: Not in unknown DNR version with jitter support
|
2011-10-27 01:49:25 +08:00
|
|
|
|
};
|
2012-11-18 01:57:36 +08:00
|
|
|
|
var checkedMethods = new Dictionary<IMethod, bool>(MethodEqualityComparer.CompareDeclaringTypes);
|
2011-10-25 14:27:36 +08:00
|
|
|
|
var callCounter = new CallCounter();
|
|
|
|
|
int typesLeft = 30;
|
|
|
|
|
foreach (var type in module.GetTypes()) {
|
2012-11-18 01:57:36 +08:00
|
|
|
|
var cctor = type.FindStaticConstructor();
|
2011-10-25 14:27:36 +08:00
|
|
|
|
if (cctor == null || cctor.Body == null)
|
|
|
|
|
continue;
|
2011-10-26 20:40:55 +08:00
|
|
|
|
if (typesLeft-- <= 0)
|
|
|
|
|
break;
|
2011-10-25 14:27:36 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
foreach (var method in DotNetUtils.GetCalledMethods(module, cctor)) {
|
2012-11-18 01:57:36 +08:00
|
|
|
|
if (!checkedMethods.ContainsKey(method)) {
|
|
|
|
|
checkedMethods[method] = false;
|
2012-03-18 03:36:41 +08:00
|
|
|
|
if (method.DeclaringType.BaseType == null || method.DeclaringType.BaseType.FullName != "System.Object")
|
2011-10-27 08:08:30 +08:00
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!DotNetUtils.IsMethod(method, "System.Void", "()"))
|
2011-10-26 20:40:55 +08:00
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!encryptedResource.CouldBeResourceDecrypter(method, additionalTypes))
|
2011-10-25 14:27:36 +08:00
|
|
|
|
continue;
|
2012-11-18 01:57:36 +08:00
|
|
|
|
checkedMethods[method] = true;
|
2011-10-25 14:27:36 +08:00
|
|
|
|
}
|
2012-11-18 01:57:36 +08:00
|
|
|
|
else if (!checkedMethods[method])
|
2011-10-28 07:33:05 +08:00
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
callCounter.Add(method);
|
2011-10-25 14:27:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
encryptedResource.Method = (MethodDef)callCounter.Most();
|
2011-10-25 14:27:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void XorEncrypt(byte[] data) {
|
2011-11-25 22:24:12 +08:00
|
|
|
|
if (xorKey == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var stream = new MemoryStream(data);
|
|
|
|
|
var reader = new BinaryReader(stream);
|
|
|
|
|
var writer = new BinaryWriter(stream);
|
|
|
|
|
int count = data.Length / 8;
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
|
long val = reader.ReadInt64();
|
|
|
|
|
val ^= xorKey;
|
|
|
|
|
stream.Position -= 8;
|
|
|
|
|
writer.Write(val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-13 14:49:34 +08:00
|
|
|
|
readonly static short[] nativeLdci4 = new short[] { 0x55, 0x8B, 0xEC, 0xB8, -1, -1, -1, -1, 0x5D, 0xC3 };
|
|
|
|
|
readonly static short[] nativeLdci4_0 = new short[] { 0x55, 0x8B, 0xEC, 0x33, 0xC0, 0x5D, 0xC3 };
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public bool Decrypt(MyPEImage peImage, ISimpleDeobfuscator simpleDeobfuscator, ref DumpedMethods dumpedMethods, Dictionary<uint, byte[]> tokenToNativeCode, bool unpackedNativeFile) {
|
2011-11-06 22:24:30 +08:00
|
|
|
|
if (encryptedResource.Method == null)
|
2011-10-27 01:49:25 +08:00
|
|
|
|
return false;
|
2011-10-26 20:40:55 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
encryptedResource.Initialize(simpleDeobfuscator);
|
2011-11-09 04:27:03 +08:00
|
|
|
|
if (!encryptedResource.FoundResource)
|
|
|
|
|
return false;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var methodsData = encryptedResource.Decrypt();
|
2011-10-26 20:40:55 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool hooksJitter = FindDnrCompileMethod(encryptedResource.Method.DeclaringType) != null;
|
2011-10-29 09:39:32 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
xorKey = GetXorKey();
|
|
|
|
|
XorEncrypt(methodsData);
|
2011-10-25 14:27:36 +08:00
|
|
|
|
|
2012-11-21 18:14:20 +08:00
|
|
|
|
var methodsDataReader = MemoryImageStream.Create(methodsData);
|
2011-10-27 01:49:25 +08:00
|
|
|
|
int patchCount = methodsDataReader.ReadInt32();
|
|
|
|
|
int mode = methodsDataReader.ReadInt32();
|
2011-10-28 04:25:44 +08:00
|
|
|
|
|
|
|
|
|
int tmp = methodsDataReader.ReadInt32();
|
2012-11-21 18:14:20 +08:00
|
|
|
|
methodsDataReader.Position -= 4;
|
2011-10-28 04:25:44 +08:00
|
|
|
|
if ((tmp & 0xFF000000) == 0x06000000) {
|
2011-10-29 08:27:34 +08:00
|
|
|
|
// It's method token + rva. DNR 3.7.0.3 (and earlier?) - 3.9.0.1
|
2012-11-21 18:14:20 +08:00
|
|
|
|
methodsDataReader.Position += 8L * patchCount;
|
2011-10-28 04:25:44 +08:00
|
|
|
|
patchCount = methodsDataReader.ReadInt32();
|
|
|
|
|
mode = methodsDataReader.ReadInt32();
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
PatchDwords(peImage, methodsDataReader, patchCount);
|
2012-11-21 18:14:20 +08:00
|
|
|
|
while (methodsDataReader.Position < methodsData.Length - 1) {
|
2011-10-27 01:49:25 +08:00
|
|
|
|
uint token = methodsDataReader.ReadUInt32();
|
2011-10-28 04:25:44 +08:00
|
|
|
|
int numDwords = methodsDataReader.ReadInt32();
|
2013-01-19 20:03:57 +08:00
|
|
|
|
PatchDwords(peImage, methodsDataReader, numDwords / 2);
|
2011-10-26 20:40:55 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-10-30 02:28:29 +08:00
|
|
|
|
else if (!hooksJitter || mode == 1) {
|
2013-10-13 14:49:34 +08:00
|
|
|
|
// DNR 3.9.8.0, 4.0+
|
2012-12-05 06:58:34 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
PatchDwords(peImage, methodsDataReader, patchCount);
|
2013-10-13 14:49:34 +08:00
|
|
|
|
bool oldCode = !IsNewer45Decryption(encryptedResource.Method);
|
2012-11-21 18:14:20 +08:00
|
|
|
|
while (methodsDataReader.Position < methodsData.Length - 1) {
|
2011-10-27 01:49:25 +08:00
|
|
|
|
uint rva = methodsDataReader.ReadUInt32();
|
2013-10-13 14:49:34 +08:00
|
|
|
|
int size;
|
|
|
|
|
if (oldCode) {
|
|
|
|
|
methodsDataReader.ReadUInt32(); // token, unknown, or index
|
|
|
|
|
size = methodsDataReader.ReadInt32();
|
2012-12-05 06:58:34 +08:00
|
|
|
|
}
|
2013-10-13 14:49:34 +08:00
|
|
|
|
else
|
|
|
|
|
size = methodsDataReader.ReadInt32() * 4;
|
|
|
|
|
|
|
|
|
|
var newData = methodsDataReader.ReadBytes(size);
|
|
|
|
|
if (unpackedNativeFile)
|
|
|
|
|
peImage.DotNetSafeWriteOffset(rva, newData);
|
|
|
|
|
else
|
|
|
|
|
peImage.DotNetSafeWrite(rva, newData);
|
2011-10-27 01:49:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-10-29 08:27:34 +08:00
|
|
|
|
else {
|
2013-10-13 14:49:34 +08:00
|
|
|
|
// DNR 4.0+ (jitter is hooked)
|
2011-10-29 08:27:34 +08:00
|
|
|
|
|
2014-05-23 22:19:25 +08:00
|
|
|
|
var methodDef = peImage.MetaData.TablesStream.MethodTable;
|
2012-11-21 18:14:20 +08:00
|
|
|
|
var rvaToIndex = new Dictionary<uint, int>((int)methodDef.Rows);
|
|
|
|
|
uint offset = (uint)methodDef.StartOffset;
|
|
|
|
|
for (int i = 0; i < methodDef.Rows; i++) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
uint rva = peImage.OffsetReadUInt32(offset);
|
2012-11-21 18:14:20 +08:00
|
|
|
|
offset += methodDef.RowSize;
|
2011-10-29 08:27:34 +08:00
|
|
|
|
if (rva == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if ((peImage.ReadByte(rva) & 3) == 2)
|
2011-10-29 08:27:34 +08:00
|
|
|
|
rva++;
|
|
|
|
|
else
|
2013-01-19 20:03:57 +08:00
|
|
|
|
rva += (uint)(4 * (peImage.ReadByte(rva + 1) >> 4));
|
2011-10-29 08:27:34 +08:00
|
|
|
|
rvaToIndex[rva] = i;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
PatchDwords(peImage, methodsDataReader, patchCount);
|
2011-10-29 08:27:34 +08:00
|
|
|
|
int count = methodsDataReader.ReadInt32();
|
2012-02-25 13:14:19 +08:00
|
|
|
|
dumpedMethods = new DumpedMethods();
|
2012-11-21 18:14:20 +08:00
|
|
|
|
while (methodsDataReader.Position < methodsData.Length - 1) {
|
2011-10-29 08:27:34 +08:00
|
|
|
|
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)) {
|
2012-11-11 12:31:11 +08:00
|
|
|
|
Logger.w("Could not find method having code RVA {0:X8}", rva);
|
2011-10-29 08:27:34 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2012-11-21 18:14:20 +08:00
|
|
|
|
|
2011-11-25 22:16:50 +08:00
|
|
|
|
uint methodToken = 0x06000001 + (uint)methodIndex;
|
2011-10-29 08:27:34 +08:00
|
|
|
|
|
|
|
|
|
if (isNativeCode) {
|
2011-12-01 01:28:48 +08:00
|
|
|
|
totalEncryptedNativeMethods++;
|
2011-11-25 22:16:50 +08:00
|
|
|
|
if (tokenToNativeCode != null)
|
|
|
|
|
tokenToNativeCode[methodToken] = methodData;
|
2011-11-12 03:55:39 +08:00
|
|
|
|
|
|
|
|
|
// Convert return true / false methods. The others are converted to
|
|
|
|
|
// throw 0xDEADCODE.
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (DeobUtils.IsCode(nativeLdci4, methodData)) {
|
2011-11-12 03:55:39 +08:00
|
|
|
|
uint val = BitConverter.ToUInt32(methodData, 4);
|
2011-11-25 22:24:12 +08:00
|
|
|
|
// ldc.i4 XXXXXXXXh / ret
|
2011-11-12 03:55:39 +08:00
|
|
|
|
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);
|
2011-10-29 08:27:34 +08:00
|
|
|
|
}
|
2013-01-19 20:03:57 +08:00
|
|
|
|
else if (DeobUtils.IsCode(nativeLdci4_0, methodData)) {
|
2011-11-25 22:24:12 +08:00
|
|
|
|
// ldc.i4.0 / ret
|
2011-11-12 03:55:39 +08:00
|
|
|
|
methodData = new byte[] { 0x16, 0x2A };
|
2011-10-29 08:27:34 +08:00
|
|
|
|
}
|
2011-11-25 22:24:12 +08:00
|
|
|
|
else {
|
|
|
|
|
tokenToNativeMethod[methodToken] = methodData;
|
|
|
|
|
|
|
|
|
|
// ldc.i4 0xDEADCODE / conv.u4 / throw
|
|
|
|
|
methodData = new byte[] { 0x20, 0xDE, 0xC0, 0xAD, 0xDE, 0x6D, 0x7A };
|
|
|
|
|
}
|
2011-11-12 03:55:39 +08:00
|
|
|
|
}
|
2011-10-29 08:27:34 +08:00
|
|
|
|
|
2011-11-12 03:55:39 +08:00
|
|
|
|
var dm = new DumpedMethod();
|
2013-01-19 20:03:57 +08:00
|
|
|
|
peImage.ReadMethodTableRowTo(dm, MDToken.ToRID(methodToken));
|
2011-11-12 03:55:39 +08:00
|
|
|
|
dm.code = methodData;
|
|
|
|
|
|
2012-11-18 06:49:19 +08:00
|
|
|
|
var codeReader = peImage.Reader;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
codeReader.Position = peImage.RvaToOffset(dm.mdRVA);
|
2012-11-21 18:14:20 +08:00
|
|
|
|
byte[] code;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var mbHeader = MethodBodyParser.ParseMethodBody(codeReader, out code, out dm.extraSections);
|
|
|
|
|
peImage.UpdateMethodHeaderInfo(dm, mbHeader);
|
2011-11-12 03:55:39 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
dumpedMethods.Add(dm);
|
2011-10-29 08:27:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-10-27 01:49:25 +08:00
|
|
|
|
|
|
|
|
|
return true;
|
2011-10-25 14:27:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-13 14:49:34 +08:00
|
|
|
|
public static bool IsNewer45Decryption(MethodDef method) {
|
|
|
|
|
if (method == null || method.Body == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count - 4; i++) {
|
|
|
|
|
var ldci4 = instrs[i];
|
|
|
|
|
if (!ldci4.IsLdcI4() || ldci4.GetLdcI4Value() != 4)
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 1].OpCode.Code != Code.Mul)
|
|
|
|
|
continue;
|
|
|
|
|
ldci4 = instrs[i + 2];
|
|
|
|
|
if (!ldci4.IsLdcI4() || ldci4.GetLdcI4Value() != 4)
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 3].OpCode.Code != Code.Ldloca_S && instrs[i + 3].OpCode.Code != Code.Ldloca)
|
|
|
|
|
continue;
|
|
|
|
|
var call = instrs[i + 4];
|
|
|
|
|
if (call.OpCode.Code != Code.Call)
|
|
|
|
|
continue;
|
|
|
|
|
if (!DotNetUtils.IsPinvokeMethod(call.Operand as MethodDef, "kernel32", "VirtualProtect"))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static void PatchDwords(MyPEImage peImage, IBinaryReader reader, int count) {
|
2011-10-28 04:25:44 +08:00
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
|
uint rva = reader.ReadUInt32();
|
|
|
|
|
uint data = reader.ReadUInt32();
|
2013-01-19 20:03:57 +08:00
|
|
|
|
peImage.DotNetSafeWrite(rva, BitConverter.GetBytes(data));
|
2011-10-28 04:25:44 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
long GetXorKey() {
|
2011-11-06 22:24:30 +08:00
|
|
|
|
var instructions = encryptedResource.Method.Body.Instructions;
|
2011-10-26 20:40:55 +08:00
|
|
|
|
for (int i = 0; i < instructions.Count - 1; i++) {
|
|
|
|
|
if (instructions[i].OpCode.Code != Code.Ldind_I8)
|
|
|
|
|
continue;
|
|
|
|
|
var ldci4 = instructions[i + 1];
|
2014-04-23 00:04:50 +08:00
|
|
|
|
if (ldci4.IsLdcI4())
|
|
|
|
|
return ldci4.GetLdcI4Value();
|
|
|
|
|
if (ldci4.OpCode.Code == Code.Ldc_I8)
|
|
|
|
|
return (long)ldci4.Operand;
|
2011-10-31 07:09:38 +08:00
|
|
|
|
}
|
2011-11-04 14:21:12 +08:00
|
|
|
|
return 0;
|
2011-10-31 07:09:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Reloaded() {
|
2011-11-25 22:24:12 +08:00
|
|
|
|
foreach (var pair in tokenToNativeMethod) {
|
|
|
|
|
int token = (int)pair.Key;
|
2012-11-18 01:57:36 +08:00
|
|
|
|
var method = module.ResolveToken(token) as MethodDef;
|
2011-11-25 22:24:12 +08:00
|
|
|
|
if (method == null)
|
|
|
|
|
throw new ApplicationException(string.Format("Could not find method {0:X8}", token));
|
|
|
|
|
methodToNativeMethod[method] = pair.Value;
|
|
|
|
|
}
|
2011-11-26 19:20:04 +08:00
|
|
|
|
tokenToNativeMethod = null;
|
2011-11-25 22:24:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void PrepareEncryptNativeMethods(ModuleWriterBase moduleWriter) {
|
2011-11-26 19:20:04 +08:00
|
|
|
|
if (methodToNativeMethod.Count == 0)
|
2011-11-25 22:24:12 +08:00
|
|
|
|
return;
|
|
|
|
|
|
2012-11-18 06:49:19 +08:00
|
|
|
|
validNativeMethods = new List<MethodDef>(methodToNativeMethod.Count);
|
|
|
|
|
int len = 12;
|
|
|
|
|
foreach (var kv in methodToNativeMethod) {
|
|
|
|
|
if (kv.Key.DeclaringType == null)
|
|
|
|
|
continue; // Method was removed
|
|
|
|
|
if (kv.Key.DeclaringType.Module != module)
|
|
|
|
|
continue; // method.DeclaringType was removed
|
|
|
|
|
validNativeMethods.Add(kv.Key);
|
|
|
|
|
len += 3 * 4 + kv.Value.Length;
|
|
|
|
|
}
|
|
|
|
|
if (validNativeMethods.Count == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
len = (len & ~15) + 16;
|
|
|
|
|
encryptedResource.Resource.Data = MemoryImageStream.Create(new byte[len]);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void EncryptNativeMethods(ModuleWriterBase moduleWriter) {
|
2012-11-18 06:49:19 +08:00
|
|
|
|
if (validNativeMethods == null || validNativeMethods.Count == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2012-11-11 12:31:11 +08:00
|
|
|
|
Logger.v("Encrypting native methods");
|
2011-11-25 22:33:13 +08:00
|
|
|
|
|
2011-11-25 22:24:12 +08:00
|
|
|
|
var stream = new MemoryStream();
|
|
|
|
|
var writer = new BinaryWriter(stream);
|
|
|
|
|
writer.Write((uint)0); // patch count
|
|
|
|
|
writer.Write((uint)0); // mode
|
2012-11-18 06:49:19 +08:00
|
|
|
|
writer.Write(validNativeMethods.Count);
|
2011-11-25 22:24:12 +08:00
|
|
|
|
|
|
|
|
|
int index = 0;
|
2012-11-18 06:49:19 +08:00
|
|
|
|
foreach (var method in validNativeMethods) {
|
|
|
|
|
var code = methodToNativeMethod[method];
|
2011-11-25 22:24:12 +08:00
|
|
|
|
|
2012-11-18 06:49:19 +08:00
|
|
|
|
var mb = moduleWriter.MetaData.GetMethodBody(method);
|
|
|
|
|
if (mb == null) {
|
|
|
|
|
Logger.e("Could not find method body for method {0} ({1:X8})", method, method.MDToken.Raw);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint codeRva = (uint)mb.RVA;
|
|
|
|
|
if (mb.IsTiny)
|
2011-11-25 22:24:12 +08:00
|
|
|
|
codeRva++;
|
|
|
|
|
else
|
2012-11-18 06:49:19 +08:00
|
|
|
|
codeRva += (uint)(4 * (mb.Code[1] >> 4));
|
2011-11-25 22:33:13 +08:00
|
|
|
|
|
2012-11-18 06:49:19 +08:00
|
|
|
|
Logger.v("Native method {0:X8}, code RVA {1:X8}", new MDToken(Table.Method, moduleWriter.MetaData.GetRid(method)).Raw, codeRva);
|
2011-11-25 22:33:13 +08:00
|
|
|
|
|
2011-11-25 22:24:12 +08:00
|
|
|
|
writer.Write(codeRva);
|
|
|
|
|
writer.Write(0x70000000 + index++);
|
|
|
|
|
writer.Write(code.Length);
|
|
|
|
|
writer.Write(code);
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-25 22:33:13 +08:00
|
|
|
|
if (index != 0)
|
2012-11-11 12:31:11 +08:00
|
|
|
|
Logger.n("Re-encrypted {0}/{1} native methods", index, totalEncryptedNativeMethods);
|
2011-11-25 22:33:13 +08:00
|
|
|
|
|
2012-11-18 06:49:19 +08:00
|
|
|
|
var resourceChunk = moduleWriter.MetaData.GetChunk(encryptedResource.Resource);
|
|
|
|
|
var resourceData = resourceChunk.Data;
|
|
|
|
|
|
|
|
|
|
var encrypted = stream.ToArray();
|
2013-01-19 20:03:57 +08:00
|
|
|
|
XorEncrypt(encrypted);
|
2012-11-18 06:49:19 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
encrypted = encryptedResource.Encrypt(encrypted);
|
2012-11-18 06:49:19 +08:00
|
|
|
|
if (encrypted.Length != resourceData.Length)
|
|
|
|
|
Logger.e("Encrypted native methods array is not same size as original array");
|
|
|
|
|
Array.Copy(encrypted, resourceData, resourceData.Length);
|
2011-11-25 22:24:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-13 14:49:34 +08:00
|
|
|
|
enum CompileMethodType {
|
|
|
|
|
Unknown,
|
|
|
|
|
V1, // <= DNR 4.5.0.0 (2012-11-06 <= endDate < 2013-01-31)
|
|
|
|
|
V2, // >= DNR 4.5.0.0 (2012-11-06 < startDate <= 2013-01-31)
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public static MethodDef FindDnrCompileMethod(TypeDef type) {
|
2011-10-31 07:09:38 +08:00
|
|
|
|
foreach (var method in type.Methods) {
|
|
|
|
|
if (!method.IsStatic || method.Body == null)
|
|
|
|
|
continue;
|
2012-11-18 01:57:36 +08:00
|
|
|
|
var sig = method.MethodSig;
|
|
|
|
|
if (sig == null || sig.Params.Count != 6)
|
2011-10-31 07:09:38 +08:00
|
|
|
|
continue;
|
2013-10-13 14:49:34 +08:00
|
|
|
|
if (GetCompileMethodType(method) == CompileMethodType.Unknown)
|
2011-10-31 07:09:38 +08:00
|
|
|
|
continue;
|
|
|
|
|
return method;
|
2011-10-25 14:27:36 +08:00
|
|
|
|
}
|
2011-10-31 07:09:38 +08:00
|
|
|
|
return null;
|
2011-10-25 14:27:36 +08:00
|
|
|
|
}
|
2013-10-13 14:49:34 +08:00
|
|
|
|
|
|
|
|
|
static CompileMethodType GetCompileMethodType(MethodDef method) {
|
|
|
|
|
if (DotNetUtils.IsMethod(method, "System.UInt32", "(System.UInt64&,System.IntPtr,System.IntPtr,System.UInt32,System.IntPtr&,System.UInt32&)"))
|
|
|
|
|
return CompileMethodType.V1;
|
|
|
|
|
if (DotNetUtils.IsMethod(method, "System.UInt32", "(System.IntPtr,System.IntPtr,System.IntPtr,System.UInt32,System.IntPtr,System.UInt32&)"))
|
|
|
|
|
return CompileMethodType.V2;
|
|
|
|
|
return CompileMethodType.Unknown;
|
|
|
|
|
}
|
2011-10-25 14:27:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|