de4dot-cex/de4dot.code/deobfuscators/CodeWall/StringDecrypter.cs

320 lines
8.8 KiB
C#
Raw Normal View History

2012-05-26 20:38:08 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
2012-05-26 20:38:08 +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 System.Text;
using dnlib.IO;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
2012-05-26 20:38:08 +08:00
using de4dot.blocks;
namespace de4dot.code.deobfuscators.CodeWall {
class StringDecrypter {
2012-11-08 17:40:58 +08:00
ModuleDefMD module;
2012-11-22 16:14:51 +08:00
MethodDefAndDeclaringTypeDict<StringEncrypterInfo> stringEncrypterInfos = new MethodDefAndDeclaringTypeDict<StringEncrypterInfo>();
2012-05-27 13:00:13 +08:00
Version version;
2012-05-26 20:38:08 +08:00
2012-05-27 13:00:13 +08:00
public enum Version {
Unknown,
V30, // 3.0 - 3.5
V36, // 3.6 - 4.1
}
2012-05-26 20:38:08 +08:00
public class StringEncrypterInfo {
MethodDef method;
2012-05-26 20:38:08 +08:00
public TypeDef Type {
2012-05-26 20:38:08 +08:00
get { return method.DeclaringType; }
}
public MethodDef Method {
2012-05-26 20:38:08 +08:00
get { return method; }
}
public EmbeddedResource Resource { get; set; }
public int Magic1 { get; set; }
public int Magic2 { get; set; }
public int Magic3 { get; set; }
2012-11-08 17:40:58 +08:00
public IBinaryReader Reader { get; set; }
2012-05-26 20:38:08 +08:00
public StringEncrypterInfo(MethodDef method) {
2012-05-26 20:38:08 +08:00
this.method = method;
}
2013-01-19 20:03:57 +08:00
public string Decrypt(int magic1, int magic2, int magic3) {
2012-05-26 20:38:08 +08:00
int dataLen = magic3 ^ Magic3;
2013-01-19 20:03:57 +08:00
var key = GetKey(magic1 ^ Magic1, dataLen);
Reader.Position = GetDataOffset(magic2);
2012-05-26 20:38:08 +08:00
var data = Reader.ReadBytes(dataLen);
for (int i = 0; i < dataLen; i++)
data[i] ^= key[i];
return Encoding.Unicode.GetString(data);
}
2013-01-19 20:03:57 +08:00
byte[] GetKey(int seed, int keyLen) {
2012-05-26 20:38:08 +08:00
var random = new Random(seed);
var key = new byte[keyLen];
random.NextBytes(key);
return key;
}
2013-01-19 20:03:57 +08:00
int GetDataOffset(int magic2) {
var pkt = GetPublicKeyToken();
2012-05-26 20:38:08 +08:00
if (pkt == null)
return magic2 ^ Magic2;
else
return magic2 ^ BitConverter.ToInt32(pkt, 0) ^ BitConverter.ToInt32(pkt, 4);
}
2013-01-19 20:03:57 +08:00
byte[] GetPublicKeyToken() {
2012-11-17 06:50:52 +08:00
var module = method.Module;
2012-11-08 17:40:58 +08:00
if (module.Assembly == null || PublicKeyBase.IsNullOrEmpty2(module.Assembly.PublicKey))
2012-05-26 20:38:08 +08:00
return null;
2012-11-08 17:40:58 +08:00
return module.Assembly.PublicKeyToken.Data;
2012-05-26 20:38:08 +08:00
}
public override string ToString() {
return string.Format("{0:X8} M1:{1:X8} M2:{2:X8} M3:{3:X8}",
Method.MDToken.ToInt32(),
2012-05-26 20:38:08 +08:00
Magic1, Magic2, Magic3);
}
}
public bool Detected {
get { return stringEncrypterInfos.Count != 0; }
}
2012-05-27 13:00:13 +08:00
public Version TheVersion {
get { return version; }
}
2012-05-26 20:38:08 +08:00
public IEnumerable<StringEncrypterInfo> Infos {
get {
var list = new List<StringEncrypterInfo>();
2013-01-19 20:03:57 +08:00
foreach (var info in stringEncrypterInfos.GetValues()) {
if (info.Resource != null)
list.Add(info);
}
return list;
}
2012-05-26 20:38:08 +08:00
}
2012-11-08 17:40:58 +08:00
public StringDecrypter(ModuleDefMD module) {
2012-05-26 20:38:08 +08:00
this.module = module;
}
2013-01-19 20:03:57 +08:00
public void Find() {
2012-05-26 20:38:08 +08:00
foreach (var type in module.Types) {
MethodDef decrypterMethod;
2013-01-19 20:03:57 +08:00
var decrypterVersion = CheckType(type, out decrypterMethod);
2012-05-27 13:00:13 +08:00
if (decrypterVersion == Version.Unknown)
2012-05-26 20:38:08 +08:00
continue;
2012-05-27 13:00:13 +08:00
version = decrypterVersion;
2013-01-19 20:03:57 +08:00
stringEncrypterInfos.Add(decrypterMethod, new StringEncrypterInfo(decrypterMethod));
2012-05-26 20:38:08 +08:00
}
}
2013-01-19 20:03:57 +08:00
Version CheckType(TypeDef type, out MethodDef decrypterMethod) {
MethodDef method;
2013-01-19 20:03:57 +08:00
if ((method = CheckTypeV30(type)) != null) {
decrypterMethod = method;
return Version.V30;
}
2013-01-19 20:03:57 +08:00
if ((method = CheckTypeV36(type)) != null) {
decrypterMethod = method;
return Version.V36;
}
decrypterMethod = null;
return Version.Unknown;
}
static readonly string[] requiredTypes_v30 = new string[] {
"System.Collections.Generic.Dictionary`2<System.Int32,System.String>",
};
static readonly string[] requiredLocals_v30 = new string[] {
"System.Int32",
"System.Byte[]",
"System.Reflection.Assembly",
"System.IO.Stream",
"System.Random",
"System.String",
};
2013-01-19 20:03:57 +08:00
MethodDef CheckTypeV30(TypeDef type) {
MethodDef decrypterMethod = CheckMethodsV30(type);
if (decrypterMethod == null)
return null;
2013-01-19 20:03:57 +08:00
if (!new FieldTypes(type).Exactly(requiredTypes_v30))
return null;
2013-01-19 20:03:57 +08:00
if (!new LocalTypes(decrypterMethod).Exactly(requiredLocals_v30))
return null;
return decrypterMethod;
}
2013-01-19 20:03:57 +08:00
static MethodDef CheckMethodsV30(TypeDef type) {
if (type.Methods.Count < 1 || type.Methods.Count > 2)
return null;
MethodDef decrypterMethod = null;
MethodDef cctor = null;
foreach (var method in type.Methods) {
if (method.Name == ".cctor") {
cctor = method;
continue;
}
if (decrypterMethod != null)
return null;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.IsMethod(method, "System.String", "(System.Int32,System.Int32,System.Int32)"))
return null;
decrypterMethod = method;
}
if (decrypterMethod == null || !decrypterMethod.IsStatic)
return null;
return decrypterMethod;
}
static readonly string[] requiredTypes_v36 = new string[] {
2012-05-26 20:38:08 +08:00
"System.Object",
"System.Collections.Generic.Dictionary`2<System.Int32,System.String>",
};
static readonly string[] requiredLocals_v36 = new string[] {
2012-05-26 20:38:08 +08:00
"System.Int32",
"System.Byte[]",
"System.Reflection.Assembly",
"System.IO.Stream",
"System.Random",
"System.String",
"System.Object",
};
2013-01-19 20:03:57 +08:00
MethodDef CheckTypeV36(TypeDef type) {
MethodDef decrypterMethod = CheckMethodsV36(type);
2012-05-26 20:38:08 +08:00
if (decrypterMethod == null)
return null;
2013-01-19 20:03:57 +08:00
if (!new FieldTypes(type).Exactly(requiredTypes_v36))
2012-05-26 20:38:08 +08:00
return null;
2013-01-19 20:03:57 +08:00
if (!new LocalTypes(decrypterMethod).Exactly(requiredLocals_v36))
2012-05-26 20:38:08 +08:00
return null;
return decrypterMethod;
}
2013-01-19 20:03:57 +08:00
static MethodDef CheckMethodsV36(TypeDef type) {
2012-05-26 20:38:08 +08:00
if (type.Methods.Count != 2)
return null;
MethodDef decrypterMethod = null;
MethodDef cctor = null;
2012-05-26 20:38:08 +08:00
foreach (var method in type.Methods) {
if (method.Name == ".cctor") {
cctor = method;
continue;
}
if (decrypterMethod != null)
return null;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.IsMethod(method, "System.String", "(System.Int32,System.Int32,System.Int32)"))
2012-05-26 20:38:08 +08:00
return null;
decrypterMethod = method;
}
if (cctor == null)
return null;
if (decrypterMethod == null || !decrypterMethod.IsStatic)
return null;
return decrypterMethod;
}
2013-01-19 20:03:57 +08:00
public void Initialize(ISimpleDeobfuscator simpleDeobfuscator) {
foreach (var info in stringEncrypterInfos.GetValues()) {
simpleDeobfuscator.Deobfuscate(info.Method);
info.Resource = FindResource(info.Method);
if (info.Resource == null) {
Logger.w("Could not find encrypted strings resource (Method {0:X8})", info.Method.MDToken.ToInt32());
continue;
}
2013-01-19 20:03:57 +08:00
info.Magic1 = FindMagic1(info.Method);
info.Magic2 = FindMagic2(info.Method);
info.Magic3 = FindMagic3(info.Method);
2012-11-08 17:40:58 +08:00
info.Reader = info.Resource.Data;
2012-11-20 08:15:27 +08:00
info.Reader.Position = 0;
2012-05-26 20:38:08 +08:00
}
}
2013-01-19 20:03:57 +08:00
EmbeddedResource FindResource(MethodDef method) {
return DotNetUtils.GetResource(module, DotNetUtils.GetCodeStrings(method)) as EmbeddedResource;
2012-05-26 20:38:08 +08:00
}
2013-01-19 20:03:57 +08:00
static int FindMagic1(MethodDef method) {
2012-05-26 20:38:08 +08:00
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count - 2; i++) {
var ldarg = instrs[i];
2012-11-08 17:40:58 +08:00
if (!ldarg.IsLdarg() || ldarg.GetParameterIndex() != 0)
2012-05-26 20:38:08 +08:00
continue;
var ldci4 = instrs[i + 1];
2012-11-08 17:40:58 +08:00
if (!ldci4.IsLdcI4())
2012-05-26 20:38:08 +08:00
continue;
if (instrs[i + 2].OpCode.Code != Code.Xor)
continue;
2012-11-08 17:40:58 +08:00
return ldci4.GetLdcI4Value();
2012-05-26 20:38:08 +08:00
}
throw new ApplicationException("Could not find magic1");
}
2013-01-19 20:03:57 +08:00
static int FindMagic2(MethodDef method) {
2012-05-26 20:38:08 +08:00
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count - 2; i++) {
var ldloc = instrs[i];
2012-11-08 17:40:58 +08:00
if (!ldloc.IsLdloc())
2012-05-26 20:38:08 +08:00
continue;
var ldci4 = instrs[i + 1];
2012-11-08 17:40:58 +08:00
if (!ldci4.IsLdcI4())
2012-05-26 20:38:08 +08:00
continue;
if (instrs[i + 2].OpCode.Code != Code.Xor)
continue;
2012-11-08 17:40:58 +08:00
return ldci4.GetLdcI4Value();
2012-05-26 20:38:08 +08:00
}
throw new ApplicationException("Could not find magic2");
}
2013-01-19 20:03:57 +08:00
static int FindMagic3(MethodDef method) {
2012-05-26 20:38:08 +08:00
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count - 2; i++) {
var ldarg = instrs[i];
2012-11-08 17:40:58 +08:00
if (!ldarg.IsLdarg() || ldarg.GetParameterIndex() != 2)
2012-05-26 20:38:08 +08:00
continue;
var ldci4 = instrs[i + 1];
2012-11-08 17:40:58 +08:00
if (!ldci4.IsLdcI4())
2012-05-26 20:38:08 +08:00
continue;
if (instrs[i + 2].OpCode.Code != Code.Xor)
continue;
2012-11-08 17:40:58 +08:00
return ldci4.GetLdcI4Value();
2012-05-26 20:38:08 +08:00
}
throw new ApplicationException("Could not find magic3");
}
2013-01-19 20:03:57 +08:00
public string Decrypt(MethodDef method, int magic1, int magic2, int magic3) {
var info = stringEncrypterInfos.Find(method);
return info.Decrypt(magic1, magic2, magic3);
2012-05-26 20:38:08 +08:00
}
}
}