2012-07-30 15:16:52 +08:00
|
|
|
|
/*
|
2014-03-12 05:17:50 +08:00
|
|
|
|
Copyright (C) 2011-2014 de4dot@gmail.com
|
2012-07-30 15:16:52 +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;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
2012-12-23 04:08:29 +08:00
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using dnlib.DotNet.Emit;
|
2012-07-30 15:16:52 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.Confuser {
|
2012-12-01 04:05:54 +08:00
|
|
|
|
class ProxyCallFixer : ProxyCallFixer2, IVersionProvider, IDisposable {
|
|
|
|
|
MethodDefAndDeclaringTypeDict<ProxyCreatorInfo> methodToInfo = new MethodDefAndDeclaringTypeDict<ProxyCreatorInfo>();
|
|
|
|
|
FieldDefAndDeclaringTypeDict<List<MethodDef>> fieldToMethods = new FieldDefAndDeclaringTypeDict<List<MethodDef>>();
|
2012-07-30 15:16:52 +08:00
|
|
|
|
string ourAsm;
|
2012-07-31 00:00:00 +08:00
|
|
|
|
ConfuserVersion version = ConfuserVersion.Unknown;
|
2012-08-04 15:26:13 +08:00
|
|
|
|
byte[] fileData;
|
|
|
|
|
x86Emulator x86emu;
|
2012-08-08 23:38:38 +08:00
|
|
|
|
ushort callvirtChar;
|
2013-09-22 22:07:15 +08:00
|
|
|
|
bool foundNewobjProxy;
|
2012-07-31 00:00:00 +08:00
|
|
|
|
|
|
|
|
|
enum ConfuserVersion {
|
|
|
|
|
Unknown,
|
|
|
|
|
v10_r42915,
|
2012-08-11 02:14:47 +08:00
|
|
|
|
v10_r42919,
|
2012-07-31 00:00:00 +08:00
|
|
|
|
v10_r48717,
|
2012-08-11 02:43:11 +08:00
|
|
|
|
v11_r50378,
|
2012-08-11 03:12:07 +08:00
|
|
|
|
v12_r54564,
|
2012-08-11 03:28:08 +08:00
|
|
|
|
v13_r55346,
|
2012-08-11 03:36:15 +08:00
|
|
|
|
v13_r55604,
|
2012-08-01 01:12:35 +08:00
|
|
|
|
v14_r58564,
|
2012-08-11 04:10:44 +08:00
|
|
|
|
v14_r58802,
|
2012-08-01 20:24:17 +08:00
|
|
|
|
v14_r58857,
|
2012-08-11 04:25:33 +08:00
|
|
|
|
v16_r66631,
|
2012-08-11 04:50:03 +08:00
|
|
|
|
v16_r70489,
|
2012-08-11 05:00:37 +08:00
|
|
|
|
v17_r73479,
|
2012-08-04 15:26:13 +08:00
|
|
|
|
v17_r73740_normal,
|
|
|
|
|
v17_r73740_native,
|
2012-08-05 18:46:54 +08:00
|
|
|
|
v17_r74708_normal,
|
|
|
|
|
v17_r74708_native,
|
2012-08-08 23:38:38 +08:00
|
|
|
|
v18_r75367_normal,
|
|
|
|
|
v18_r75367_native,
|
2012-08-11 05:18:44 +08:00
|
|
|
|
v18_r75369_normal,
|
|
|
|
|
v18_r75369_native,
|
2012-08-09 03:22:00 +08:00
|
|
|
|
v19_r76101_normal,
|
|
|
|
|
v19_r76101_native,
|
2013-02-03 00:03:56 +08:00
|
|
|
|
v19_r78363_normal,
|
|
|
|
|
v19_r78363_native,
|
2013-09-22 22:07:15 +08:00
|
|
|
|
v19_r78963_normal_Newobj,
|
|
|
|
|
v19_r78963_native_Newobj,
|
2012-07-31 00:00:00 +08:00
|
|
|
|
}
|
2012-07-30 15:16:52 +08:00
|
|
|
|
|
|
|
|
|
enum ProxyCreatorType {
|
|
|
|
|
None,
|
|
|
|
|
CallOrCallvirt,
|
|
|
|
|
Newobj,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ProxyCreatorInfo {
|
2012-11-19 06:42:43 +08:00
|
|
|
|
public readonly MethodDef creatorMethod;
|
2012-07-30 15:16:52 +08:00
|
|
|
|
public readonly ProxyCreatorType proxyCreatorType;
|
2012-07-31 00:00:00 +08:00
|
|
|
|
public readonly ConfuserVersion version;
|
2012-08-01 01:12:35 +08:00
|
|
|
|
public readonly uint magic;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
public readonly MethodDef nativeMethod;
|
2012-08-08 23:38:38 +08:00
|
|
|
|
public readonly ushort callvirtChar;
|
2012-07-30 15:16:52 +08:00
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
public ProxyCreatorInfo(MethodDef creatorMethod, ProxyCreatorType proxyCreatorType, ConfuserVersion version, uint magic, MethodDef nativeMethod, ushort callvirtChar) {
|
2012-07-30 15:16:52 +08:00
|
|
|
|
this.creatorMethod = creatorMethod;
|
|
|
|
|
this.proxyCreatorType = proxyCreatorType;
|
2012-07-31 00:00:00 +08:00
|
|
|
|
this.version = version;
|
2012-08-01 01:12:35 +08:00
|
|
|
|
this.magic = magic;
|
2012-08-04 15:26:13 +08:00
|
|
|
|
this.nativeMethod = nativeMethod;
|
2012-08-08 23:38:38 +08:00
|
|
|
|
this.callvirtChar = callvirtChar;
|
2012-07-30 15:16:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class DelegateInitInfo {
|
|
|
|
|
public readonly byte[] data;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
public readonly FieldDef field;
|
|
|
|
|
public readonly MethodDef creatorMethod;
|
2012-07-30 15:16:52 +08:00
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
public DelegateInitInfo(FieldDef field, MethodDef creatorMethod) {
|
2012-07-31 00:00:00 +08:00
|
|
|
|
this.field = field;
|
|
|
|
|
this.creatorMethod = creatorMethod;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
public DelegateInitInfo(string data, FieldDef field, MethodDef creatorMethod) {
|
2012-07-30 15:16:52 +08:00
|
|
|
|
this.data = Convert.FromBase64String(data);
|
|
|
|
|
this.field = field;
|
|
|
|
|
this.creatorMethod = creatorMethod;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool ProxyCallIsObfuscated {
|
|
|
|
|
get { return true; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
public IEnumerable<FieldDef> Fields {
|
2012-07-31 10:44:30 +08:00
|
|
|
|
get {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
var fields = new List<FieldDef>(fieldToMethods.GetKeys());
|
|
|
|
|
var type = DotNetUtils.GetModuleType(module);
|
2012-07-31 10:44:30 +08:00
|
|
|
|
if (fields.Count > 0 && type != null) {
|
|
|
|
|
foreach (var field in type.Fields) {
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var fieldType = field.FieldType.TryGetTypeDef();
|
2012-07-31 10:44:30 +08:00
|
|
|
|
if (fieldType != null && delegateTypesDict.ContainsKey(fieldType))
|
|
|
|
|
fields.Add(field);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return fields;
|
|
|
|
|
}
|
2012-07-30 15:16:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
public override IEnumerable<Tuple<MethodDef, string>> OtherMethods {
|
2012-07-30 15:16:52 +08:00
|
|
|
|
get {
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var list = new List<Tuple<MethodDef, string>>();
|
2013-01-19 20:09:49 +08:00
|
|
|
|
foreach (var info in methodToInfo.GetValues()) {
|
2012-11-19 06:42:43 +08:00
|
|
|
|
list.Add(new Tuple<MethodDef, string> {
|
2012-08-04 15:26:13 +08:00
|
|
|
|
Item1 = info.creatorMethod,
|
2012-07-30 15:16:52 +08:00
|
|
|
|
Item2 = "Delegate creator method",
|
|
|
|
|
});
|
2012-11-19 06:42:43 +08:00
|
|
|
|
list.Add(new Tuple<MethodDef, string> {
|
2012-08-04 15:26:13 +08:00
|
|
|
|
Item1 = info.nativeMethod,
|
|
|
|
|
Item2 = "Calculate RID native method",
|
|
|
|
|
});
|
2012-07-30 15:16:52 +08:00
|
|
|
|
}
|
2013-01-19 20:09:49 +08:00
|
|
|
|
foreach (var methods in fieldToMethods.GetValues()) {
|
2012-07-31 10:44:30 +08:00
|
|
|
|
foreach (var method in methods) {
|
2012-11-19 06:42:43 +08:00
|
|
|
|
list.Add(new Tuple<MethodDef, string> {
|
2012-07-31 10:44:30 +08:00
|
|
|
|
Item1 = method,
|
|
|
|
|
Item2 = "Proxy delegate method",
|
|
|
|
|
});
|
|
|
|
|
}
|
2012-07-30 15:16:52 +08:00
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
public ProxyCallFixer(ModuleDefMD module, byte[] fileData)
|
2012-07-30 15:16:52 +08:00
|
|
|
|
: base(module) {
|
2012-08-04 15:26:13 +08:00
|
|
|
|
this.fileData = fileData;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (module.Assembly == null)
|
|
|
|
|
ourAsm = " -1-1-1-1-1- , Version=1.2.3.4, Culture=neutral, PublicKeyToken=null";
|
2012-08-04 02:15:13 +08:00
|
|
|
|
else
|
|
|
|
|
ourAsm = module.Assembly.FullName;
|
2012-07-30 15:16:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
protected override object CheckCctor(TypeDef type, MethodDef cctor) {
|
2012-07-31 13:15:38 +08:00
|
|
|
|
// Here if 1.2 r54564 (almost 1.3) or later
|
|
|
|
|
|
2012-12-01 04:05:54 +08:00
|
|
|
|
var fieldToInfo = new FieldDefAndDeclaringTypeDict<DelegateInitInfo>();
|
2012-07-31 13:15:38 +08:00
|
|
|
|
|
|
|
|
|
var instrs = cctor.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count - 1; i++) {
|
|
|
|
|
var ldtoken = instrs[i];
|
|
|
|
|
if (ldtoken.OpCode.Code != Code.Ldtoken)
|
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var field = ldtoken.Operand as FieldDef;
|
2012-07-31 13:15:38 +08:00
|
|
|
|
if (field == null || field.DeclaringType != cctor.DeclaringType)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var call = instrs[i + 1];
|
|
|
|
|
if (call.OpCode.Code != Code.Call)
|
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var calledMethod = call.Operand as MethodDef;
|
2012-07-31 13:15:38 +08:00
|
|
|
|
if (calledMethod == null)
|
|
|
|
|
continue;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!IsDelegateCreatorMethod(calledMethod))
|
2012-07-31 13:15:38 +08:00
|
|
|
|
continue;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
var info = methodToInfo.Find(calledMethod);
|
2012-07-31 13:15:38 +08:00
|
|
|
|
if (info == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
i++;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
fieldToInfo.Add(field, new DelegateInitInfo(field, calledMethod));
|
2012-07-31 13:15:38 +08:00
|
|
|
|
}
|
|
|
|
|
return fieldToInfo.Count == 0 ? null : fieldToInfo;
|
2012-07-30 15:16:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
protected override void GetCallInfo(object context, FieldDef field, out IMethod calledMethod, out OpCode callOpcode) {
|
2012-07-31 13:15:38 +08:00
|
|
|
|
var info = context as DelegateInitInfo;
|
|
|
|
|
if (info == null) {
|
2012-12-01 04:05:54 +08:00
|
|
|
|
var fieldToInfo = context as FieldDefAndDeclaringTypeDict<DelegateInitInfo>;
|
2012-07-31 13:15:38 +08:00
|
|
|
|
if (fieldToInfo != null)
|
2013-01-19 20:09:49 +08:00
|
|
|
|
info = fieldToInfo.Find(field);
|
2012-07-31 13:15:38 +08:00
|
|
|
|
}
|
|
|
|
|
if (info == null)
|
|
|
|
|
throw new ApplicationException("Couldn't get the delegate info");
|
2013-01-19 20:09:49 +08:00
|
|
|
|
var creatorInfo = methodToInfo.Find(info.creatorMethod);
|
2012-07-30 15:16:52 +08:00
|
|
|
|
|
2012-07-31 00:00:00 +08:00
|
|
|
|
switch (creatorInfo.version) {
|
|
|
|
|
case ConfuserVersion.v10_r42915:
|
2012-08-11 02:14:47 +08:00
|
|
|
|
case ConfuserVersion.v10_r42919:
|
2013-01-19 20:09:49 +08:00
|
|
|
|
GetCallInfo_v10_r42915(info, creatorInfo, out calledMethod, out callOpcode);
|
2012-07-31 00:00:00 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v10_r48717:
|
2012-08-11 02:43:11 +08:00
|
|
|
|
case ConfuserVersion.v11_r50378:
|
2012-08-11 03:12:07 +08:00
|
|
|
|
case ConfuserVersion.v12_r54564:
|
2012-08-11 03:28:08 +08:00
|
|
|
|
case ConfuserVersion.v13_r55346:
|
2012-08-11 03:36:15 +08:00
|
|
|
|
case ConfuserVersion.v13_r55604:
|
2012-08-01 01:12:35 +08:00
|
|
|
|
case ConfuserVersion.v14_r58564:
|
2012-08-11 04:10:44 +08:00
|
|
|
|
case ConfuserVersion.v14_r58802:
|
2013-01-19 20:09:49 +08:00
|
|
|
|
GetCallInfo_v10_r48717(info, creatorInfo, out calledMethod, out callOpcode);
|
2012-07-31 00:00:00 +08:00
|
|
|
|
break;
|
|
|
|
|
|
2012-08-01 20:24:17 +08:00
|
|
|
|
case ConfuserVersion.v14_r58857:
|
2012-08-11 04:25:33 +08:00
|
|
|
|
case ConfuserVersion.v16_r66631:
|
2012-08-11 04:50:03 +08:00
|
|
|
|
case ConfuserVersion.v16_r70489:
|
2012-08-11 05:00:37 +08:00
|
|
|
|
case ConfuserVersion.v17_r73479:
|
2013-01-19 20:09:49 +08:00
|
|
|
|
GetCallInfo_v14_r58857(info, creatorInfo, out calledMethod, out callOpcode);
|
2012-08-01 20:24:17 +08:00
|
|
|
|
break;
|
|
|
|
|
|
2012-08-04 15:26:13 +08:00
|
|
|
|
case ConfuserVersion.v17_r73740_normal:
|
2012-08-05 18:46:54 +08:00
|
|
|
|
case ConfuserVersion.v17_r74708_normal:
|
2013-01-19 20:09:49 +08:00
|
|
|
|
GetCallInfo_v17_r73740_normal(info, creatorInfo, out calledMethod, out callOpcode);
|
2012-08-04 15:26:13 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v17_r73740_native:
|
2012-08-05 18:46:54 +08:00
|
|
|
|
case ConfuserVersion.v17_r74708_native:
|
2013-01-19 20:09:49 +08:00
|
|
|
|
GetCallInfo_v17_r73740_native(info, creatorInfo, out calledMethod, out callOpcode);
|
2012-08-04 15:26:13 +08:00
|
|
|
|
break;
|
|
|
|
|
|
2012-08-08 23:38:38 +08:00
|
|
|
|
case ConfuserVersion.v18_r75367_normal:
|
2012-08-11 05:18:44 +08:00
|
|
|
|
case ConfuserVersion.v18_r75369_normal:
|
2012-08-09 03:22:00 +08:00
|
|
|
|
case ConfuserVersion.v19_r76101_normal:
|
2013-02-03 00:03:56 +08:00
|
|
|
|
case ConfuserVersion.v19_r78363_normal:
|
2013-09-22 22:07:15 +08:00
|
|
|
|
case ConfuserVersion.v19_r78963_normal_Newobj:
|
2013-01-19 20:09:49 +08:00
|
|
|
|
GetCallInfo_v18_r75367_normal(info, creatorInfo, out calledMethod, out callOpcode);
|
2012-08-08 23:38:38 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v18_r75367_native:
|
2012-08-11 05:18:44 +08:00
|
|
|
|
case ConfuserVersion.v18_r75369_native:
|
2012-08-09 03:22:00 +08:00
|
|
|
|
case ConfuserVersion.v19_r76101_native:
|
2013-02-03 00:03:56 +08:00
|
|
|
|
case ConfuserVersion.v19_r78363_native:
|
2013-09-22 22:07:15 +08:00
|
|
|
|
case ConfuserVersion.v19_r78963_native_Newobj:
|
2013-01-19 20:09:49 +08:00
|
|
|
|
GetCallInfo_v18_r75367_native(info, creatorInfo, out calledMethod, out callOpcode);
|
2012-08-08 23:38:38 +08:00
|
|
|
|
break;
|
|
|
|
|
|
2012-07-31 00:00:00 +08:00
|
|
|
|
default:
|
|
|
|
|
throw new ApplicationException("Unknown version");
|
|
|
|
|
}
|
2012-08-01 19:03:36 +08:00
|
|
|
|
|
|
|
|
|
if (calledMethod == null) {
|
2012-11-19 06:42:43 +08:00
|
|
|
|
Logger.w("Could not find real method. Proxy field: {0:X8}", info.field.MDToken.ToInt32());
|
2012-08-01 19:03:36 +08:00
|
|
|
|
errors++;
|
|
|
|
|
}
|
2012-07-31 00:00:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
void GetCallInfo_v10_r42915(DelegateInitInfo info, ProxyCreatorInfo creatorInfo, out IMethod calledMethod, out OpCode callOpcode) {
|
2012-07-30 15:16:52 +08:00
|
|
|
|
var reader = new BinaryReader(new MemoryStream(info.data));
|
|
|
|
|
|
|
|
|
|
bool isCallvirt = false;
|
|
|
|
|
if (creatorInfo.proxyCreatorType == ProxyCreatorType.CallOrCallvirt)
|
|
|
|
|
isCallvirt = reader.ReadBoolean();
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
var asmRef = ReadAssemblyNameReference(reader);
|
2012-07-31 00:00:00 +08:00
|
|
|
|
// If < 1.0 r42919, then high byte is 06, else it's cleared.
|
|
|
|
|
uint token = (reader.ReadUInt32() & 0x00FFFFFF) | 0x06000000;
|
2012-07-30 15:16:52 +08:00
|
|
|
|
if (reader.BaseStream.Position != reader.BaseStream.Length)
|
|
|
|
|
throw new ApplicationException("Extra data");
|
|
|
|
|
|
|
|
|
|
if (asmRef.FullName == ourAsm)
|
2012-11-19 06:42:43 +08:00
|
|
|
|
calledMethod = module.ResolveToken(token) as IMethod;
|
2012-07-30 15:16:52 +08:00
|
|
|
|
else
|
2013-01-19 20:09:49 +08:00
|
|
|
|
calledMethod = CreateMethodReference(asmRef, token);
|
2012-07-30 15:16:52 +08:00
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
callOpcode = GetCallOpCode(creatorInfo, isCallvirt);
|
2012-07-30 15:16:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
void GetCallInfo_v10_r48717(DelegateInitInfo info, ProxyCreatorInfo creatorInfo, out IMethod calledMethod, out OpCode callOpcode) {
|
2012-08-11 04:10:44 +08:00
|
|
|
|
bool isNew = creatorInfo.version == ConfuserVersion.v14_r58802;
|
|
|
|
|
|
|
|
|
|
int offs = creatorInfo.proxyCreatorType == ProxyCreatorType.CallOrCallvirt ? 2 : 1;
|
|
|
|
|
if (isNew)
|
|
|
|
|
offs--;
|
|
|
|
|
int callvirtOffs = isNew ? 0 : 1;
|
|
|
|
|
|
|
|
|
|
// This is an obfuscator bug. Field names are stored in the #Strings heap,
|
|
|
|
|
// and strings in that heap are UTF8 zero terminated strings, but Confuser
|
|
|
|
|
// can generate names with zeros in them. This was fixed in 1.4 58857.
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (offs + 2 > info.field.Name.String.Length) {
|
2012-08-01 19:03:36 +08:00
|
|
|
|
calledMethod = null;
|
|
|
|
|
callOpcode = OpCodes.Call;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
uint token = BitConverter.ToUInt32(Encoding.Unicode.GetBytes(info.field.Name.String.ToCharArray(), offs, 2), 0) ^ creatorInfo.magic;
|
2012-08-01 16:01:26 +08:00
|
|
|
|
uint table = token >> 24;
|
|
|
|
|
if (table != 0 && table != 6 && table != 0x0A && table != 0x2B)
|
|
|
|
|
throw new ApplicationException("Invalid method token");
|
|
|
|
|
|
2012-07-31 13:30:21 +08:00
|
|
|
|
// 1.3 r55346 now correctly uses method reference tokens and finally fixed the old
|
|
|
|
|
// bug of using methoddef tokens to reference external methods.
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (isNew || info.field.Name.String[0] == (char)1 || table != 0x06)
|
|
|
|
|
calledMethod = module.ResolveToken(token) as IMethod;
|
2012-07-31 00:00:00 +08:00
|
|
|
|
else {
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var asmRef = module.ResolveAssemblyRef((uint)info.field.Name.String[0] - 2 + 1);
|
2013-01-19 20:09:49 +08:00
|
|
|
|
calledMethod = CreateMethodReference(asmRef, token);
|
2012-07-31 00:00:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isCallvirt = false;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (creatorInfo.proxyCreatorType == ProxyCreatorType.CallOrCallvirt && info.field.Name.String[callvirtOffs] == '\r')
|
2012-07-31 00:00:00 +08:00
|
|
|
|
isCallvirt = true;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
callOpcode = GetCallOpCode(creatorInfo, isCallvirt);
|
2012-07-31 00:00:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
void GetCallInfo_v14_r58857(DelegateInitInfo info, ProxyCreatorInfo creatorInfo, out IMethod calledMethod, out OpCode callOpcode) {
|
2012-08-01 20:24:17 +08:00
|
|
|
|
int offs = creatorInfo.proxyCreatorType == ProxyCreatorType.CallOrCallvirt ? 1 : 0;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
var nameInfo = DecryptFieldName(info.field.Name.String);
|
2012-08-01 20:24:17 +08:00
|
|
|
|
|
|
|
|
|
uint token = BitConverter.ToUInt32(nameInfo, offs) ^ creatorInfo.magic;
|
|
|
|
|
uint table = token >> 24;
|
|
|
|
|
if (table != 6 && table != 0x0A && table != 0x2B)
|
|
|
|
|
throw new ApplicationException("Invalid method token");
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
calledMethod = module.ResolveToken(token) as IMethod;
|
2012-08-01 20:24:17 +08:00
|
|
|
|
|
|
|
|
|
bool isCallvirt = false;
|
|
|
|
|
if (creatorInfo.proxyCreatorType == ProxyCreatorType.CallOrCallvirt && nameInfo[0] == '\r')
|
|
|
|
|
isCallvirt = true;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
callOpcode = GetCallOpCode(creatorInfo, isCallvirt);
|
2012-08-01 20:24:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static byte[] DecryptFieldName(string name) {
|
2012-08-01 20:24:17 +08:00
|
|
|
|
var chars = new char[name.Length];
|
|
|
|
|
for (int i = 0; i < chars.Length; i++)
|
|
|
|
|
chars[i] = (char)((byte)name[i] ^ i);
|
|
|
|
|
return Convert.FromBase64CharArray(chars, 0, chars.Length);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
void Extract_v17_r73740(ProxyCreatorInfo creatorInfo, byte[] nameInfo, out uint arg, out uint table, out bool isCallvirt) {
|
2012-08-04 15:26:13 +08:00
|
|
|
|
switch (creatorInfo.proxyCreatorType) {
|
|
|
|
|
case ProxyCreatorType.CallOrCallvirt:
|
|
|
|
|
arg = BitConverter.ToUInt32(nameInfo, 1);
|
|
|
|
|
table = (uint)(nameInfo[0] & 0x7F) << 24;
|
|
|
|
|
isCallvirt = (nameInfo[0] & 0x80) != 0;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ProxyCreatorType.Newobj:
|
|
|
|
|
arg = BitConverter.ToUInt32(nameInfo, 0);
|
|
|
|
|
table = (uint)nameInfo[4] << 24;
|
|
|
|
|
isCallvirt = false;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw new ApplicationException("Invalid creator type");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
void GetCallInfo_v17_r73740_normal(DelegateInitInfo info, ProxyCreatorInfo creatorInfo, out IMethod calledMethod, out OpCode callOpcode) {
|
|
|
|
|
var nameInfo = DecryptFieldName(info.field.Name.String);
|
2012-08-04 15:26:13 +08:00
|
|
|
|
uint arg, table;
|
|
|
|
|
bool isCallvirt;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
Extract_v17_r73740(creatorInfo, nameInfo, out arg, out table, out isCallvirt);
|
2012-08-04 15:26:13 +08:00
|
|
|
|
uint token = (arg ^ creatorInfo.magic) | table;
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
calledMethod = module.ResolveToken((int)token) as IMethod;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
callOpcode = GetCallOpCode(creatorInfo, isCallvirt);
|
2012-08-04 15:26:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
void GetCallInfo_v17_r73740_native(DelegateInitInfo info, ProxyCreatorInfo creatorInfo, out IMethod calledMethod, out OpCode callOpcode) {
|
|
|
|
|
var nameInfo = DecryptFieldName(info.field.Name.String);
|
2012-08-04 15:26:13 +08:00
|
|
|
|
uint arg, table;
|
|
|
|
|
bool isCallvirt;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
Extract_v17_r73740(creatorInfo, nameInfo, out arg, out table, out isCallvirt);
|
2012-08-04 15:26:13 +08:00
|
|
|
|
if (x86emu == null)
|
2012-12-01 04:05:54 +08:00
|
|
|
|
x86emu = new x86Emulator(fileData);
|
2013-01-19 20:09:49 +08:00
|
|
|
|
uint token = x86emu.Emulate((uint)creatorInfo.nativeMethod.RVA, arg) | table;
|
2012-08-04 15:26:13 +08:00
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
calledMethod = module.ResolveToken((int)token) as IMethod;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
callOpcode = GetCallOpCode(creatorInfo, isCallvirt);
|
2012-08-04 15:26:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
void GetCallInfo_v18_r75367_normal(DelegateInitInfo info, ProxyCreatorInfo creatorInfo, out IMethod calledMethod, out OpCode callOpcode) {
|
|
|
|
|
GetCallInfo_v18_r75367(info, creatorInfo, out calledMethod, out callOpcode, (creatorInfo2, magic) => creatorInfo2.magic ^ magic);
|
2012-08-08 23:38:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
void GetCallInfo_v18_r75367_native(DelegateInitInfo info, ProxyCreatorInfo creatorInfo, out IMethod calledMethod, out OpCode callOpcode) {
|
|
|
|
|
GetCallInfo_v18_r75367(info, creatorInfo, out calledMethod, out callOpcode, (creatorInfo2, magic) => {
|
2012-08-08 23:38:38 +08:00
|
|
|
|
if (x86emu == null)
|
2012-12-01 04:05:54 +08:00
|
|
|
|
x86emu = new x86Emulator(fileData);
|
2013-01-19 20:09:49 +08:00
|
|
|
|
return x86emu.Emulate((uint)creatorInfo2.nativeMethod.RVA, magic);
|
2012-08-08 23:38:38 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
void GetCallInfo_v18_r75367(DelegateInitInfo info, ProxyCreatorInfo creatorInfo, out IMethod calledMethod, out OpCode callOpcode, Func<ProxyCreatorInfo, uint, uint> getRid) {
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var sig = module.ReadBlob(info.field.MDToken.Raw);
|
2012-08-08 23:38:38 +08:00
|
|
|
|
int len = sig.Length;
|
|
|
|
|
uint magic = (uint)((sig[len - 2] << 24) | (sig[len - 3] << 16) | (sig[len - 5] << 8) | sig[len - 6]);
|
|
|
|
|
uint rid = getRid(creatorInfo, magic);
|
|
|
|
|
int token = (sig[len - 7] << 24) | (int)rid;
|
|
|
|
|
uint table = (uint)token >> 24;
|
|
|
|
|
if (table != 6 && table != 0x0A && table != 0x2B)
|
|
|
|
|
throw new ApplicationException("Invalid method token");
|
2012-11-19 06:42:43 +08:00
|
|
|
|
calledMethod = module.ResolveToken(token) as IMethod;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
callOpcode = GetCallOpCode(creatorInfo, info.field);
|
2012-08-08 23:38:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static OpCode GetCallOpCode(ProxyCreatorInfo info, FieldDef field) {
|
2012-08-08 23:38:38 +08:00
|
|
|
|
switch (info.proxyCreatorType) {
|
|
|
|
|
case ProxyCreatorType.CallOrCallvirt:
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (field.Name.String.Length > 0 && field.Name.String[0] == info.callvirtChar)
|
2012-08-08 23:38:38 +08:00
|
|
|
|
return OpCodes.Callvirt;
|
|
|
|
|
return OpCodes.Call;
|
|
|
|
|
|
|
|
|
|
case ProxyCreatorType.Newobj:
|
|
|
|
|
return OpCodes.Newobj;
|
|
|
|
|
|
|
|
|
|
default: throw new NotSupportedException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-30 15:16:52 +08:00
|
|
|
|
// A method token is not a stable value so this method can fail to return the correct method!
|
2012-07-31 13:30:21 +08:00
|
|
|
|
// There's nothing I can do about that. It's an obfuscator bug. It was fixed in 1.3 r55346.
|
2013-01-19 20:09:49 +08:00
|
|
|
|
IMethod CreateMethodReference(AssemblyRef asmRef, uint methodToken) {
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var asm = module.Context.AssemblyResolver.Resolve(asmRef, module);
|
2012-07-30 15:16:52 +08:00
|
|
|
|
if (asm == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var method = ((ModuleDefMD)asm.ManifestModule).ResolveToken(methodToken) as MethodDef;
|
2012-07-30 15:16:52 +08:00
|
|
|
|
if (method == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return module.Import(method);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
AssemblyRef ReadAssemblyNameReference(BinaryReader reader) {
|
|
|
|
|
var name = ReadString(reader);
|
2012-07-30 15:16:52 +08:00
|
|
|
|
var version = new Version(reader.ReadInt16(), reader.ReadInt16(), reader.ReadInt16(), reader.ReadInt16());
|
2013-01-19 20:09:49 +08:00
|
|
|
|
var culture = ReadString(reader);
|
2012-07-30 15:16:52 +08:00
|
|
|
|
byte[] pkt = reader.ReadBoolean() ? reader.ReadBytes(8) : null;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
return module.UpdateRowId(new AssemblyRefUser(name, version, pkt == null ? null : new PublicKeyToken(pkt), culture));
|
2012-07-30 15:16:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static string ReadString(BinaryReader reader) {
|
2012-07-30 15:16:52 +08:00
|
|
|
|
int len = reader.ReadByte();
|
|
|
|
|
var bytes = new byte[len];
|
|
|
|
|
for (int i = 0; i < len; i++)
|
|
|
|
|
bytes[i] = (byte)(reader.ReadByte() ^ len);
|
|
|
|
|
return Encoding.UTF8.GetString(bytes);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static OpCode GetCallOpCode(ProxyCreatorInfo info, bool isCallvirt) {
|
2012-07-30 15:16:52 +08:00
|
|
|
|
switch (info.proxyCreatorType) {
|
|
|
|
|
case ProxyCreatorType.Newobj:
|
|
|
|
|
return OpCodes.Newobj;
|
|
|
|
|
|
|
|
|
|
case ProxyCreatorType.CallOrCallvirt:
|
|
|
|
|
return isCallvirt ? OpCodes.Callvirt : OpCodes.Call;
|
|
|
|
|
|
|
|
|
|
default: throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
public void FindDelegateCreator(ISimpleDeobfuscator simpleDeobfuscator) {
|
|
|
|
|
var type = DotNetUtils.GetModuleType(module);
|
2012-08-08 23:38:38 +08:00
|
|
|
|
if (type == null)
|
|
|
|
|
return;
|
2012-07-30 15:16:52 +08:00
|
|
|
|
foreach (var method in type.Methods) {
|
|
|
|
|
if (method.Body == null || !method.IsStatic || !method.IsAssembly)
|
|
|
|
|
continue;
|
2012-07-31 00:00:00 +08:00
|
|
|
|
ConfuserVersion theVersion = ConfuserVersion.Unknown;
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (DotNetUtils.IsMethod(method, "System.Void", "(System.String,System.RuntimeFieldHandle)"))
|
2012-07-31 00:00:00 +08:00
|
|
|
|
theVersion = ConfuserVersion.v10_r42915;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
else if (DotNetUtils.IsMethod(method, "System.Void", "(System.RuntimeFieldHandle)"))
|
2012-07-31 00:00:00 +08:00
|
|
|
|
theVersion = ConfuserVersion.v10_r48717;
|
|
|
|
|
else
|
2012-07-30 15:16:52 +08:00
|
|
|
|
continue;
|
2012-07-31 00:00:00 +08:00
|
|
|
|
|
2013-02-03 00:03:56 +08:00
|
|
|
|
int tmpVer;
|
|
|
|
|
var proxyType = GetProxyCreatorType(method, simpleDeobfuscator, out tmpVer);
|
2012-07-30 15:16:52 +08:00
|
|
|
|
if (proxyType == ProxyCreatorType.None)
|
|
|
|
|
continue;
|
2013-09-22 22:07:15 +08:00
|
|
|
|
if (proxyType == ProxyCreatorType.Newobj)
|
|
|
|
|
foundNewobjProxy = true;
|
2012-08-04 15:26:13 +08:00
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
simpleDeobfuscator.Deobfuscate(method);
|
2012-11-19 06:42:43 +08:00
|
|
|
|
MethodDef nativeMethod = null;
|
2012-08-01 01:12:35 +08:00
|
|
|
|
uint magic;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (FindMagic_v14_r58564(method, out magic)) {
|
|
|
|
|
if (!DotNetUtils.CallsMethod(method, "System.Byte[] System.Convert::FromBase64String(System.String)")) {
|
|
|
|
|
if (!IsMethodCreator_v14_r58802(method, proxyType))
|
2012-08-11 04:10:44 +08:00
|
|
|
|
theVersion = ConfuserVersion.v14_r58564;
|
|
|
|
|
else
|
|
|
|
|
theVersion = ConfuserVersion.v14_r58802;
|
|
|
|
|
}
|
2013-01-19 20:09:49 +08:00
|
|
|
|
else if (DotNetUtils.CallsMethod(method, "System.Reflection.Module System.Reflection.MemberInfo::get_Module()"))
|
2012-08-11 05:00:37 +08:00
|
|
|
|
theVersion = ConfuserVersion.v17_r73479;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
else if (proxyType != ProxyCreatorType.CallOrCallvirt || !HasFieldReference(method, "System.Reflection.Emit.OpCode System.Reflection.Emit.OpCodes::Castclass"))
|
2012-08-01 20:24:17 +08:00
|
|
|
|
theVersion = ConfuserVersion.v14_r58857;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
else if (proxyType == ProxyCreatorType.CallOrCallvirt && DotNetUtils.CallsMethod(method, "System.Void System.Reflection.Emit.DynamicMethod::.ctor(System.String,System.Type,System.Type[],System.Boolean)"))
|
2012-08-11 04:25:33 +08:00
|
|
|
|
theVersion = ConfuserVersion.v16_r66631;
|
2012-08-11 04:50:03 +08:00
|
|
|
|
else if (proxyType == ProxyCreatorType.CallOrCallvirt)
|
|
|
|
|
theVersion = ConfuserVersion.v16_r70489;
|
2012-08-01 20:24:17 +08:00
|
|
|
|
}
|
2013-01-19 20:09:49 +08:00
|
|
|
|
else if (!DotNetUtils.CallsMethod(method, "System.Byte[] System.Convert::FromBase64String(System.String)") &&
|
|
|
|
|
DotNetUtils.CallsMethod(method, "System.Reflection.MethodBase System.Reflection.Module::ResolveMethod(System.Int32)")) {
|
|
|
|
|
if (proxyType == ProxyCreatorType.CallOrCallvirt && !FindCallvirtChar(method, out callvirtChar))
|
2012-08-08 23:38:38 +08:00
|
|
|
|
continue;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if ((nativeMethod = FindNativeMethod_v18_r75367(method)) != null)
|
2012-08-11 05:18:44 +08:00
|
|
|
|
theVersion = proxyType != ProxyCreatorType.CallOrCallvirt || callvirtChar == 9 ? ConfuserVersion.v18_r75367_native : ConfuserVersion.v18_r75369_native;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
else if (FindMagic_v18_r75367(method, out magic))
|
2012-08-11 05:18:44 +08:00
|
|
|
|
theVersion = proxyType != ProxyCreatorType.CallOrCallvirt || callvirtChar == 9 ? ConfuserVersion.v18_r75367_normal : ConfuserVersion.v18_r75369_normal;
|
2013-09-22 22:07:15 +08:00
|
|
|
|
else if (FindMagic_v19_r76101(method, out magic))
|
|
|
|
|
CommonCheckVersion19(method, true, tmpVer, ref theVersion);
|
|
|
|
|
else if ((nativeMethod = FindNativeMethod_v19_r76101(method)) != null)
|
|
|
|
|
CommonCheckVersion19(method, false, tmpVer, ref theVersion);
|
2012-08-11 03:12:07 +08:00
|
|
|
|
else {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (proxyType == ProxyCreatorType.CallOrCallvirt && !DotNetUtils.CallsMethod(method, "System.Int32 System.String::get_Length()"))
|
2012-08-11 03:12:07 +08:00
|
|
|
|
theVersion = ConfuserVersion.v11_r50378;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
int numCalls = ConfuserUtils.CountCalls(method, "System.Byte[] System.Text.Encoding::GetBytes(System.Char[],System.Int32,System.Int32)");
|
2012-08-11 03:12:07 +08:00
|
|
|
|
if (numCalls == 2)
|
|
|
|
|
theVersion = ConfuserVersion.v12_r54564;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!DotNetUtils.CallsMethod(method, "System.Reflection.Assembly System.Reflection.Assembly::Load(System.Reflection.AssemblyName)"))
|
2012-08-11 03:28:08 +08:00
|
|
|
|
theVersion = ConfuserVersion.v13_r55346;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (DotNetUtils.CallsMethod(method, "System.Void System.Runtime.CompilerServices.RuntimeHelpers::RunClassConstructor(System.RuntimeTypeHandle)"))
|
2012-08-11 03:36:15 +08:00
|
|
|
|
theVersion = ConfuserVersion.v13_r55604;
|
2012-08-11 03:12:07 +08:00
|
|
|
|
}
|
2012-08-08 23:38:38 +08:00
|
|
|
|
}
|
2013-01-19 20:09:49 +08:00
|
|
|
|
else if (Is_v17_r73740(method)) {
|
|
|
|
|
if (DotNetUtils.CallsMethod(method, "System.Boolean System.Type::get_IsArray()")) {
|
|
|
|
|
if ((nativeMethod = FindNativeMethod_v17_r73740(method)) != null)
|
2012-08-05 18:46:54 +08:00
|
|
|
|
theVersion = ConfuserVersion.v17_r74708_native;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
else if (FindMagic_v17_r73740(method, out magic))
|
2012-08-05 18:46:54 +08:00
|
|
|
|
theVersion = ConfuserVersion.v17_r74708_normal;
|
|
|
|
|
else
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if ((nativeMethod = FindNativeMethod_v17_r73740(method)) != null)
|
2012-08-05 18:46:54 +08:00
|
|
|
|
theVersion = ConfuserVersion.v17_r73740_native;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
else if (FindMagic_v17_r73740(method, out magic))
|
2012-08-05 18:46:54 +08:00
|
|
|
|
theVersion = ConfuserVersion.v17_r73740_normal;
|
|
|
|
|
else
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2012-08-04 15:26:13 +08:00
|
|
|
|
}
|
2012-08-11 02:14:47 +08:00
|
|
|
|
else if (theVersion == ConfuserVersion.v10_r42915) {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (DeobUtils.HasInteger(method, 0x06000000))
|
2012-08-11 02:14:47 +08:00
|
|
|
|
theVersion = ConfuserVersion.v10_r42919;
|
|
|
|
|
}
|
2012-07-31 00:00:00 +08:00
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
SetDelegateCreatorMethod(method);
|
|
|
|
|
methodToInfo.Add(method, new ProxyCreatorInfo(method, proxyType, theVersion, magic, nativeMethod, callvirtChar));
|
2012-08-11 03:14:11 +08:00
|
|
|
|
version = (ConfuserVersion)Math.Max((int)version, (int)theVersion);
|
2012-07-30 15:16:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-22 22:07:15 +08:00
|
|
|
|
static bool CommonCheckVersion19(MethodDef method, bool isNormal, int tmpProxyVer, ref ConfuserVersion theVersion) {
|
|
|
|
|
if (tmpProxyVer == 1) {
|
|
|
|
|
theVersion = isNormal ? ConfuserVersion.v19_r76101_normal : ConfuserVersion.v19_r76101_native;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (tmpProxyVer == 2) {
|
|
|
|
|
if (!CheckCtorProxyType_v19_r78963(method))
|
|
|
|
|
theVersion = isNormal ? ConfuserVersion.v19_r78363_normal : ConfuserVersion.v19_r78363_native;
|
|
|
|
|
else
|
|
|
|
|
theVersion = isNormal ? ConfuserVersion.v19_r78963_normal_Newobj : ConfuserVersion.v19_r78963_native_Newobj;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static bool HasFieldReference(MethodDef method, string fieldFullName) {
|
2012-08-11 04:25:33 +08:00
|
|
|
|
foreach (var instr in method.Body.Instructions) {
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var field = instr.Operand as IField;
|
2012-08-11 04:25:33 +08:00
|
|
|
|
if (field == null)
|
|
|
|
|
continue;
|
|
|
|
|
if (field.FullName == fieldFullName)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static bool IsMethodCreator_v14_r58802(MethodDef method, ProxyCreatorType proxyType) {
|
|
|
|
|
int index = GetFieldNameIndex(method);
|
2012-08-11 04:10:44 +08:00
|
|
|
|
if (index < 0)
|
|
|
|
|
throw new ApplicationException("Could not find field name index");
|
|
|
|
|
switch (proxyType) {
|
|
|
|
|
case ProxyCreatorType.Newobj:
|
|
|
|
|
if (index == 1)
|
|
|
|
|
return false;
|
|
|
|
|
if (index == 0)
|
|
|
|
|
return true;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ProxyCreatorType.CallOrCallvirt:
|
|
|
|
|
if (index == 2)
|
|
|
|
|
return false;
|
|
|
|
|
if (index == 1)
|
|
|
|
|
return true;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default: throw new ApplicationException("Invalid proxy creator type");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new ApplicationException("Could not find field name index");
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static int GetFieldNameIndex(MethodDef method) {
|
2012-08-11 04:10:44 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count; i++) {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
i = ConfuserUtils.FindCallMethod(instrs, i, Code.Callvirt, "System.Byte[] System.Text.Encoding::GetBytes(System.Char[],System.Int32,System.Int32)");
|
2012-08-11 04:10:44 +08:00
|
|
|
|
if (i < 0)
|
|
|
|
|
break;
|
|
|
|
|
if (i < 2)
|
|
|
|
|
continue;
|
|
|
|
|
var ldci4 = instrs[i - 2];
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!ldci4.IsLdcI4())
|
2012-08-11 04:10:44 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
return ldci4.GetLdcI4Value();
|
2012-08-11 04:10:44 +08:00
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static bool FindMagic_v19_r76101(MethodDef method, out uint magic) {
|
2012-08-09 03:22:00 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count - 7; i++) {
|
|
|
|
|
var ldci4_1 = instrs[i];
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!ldci4_1.IsLdcI4() || ldci4_1.GetLdcI4Value() != 24)
|
2012-08-09 03:22:00 +08:00
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 1].OpCode.Code != Code.Shl)
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 2].OpCode.Code != Code.Or)
|
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[i + 3].IsStloc())
|
2012-08-09 03:22:00 +08:00
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[i + 4].IsLdloc())
|
2012-08-09 03:22:00 +08:00
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[i + 5].IsLdloc())
|
2012-08-09 03:22:00 +08:00
|
|
|
|
continue;
|
|
|
|
|
var ldci4_2 = instrs[i + 6];
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!ldci4_2.IsLdcI4())
|
2012-08-09 03:22:00 +08:00
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 7].OpCode.Code != Code.Xor)
|
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
magic = (uint)ldci4_2.GetLdcI4Value();
|
2012-08-09 03:22:00 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
magic = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static MethodDef FindNativeMethod_v19_r76101(MethodDef method) {
|
2012-08-09 03:22:00 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count - 6; i++) {
|
|
|
|
|
var ldci4 = instrs[i];
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!ldci4.IsLdcI4() || ldci4.GetLdcI4Value() != 24)
|
2012-08-09 03:22:00 +08:00
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 1].OpCode.Code != Code.Shl)
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 2].OpCode.Code != Code.Or)
|
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[i + 3].IsStloc())
|
2012-08-09 03:22:00 +08:00
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[i + 4].IsLdloc())
|
2012-08-09 03:22:00 +08:00
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[i + 5].IsLdloc())
|
2012-08-09 03:22:00 +08:00
|
|
|
|
continue;
|
|
|
|
|
var call = instrs[i + 6];
|
|
|
|
|
if (call.OpCode.Code != Code.Call)
|
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var calledMethod = call.Operand as MethodDef;
|
2012-08-09 03:22:00 +08:00
|
|
|
|
if (calledMethod == null || calledMethod.Body != null || !calledMethod.IsNative)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return calledMethod;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static bool FindMagic_v18_r75367(MethodDef method, out uint magic) {
|
2012-08-08 23:38:38 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count; i++) {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
i = ConfuserUtils.FindCallMethod(instrs, i, Code.Callvirt, "System.Reflection.Module System.Reflection.MemberInfo::get_Module()");
|
2012-08-08 23:38:38 +08:00
|
|
|
|
if (i < 0 || i + 3 >= instrs.Count)
|
|
|
|
|
break;
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[i + 1].IsLdloc())
|
2012-08-08 23:38:38 +08:00
|
|
|
|
continue;
|
|
|
|
|
var ldci4 = instrs[i + 2];
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!ldci4.IsLdcI4())
|
2012-08-08 23:38:38 +08:00
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i+3].OpCode.Code != Code.Xor)
|
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
magic = (uint)ldci4.GetLdcI4Value();
|
2012-08-08 23:38:38 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
magic = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static MethodDef FindNativeMethod_v18_r75367(MethodDef method) {
|
2012-08-08 23:38:38 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count; i++) {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
i = ConfuserUtils.FindCallMethod(instrs, i, Code.Callvirt, "System.Reflection.Module System.Reflection.MemberInfo::get_Module()");
|
2012-08-08 23:38:38 +08:00
|
|
|
|
if (i < 0 || i + 2 >= instrs.Count)
|
|
|
|
|
break;
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[i + 1].IsLdloc())
|
2012-08-08 23:38:38 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var call = instrs[i + 2];
|
|
|
|
|
if (call.OpCode.Code != Code.Call)
|
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var calledMethod = call.Operand as MethodDef;
|
2012-08-08 23:38:38 +08:00
|
|
|
|
if (calledMethod == null || calledMethod.Body != null || !calledMethod.IsNative)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return calledMethod;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static bool FindMagic_v17_r73740(MethodDef method, out uint magic) {
|
2012-08-04 15:26:13 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count; i++) {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
int index = ConfuserUtils.FindCallMethod(instrs, i, Code.Call, "System.Int32 System.BitConverter::ToInt32(System.Byte[],System.Int32)");
|
2012-08-04 15:26:13 +08:00
|
|
|
|
if (index < 0)
|
|
|
|
|
break;
|
|
|
|
|
if (index < 1 || index + 2 >= instrs.Count)
|
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[index - 1].IsLdcI4())
|
2012-08-04 15:26:13 +08:00
|
|
|
|
continue;
|
|
|
|
|
var ldci4 = instrs[index + 1];
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!ldci4.IsLdcI4())
|
2012-08-04 15:26:13 +08:00
|
|
|
|
continue;
|
|
|
|
|
if (instrs[index + 2].OpCode.Code != Code.Xor)
|
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
magic = (uint)ldci4.GetLdcI4Value();
|
2012-08-04 15:26:13 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
magic = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static MethodDef FindNativeMethod_v17_r73740(MethodDef method) {
|
2012-08-04 15:26:13 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count; i++) {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
int index = ConfuserUtils.FindCallMethod(instrs, i, Code.Call, "System.Int32 System.BitConverter::ToInt32(System.Byte[],System.Int32)");
|
2012-08-04 15:26:13 +08:00
|
|
|
|
if (index < 0)
|
|
|
|
|
break;
|
|
|
|
|
if (index < 1 || index + 1 >= instrs.Count)
|
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[index - 1].IsLdcI4())
|
2012-08-04 15:26:13 +08:00
|
|
|
|
continue;
|
|
|
|
|
var call = instrs[index + 1];
|
|
|
|
|
if (call.OpCode.Code != Code.Call)
|
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var calledMethod = call.Operand as MethodDef;
|
2012-08-04 15:26:13 +08:00
|
|
|
|
if (calledMethod == null || calledMethod.Body != null || !calledMethod.IsNative)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return calledMethod;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static bool Is_v17_r73740(MethodDef method) {
|
2012-08-04 15:26:13 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count; i++) {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
int index = ConfuserUtils.FindCallMethod(instrs, i, Code.Callvirt, "System.Reflection.MethodBase System.Reflection.Module::ResolveMethod(System.Int32)");
|
2012-08-04 15:26:13 +08:00
|
|
|
|
if (index < 0)
|
|
|
|
|
break;
|
|
|
|
|
if (index < 3)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
index -= 3;
|
|
|
|
|
var ldci4 = instrs[index];
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!ldci4.IsLdcI4() || ldci4.GetLdcI4Value() != 24)
|
2012-08-04 15:26:13 +08:00
|
|
|
|
continue;
|
|
|
|
|
if (instrs[index + 1].OpCode.Code != Code.Shl)
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[index + 2].OpCode.Code != Code.Or)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static bool FindMagic_v14_r58564(MethodDef method, out uint magic) {
|
2012-08-01 01:12:35 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count; i++) {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
int index = ConfuserUtils.FindCallMethod(instrs, i, Code.Call, "System.Int32 System.BitConverter::ToInt32(System.Byte[],System.Int32)");
|
2012-08-01 01:12:35 +08:00
|
|
|
|
if (index < 0)
|
|
|
|
|
break;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
int index2 = ConfuserUtils.FindCallMethod(instrs, i, Code.Callvirt, "System.Reflection.MethodBase System.Reflection.Module::ResolveMethod(System.Int32)");
|
2012-08-01 01:12:35 +08:00
|
|
|
|
if (index2 < 0 || index2 - index != 3)
|
|
|
|
|
continue;
|
|
|
|
|
var ldci4 = instrs[index + 1];
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!ldci4.IsLdcI4())
|
2012-08-01 01:12:35 +08:00
|
|
|
|
continue;
|
|
|
|
|
if (instrs[index + 2].OpCode.Code != Code.Xor)
|
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
magic = (uint)ldci4.GetLdcI4Value();
|
2012-08-01 01:12:35 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
magic = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-03 00:03:56 +08:00
|
|
|
|
static ProxyCreatorType GetProxyCreatorType(MethodDef method, ISimpleDeobfuscator simpleDeobfuscator, out int version) {
|
|
|
|
|
var type = GetProxyCreatorTypeV1(method);
|
|
|
|
|
if (type != ProxyCreatorType.None) {
|
|
|
|
|
version = 1;
|
|
|
|
|
return type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
simpleDeobfuscator.Deobfuscate(method);
|
|
|
|
|
|
|
|
|
|
type = GetProxyCreatorTypeV2(method);
|
|
|
|
|
if (type != ProxyCreatorType.None) {
|
|
|
|
|
version = 2;
|
|
|
|
|
return type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
version = 0;
|
|
|
|
|
return ProxyCreatorType.None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <= 1.9 r78342 (refs to System.Reflection.Emit.OpCodes)
|
|
|
|
|
static ProxyCreatorType GetProxyCreatorTypeV1(MethodDef method) {
|
2012-07-30 15:16:52 +08:00
|
|
|
|
foreach (var instr in method.Body.Instructions) {
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var field = instr.Operand as IField;
|
2012-07-30 15:16:52 +08:00
|
|
|
|
if (field == null)
|
|
|
|
|
continue;
|
|
|
|
|
switch (field.FullName) {
|
|
|
|
|
case "System.Reflection.Emit.OpCode System.Reflection.Emit.OpCodes::Call":
|
|
|
|
|
case "System.Reflection.Emit.OpCode System.Reflection.Emit.OpCodes::Callvirt":
|
|
|
|
|
return ProxyCreatorType.CallOrCallvirt;
|
|
|
|
|
|
|
|
|
|
case "System.Reflection.Emit.OpCode System.Reflection.Emit.OpCodes::Newobj":
|
|
|
|
|
return ProxyCreatorType.Newobj;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ProxyCreatorType.None;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-03 00:03:56 +08:00
|
|
|
|
// >= 1.9 r78363 (no refs to System.Reflection.Emit.OpCodes)
|
|
|
|
|
static ProxyCreatorType GetProxyCreatorTypeV2(MethodDef method) {
|
|
|
|
|
if (!DeobUtils.HasInteger(method, 0x2A))
|
|
|
|
|
return ProxyCreatorType.None;
|
|
|
|
|
if (CheckCtorProxyTypeV2(method))
|
|
|
|
|
return ProxyCreatorType.Newobj;
|
|
|
|
|
if (CheckCallProxyTypeV2(method))
|
|
|
|
|
return ProxyCreatorType.CallOrCallvirt;
|
|
|
|
|
return ProxyCreatorType.None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool CheckCtorProxyTypeV2(MethodDef method) {
|
|
|
|
|
var instrs = method.Body.Instructions;
|
2013-09-22 22:07:15 +08:00
|
|
|
|
for (int i = 0; i < instrs.Count - 3; i++) {
|
2013-02-03 00:03:56 +08:00
|
|
|
|
var ldci4 = instrs[i];
|
|
|
|
|
if (!ldci4.IsLdcI4() || ldci4.GetLdcI4Value() != 2)
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 1].OpCode.Code != Code.Mul)
|
|
|
|
|
continue;
|
|
|
|
|
ldci4 = instrs[i + 2];
|
|
|
|
|
if (!ldci4.IsLdcI4() || ldci4.GetLdcI4Value() != 0x73)
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 3].OpCode.Code != Code.Stelem_I1)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool CheckCallProxyTypeV2(MethodDef method) {
|
|
|
|
|
return DeobUtils.HasInteger(method, 0x28) &&
|
|
|
|
|
DeobUtils.HasInteger(method, 0x6F);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-22 22:07:15 +08:00
|
|
|
|
// r78963 adds a 'castclass' opcode to the generated code. This code assumes
|
|
|
|
|
// CheckCtorProxyTypeV2() has returned true.
|
|
|
|
|
static bool CheckCtorProxyType_v19_r78963(MethodDef method) {
|
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count - 2; i++) {
|
|
|
|
|
if (instrs[i].OpCode.Code != Code.Add)
|
|
|
|
|
continue;
|
|
|
|
|
var ldci4 = instrs[i + 1];
|
|
|
|
|
if (!ldci4.IsLdcI4() || ldci4.GetLdcI4Value() != 0x74)
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 2].OpCode.Code != Code.Stelem_I1)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
public new void Find() {
|
2012-07-30 15:16:52 +08:00
|
|
|
|
if (delegateCreatorMethods.Count == 0)
|
|
|
|
|
return;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
var cctor = DotNetUtils.GetModuleTypeCctor(module);
|
2012-07-30 15:16:52 +08:00
|
|
|
|
if (cctor == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
Logger.v("Finding all proxy delegates");
|
2012-07-30 15:16:52 +08:00
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
var delegateInfos = CreateDelegateInitInfos(cctor);
|
|
|
|
|
fieldToMethods = CreateFieldToMethodsDictionary(cctor.DeclaringType);
|
2012-07-31 10:44:30 +08:00
|
|
|
|
if (delegateInfos.Count < fieldToMethods.Count)
|
2012-07-30 15:16:52 +08:00
|
|
|
|
throw new ApplicationException("Missing proxy delegates");
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var delegateToFields = new Dictionary<TypeDef, List<FieldDef>>();
|
2013-01-19 20:09:49 +08:00
|
|
|
|
foreach (var field in fieldToMethods.GetKeys()) {
|
2012-11-19 06:42:43 +08:00
|
|
|
|
List<FieldDef> list;
|
|
|
|
|
if (!delegateToFields.TryGetValue(field.FieldType.TryGetTypeDef(), out list))
|
|
|
|
|
delegateToFields[field.FieldType.TryGetTypeDef()] = list = new List<FieldDef>();
|
2012-07-30 15:16:52 +08:00
|
|
|
|
list.Add(field);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var kv in delegateToFields) {
|
|
|
|
|
var type = kv.Key;
|
|
|
|
|
var fields = kv.Value;
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
Logger.v("Found proxy delegate: {0} ({1:X8})", Utils.RemoveNewlines(type), type.MDToken.ToInt32());
|
2012-07-30 15:16:52 +08:00
|
|
|
|
RemovedDelegateCreatorCalls++;
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
Logger.Instance.Indent();
|
2012-07-30 15:16:52 +08:00
|
|
|
|
foreach (var field in fields) {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
var proxyMethods = fieldToMethods.Find(field);
|
2012-07-31 10:44:30 +08:00
|
|
|
|
if (proxyMethods == null)
|
2012-07-30 15:16:52 +08:00
|
|
|
|
continue;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
var info = delegateInfos.Find(field);
|
2012-07-30 15:16:52 +08:00
|
|
|
|
if (info == null)
|
|
|
|
|
throw new ApplicationException("Missing proxy info");
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
IMethod calledMethod;
|
2012-07-30 15:16:52 +08:00
|
|
|
|
OpCode callOpcode;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
GetCallInfo(info, field, out calledMethod, out callOpcode);
|
2012-07-30 15:16:52 +08:00
|
|
|
|
|
|
|
|
|
if (calledMethod == null)
|
|
|
|
|
continue;
|
2012-07-31 10:44:30 +08:00
|
|
|
|
foreach (var proxyMethod in proxyMethods) {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
Add(proxyMethod, new DelegateInfo(field, calledMethod, callOpcode));
|
2012-11-19 06:42:43 +08:00
|
|
|
|
Logger.v("Field: {0}, Opcode: {1}, Method: {2} ({3:X8})",
|
2013-01-19 20:09:49 +08:00
|
|
|
|
Utils.RemoveNewlines(field.Name),
|
2012-07-31 10:44:30 +08:00
|
|
|
|
callOpcode,
|
2013-01-19 20:09:49 +08:00
|
|
|
|
Utils.RemoveNewlines(calledMethod),
|
2012-11-19 06:42:43 +08:00
|
|
|
|
calledMethod.MDToken.ToUInt32());
|
2012-07-31 10:44:30 +08:00
|
|
|
|
}
|
2012-07-30 15:16:52 +08:00
|
|
|
|
}
|
2013-01-19 20:09:49 +08:00
|
|
|
|
Logger.Instance.DeIndent();
|
2012-07-30 15:16:52 +08:00
|
|
|
|
delegateTypesDict[type] = true;
|
|
|
|
|
}
|
2012-07-31 13:15:38 +08:00
|
|
|
|
|
|
|
|
|
// 1.2 r54564 (almost 1.3) now moves method proxy init code to the delegate cctors
|
2013-01-19 20:09:49 +08:00
|
|
|
|
Find2();
|
2012-07-30 15:16:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
FieldDefAndDeclaringTypeDict<DelegateInitInfo> CreateDelegateInitInfos(MethodDef method) {
|
2012-07-31 00:00:00 +08:00
|
|
|
|
switch (version) {
|
2012-08-04 15:26:13 +08:00
|
|
|
|
case ConfuserVersion.v10_r42915:
|
2012-08-11 02:14:47 +08:00
|
|
|
|
case ConfuserVersion.v10_r42919:
|
2013-01-19 20:09:49 +08:00
|
|
|
|
return CreateDelegateInitInfos_v10_r42915(method);
|
2012-08-04 15:26:13 +08:00
|
|
|
|
default:
|
2013-01-19 20:09:49 +08:00
|
|
|
|
return CreateDelegateInitInfos_v10_r48717(method);
|
2012-07-31 00:00:00 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
FieldDefAndDeclaringTypeDict<DelegateInitInfo> CreateDelegateInitInfos_v10_r42915(MethodDef method) {
|
2012-12-01 04:05:54 +08:00
|
|
|
|
var infos = new FieldDefAndDeclaringTypeDict<DelegateInitInfo>();
|
2012-07-30 15:16:52 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count - 2; i++) {
|
|
|
|
|
var ldstr = instrs[i];
|
|
|
|
|
if (ldstr.OpCode.Code != Code.Ldstr)
|
|
|
|
|
continue;
|
|
|
|
|
var info = ldstr.Operand as string;
|
|
|
|
|
if (info == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var ldtoken = instrs[i + 1];
|
|
|
|
|
if (ldtoken.OpCode.Code != Code.Ldtoken)
|
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var delegateField = ldtoken.Operand as FieldDef;
|
2012-07-30 15:16:52 +08:00
|
|
|
|
if (delegateField == null)
|
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var delegateType = delegateField.FieldType.TryGetTypeDef();
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!DotNetUtils.DerivesFromDelegate(delegateType))
|
2012-07-30 15:16:52 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var call = instrs[i + 2];
|
|
|
|
|
if (call.OpCode.Code != Code.Call)
|
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var delegateCreatorMethod = call.Operand as MethodDef;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (delegateCreatorMethod == null || !IsDelegateCreatorMethod(delegateCreatorMethod))
|
2012-07-30 15:16:52 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
infos.Add(delegateField, new DelegateInitInfo(info, delegateField, delegateCreatorMethod));
|
2012-07-30 15:16:52 +08:00
|
|
|
|
i += 2;
|
|
|
|
|
}
|
|
|
|
|
return infos;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
FieldDefAndDeclaringTypeDict<DelegateInitInfo> CreateDelegateInitInfos_v10_r48717(MethodDef method) {
|
2012-12-01 04:05:54 +08:00
|
|
|
|
var infos = new FieldDefAndDeclaringTypeDict<DelegateInitInfo>();
|
2012-07-31 00:00:00 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count - 1; i++) {
|
|
|
|
|
var ldtoken = instrs[i];
|
|
|
|
|
if (ldtoken.OpCode.Code != Code.Ldtoken)
|
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var delegateField = ldtoken.Operand as FieldDef;
|
2012-07-31 00:00:00 +08:00
|
|
|
|
if (delegateField == null)
|
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var delegateType = delegateField.FieldType.TryGetTypeDef();
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!DotNetUtils.DerivesFromDelegate(delegateType))
|
2012-07-31 00:00:00 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var call = instrs[i + 1];
|
|
|
|
|
if (call.OpCode.Code != Code.Call)
|
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var delegateCreatorMethod = call.Operand as MethodDef;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (delegateCreatorMethod == null || !IsDelegateCreatorMethod(delegateCreatorMethod))
|
2012-07-31 00:00:00 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
infos.Add(delegateField, new DelegateInitInfo(delegateField, delegateCreatorMethod));
|
2012-07-31 00:00:00 +08:00
|
|
|
|
i += 1;
|
|
|
|
|
}
|
|
|
|
|
return infos;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static FieldDefAndDeclaringTypeDict<List<MethodDef>> CreateFieldToMethodsDictionary(TypeDef type) {
|
2012-12-01 04:05:54 +08:00
|
|
|
|
var dict = new FieldDefAndDeclaringTypeDict<List<MethodDef>>();
|
2012-07-30 15:16:52 +08:00
|
|
|
|
foreach (var method in type.Methods) {
|
|
|
|
|
if (!method.IsStatic || method.Body == null || method.Name == ".cctor")
|
|
|
|
|
continue;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
var delegateField = GetDelegateField(method);
|
2012-07-30 15:16:52 +08:00
|
|
|
|
if (delegateField == null)
|
|
|
|
|
continue;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
var methods = dict.Find(delegateField);
|
2012-07-31 10:44:30 +08:00
|
|
|
|
if (methods == null)
|
2013-01-19 20:09:49 +08:00
|
|
|
|
dict.Add(delegateField, methods = new List<MethodDef>());
|
2012-07-31 10:44:30 +08:00
|
|
|
|
methods.Add(method);
|
2012-07-30 15:16:52 +08:00
|
|
|
|
}
|
|
|
|
|
return dict;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static FieldDef GetDelegateField(MethodDef method) {
|
2012-07-30 15:16:52 +08:00
|
|
|
|
if (method == null || method.Body == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
FieldDef field = null;
|
2012-07-30 15:16:52 +08:00
|
|
|
|
bool foundInvoke = false;
|
|
|
|
|
foreach (var instr in method.Body.Instructions) {
|
|
|
|
|
if (instr.OpCode.Code == Code.Ldsfld) {
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var field2 = instr.Operand as FieldDef;
|
2012-07-30 15:16:52 +08:00
|
|
|
|
if (field2 == null || field2.DeclaringType != method.DeclaringType)
|
|
|
|
|
continue;
|
|
|
|
|
if (field != null)
|
|
|
|
|
return null;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!DotNetUtils.DerivesFromDelegate(field2.FieldType.TryGetTypeDef()))
|
2012-07-30 15:16:52 +08:00
|
|
|
|
continue;
|
|
|
|
|
field = field2;
|
|
|
|
|
}
|
|
|
|
|
else if (instr.OpCode.Code == Code.Call || instr.OpCode.Code == Code.Callvirt) {
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var calledMethod = instr.Operand as IMethod;
|
2012-07-30 15:16:52 +08:00
|
|
|
|
foundInvoke |= calledMethod != null && calledMethod.Name == "Invoke";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return foundInvoke ? field : null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static bool FindCallvirtChar(MethodDef method, out ushort callvirtChar) {
|
2012-08-08 23:38:38 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int index = 0; index < instrs.Count; index++) {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
index = ConfuserUtils.FindCallMethod(instrs, index, Code.Callvirt, "System.Char System.String::get_Chars(System.Int32)");
|
2012-08-08 23:38:38 +08:00
|
|
|
|
if (index < 0)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
index++;
|
|
|
|
|
if (index >= instrs.Count)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
var ldci4 = instrs[index];
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!ldci4.IsLdcI4())
|
2012-08-08 23:38:38 +08:00
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
callvirtChar = (ushort)ldci4.GetLdcI4Value();
|
2012-08-08 23:38:38 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
callvirtChar = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
public void CleanUp() {
|
2012-07-30 15:16:52 +08:00
|
|
|
|
if (!Detected)
|
|
|
|
|
return;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
var cctor = DotNetUtils.GetModuleTypeCctor(module);
|
2012-07-30 15:16:52 +08:00
|
|
|
|
if (cctor == null)
|
|
|
|
|
return;
|
|
|
|
|
cctor.Body.Instructions.Clear();
|
|
|
|
|
cctor.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
|
|
|
|
|
}
|
2012-08-09 17:47:18 +08:00
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
public bool GetRevisionRange(out int minRev, out int maxRev) {
|
2012-08-09 17:47:18 +08:00
|
|
|
|
switch (version) {
|
|
|
|
|
case ConfuserVersion.Unknown:
|
|
|
|
|
minRev = maxRev = 0;
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v10_r42915:
|
|
|
|
|
minRev = 42915;
|
2012-08-11 02:14:47 +08:00
|
|
|
|
maxRev = 42917;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v10_r42919:
|
|
|
|
|
minRev = 42919;
|
2012-08-09 17:47:18 +08:00
|
|
|
|
maxRev = 48509;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v10_r48717:
|
|
|
|
|
minRev = 48717;
|
2012-08-11 04:25:33 +08:00
|
|
|
|
maxRev = 54431;
|
2012-08-11 02:43:11 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v11_r50378:
|
|
|
|
|
minRev = 50378;
|
2012-08-11 03:12:07 +08:00
|
|
|
|
maxRev = 54431;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v12_r54564:
|
|
|
|
|
minRev = 54564;
|
2012-08-11 03:28:08 +08:00
|
|
|
|
maxRev = 54574;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v13_r55346:
|
|
|
|
|
minRev = 55346;
|
2012-08-11 03:36:15 +08:00
|
|
|
|
maxRev = 55346;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v13_r55604:
|
|
|
|
|
minRev = 55604;
|
2012-08-09 17:47:18 +08:00
|
|
|
|
maxRev = 58446;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v14_r58564:
|
|
|
|
|
minRev = 58564;
|
2012-08-11 04:10:44 +08:00
|
|
|
|
maxRev = 58741;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v14_r58802:
|
|
|
|
|
minRev = 58802;
|
2012-08-09 17:47:18 +08:00
|
|
|
|
maxRev = 58852;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v14_r58857:
|
|
|
|
|
minRev = 58857;
|
2012-08-11 05:00:37 +08:00
|
|
|
|
maxRev = 73477;
|
2012-08-09 17:47:18 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
2012-08-11 04:25:33 +08:00
|
|
|
|
case ConfuserVersion.v16_r66631:
|
|
|
|
|
minRev = 66631;
|
2012-08-11 04:50:03 +08:00
|
|
|
|
maxRev = 69666;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v16_r70489:
|
|
|
|
|
minRev = 70489;
|
2012-08-11 05:00:37 +08:00
|
|
|
|
maxRev = 73477;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v17_r73479:
|
|
|
|
|
minRev = 73479;
|
2012-08-11 04:25:33 +08:00
|
|
|
|
maxRev = 73605;
|
|
|
|
|
return true;
|
|
|
|
|
|
2012-08-09 17:47:18 +08:00
|
|
|
|
case ConfuserVersion.v17_r73740_normal:
|
|
|
|
|
case ConfuserVersion.v17_r73740_native:
|
|
|
|
|
minRev = 73740;
|
|
|
|
|
maxRev = 74637;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v17_r74708_normal:
|
|
|
|
|
case ConfuserVersion.v17_r74708_native:
|
|
|
|
|
minRev = 74708;
|
|
|
|
|
maxRev = 75349;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v18_r75367_normal:
|
|
|
|
|
case ConfuserVersion.v18_r75367_native:
|
|
|
|
|
minRev = 75367;
|
|
|
|
|
maxRev = 75926;
|
|
|
|
|
return true;
|
|
|
|
|
|
2012-08-11 05:18:44 +08:00
|
|
|
|
case ConfuserVersion.v18_r75369_normal:
|
|
|
|
|
case ConfuserVersion.v18_r75369_native:
|
|
|
|
|
minRev = 75369;
|
|
|
|
|
maxRev = 75926;
|
|
|
|
|
return true;
|
|
|
|
|
|
2012-08-09 17:47:18 +08:00
|
|
|
|
case ConfuserVersion.v19_r76101_normal:
|
|
|
|
|
case ConfuserVersion.v19_r76101_native:
|
|
|
|
|
minRev = 76101;
|
2013-02-03 00:03:56 +08:00
|
|
|
|
maxRev = 78342;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v19_r78363_normal:
|
|
|
|
|
case ConfuserVersion.v19_r78363_native:
|
|
|
|
|
minRev = 78363;
|
2013-09-22 22:07:15 +08:00
|
|
|
|
// We can only detect the r78963 version if a method ctor proxy is used.
|
|
|
|
|
// If it's not used, then maxRev must be the same maxRev as in the next case.
|
|
|
|
|
// If a method ctor proxy is found, then we know that rev <= 78962.
|
|
|
|
|
if (foundNewobjProxy)
|
|
|
|
|
maxRev = 78962;
|
|
|
|
|
else
|
|
|
|
|
maxRev = int.MaxValue;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v19_r78963_normal_Newobj:
|
|
|
|
|
case ConfuserVersion.v19_r78963_native_Newobj:
|
|
|
|
|
minRev = 78963;
|
2012-08-09 17:47:18 +08:00
|
|
|
|
maxRev = int.MaxValue;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
default: throw new ApplicationException("Invalid version");
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-12-01 04:05:54 +08:00
|
|
|
|
|
|
|
|
|
public void Dispose() {
|
|
|
|
|
if (x86emu != null)
|
|
|
|
|
x86emu.Dispose();
|
|
|
|
|
x86emu = null;
|
|
|
|
|
}
|
2012-07-30 15:16:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|