de4dot-cex/de4dot.code/deobfuscators/SmartAssembly/ProxyCallFixer.cs

119 lines
3.8 KiB
C#
Raw Normal View History

2011-09-22 10:55:30 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
2011-09-22 10:55:30 +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/>.
*/
2011-10-23 19:43:32 +08:00
using System;
2011-09-22 10:55:30 +08:00
using System.Collections.Generic;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
2011-09-24 16:26:29 +08:00
using de4dot.blocks;
2011-09-22 10:55:30 +08:00
namespace de4dot.code.deobfuscators.SmartAssembly {
2012-05-29 17:13:39 +08:00
class ProxyCallFixer : ProxyCallFixer1 {
2011-09-22 10:55:30 +08:00
static readonly Dictionary<char, int> specialCharsDict = new Dictionary<char, int>();
static readonly char[] specialChars = new char[] {
'\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', '\x08',
'\x0E', '\x0F', '\x10', '\x11', '\x12', '\x13', '\x14', '\x15',
'\x16', '\x17', '\x18', '\x19', '\x1A', '\x1B', '\x1C', '\x1D',
'\x1E', '\x1F', '\x7F', '\x80', '\x81', '\x82', '\x83', '\x84',
'\x86', '\x87', '\x88', '\x89', '\x8A', '\x8B', '\x8C', '\x8D',
'\x8E', '\x8F', '\x90', '\x91', '\x92', '\x93', '\x94', '\x95',
'\x96', '\x97', '\x98', '\x99', '\x9A', '\x9B', '\x9C', '\x9D',
'\x9E', '\x9F',
};
2011-11-13 04:04:24 +08:00
ISimpleDeobfuscator simpleDeobfuscator;
2011-10-23 19:43:32 +08:00
2012-05-29 17:13:39 +08:00
static ProxyCallFixer() {
2011-09-22 10:55:30 +08:00
for (int i = 0; i < specialChars.Length; i++)
specialCharsDict[specialChars[i]] = i;
}
2012-11-19 00:07:02 +08:00
public ProxyCallFixer(ModuleDefMD module, ISimpleDeobfuscator simpleDeobfuscator)
: base(module) {
2011-11-13 04:04:24 +08:00
this.simpleDeobfuscator = simpleDeobfuscator;
2011-10-23 19:43:32 +08:00
}
2013-01-19 20:03:57 +08:00
protected override object CheckCctor(ref TypeDef type, MethodDef cctor) {
2011-10-23 19:43:32 +08:00
var instrs = cctor.Body.Instructions;
2011-11-13 04:04:24 +08:00
if (instrs.Count > 10)
return null;
if (instrs.Count != 3)
2013-01-19 20:03:57 +08:00
simpleDeobfuscator.Deobfuscate(cctor);
2011-10-23 19:43:32 +08:00
if (instrs.Count != 3)
return null;
2012-11-19 00:07:02 +08:00
if (!instrs[0].IsLdcI4())
2011-10-23 19:43:32 +08:00
return null;
2013-01-19 20:03:57 +08:00
if (instrs[1].OpCode != OpCodes.Call || !IsDelegateCreatorMethod(instrs[1].Operand as MethodDef))
2011-10-23 19:43:32 +08:00
return null;
if (instrs[2].OpCode != OpCodes.Ret)
return null;
2012-11-19 00:07:02 +08:00
int delegateToken = 0x02000001 + instrs[0].GetLdcI4Value();
if (type.MDToken.ToInt32() != delegateToken) {
Logger.w("Delegate token is not current type");
2011-10-23 19:43:32 +08:00
return null;
}
return new object();
2011-09-22 10:55:30 +08:00
}
2013-01-19 20:03:57 +08:00
protected override void GetCallInfo(object context, FieldDef field, out IMethod calledMethod, out OpCode callOpcode) {
2011-10-23 19:43:32 +08:00
callOpcode = OpCodes.Call;
2012-11-19 00:07:02 +08:00
string name = field.Name.String;
2011-09-22 10:55:30 +08:00
2012-11-19 00:07:02 +08:00
uint memberRefRid = 0;
2011-09-22 10:55:30 +08:00
for (int i = name.Length - 1; i >= 0; i--) {
char c = name[i];
if (c == '~') {
2011-10-23 19:43:32 +08:00
callOpcode = OpCodes.Callvirt;
2011-09-22 10:55:30 +08:00
break;
}
int val;
if (specialCharsDict.TryGetValue(c, out val))
2012-11-19 00:07:02 +08:00
memberRefRid = memberRefRid * (uint)specialChars.Length + (uint)val;
2011-09-22 10:55:30 +08:00
}
2012-11-19 00:07:02 +08:00
memberRefRid++;
2012-04-11 08:54:07 +08:00
2012-11-19 00:07:02 +08:00
calledMethod = module.ResolveMemberRef(memberRefRid);
if (calledMethod == null)
Logger.w("Ignoring invalid method RID: {0:X8}, field: {1:X8}", memberRefRid, field.MDToken.ToInt32());
2011-09-22 10:55:30 +08:00
}
2013-01-19 20:03:57 +08:00
public void FindDelegateCreator(ModuleDefMD module) {
2011-09-22 10:55:30 +08:00
var callCounter = new CallCounter();
foreach (var type in module.Types) {
2013-01-19 20:03:57 +08:00
if (type.Namespace != "" || !DotNetUtils.DerivesFromDelegate(type))
2011-09-22 10:55:30 +08:00
continue;
2012-11-19 00:07:02 +08:00
var cctor = type.FindStaticConstructor();
2011-09-22 10:55:30 +08:00
if (cctor == null)
continue;
2013-01-19 20:03:57 +08:00
foreach (var method in DotNetUtils.GetMethodCalls(cctor))
callCounter.Add(method);
2011-09-22 10:55:30 +08:00
}
2013-01-19 20:03:57 +08:00
var mostCalls = callCounter.Most();
2011-09-22 10:55:30 +08:00
if (mostCalls == null)
return;
2013-01-19 20:03:57 +08:00
SetDelegateCreatorMethod(DotNetUtils.GetMethod(module, mostCalls));
2011-09-22 10:55:30 +08:00
}
}
}