This commit is contained in:
de4dot 2012-08-05 20:25:43 +02:00
parent ab57733ae4
commit 9db99626f2
6 changed files with 499 additions and 419 deletions

View File

@ -72,6 +72,8 @@
<Compile Include="deobfuscators\Confuser\Arg64ConstantsReader.cs" />
<Compile Include="deobfuscators\Confuser\ConfuserUtils.cs" />
<Compile Include="deobfuscators\Confuser\ConstantsDecrypter.cs" />
<Compile Include="deobfuscators\Confuser\ConstantsDecrypterBase.cs" />
<Compile Include="deobfuscators\Confuser\ConstantsDecrypterUtils.cs" />
<Compile Include="deobfuscators\Confuser\ConstantsDecrypterV15.cs" />
<Compile Include="deobfuscators\Confuser\ConstantsFolder.cs" />
<Compile Include="deobfuscators\Confuser\ConstantsInliner.cs" />

View File

@ -155,62 +155,14 @@ namespace de4dot.code.deobfuscators.Confuser {
return;
simpleDeobfuscator.deobfuscate(cctor, true);
if ((dictField = findDictField(cctor, cctor.DeclaringType)) == null)
if ((dictField = ConstantsDecrypterUtils.findDictField(cctor, cctor.DeclaringType)) == null)
return;
if ((dataField = findDataField(cctor, cctor.DeclaringType)) == null)
if ((dataField = ConstantsDecrypterUtils.findDataField(cctor, cctor.DeclaringType)) == null)
return;
installMethod = cctor;
}
static FieldDefinition findDictField(MethodDefinition method, TypeDefinition declaringType) {
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count - 1; i++) {
var newobj = instrs[i];
if (newobj.OpCode.Code != Code.Newobj)
continue;
var ctor = newobj.Operand as MethodReference;
if (ctor == null || ctor.FullName != "System.Void System.Collections.Generic.Dictionary`2<System.UInt32,System.Object>::.ctor()")
continue;
var stsfld = instrs[i + 1];
if (stsfld.OpCode.Code != Code.Stsfld)
continue;
var field = stsfld.Operand as FieldDefinition;
if (field == null || field.DeclaringType != declaringType)
continue;
if (field.FieldType.FullName != "System.Collections.Generic.Dictionary`2<System.UInt32,System.Object>")
continue;
return field;
}
return null;
}
static FieldDefinition findDataField(MethodDefinition method, TypeDefinition declaringType) {
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count - 1; i++) {
var callvirt = instrs[i];
if (callvirt.OpCode.Code != Code.Callvirt)
continue;
var calledMethod = callvirt.Operand as MethodReference;
if (calledMethod == null || calledMethod.FullName != "System.Byte[] System.IO.MemoryStream::ToArray()")
continue;
var stsfld = instrs[i + 1];
if (stsfld.OpCode.Code != Code.Stsfld)
continue;
var field = stsfld.Operand as FieldDefinition;
if (field == null || field.DeclaringType != declaringType)
continue;
if (field.FieldType.FullName != "System.Byte[]")
continue;
return field;
}
return null;
}
public void initialize() {
if (installMethod == null)
return;

View File

@ -0,0 +1,331 @@
/*
Copyright (C) 2011-2012 de4dot@gmail.com
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;
using Mono.Cecil;
using Mono.Cecil.Cil;
using de4dot.blocks;
namespace de4dot.code.deobfuscators.Confuser {
abstract class ConstantsDecrypterBase {
protected ModuleDefinition module;
protected byte[] fileData;
protected ISimpleDeobfuscator simpleDeobfuscator;
MethodDefinitionAndDeclaringTypeDict<DecrypterInfo> methodToDecrypterInfo = new MethodDefinitionAndDeclaringTypeDict<DecrypterInfo>();
FieldDefinitionAndDeclaringTypeDict<bool> fields = new FieldDefinitionAndDeclaringTypeDict<bool>();
protected EmbeddedResource resource;
protected BinaryReader reader;
public class DecrypterInfo {
public MethodDefinition decryptMethod;
public MethodDefinition nativeMethod;
public uint key0, key1, key2, key3;
public byte doubleType, singleType, int32Type, int64Type, stringType;
public void initialize() {
if (!initializeKeys())
throw new ApplicationException("Could not find all keys");
if (!initializeTypeCodes())
throw new ApplicationException("Could not find all type codes");
}
bool initializeKeys() {
if (!findKey0(decryptMethod, out key0))
return false;
if (!findKey1(decryptMethod, out key1))
return false;
if (!findKey2Key3(decryptMethod, out key2, out key3))
return false;
return true;
}
static bool findKey0(MethodDefinition method, out uint key) {
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count - 5; i++) {
if (!DotNetUtils.isLdloc(instrs[i]))
continue;
if (instrs[i + 1].OpCode.Code != Code.Or)
continue;
var ldci4 = instrs[i + 2];
if (!DotNetUtils.isLdcI4(ldci4))
continue;
if (instrs[i + 3].OpCode.Code != Code.Xor)
continue;
if (instrs[i + 4].OpCode.Code != Code.Add)
continue;
if (!DotNetUtils.isStloc(instrs[i + 5]))
continue;
key = (uint)DotNetUtils.getLdcI4Value(ldci4);
return true;
}
key = 0;
return false;
}
static bool findKey1(MethodDefinition method, out uint key) {
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count; i++) {
int index = ConfuserUtils.findCallMethod(instrs, i, Code.Callvirt, "System.Int32 System.Reflection.MemberInfo::get_MetadataToken()");
if (index < 0)
break;
if (index + 2 > instrs.Count)
break;
if (!DotNetUtils.isStloc(instrs[index + 1]))
continue;
var ldci4 = instrs[index + 2];
if (!DotNetUtils.isLdcI4(ldci4))
continue;
key = (uint)DotNetUtils.getLdcI4Value(ldci4);
return true;
}
key = 0;
return false;
}
static bool findKey2Key3(MethodDefinition method, out uint key2, out uint key3) {
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count - 3; i++) {
var ldci4_1 = instrs[i];
if (!DotNetUtils.isLdcI4(ldci4_1))
continue;
if (!DotNetUtils.isStloc(instrs[i + 1]))
continue;
var ldci4_2 = instrs[i + 2];
if (!DotNetUtils.isLdcI4(ldci4_2))
continue;
if (!DotNetUtils.isStloc(instrs[i + 3]))
continue;
key2 = (uint)DotNetUtils.getLdcI4Value(ldci4_1);
key3 = (uint)DotNetUtils.getLdcI4Value(ldci4_2);
return true;
}
key2 = 0;
key3 = 0;
return false;
}
bool initializeTypeCodes() {
var allBlocks = new Blocks(decryptMethod).MethodBlocks.getAllBlocks();
if (!findTypeCode(allBlocks, out doubleType, Code.Call, "System.Double System.BitConverter::ToDouble(System.Byte[],System.Int32)"))
return false;
if (!findTypeCode(allBlocks, out singleType, Code.Call, "System.Single System.BitConverter::ToSingle(System.Byte[],System.Int32)"))
return false;
if (!findTypeCode(allBlocks, out int32Type, Code.Call, "System.Int32 System.BitConverter::ToInt32(System.Byte[],System.Int32)"))
return false;
if (!findTypeCode(allBlocks, out int64Type, Code.Call, "System.Int64 System.BitConverter::ToInt64(System.Byte[],System.Int32)"))
return false;
if (!findTypeCode(allBlocks, out stringType, Code.Callvirt, "System.String System.Text.Encoding::GetString(System.Byte[])") &&
!findTypeCode(allBlocks, out stringType, Code.Callvirt, "System.String System.Text.Encoding::GetString(System.Byte[],System.Int32,System.Int32)"))
return false;
return true;
}
static bool findTypeCode(IList<Block> allBlocks, out byte typeCode, Code callCode, string bitConverterMethod) {
foreach (var block in allBlocks) {
if (block.Sources.Count != 1)
continue;
int index = ConfuserUtils.findCallMethod(block.Instructions, 0, callCode, bitConverterMethod);
if (index < 0)
continue;
if (!findTypeCode(block.Sources[0], out typeCode))
continue;
return true;
}
typeCode = 0;
return false;
}
static Block fixBlock(Block block) {
if (block.Sources.Count != 1)
return block;
if (block.getOnlyTarget() == null)
return block;
if (block.Instructions.Count == 0) {
}
else if (block.Instructions.Count == 1 && block.Instructions[0].OpCode.Code == Code.Nop) {
}
else
return block;
return block.Sources[0];
}
static bool findTypeCode(Block block, out byte typeCode) {
block = fixBlock(block);
var instrs = block.Instructions;
int numCeq = 0;
for (int i = instrs.Count - 1; i >= 0; i--) {
var instr = instrs[i];
if (instr.OpCode.Code == Code.Ceq) {
numCeq++;
continue;
}
if (!DotNetUtils.isLdcI4(instr.Instruction))
continue;
if (numCeq != 0 && numCeq != 2)
continue;
typeCode = (byte)DotNetUtils.getLdcI4Value(instr.Instruction);
return true;
}
typeCode = 0;
return false;
}
public uint calcHash(uint x) {
uint h0 = key1 ^ x;
uint h1 = key2;
uint h2 = key3;
for (uint i = 1; i <= 64; i++) {
h0 = (h0 << 8) | (h0 >> 24);
uint n = h0 & 0x3F;
if (n >= 0 && n < 16) {
h1 |= ((byte)(h0 >> 8) & (h0 >> 16)) ^ (byte)~h0;
h2 ^= (h0 * i + 1) & 0xF;
h0 += (h1 | h2) ^ key0;
}
else if (n >= 16 && n < 32) {
h1 ^= ((h0 & 0x00FF00FF) << 8) ^ (ushort)((h0 >> 8) | ~h0);
h2 += (h0 * i) & 0x1F;
h0 |= (h1 + ~h2) & key0;
}
else if (n >= 32 && n < 48) {
h1 += (byte)(h0 | (h0 >> 16)) + (~h0 & 0xFF);
h2 -= ~(h0 + n) % 48;
h0 ^= (h1 % h2) | key0;
}
else if (n >= 48 && n < 64) {
h1 ^= ((byte)(h0 >> 16) | ~(h0 & 0xFF)) * (~h0 & 0x00FF0000);
h2 += (h0 ^ (i - 1)) % n;
h0 -= ~(h1 ^ h2) + key0;
}
}
return h0;
}
}
public abstract bool Detected { get; }
public EmbeddedResource Resource {
get { return resource; }
}
public IEnumerable<FieldDefinition> Fields {
get { return fields.getKeys(); }
}
protected bool HasDecrypterInfos {
get { return methodToDecrypterInfo.Count > 0; }
}
public IEnumerable<DecrypterInfo> DecrypterInfos {
get { return methodToDecrypterInfo.getValues(); }
}
public ConstantsDecrypterBase(ModuleDefinition module, byte[] fileData, ISimpleDeobfuscator simpleDeobfuscator) {
this.module = module;
this.fileData = fileData;
this.simpleDeobfuscator = simpleDeobfuscator;
}
public abstract void initialize();
protected void add(DecrypterInfo info) {
methodToDecrypterInfo.add(info.decryptMethod, info);
}
protected bool add(FieldDefinition field) {
if (field == null)
return false;
fields.add(field, true);
return true;
}
protected void initializeDecrypterInfos() {
foreach (var info in methodToDecrypterInfo.getValues()) {
simpleDeobfuscator.deobfuscate(info.decryptMethod);
info.initialize();
}
}
public object decryptInt32(MethodDefinition caller, MethodDefinition decryptMethod, object[] args) {
var info = methodToDecrypterInfo.find(decryptMethod);
byte typeCode;
var data = decryptData(info, caller, args, out typeCode);
if (typeCode != info.int32Type)
return null;
if (data.Length != 4)
throw new ApplicationException("Invalid data length");
return BitConverter.ToInt32(data, 0);
}
public object decryptInt64(MethodDefinition caller, MethodDefinition decryptMethod, object[] args) {
var info = methodToDecrypterInfo.find(decryptMethod);
byte typeCode;
var data = decryptData(info, caller, args, out typeCode);
if (typeCode != info.int64Type)
return null;
if (data.Length != 8)
throw new ApplicationException("Invalid data length");
return BitConverter.ToInt64(data, 0);
}
public object decryptSingle(MethodDefinition caller, MethodDefinition decryptMethod, object[] args) {
var info = methodToDecrypterInfo.find(decryptMethod);
byte typeCode;
var data = decryptData(info, caller, args, out typeCode);
if (typeCode != info.singleType)
return null;
if (data.Length != 4)
throw new ApplicationException("Invalid data length");
return BitConverter.ToSingle(data, 0);
}
public object decryptDouble(MethodDefinition caller, MethodDefinition decryptMethod, object[] args) {
var info = methodToDecrypterInfo.find(decryptMethod);
byte typeCode;
var data = decryptData(info, caller, args, out typeCode);
if (typeCode != info.doubleType)
return null;
if (data.Length != 8)
throw new ApplicationException("Invalid data length");
return BitConverter.ToDouble(data, 0);
}
public string decryptString(MethodDefinition caller, MethodDefinition decryptMethod, object[] args) {
var info = methodToDecrypterInfo.find(decryptMethod);
byte typeCode;
var data = decryptData(info, caller, args, out typeCode);
if (typeCode != info.stringType)
return null;
return Encoding.UTF8.GetString(data);
}
protected abstract byte[] decryptData(DecrypterInfo info, MethodDefinition caller, object[] args, out byte typeCode);
}
}

View File

@ -0,0 +1,97 @@
/*
Copyright (C) 2011-2012 de4dot@gmail.com
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 Mono.Cecil;
using Mono.Cecil.Cil;
namespace de4dot.code.deobfuscators.Confuser {
static class ConstantsDecrypterUtils {
public static FieldDefinition findDictField(MethodDefinition method, TypeDefinition declaringType) {
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count - 1; i++) {
var newobj = instrs[i];
if (newobj.OpCode.Code != Code.Newobj)
continue;
var ctor = newobj.Operand as MethodReference;
if (ctor == null || ctor.FullName != "System.Void System.Collections.Generic.Dictionary`2<System.UInt32,System.Object>::.ctor()")
continue;
var stsfld = instrs[i + 1];
if (stsfld.OpCode.Code != Code.Stsfld)
continue;
var field = stsfld.Operand as FieldDefinition;
if (field == null || field.DeclaringType != declaringType)
continue;
if (field.FieldType.FullName != "System.Collections.Generic.Dictionary`2<System.UInt32,System.Object>")
continue;
return field;
}
return null;
}
public static FieldDefinition findDataField(MethodDefinition method, TypeDefinition declaringType) {
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count - 1; i++) {
var callvirt = instrs[i];
if (callvirt.OpCode.Code != Code.Callvirt)
continue;
var calledMethod = callvirt.Operand as MethodReference;
if (calledMethod == null || calledMethod.FullName != "System.Byte[] System.IO.MemoryStream::ToArray()")
continue;
var stsfld = instrs[i + 1];
if (stsfld.OpCode.Code != Code.Stsfld)
continue;
var field = stsfld.Operand as FieldDefinition;
if (field == null || field.DeclaringType != declaringType)
continue;
if (field.FieldType.FullName != "System.Byte[]")
continue;
return field;
}
return null;
}
public static FieldDefinition findStreamField(MethodDefinition method, TypeDefinition declaringType) {
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count - 1; i++) {
var newobj = instrs[i];
if (newobj.OpCode.Code != Code.Newobj)
continue;
var ctor = newobj.Operand as MethodReference;
if (ctor == null || ctor.FullName != "System.Void System.IO.MemoryStream::.ctor()")
continue;
var stsfld = instrs[i + 1];
if (stsfld.OpCode.Code != Code.Stsfld)
continue;
var field = stsfld.Operand as FieldDefinition;
if (field == null || field.DeclaringType != declaringType)
continue;
if (field.FieldType.FullName != "System.IO.MemoryStream")
continue;
return field;
}
return null;
}
}
}

View File

@ -20,25 +20,15 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Mono.Cecil;
using Mono.Cecil.Cil;
using de4dot.blocks;
using de4dot.PE;
namespace de4dot.code.deobfuscators.Confuser {
class ConstantsDecrypterV15 {
ModuleDefinition module;
byte[] fileData;
ISimpleDeobfuscator simpleDeobfuscator;
FieldDefinition dictField, streamField;
MethodDefinition decryptMethod;
MethodDefinition nativeMethod;
EmbeddedResource resource;
uint key0, key1, key2, key3;
byte doubleType, singleType, int32Type, int64Type, stringType;
BinaryReader reader;
class ConstantsDecrypterV15 : ConstantsDecrypterBase {
ConfuserVersion version = ConfuserVersion.Unknown;
DecrypterInfo theDecrypterInfo;
enum ConfuserVersion {
Unknown,
@ -53,35 +43,12 @@ namespace de4dot.code.deobfuscators.Confuser {
v17_r73822_native,
}
public MethodDefinition Method {
get { return decryptMethod; }
public override bool Detected {
get { return theDecrypterInfo != null; }
}
public MethodDefinition NativeMethod {
get { return nativeMethod; }
}
public IEnumerable<FieldDefinition> Fields {
get {
return new List<FieldDefinition> {
streamField,
dictField,
};
}
}
public EmbeddedResource Resource {
get { return resource; }
}
public bool Detected {
get { return decryptMethod != null; }
}
public ConstantsDecrypterV15(ModuleDefinition module, byte[] fileData, ISimpleDeobfuscator simpleDeobfuscator) {
this.module = module;
this.fileData = fileData;
this.simpleDeobfuscator = simpleDeobfuscator;
public ConstantsDecrypterV15(ModuleDefinition module, byte[] fileData, ISimpleDeobfuscator simpleDeobfuscator)
: base(module, fileData, simpleDeobfuscator) {
}
static readonly string[] requiredLocals1 = new string[] {
@ -108,6 +75,7 @@ namespace de4dot.code.deobfuscators.Confuser {
if (!DotNetUtils.isMethod(method, "System.Object", "(System.UInt32)"))
continue;
DecrypterInfo info = new DecrypterInfo();
var localTypes = new LocalTypes(method);
if (localTypes.all(requiredLocals1)) {
if (localTypes.exists("System.Collections.BitArray")) // or System.Random
@ -123,7 +91,7 @@ namespace de4dot.code.deobfuscators.Confuser {
version = ConfuserVersion.v17_r73740_dynamic;
}
else if (DotNetUtils.callsMethod(method, "System.String System.Text.Encoding::GetString(System.Byte[],System.Int32,System.Int32)")) {
if ((nativeMethod = findNativeMethod(method)) == null)
if ((info.nativeMethod = findNativeMethod(method)) == null)
version = ConfuserVersion.v17_r73764_dynamic;
else
version = ConfuserVersion.v17_r73764_native;
@ -136,7 +104,7 @@ namespace de4dot.code.deobfuscators.Confuser {
DeobUtils.hasInteger(method, 0x10000) &&
DeobUtils.hasInteger(method, 0xFFFF))
version = ConfuserVersion.v17_r73822_normal;
else if ((nativeMethod = findNativeMethod(method)) == null)
else if ((info.nativeMethod = findNativeMethod(method)) == null)
version = ConfuserVersion.v17_r73822_dynamic;
else
version = ConfuserVersion.v17_r73822_native;
@ -144,7 +112,9 @@ namespace de4dot.code.deobfuscators.Confuser {
else
continue;
decryptMethod = method;
info.decryptMethod = method;
theDecrypterInfo = info;
add(info);
break;
}
}
@ -166,30 +136,26 @@ namespace de4dot.code.deobfuscators.Confuser {
return null;
}
public void initialize() {
if ((resource = findResource(decryptMethod)) == null)
public override void initialize() {
if ((resource = findResource(theDecrypterInfo.decryptMethod)) == null)
throw new ApplicationException("Could not find encrypted consts resource");
simpleDeobfuscator.deobfuscate(decryptMethod);
if (!initializeKeys())
throw new ApplicationException("Could not find all keys");
if (!initializeTypeCodes())
throw new ApplicationException("Could not find all type codes");
if (!initializeFields())
initializeDecrypterInfos();
if (!initializeFields(theDecrypterInfo))
throw new ApplicationException("Could not find all fields");
var constants = DeobUtils.inflate(resource.GetResourceData(), true);
reader = new BinaryReader(new MemoryStream(constants));
}
bool initializeFields() {
bool initializeFields(DecrypterInfo info) {
switch (version) {
case ConfuserVersion.v17_r73822_normal:
case ConfuserVersion.v17_r73822_dynamic:
case ConfuserVersion.v17_r73822_native:
if ((dictField = findDictField(decryptMethod, decryptMethod.DeclaringType)) == null)
if (!add(ConstantsDecrypterUtils.findDictField(info.decryptMethod, info.decryptMethod.DeclaringType)))
return false;
if ((streamField = findStreamField(decryptMethod, decryptMethod.DeclaringType)) == null)
if (!add(ConstantsDecrypterUtils.findStreamField(info.decryptMethod, info.decryptMethod.DeclaringType)))
return false;
break;
@ -200,285 +166,40 @@ namespace de4dot.code.deobfuscators.Confuser {
return true;
}
static FieldDefinition findDictField(MethodDefinition method, TypeDefinition declaringType) {
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count - 1; i++) {
var newobj = instrs[i];
if (newobj.OpCode.Code != Code.Newobj)
continue;
var ctor = newobj.Operand as MethodReference;
if (ctor == null || ctor.FullName != "System.Void System.Collections.Generic.Dictionary`2<System.UInt32,System.Object>::.ctor()")
continue;
var stsfld = instrs[i + 1];
if (stsfld.OpCode.Code != Code.Stsfld)
continue;
var field = stsfld.Operand as FieldDefinition;
if (field == null || field.DeclaringType != declaringType)
continue;
if (field.FieldType.FullName != "System.Collections.Generic.Dictionary`2<System.UInt32,System.Object>")
continue;
return field;
}
return null;
}
static FieldDefinition findStreamField(MethodDefinition method, TypeDefinition declaringType) {
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count - 1; i++) {
var newobj = instrs[i];
if (newobj.OpCode.Code != Code.Newobj)
continue;
var ctor = newobj.Operand as MethodReference;
if (ctor == null || ctor.FullName != "System.Void System.IO.MemoryStream::.ctor()")
continue;
var stsfld = instrs[i + 1];
if (stsfld.OpCode.Code != Code.Stsfld)
continue;
var field = stsfld.Operand as FieldDefinition;
if (field == null || field.DeclaringType != declaringType)
continue;
if (field.FieldType.FullName != "System.IO.MemoryStream")
continue;
return field;
}
return null;
}
bool initializeKeys() {
if (!findKey0(decryptMethod, out key0))
return false;
if (!findKey1(decryptMethod, out key1))
return false;
if (!findKey2Key3(decryptMethod, out key2, out key3))
return false;
return true;
}
static bool findKey0(MethodDefinition method, out uint key) {
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count - 5; i++) {
if (!DotNetUtils.isLdloc(instrs[i]))
continue;
if (instrs[i + 1].OpCode.Code != Code.Or)
continue;
var ldci4 = instrs[i + 2];
if (!DotNetUtils.isLdcI4(ldci4))
continue;
if (instrs[i + 3].OpCode.Code != Code.Xor)
continue;
if (instrs[i + 4].OpCode.Code != Code.Add)
continue;
if (!DotNetUtils.isStloc(instrs[i + 5]))
continue;
key = (uint)DotNetUtils.getLdcI4Value(ldci4);
return true;
}
key = 0;
return false;
}
static bool findKey1(MethodDefinition method, out uint key) {
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count; i++) {
int index = ConfuserUtils.findCallMethod(instrs, i, Code.Callvirt, "System.Int32 System.Reflection.MemberInfo::get_MetadataToken()");
if (index < 0)
break;
if (index + 2 > instrs.Count)
break;
if (!DotNetUtils.isStloc(instrs[index + 1]))
continue;
var ldci4 = instrs[index + 2];
if (!DotNetUtils.isLdcI4(ldci4))
continue;
key = (uint)DotNetUtils.getLdcI4Value(ldci4);
return true;
}
key = 0;
return false;
}
static bool findKey2Key3(MethodDefinition method, out uint key2, out uint key3) {
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count - 3; i++) {
var ldci4_1 = instrs[i];
if (!DotNetUtils.isLdcI4(ldci4_1))
continue;
if (!DotNetUtils.isStloc(instrs[i + 1]))
continue;
var ldci4_2 = instrs[i + 2];
if (!DotNetUtils.isLdcI4(ldci4_2))
continue;
if (!DotNetUtils.isStloc(instrs[i + 3]))
continue;
key2 = (uint)DotNetUtils.getLdcI4Value(ldci4_1);
key3 = (uint)DotNetUtils.getLdcI4Value(ldci4_2);
return true;
}
key2 = 0;
key3 = 0;
return false;
}
bool initializeTypeCodes() {
var allBlocks = new Blocks(decryptMethod).MethodBlocks.getAllBlocks();
if (!findTypeCode(allBlocks, out doubleType, Code.Call, "System.Double System.BitConverter::ToDouble(System.Byte[],System.Int32)"))
return false;
if (!findTypeCode(allBlocks, out singleType, Code.Call, "System.Single System.BitConverter::ToSingle(System.Byte[],System.Int32)"))
return false;
if (!findTypeCode(allBlocks, out int32Type, Code.Call, "System.Int32 System.BitConverter::ToInt32(System.Byte[],System.Int32)"))
return false;
if (!findTypeCode(allBlocks, out int64Type, Code.Call, "System.Int64 System.BitConverter::ToInt64(System.Byte[],System.Int32)"))
return false;
if (!findTypeCode(allBlocks, out stringType, Code.Callvirt, "System.String System.Text.Encoding::GetString(System.Byte[])") &&
!findTypeCode(allBlocks, out stringType, Code.Callvirt, "System.String System.Text.Encoding::GetString(System.Byte[],System.Int32,System.Int32)"))
return false;
return true;
}
static bool findTypeCode(IList<Block> allBlocks, out byte typeCode, Code callCode, string bitConverterMethod) {
foreach (var block in allBlocks) {
if (block.Sources.Count != 1)
continue;
int index = ConfuserUtils.findCallMethod(block.Instructions, 0, callCode, bitConverterMethod);
if (index < 0)
continue;
if (!findTypeCode(block.Sources[0], out typeCode))
continue;
return true;
}
typeCode = 0;
return false;
}
static Block fixBlock(Block block) {
if (block.Sources.Count != 1)
return block;
if (block.getOnlyTarget() == null)
return block;
if (block.Instructions.Count == 0) {
}
else if (block.Instructions.Count == 1 && block.Instructions[0].OpCode.Code == Code.Nop) {
}
else
return block;
return block.Sources[0];
}
static bool findTypeCode(Block block, out byte typeCode) {
block = fixBlock(block);
var instrs = block.Instructions;
int numCeq = 0;
for (int i = instrs.Count - 1; i >= 0; i--) {
var instr = instrs[i];
if (instr.OpCode.Code == Code.Ceq) {
numCeq++;
continue;
}
if (!DotNetUtils.isLdcI4(instr.Instruction))
continue;
if (numCeq != 0 && numCeq != 2)
continue;
typeCode = (byte)DotNetUtils.getLdcI4Value(instr.Instruction);
return true;
}
typeCode = 0;
return false;
}
EmbeddedResource findResource(MethodDefinition method) {
return DotNetUtils.getResource(module, DotNetUtils.getCodeStrings(method)) as EmbeddedResource;
}
public object decryptInt32(MethodDefinition caller, uint magic) {
byte typeCode;
var data = decryptData(caller, magic, out typeCode);
if (typeCode != int32Type)
return null;
if (data.Length != 4)
throw new ApplicationException("Invalid data length");
return BitConverter.ToInt32(data, 0);
}
public object decryptInt64(MethodDefinition caller, uint magic) {
byte typeCode;
var data = decryptData(caller, magic, out typeCode);
if (typeCode != int64Type)
return null;
if (data.Length != 8)
throw new ApplicationException("Invalid data length");
return BitConverter.ToInt64(data, 0);
}
public object decryptSingle(MethodDefinition caller, uint magic) {
byte typeCode;
var data = decryptData(caller, magic, out typeCode);
if (typeCode != singleType)
return null;
if (data.Length != 4)
throw new ApplicationException("Invalid data length");
return BitConverter.ToSingle(data, 0);
}
public object decryptDouble(MethodDefinition caller, uint magic) {
byte typeCode;
var data = decryptData(caller, magic, out typeCode);
if (typeCode != doubleType)
return null;
if (data.Length != 8)
throw new ApplicationException("Invalid data length");
return BitConverter.ToDouble(data, 0);
}
public string decryptString(MethodDefinition caller, uint magic) {
byte typeCode;
var data = decryptData(caller, magic, out typeCode);
if (typeCode != stringType)
return null;
return Encoding.UTF8.GetString(data);
}
byte[] decryptData(MethodDefinition caller, uint magic, out byte typeCode) {
uint offs = calcHash(caller.MetadataToken.ToUInt32()) ^ magic;
protected override byte[] decryptData(DecrypterInfo info, MethodDefinition caller, object[] args, out byte typeCode) {
uint offs = info.calcHash(caller.MetadataToken.ToUInt32()) ^ (uint)args[0];
reader.BaseStream.Position = offs;
typeCode = reader.ReadByte();
if (typeCode != int32Type && typeCode != int64Type &&
typeCode != singleType && typeCode != doubleType &&
typeCode != stringType)
if (typeCode != info.int32Type && typeCode != info.int64Type &&
typeCode != info.singleType && typeCode != info.doubleType &&
typeCode != info.stringType)
throw new ApplicationException("Invalid type code");
var encrypted = reader.ReadBytes(reader.ReadInt32());
return decryptConstant(encrypted, offs);
return decryptConstant(info, encrypted, offs);
}
byte[] decryptConstant(byte[] encrypted, uint offs) {
byte[] decryptConstant(DecrypterInfo info, byte[] encrypted, uint offs) {
switch (version) {
case ConfuserVersion.v15_r60785_normal: return decryptConstant_v15_r60785_normal(encrypted, offs);
case ConfuserVersion.v15_r60785_dynamic: return decryptConstant_v15_r60785_dynamic(encrypted, offs);
case ConfuserVersion.v17_r73404_normal: return decryptConstant_v17_r73404_normal(encrypted, offs);
case ConfuserVersion.v17_r73740_dynamic: return decryptConstant_v17_r73740_dynamic(encrypted, offs);
case ConfuserVersion.v17_r73764_dynamic: return decryptConstant_v17_r73740_dynamic(encrypted, offs);
case ConfuserVersion.v17_r73764_native: return decryptConstant_v17_r73764_native(encrypted, offs);
case ConfuserVersion.v17_r73822_normal: return decryptConstant_v17_r73404_normal(encrypted, offs);
case ConfuserVersion.v17_r73822_dynamic: return decryptConstant_v17_r73740_dynamic(encrypted, offs);
case ConfuserVersion.v17_r73822_native: return decryptConstant_v17_r73764_native(encrypted, offs);
case ConfuserVersion.v15_r60785_normal: return decryptConstant_v15_r60785_normal(info, encrypted, offs);
case ConfuserVersion.v15_r60785_dynamic: return decryptConstant_v15_r60785_dynamic(info, encrypted, offs);
case ConfuserVersion.v17_r73404_normal: return decryptConstant_v17_r73404_normal(info, encrypted, offs);
case ConfuserVersion.v17_r73740_dynamic: return decryptConstant_v17_r73740_dynamic(info, encrypted, offs);
case ConfuserVersion.v17_r73764_dynamic: return decryptConstant_v17_r73740_dynamic(info, encrypted, offs);
case ConfuserVersion.v17_r73764_native: return decryptConstant_v17_r73764_native(info, encrypted, offs);
case ConfuserVersion.v17_r73822_normal: return decryptConstant_v17_r73404_normal(info, encrypted, offs);
case ConfuserVersion.v17_r73822_dynamic: return decryptConstant_v17_r73740_dynamic(info, encrypted, offs);
case ConfuserVersion.v17_r73822_native: return decryptConstant_v17_r73764_native(info, encrypted, offs);
default: throw new ApplicationException("Invalid version");
}
}
byte[] decryptConstant_v15_r60785_normal(byte[] encrypted, uint offs) {
var rand = new Random((int)(key0 ^ offs));
byte[] decryptConstant_v15_r60785_normal(DecrypterInfo info, byte[] encrypted, uint offs) {
var rand = new Random((int)(info.key0 ^ offs));
var decrypted = new byte[encrypted.Length];
rand.NextBytes(decrypted);
for (int i = 0; i < decrypted.Length; i++)
@ -486,8 +207,8 @@ namespace de4dot.code.deobfuscators.Confuser {
return decrypted;
}
byte[] decryptConstant_v15_r60785_dynamic(byte[] encrypted, uint offs) {
var instrs = decryptMethod.Body.Instructions;
byte[] decryptConstant_v15_r60785_dynamic(DecrypterInfo info, byte[] encrypted, uint offs) {
var instrs = info.decryptMethod.Body.Instructions;
int startIndex = getDynamicStartIndex_v15_r60785(instrs);
int endIndex = getDynamicEndIndex_v15_r60785(instrs, startIndex);
if (endIndex < 0)
@ -530,8 +251,8 @@ namespace de4dot.code.deobfuscators.Confuser {
return -1;
}
byte[] decryptConstant_v17_r73404_normal(byte[] encrypted, uint offs) {
return ConfuserUtils.decrypt(key0 ^ offs, encrypted);
byte[] decryptConstant_v17_r73404_normal(DecrypterInfo info, byte[] encrypted, uint offs) {
return ConfuserUtils.decrypt(info.key0 ^ offs, encrypted);
}
static VariableDefinition getDynamicLocal_v17_r73740(MethodDefinition method) {
@ -596,17 +317,17 @@ namespace de4dot.code.deobfuscators.Confuser {
return -1;
}
byte[] decryptConstant_v17_r73740_dynamic(byte[] encrypted, uint offs) {
var local = getDynamicLocal_v17_r73740(decryptMethod);
byte[] decryptConstant_v17_r73740_dynamic(DecrypterInfo info, byte[] encrypted, uint offs) {
var local = getDynamicLocal_v17_r73740(info.decryptMethod);
if (local == null)
throw new ApplicationException("Could not find local");
int endIndex = getDynamicEndIndex_v17_r73740(decryptMethod, local);
int startIndex = getDynamicStartIndex_v17_r73740(decryptMethod, endIndex);
int endIndex = getDynamicEndIndex_v17_r73740(info.decryptMethod, local);
int startIndex = getDynamicStartIndex_v17_r73740(info.decryptMethod, endIndex);
if (startIndex < 0)
throw new ApplicationException("Could not find start/end index");
var constReader = new ConstantsReader(decryptMethod);
var constReader = new ConstantsReader(info.decryptMethod);
return decrypt(encrypted, magic => {
constReader.setConstantInt32(local, magic);
int index = startIndex, result;
@ -616,9 +337,9 @@ namespace de4dot.code.deobfuscators.Confuser {
});
}
byte[] decryptConstant_v17_r73764_native(byte[] encrypted, uint offs) {
byte[] decryptConstant_v17_r73764_native(DecrypterInfo info, byte[] encrypted, uint offs) {
var x86Emu = new x86Emulator(new PeImage(fileData));
return decrypt(encrypted, magic => (byte)x86Emu.emulate((uint)nativeMethod.RVA, magic));
return decrypt(encrypted, magic => (byte)x86Emu.emulate((uint)info.nativeMethod.RVA, magic));
}
byte[] decrypt(byte[] encrypted, Func<uint, byte> decryptFunc) {
@ -631,36 +352,5 @@ namespace de4dot.code.deobfuscators.Confuser {
return decrypted;
}
uint calcHash(uint x) {
uint h0 = key1 ^ x;
uint h1 = key2;
uint h2 = key3;
for (uint i = 1; i <= 64; i++) {
h0 = (h0 << 8) | (h0 >> 24);
uint n = h0 & 0x3F;
if (n >= 0 && n < 16) {
h1 |= ((byte)(h0 >> 8) & (h0 >> 16)) ^ (byte)~h0;
h2 ^= (h0 * i + 1) & 0xF;
h0 += (h1 | h2) ^ key0;
}
else if (n >= 16 && n < 32) {
h1 ^= ((h0 & 0x00FF00FF) << 8) ^ (ushort)((h0 >> 8) | ~h0);
h2 += (h0 * i) & 0x1F;
h0 |= (h1 + ~h2) & key0;
}
else if (n >= 32 && n < 48) {
h1 += (byte)(h0 | (h0 >> 16)) + (~h0 & 0xFF);
h2 -= ~(h0 + n) % 48;
h0 ^= (h1 % h2) | key0;
}
else if (n >= 48 && n < 64) {
h1 ^= ((byte)(h0 >> 16) | ~(h0 & 0xFF)) * (~h0 & 0x00FF0000);
h2 += (h0 ^ (i - 1)) % n;
h0 -= ~(h1 ^ h2) + key0;
}
}
return h0;
}
}
}

View File

@ -428,30 +428,38 @@ namespace de4dot.code.deobfuscators.Confuser {
bool hasInitializedConstantsDecrypter15 = false;
void initializeConstantsDecrypter15() {
if (hasInitializedConstantsDecrypter15 || (constantsDecrypterV15 == null || !constantsDecrypterV15.Detected))
initialize(constantsDecrypterV15, ref hasInitializedConstantsDecrypter15);
}
void initialize(ConstantsDecrypterBase constDecrypter, ref bool hasInitialized) {
if (hasInitialized || (constDecrypter == null || !constDecrypter.Detected))
return;
hasInitializedConstantsDecrypter15 = true;
decryptResources();
constantsDecrypterV15.initialize();
constDecrypter.initialize();
int32ValueInliner = new Int32ValueInliner();
int64ValueInliner = new Int64ValueInliner();
singleValueInliner = new SingleValueInliner();
doubleValueInliner = new DoubleValueInliner();
staticStringInliner.add(constantsDecrypterV15.Method, (method, gim, args) => constantsDecrypterV15.decryptString(staticStringInliner.Method, (uint)args[0]));
int32ValueInliner.add(constantsDecrypterV15.Method, (method, gim, args) => constantsDecrypterV15.decryptInt32(int32ValueInliner.Method, (uint)args[0]));
int64ValueInliner.add(constantsDecrypterV15.Method, (method, gim, args) => constantsDecrypterV15.decryptInt64(int64ValueInliner.Method, (uint)args[0]));
singleValueInliner.add(constantsDecrypterV15.Method, (method, gim, args) => constantsDecrypterV15.decryptSingle(singleValueInliner.Method, (uint)args[0]));
doubleValueInliner.add(constantsDecrypterV15.Method, (method, gim, args) => constantsDecrypterV15.decryptDouble(doubleValueInliner.Method, (uint)args[0]));
foreach (var info in constDecrypter.DecrypterInfos) {
staticStringInliner.add(info.decryptMethod, (method, gim, args) => constDecrypter.decryptString(staticStringInliner.Method, method, args));
int32ValueInliner.add(info.decryptMethod, (method, gim, args) => constDecrypter.decryptInt32(int32ValueInliner.Method, method, args));
int64ValueInliner.add(info.decryptMethod, (method, gim, args) => constDecrypter.decryptInt64(int64ValueInliner.Method, method, args));
singleValueInliner.add(info.decryptMethod, (method, gim, args) => constDecrypter.decryptSingle(singleValueInliner.Method, method, args));
doubleValueInliner.add(info.decryptMethod, (method, gim, args) => constDecrypter.decryptDouble(doubleValueInliner.Method, method, args));
}
int32ValueInliner.RemoveUnbox = true;
int64ValueInliner.RemoveUnbox = true;
singleValueInliner.RemoveUnbox = true;
doubleValueInliner.RemoveUnbox = true;
DeobfuscatedFile.stringDecryptersAdded();
addFieldsToBeRemoved(constantsDecrypterV15.Fields, "Constants decrypter field");
addMethodToBeRemoved(constantsDecrypterV15.Method, "Constants decrypter method");
addMethodToBeRemoved(constantsDecrypterV15.NativeMethod, "Constants decrypter native method");
addResourceToBeRemoved(constantsDecrypterV15.Resource, "Encrypted constants");
addFieldsToBeRemoved(constDecrypter.Fields, "Constants decrypter field");
foreach (var info in constDecrypter.DecrypterInfos) {
addMethodToBeRemoved(info.decryptMethod, "Constants decrypter method");
addMethodToBeRemoved(info.nativeMethod, "Constants decrypter native method");
}
addResourceToBeRemoved(constDecrypter.Resource, "Encrypted constants");
}
void decryptResources() {