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.Text;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using dnlib.DotNet.Emit;
|
2011-12-29 15:26:36 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.Goliath_NET {
|
|
|
|
|
class StringDecrypter : DecrypterBase {
|
2012-11-18 10:02:12 +08:00
|
|
|
|
IType delegateReturnType;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
FieldDef stringStructField;
|
2012-01-02 14:02:43 +08:00
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public TypeDef StringStruct {
|
2012-01-02 14:02:43 +08:00
|
|
|
|
get { return Detected && stringStructField != null ? stringStructField.DeclaringType : null; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-18 10:02:12 +08:00
|
|
|
|
public StringDecrypter(ModuleDefMD module)
|
2011-12-29 15:26:36 +08:00
|
|
|
|
: base(module) {
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-02 14:02:43 +08:00
|
|
|
|
static string[] requiredFields = new string[] {
|
2011-12-29 15:26:36 +08:00
|
|
|
|
"System.Byte[]",
|
|
|
|
|
"System.Collections.Generic.Dictionary`2<System.Int32,System.String>",
|
2012-01-02 14:02:43 +08:00
|
|
|
|
};
|
2013-01-19 20:03:57 +08:00
|
|
|
|
protected override bool CheckDecrypterType(TypeDef type) {
|
2012-01-02 14:02:43 +08:00
|
|
|
|
var fields = type.Fields;
|
|
|
|
|
if (fields.Count != 2)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (fields[0].FieldType.FullName != "System.Byte[]")
|
|
|
|
|
return false;
|
|
|
|
|
|
2012-11-18 10:02:12 +08:00
|
|
|
|
var dict = fields[1].FieldType.ToGenericInstSig();
|
2012-01-02 14:02:43 +08:00
|
|
|
|
if (dict == null || dict.GenericArguments.Count != 2)
|
|
|
|
|
return false;
|
2012-11-18 10:02:12 +08:00
|
|
|
|
if (dict.GenericType.GetFullName() != "System.Collections.Generic.Dictionary`2")
|
2012-01-02 14:02:43 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (dict.GenericArguments[0].FullName != "System.Int32")
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var garg = dict.GenericArguments[1];
|
|
|
|
|
if (garg.FullName != "System.String") {
|
|
|
|
|
if (!garg.IsValueType)
|
|
|
|
|
return false;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var gargType = DotNetUtils.GetType(module, garg);
|
2012-01-02 14:02:43 +08:00
|
|
|
|
if (gargType == null || !gargType.IsClass)
|
|
|
|
|
return false;
|
|
|
|
|
if (gargType.Fields.Count != 1)
|
|
|
|
|
return false;
|
|
|
|
|
var field = gargType.Fields[0];
|
|
|
|
|
if (field.FieldType.FullName != "System.String")
|
|
|
|
|
return false;
|
|
|
|
|
delegateReturnType = gargType;
|
|
|
|
|
stringStructField = field;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
delegateReturnType = garg;
|
|
|
|
|
stringStructField = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
2011-12-29 15:26:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
protected override bool CheckDelegateInvokeMethod(MethodDef invokeMethod) {
|
|
|
|
|
return DotNetUtils.IsMethod(invokeMethod, delegateReturnType.FullName, "(System.Int32)");
|
2011-12-29 15:26:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public string Decrypt(MethodDef method) {
|
|
|
|
|
var info = GetInfo(method);
|
2011-12-29 15:26:36 +08:00
|
|
|
|
decryptedReader.BaseStream.Position = info.offset;
|
|
|
|
|
int len = decryptedReader.ReadInt32();
|
|
|
|
|
return Encoding.UTF8.GetString(decryptedReader.ReadBytes(len));
|
|
|
|
|
}
|
2012-01-02 14:02:43 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Deobfuscate(Blocks blocks) {
|
2012-01-02 14:02:43 +08:00
|
|
|
|
if (!Detected)
|
|
|
|
|
return;
|
|
|
|
|
if (stringStructField == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
foreach (var block in blocks.MethodBlocks.GetAllBlocks()) {
|
2012-01-02 14:02:43 +08:00
|
|
|
|
var instrs = block.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count - 1; i++) {
|
|
|
|
|
var ldstr = instrs[i];
|
|
|
|
|
if (ldstr.OpCode.Code != Code.Ldstr)
|
|
|
|
|
continue;
|
|
|
|
|
var ldfld = instrs[i + 1];
|
|
|
|
|
if (ldfld.OpCode.Code != Code.Ldfld)
|
|
|
|
|
continue;
|
2012-11-18 10:02:12 +08:00
|
|
|
|
if (!FieldEqualityComparer.CompareDeclaringTypes.Equals(stringStructField, ldfld.Operand as IField))
|
2012-01-02 14:02:43 +08:00
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
block.Remove(i + 1, 1);
|
2012-01-02 14:02:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-12-29 15:26:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|