de4dot-cex/de4dot.code/deobfuscators/Goliath_NET/DecrypterBase.cs

301 lines
8.0 KiB
C#
Raw Normal View History

2011-12-29 15:26:36 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
2011-12-29 15:26:36 +08:00
This file is part of de4dot.
de4dot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
de4dot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with de4dot. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.IO;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
2011-12-29 15:26:36 +08:00
using de4dot.blocks;
namespace de4dot.code.deobfuscators.Goliath_NET {
abstract class DecrypterBase {
2012-11-18 10:02:12 +08:00
protected ModuleDefMD module;
2011-12-29 15:26:36 +08:00
EmbeddedResource encryptedResource;
TypeDef decrypterType;
TypeDef delegateType;
TypeDef delegateInitType;
2011-12-29 15:26:36 +08:00
protected BinaryReader decryptedReader;
2012-11-22 16:14:51 +08:00
MethodDefAndDeclaringTypeDict<Info> decrypterMethods = new MethodDefAndDeclaringTypeDict<Info>();
2011-12-29 15:26:36 +08:00
protected class Info {
public MethodDef method;
2011-12-29 15:26:36 +08:00
public int offset;
public bool referenced = false;
public Info(MethodDef method, int offset) {
2011-12-29 15:26:36 +08:00
this.method = method;
this.offset = offset;
}
}
public bool Detected {
get { return encryptedResource != null; }
}
public Resource EncryptedResource {
get { return encryptedResource; }
}
public TypeDef Type {
2011-12-29 15:26:36 +08:00
get { return decrypterType; }
}
public TypeDef DelegateInitType {
2014-04-08 06:16:57 +08:00
get { return delegateInitType ?? FindDelegateInitType(); }
2011-12-29 15:26:36 +08:00
}
public TypeDef DelegateType {
2011-12-29 15:26:36 +08:00
get { return delegateType; }
}
public IEnumerable<TypeDef> DecrypterTypes {
2011-12-29 15:26:36 +08:00
get {
2012-11-22 16:14:51 +08:00
var types = new TypeDefDict<TypeDef>();
2013-01-19 20:03:57 +08:00
foreach (var info in decrypterMethods.GetValues()) {
2011-12-29 15:26:36 +08:00
if (info.referenced)
2013-01-19 20:03:57 +08:00
types.Add(info.method.DeclaringType, info.method.DeclaringType);
2011-12-29 15:26:36 +08:00
}
2013-01-19 20:03:57 +08:00
return types.GetValues();
2011-12-29 15:26:36 +08:00
}
}
2012-11-18 10:02:12 +08:00
public DecrypterBase(ModuleDefMD module) {
2011-12-29 15:26:36 +08:00
this.module = module;
}
2013-01-19 20:03:57 +08:00
protected Info GetInfo(MethodDef method) {
var info = decrypterMethods.Find(method);
2011-12-29 15:26:36 +08:00
if (info == null)
return null;
info.referenced = true;
return info;
}
2013-01-19 20:03:57 +08:00
public void Find() {
2012-01-02 01:50:46 +08:00
foreach (var tmp in module.Resources) {
var resource = tmp as EmbeddedResource;
2011-12-29 15:26:36 +08:00
if (resource == null)
continue;
2012-11-18 10:02:12 +08:00
if (!resource.Name.String.EndsWith(".resources", StringComparison.Ordinal))
2012-01-02 01:50:46 +08:00
continue;
string ns, name;
2013-01-19 20:03:57 +08:00
SplitTypeName(resource.Name.String.Substring(0, resource.Name.String.Length - 10), out ns, out name);
2012-11-18 10:02:12 +08:00
var type = new TypeRefUser(module, ns, name, module).Resolve();
2012-01-02 01:50:46 +08:00
if (type == null)
continue;
2013-01-19 20:03:57 +08:00
if (!CheckDecrypterType(type))
2011-12-29 15:26:36 +08:00
continue;
encryptedResource = resource;
decrypterType = type;
break;
}
}
2013-01-19 20:03:57 +08:00
protected abstract bool CheckDecrypterType(TypeDef type);
2012-01-02 14:02:43 +08:00
2013-01-19 20:03:57 +08:00
void SplitTypeName(string fullName, out string ns, out string name) {
2012-01-02 01:50:46 +08:00
int index = fullName.LastIndexOf('.');
if (index < 0) {
ns = "";
name = fullName;
}
else {
ns = fullName.Substring(0, index);
name = fullName.Substring(index + 1);
}
}
2013-01-19 20:03:57 +08:00
public void Initialize() {
2011-12-29 15:26:36 +08:00
if (encryptedResource == null)
return;
2013-01-19 20:03:57 +08:00
decryptedReader = new BinaryReader(new MemoryStream(Decrypt(encryptedResource.GetResourceData())));
2011-12-29 15:26:36 +08:00
delegateType = null;
foreach (var type in module.GetTypes()) {
2012-11-18 10:02:12 +08:00
var cctor = type.FindStaticConstructor();
2011-12-29 15:26:36 +08:00
if (cctor == null)
continue;
if (type.Fields.Count != 1)
continue;
var field = type.Fields[0];
2013-01-19 20:03:57 +08:00
var tmpDelegateType = DotNetUtils.GetType(module, field.FieldType);
2011-12-29 15:26:36 +08:00
if (tmpDelegateType == null)
continue;
2013-01-19 20:03:57 +08:00
if (!CheckDelegateType(tmpDelegateType))
2011-12-29 15:26:36 +08:00
continue;
if (delegateType != null && delegateType != tmpDelegateType)
continue;
2013-01-19 20:03:57 +08:00
if (!CheckCctor(cctor))
2011-12-29 15:26:36 +08:00
continue;
delegateType = tmpDelegateType;
foreach (var method in type.Methods) {
if (method.Name == ".cctor")
continue;
if (!method.IsStatic || method.Body == null)
continue;
2012-11-18 10:02:12 +08:00
var sig = method.MethodSig;
if (sig == null || sig.Params.Count != 0)
2011-12-29 15:26:36 +08:00
continue;
2012-11-18 10:02:12 +08:00
if (sig.RetType.GetElementType() == ElementType.Void)
2011-12-29 15:26:36 +08:00
continue;
2013-01-19 20:03:57 +08:00
var info = GetDecrypterInfo(method, field);
2011-12-29 15:26:36 +08:00
if (info == null)
continue;
2013-01-19 20:03:57 +08:00
decrypterMethods.Add(info.method, info);
2011-12-29 15:26:36 +08:00
}
}
}
2013-01-19 20:03:57 +08:00
Info GetDecrypterInfo(MethodDef method, FieldDef delegateField) {
2011-12-29 15:26:36 +08:00
try {
int index = 0;
var instrs = method.Body.Instructions;
if (instrs[index].OpCode.Code != Code.Ldsfld)
return null;
var field = instrs[index++].Operand as FieldDef;
2011-12-29 15:26:36 +08:00
if (field != delegateField)
return null;
2012-11-18 10:02:12 +08:00
if (!instrs[index].IsLdcI4())
2011-12-29 15:26:36 +08:00
return null;
2012-11-18 10:02:12 +08:00
int offset = instrs[index++].GetLdcI4Value();
2011-12-29 15:26:36 +08:00
if (instrs[index].OpCode.Code != Code.Call && instrs[index].OpCode.Code != Code.Callvirt)
return null;
2012-11-18 10:02:12 +08:00
var calledMethod = instrs[index++].Operand as IMethod;
2011-12-29 15:26:36 +08:00
if (calledMethod.Name != "Invoke")
return null;
if (instrs[index].OpCode.Code == Code.Unbox_Any)
index++;
if (instrs[index++].OpCode.Code != Code.Ret)
return null;
return new Info(method, offset);
}
catch (ArgumentOutOfRangeException) {
return null;
}
}
2013-01-19 20:03:57 +08:00
bool CheckCctor(MethodDef cctor) {
var ldtokenType = GetLdtokenType(cctor);
2012-11-18 10:02:12 +08:00
if (!new SigComparer().Equals(ldtokenType, cctor.DeclaringType))
2011-12-29 15:26:36 +08:00
return false;
MethodDef initMethod = null;
2013-01-19 20:03:57 +08:00
foreach (var method in DotNetUtils.GetCalledMethods(module, cctor)) {
if (DotNetUtils.IsMethod(method, "System.Void", "(System.Type)")) {
2011-12-29 15:26:36 +08:00
initMethod = method;
break;
}
}
if (initMethod == null || initMethod.Body == null)
return false;
return true;
}
2013-01-19 20:03:57 +08:00
static ITypeDefOrRef GetLdtokenType(MethodDef method) {
2011-12-29 15:26:36 +08:00
if (method == null || method.Body == null)
return null;
foreach (var instr in method.Body.Instructions) {
if (instr.OpCode.Code != Code.Ldtoken)
continue;
2012-11-18 10:02:12 +08:00
return instr.Operand as ITypeDefOrRef;
2011-12-29 15:26:36 +08:00
}
return null;
}
2013-01-19 20:03:57 +08:00
bool CheckDelegateType(TypeDef type) {
if (!DotNetUtils.DerivesFromDelegate(type))
2011-12-29 15:26:36 +08:00
return false;
2012-11-18 10:02:12 +08:00
var invoke = type.FindMethod("Invoke");
2011-12-29 15:26:36 +08:00
if (invoke == null)
return false;
2013-01-19 20:03:57 +08:00
return CheckDelegateInvokeMethod(invoke);
2011-12-29 15:26:36 +08:00
}
2013-01-19 20:03:57 +08:00
protected abstract bool CheckDelegateInvokeMethod(MethodDef invokeMethod);
2011-12-29 15:26:36 +08:00
2013-01-19 20:03:57 +08:00
byte[] Decrypt(byte[] encryptedData) {
2011-12-29 15:26:36 +08:00
const int KEY_LEN = 0x100;
if (encryptedData.Length < KEY_LEN)
throw new ApplicationException("Invalid encrypted data length");
var decryptedData = new byte[encryptedData.Length - KEY_LEN];
2012-11-18 10:02:12 +08:00
var pkt = PublicKey.GetRawData(PublicKeyBase.ToPublicKeyToken(module.Assembly.PublicKey));
2011-12-29 15:26:36 +08:00
if (pkt == null || pkt.Length == 0)
pkt = new byte[8];
for (int i = 0, j = 0, ki = 0; i < decryptedData.Length; i++) {
ki = (ki + 1) % (KEY_LEN - 1);
j = (j + encryptedData[ki] + pkt[i % 8]) % (KEY_LEN - 1);
var tmp = encryptedData[j];
encryptedData[j] = encryptedData[ki];
encryptedData[ki] = tmp;
decryptedData[i] = (byte)(encryptedData[KEY_LEN + i] ^ encryptedData[(encryptedData[j] + encryptedData[ki]) % (KEY_LEN - 1)]);
}
return decryptedData;
}
2013-01-19 20:03:57 +08:00
TypeDef FindDelegateInitType() {
2011-12-29 15:26:36 +08:00
if (delegateType == null)
return null;
foreach (var type in module.Types) {
if (type.HasProperties || type.HasEvents || type.HasFields)
continue;
foreach (var method in type.Methods) {
if (!method.IsStatic || method.IsPrivate || method.Body == null)
continue;
2013-01-19 20:03:57 +08:00
var ldtokenType = GetLdtokenType(method);
2011-12-29 15:26:36 +08:00
if (ldtokenType == null)
continue;
2012-11-18 10:02:12 +08:00
if (!new SigComparer().Equals(ldtokenType, delegateType))
2011-12-29 15:26:36 +08:00
continue;
delegateInitType = type;
return delegateInitType;
}
}
return null;
}
2013-01-19 20:03:57 +08:00
public IEnumerable<MethodDef> GetMethods() {
var list = new List<MethodDef>(decrypterMethods.Count);
2013-01-19 20:03:57 +08:00
foreach (var info in decrypterMethods.GetValues())
2011-12-29 15:26:36 +08:00
list.Add(info.method);
return list;
}
}
}