2012-07-27 14:47:37 +08:00
|
|
|
|
/*
|
2015-10-30 05:45:26 +08:00
|
|
|
|
Copyright (C) 2011-2015 de4dot@gmail.com
|
2012-07-27 14:47:37 +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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2012-08-12 04:35:34 +08:00
|
|
|
|
using System;
|
2012-12-23 04:08:29 +08:00
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using dnlib.DotNet.Emit;
|
2012-07-27 14:47:37 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.Confuser {
|
2012-08-09 17:47:18 +08:00
|
|
|
|
class AntiDumping : IVersionProvider {
|
2012-11-19 06:42:43 +08:00
|
|
|
|
ModuleDefMD module;
|
|
|
|
|
MethodDef initMethod;
|
2012-08-12 04:35:34 +08:00
|
|
|
|
ConfuserVersion version = ConfuserVersion.Unknown;
|
|
|
|
|
|
|
|
|
|
enum ConfuserVersion {
|
|
|
|
|
Unknown,
|
|
|
|
|
v14_r58564,
|
|
|
|
|
v14_r58852,
|
|
|
|
|
v16_r69339,
|
|
|
|
|
v17_r74708,
|
|
|
|
|
v18_r75257,
|
|
|
|
|
v19_r75725,
|
|
|
|
|
v19_r76186,
|
|
|
|
|
}
|
2012-07-27 14:47:37 +08:00
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
public MethodDef InitMethod {
|
2012-07-27 14:47:37 +08:00
|
|
|
|
get { return initMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
public TypeDef Type {
|
2012-07-27 14:47:37 +08:00
|
|
|
|
get { return initMethod != null ? initMethod.DeclaringType : null; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Detected {
|
|
|
|
|
get { return initMethod != null; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
public AntiDumping(ModuleDefMD module) {
|
2012-07-27 14:47:37 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
public void Find(ISimpleDeobfuscator simpleDeobfuscator) {
|
|
|
|
|
if (CheckMethod(simpleDeobfuscator, DotNetUtils.GetModuleTypeCctor(module)))
|
2012-07-27 14:47:37 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
bool CheckMethod(ISimpleDeobfuscator simpleDeobfuscator, MethodDef method) {
|
2012-07-27 14:47:37 +08:00
|
|
|
|
if (method == null || method.Body == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
foreach (var instr in method.Body.Instructions) {
|
|
|
|
|
if (instr.OpCode.Code != Code.Call)
|
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var calledMethod = instr.Operand as MethodDef;
|
2012-07-27 14:47:37 +08:00
|
|
|
|
if (calledMethod == null)
|
|
|
|
|
continue;
|
2012-08-12 04:35:34 +08:00
|
|
|
|
if (calledMethod == null || !calledMethod.IsStatic)
|
|
|
|
|
continue;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!DotNetUtils.IsMethod(calledMethod, "System.Void", "()"))
|
2012-07-30 15:13:17 +08:00
|
|
|
|
continue;
|
2012-08-12 04:35:34 +08:00
|
|
|
|
var type = calledMethod.DeclaringType;
|
|
|
|
|
if (type.NestedTypes.Count > 0)
|
|
|
|
|
continue;
|
2012-07-27 14:47:37 +08:00
|
|
|
|
|
2015-10-30 05:36:17 +08:00
|
|
|
|
simpleDeobfuscator.Deobfuscate(calledMethod, SimpleDeobfuscatorFlags.Force | SimpleDeobfuscatorFlags.DisableConstantsFolderExtraInstrs);
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (CheckType(type, calledMethod)) {
|
2012-07-27 14:47:37 +08:00
|
|
|
|
initMethod = calledMethod;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
bool CheckType(TypeDef type, MethodDef initMethod) {
|
|
|
|
|
return CheckType_v14_r58564(type, initMethod) ||
|
|
|
|
|
CheckType_v14_r58852(type, initMethod);
|
2012-08-12 04:35:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
bool CheckType_v14_r58564(TypeDef type, MethodDef initMethod) {
|
|
|
|
|
var virtualProtect = DotNetUtils.GetPInvokeMethod(type, "VirtualProtect");
|
2012-08-12 04:35:34 +08:00
|
|
|
|
if (virtualProtect == null)
|
2012-08-01 02:05:52 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!DotNetUtils.CallsMethod(initMethod, "System.IntPtr System.Runtime.InteropServices.Marshal::GetHINSTANCE(System.Reflection.Module)"))
|
2012-08-01 02:05:52 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (ConfuserUtils.CountCalls(initMethod, virtualProtect) != 3)
|
2012-08-01 02:05:52 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!DeobUtils.HasInteger(initMethod, 224))
|
2012-08-01 02:05:52 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!DeobUtils.HasInteger(initMethod, 240))
|
2012-08-01 02:05:52 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!DeobUtils.HasInteger(initMethod, 267))
|
2012-08-01 02:05:52 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
2012-08-12 04:35:34 +08:00
|
|
|
|
version = ConfuserVersion.v14_r58564;
|
2012-08-01 02:05:52 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
bool CheckType_v14_r58852(TypeDef type, MethodDef initMethod) {
|
|
|
|
|
var virtualProtect = DotNetUtils.GetPInvokeMethod(type, "VirtualProtect");
|
2012-08-12 04:35:34 +08:00
|
|
|
|
if (virtualProtect == null)
|
2012-07-27 14:47:37 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!DotNetUtils.CallsMethod(initMethod, "System.IntPtr System.Runtime.InteropServices.Marshal::GetHINSTANCE(System.Reflection.Module)"))
|
2012-07-27 14:47:37 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
int virtualProtectCalls = ConfuserUtils.CountCalls(initMethod, virtualProtect);
|
2012-08-12 04:35:34 +08:00
|
|
|
|
if (virtualProtectCalls != 14 && virtualProtectCalls != 16)
|
2012-07-27 14:47:37 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!DeobUtils.HasInteger(initMethod, 0x3C))
|
2012-07-27 14:47:37 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!DeobUtils.HasInteger(initMethod, 0x6c64746e))
|
2012-07-27 14:47:37 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!DeobUtils.HasInteger(initMethod, 0x6c642e6c))
|
2012-07-27 14:47:37 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!DeobUtils.HasInteger(initMethod, 0x6f43744e))
|
2012-07-27 14:47:37 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!DeobUtils.HasInteger(initMethod, 0x6e69746e))
|
2012-08-12 04:35:34 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
int locallocs = ConfuserUtils.CountOpCode(initMethod, Code.Localloc);
|
2012-08-12 04:35:34 +08:00
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (DeobUtils.HasInteger(initMethod, 0x18))
|
2012-08-12 04:35:34 +08:00
|
|
|
|
version = ConfuserVersion.v14_r58852;
|
|
|
|
|
else if (virtualProtectCalls == 16)
|
|
|
|
|
version = ConfuserVersion.v16_r69339;
|
|
|
|
|
else if (virtualProtectCalls == 14) {
|
|
|
|
|
if (locallocs == 2)
|
|
|
|
|
version = ConfuserVersion.v17_r74708;
|
|
|
|
|
else if (locallocs == 1) {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (DotNetUtils.HasString(initMethod, "<Unknown>"))
|
2012-08-12 04:35:34 +08:00
|
|
|
|
version = ConfuserVersion.v18_r75257;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
else if (IsRev75725(initMethod))
|
2012-08-12 04:35:34 +08:00
|
|
|
|
version = ConfuserVersion.v19_r75725;
|
|
|
|
|
else
|
|
|
|
|
version = ConfuserVersion.v19_r76186;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
2012-07-27 14:47:37 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2012-08-09 17:47:18 +08:00
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static bool IsRev75725(MethodDef method) {
|
2012-08-12 04:35:34 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count - 9; i++) {
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[i].IsLdcI4() || instrs[i].GetLdcI4Value() != 8)
|
2012-08-12 04:35:34 +08:00
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[i + 1].IsLdcI4() || instrs[i + 1].GetLdcI4Value() != 64)
|
2012-08-12 04:35:34 +08:00
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 2].OpCode.Code != Code.Ldloca && instrs[i + 2].OpCode.Code != Code.Ldloca_S)
|
|
|
|
|
continue;
|
|
|
|
|
var call = instrs[i + 3];
|
|
|
|
|
if (call.OpCode.Code != Code.Call)
|
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var calledMethod = call.Operand as MethodDef;
|
|
|
|
|
if (calledMethod == null || calledMethod.ImplMap == null || calledMethod.ImplMap.Name != "VirtualProtect")
|
2012-08-12 04:35:34 +08:00
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 4].OpCode.Code != Code.Pop)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var ldloc = instrs[i + 5];
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!ldloc.IsLdloc())
|
2012-08-12 04:35:34 +08:00
|
|
|
|
continue;
|
2012-11-20 15:02:10 +08:00
|
|
|
|
var local = ldloc.GetLocal(method.Body.Variables);
|
2012-08-12 04:35:34 +08:00
|
|
|
|
if (local == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[i + 6].IsLdcI4() || instrs[i + 6].GetLdcI4Value() != 0)
|
2012-08-12 04:35:34 +08:00
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 7].OpCode.Code != Code.Stind_I4)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
ldloc = instrs[i + 8];
|
2012-11-20 15:02:10 +08:00
|
|
|
|
if (!ldloc.IsLdloc() || local != ldloc.GetLocal(method.Body.Variables))
|
2012-08-12 04:35:34 +08:00
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
if (!instrs[i + 9].IsLdcI4() || instrs[i + 9].GetLdcI4Value() != 4)
|
2012-08-12 04:35:34 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2012-08-09 17:47:18 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2012-08-12 04:35:34 +08:00
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
public bool GetRevisionRange(out int minRev, out int maxRev) {
|
2012-08-12 04:35:34 +08:00
|
|
|
|
switch (version) {
|
|
|
|
|
case ConfuserVersion.Unknown:
|
|
|
|
|
minRev = maxRev = 0;
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v14_r58564:
|
|
|
|
|
minRev = 58564;
|
|
|
|
|
maxRev = 58817;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v14_r58852:
|
|
|
|
|
minRev = 58852;
|
|
|
|
|
maxRev = 67058;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v16_r69339:
|
|
|
|
|
minRev = 69339;
|
|
|
|
|
maxRev = 74637;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v17_r74708:
|
|
|
|
|
minRev = 74708;
|
|
|
|
|
maxRev = 75184;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v18_r75257:
|
|
|
|
|
minRev = 75257;
|
|
|
|
|
maxRev = 75720;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v19_r75725:
|
|
|
|
|
minRev = 75725;
|
|
|
|
|
maxRev = 76163;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v19_r76186:
|
|
|
|
|
minRev = 76186;
|
|
|
|
|
maxRev = int.MaxValue;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
default: throw new ApplicationException("Invalid version");
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-27 14:47:37 +08:00
|
|
|
|
}
|
|
|
|
|
}
|