2012-06-06 17:40:48 +08:00
|
|
|
|
/*
|
2013-01-02 00:03:16 +08:00
|
|
|
|
Copyright (C) 2011-2013 de4dot@gmail.com
|
2012-06-06 17:40:48 +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;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using dnlib.DotNet.Emit;
|
2012-06-06 17:40:48 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.Rummage {
|
|
|
|
|
class StringDecrypter {
|
2012-11-07 00:21:56 +08:00
|
|
|
|
ModuleDefMD module;
|
2012-11-22 16:14:51 +08:00
|
|
|
|
FieldDefAndDeclaringTypeDict<StringInfo> stringInfos = new FieldDefAndDeclaringTypeDict<StringInfo>();
|
2012-12-02 23:20:25 +08:00
|
|
|
|
IDecrypter decrypter;
|
|
|
|
|
|
|
|
|
|
interface IDecrypter {
|
|
|
|
|
RummageVersion Version { get; }
|
|
|
|
|
MethodDef Method { get; }
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void Initialize();
|
|
|
|
|
string Decrypt(int stringId);
|
2012-12-02 23:20:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
abstract class DecrypterBaseV11 : IDecrypter {
|
|
|
|
|
RummageVersion version;
|
|
|
|
|
MethodDef decrypterMethod;
|
|
|
|
|
protected int fileDispl;
|
|
|
|
|
protected BinaryReader reader;
|
|
|
|
|
protected uint[] key;
|
|
|
|
|
|
|
|
|
|
public RummageVersion Version {
|
|
|
|
|
get { return version; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MethodDef Method {
|
|
|
|
|
get { return decrypterMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected DecrypterBaseV11(RummageVersion version, MethodDef decrypterMethod, int fileDispl) {
|
|
|
|
|
this.version = version;
|
|
|
|
|
this.decrypterMethod = decrypterMethod;
|
|
|
|
|
this.fileDispl = fileDispl;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Initialize() {
|
2012-12-02 23:20:25 +08:00
|
|
|
|
reader = new BinaryReader(new FileStream(decrypterMethod.DeclaringType.Module.Location, FileMode.Open, FileAccess.Read, FileShare.Read));
|
2013-01-19 20:03:57 +08:00
|
|
|
|
InitializeImpl();
|
2012-12-02 23:20:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
protected abstract void InitializeImpl();
|
2012-12-02 23:20:25 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
protected static MethodDef FindDecrypterMethod(TypeDef type) {
|
2012-12-02 23:20:25 +08:00
|
|
|
|
MethodDef cctor = null, decrypterMethod = null;
|
|
|
|
|
foreach (var method in type.Methods) {
|
|
|
|
|
if (!method.IsStatic || method.Body == null)
|
|
|
|
|
return null;
|
|
|
|
|
if (method.Name == ".cctor")
|
|
|
|
|
cctor = method;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
else if (DotNetUtils.IsMethod(method, "System.String", "(System.Int32)"))
|
2012-12-02 23:20:25 +08:00
|
|
|
|
decrypterMethod = method;
|
|
|
|
|
else
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if (cctor == null || decrypterMethod == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return decrypterMethod;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public abstract string Decrypt(int stringId);
|
2012-12-02 23:20:25 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
protected string DecryptInternal(int stringId) {
|
2012-12-02 23:20:25 +08:00
|
|
|
|
uint v0 = reader.ReadUInt32();
|
|
|
|
|
uint v1 = reader.ReadUInt32();
|
2013-01-19 20:03:57 +08:00
|
|
|
|
DeobUtils.XteaDecrypt(ref v0, ref v1, key, 32);
|
2012-12-02 23:20:25 +08:00
|
|
|
|
int utf8Length = (int)v0;
|
|
|
|
|
var decrypted = new uint[(utf8Length + 11) / 8 * 2 - 1];
|
|
|
|
|
decrypted[0] = v1;
|
|
|
|
|
for (int i = 1; i + 1 < decrypted.Length; i += 2) {
|
|
|
|
|
v0 = reader.ReadUInt32();
|
|
|
|
|
v1 = reader.ReadUInt32();
|
2013-01-19 20:03:57 +08:00
|
|
|
|
DeobUtils.XteaDecrypt(ref v0, ref v1, key, 32);
|
2012-12-02 23:20:25 +08:00
|
|
|
|
decrypted[i] = v0;
|
|
|
|
|
decrypted[i + 1] = v1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var utf8 = new byte[utf8Length];
|
|
|
|
|
Buffer.BlockCopy(decrypted, 0, utf8, 0, utf8.Length);
|
|
|
|
|
return Encoding.UTF8.GetString(utf8);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class DecrypterV11 : DecrypterBaseV11 {
|
|
|
|
|
DecrypterV11(MethodDef decrypterMethod, int fileDispl)
|
|
|
|
|
: base(RummageVersion.V1_1_445, decrypterMethod, fileDispl) {
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public static DecrypterV11 Create(MethodDef cctor) {
|
|
|
|
|
var method = CheckType(cctor);
|
2012-12-02 23:20:25 +08:00
|
|
|
|
if (method == null)
|
|
|
|
|
return null;
|
|
|
|
|
int fileDispl;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!GetDispl(method, out fileDispl))
|
2012-12-02 23:20:25 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return new DecrypterV11(method, fileDispl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static readonly string[] requiredFields = new string[] {
|
|
|
|
|
"System.UInt32[]",
|
|
|
|
|
};
|
|
|
|
|
static readonly string[] requiredLocals = new string[] {
|
|
|
|
|
"System.Byte[]",
|
|
|
|
|
"System.Int32",
|
|
|
|
|
"System.IO.FileStream",
|
|
|
|
|
};
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static MethodDef CheckType(MethodDef cctor) {
|
2012-12-02 23:20:25 +08:00
|
|
|
|
var type = cctor.DeclaringType;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!new FieldTypes(type).Exactly(requiredFields))
|
2012-12-02 23:20:25 +08:00
|
|
|
|
return null;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!new LocalTypes(cctor).All(requiredLocals))
|
2012-12-02 23:20:25 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
return FindDecrypterMethod(type);
|
2012-12-02 23:20:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static bool GetDispl(MethodDef method, out int displ) {
|
2012-12-02 23:20:25 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count - 2; i++) {
|
|
|
|
|
var mul = instrs[i];
|
|
|
|
|
if (mul.OpCode.Code != Code.Mul)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var ldci4 = instrs[i + 1];
|
|
|
|
|
if (!ldci4.IsLdcI4())
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var sub = instrs[i + 2];
|
|
|
|
|
if (sub.OpCode.Code != Code.Sub)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
displ = ldci4.GetLdcI4Value();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
displ = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
protected override void InitializeImpl() {
|
|
|
|
|
InitKey();
|
2012-12-02 23:20:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void InitKey() {
|
2012-12-02 23:20:25 +08:00
|
|
|
|
reader.BaseStream.Position = reader.BaseStream.Length - 48;
|
|
|
|
|
key = new uint[4];
|
|
|
|
|
for (int i = 0; i < key.Length; i++)
|
|
|
|
|
key[i] = reader.ReadUInt32();
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public override string Decrypt(int stringId) {
|
2012-12-02 23:20:25 +08:00
|
|
|
|
reader.BaseStream.Position = reader.BaseStream.Length + (stringId * 4 - fileDispl);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
return DecryptInternal(stringId);
|
2012-12-02 23:20:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class DecrypterV21 : DecrypterBaseV11 {
|
|
|
|
|
long baseOffs;
|
|
|
|
|
|
|
|
|
|
public DecrypterV21(MethodDef decrypterMethod, int fileDispl)
|
|
|
|
|
: base(RummageVersion.V2_1_729, decrypterMethod, fileDispl) {
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public static DecrypterV21 Create(MethodDef cctor) {
|
|
|
|
|
var method = CheckType(cctor);
|
2012-12-02 23:20:25 +08:00
|
|
|
|
if (method == null)
|
|
|
|
|
return null;
|
|
|
|
|
int fileDispl;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!GetDispl(method, out fileDispl))
|
2012-12-02 23:20:25 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return new DecrypterV21(method, fileDispl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static readonly string[] requiredFields = new string[] {
|
|
|
|
|
"System.UInt32[]",
|
|
|
|
|
"System.Int64",
|
|
|
|
|
};
|
|
|
|
|
static readonly string[] requiredLocals = new string[] {
|
|
|
|
|
"System.Boolean",
|
|
|
|
|
"System.Byte[]",
|
|
|
|
|
"System.Int32",
|
|
|
|
|
"System.IO.FileStream",
|
|
|
|
|
};
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static MethodDef CheckType(MethodDef cctor) {
|
2012-12-02 23:20:25 +08:00
|
|
|
|
var type = cctor.DeclaringType;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!new FieldTypes(type).Exactly(requiredFields))
|
2012-12-02 23:20:25 +08:00
|
|
|
|
return null;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!new LocalTypes(cctor).All(requiredLocals))
|
2012-12-02 23:20:25 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
return FindDecrypterMethod(type);
|
2012-12-02 23:20:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static bool GetDispl(MethodDef method, out int displ) {
|
2012-12-02 23:20:25 +08:00
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count - 6; i++) {
|
|
|
|
|
var ldci4_1 = instrs[i];
|
|
|
|
|
if (!ldci4_1.IsLdcI4() || ldci4_1.GetLdcI4Value() != 4)
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 1].OpCode.Code != Code.Mul)
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 2].OpCode.Code != Code.Conv_I8)
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 3].OpCode.Code != Code.Add)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var ldci4 = instrs[i + 4];
|
|
|
|
|
if (!ldci4.IsLdcI4())
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (instrs[i + 5].OpCode.Code != Code.Conv_I8)
|
|
|
|
|
continue;
|
|
|
|
|
if (instrs[i + 6].OpCode.Code != Code.Sub)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
displ = ldci4.GetLdcI4Value();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
displ = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static readonly byte[] magic = new byte[32] {
|
|
|
|
|
0xC9, 0x76, 0xC3, 0x0D, 0xE2, 0x83, 0x72, 0xE4,
|
|
|
|
|
0xD5, 0xC7, 0x35, 0xF8, 0x86, 0xD0, 0x60, 0x69,
|
|
|
|
|
0xEE, 0xE1, 0x4C, 0x5E, 0x07, 0xA1, 0xC1, 0xFE,
|
|
|
|
|
0x61, 0xE3, 0xAA, 0xBC, 0xE4, 0xB1, 0xF0, 0x92,
|
|
|
|
|
};
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
protected override void InitializeImpl() {
|
|
|
|
|
baseOffs = InitializeBaseOffs();
|
|
|
|
|
InitKey();
|
2012-12-02 23:20:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void InitKey() {
|
2012-12-02 23:20:25 +08:00
|
|
|
|
reader.BaseStream.Position = baseOffs - 16;
|
|
|
|
|
key = new uint[4];
|
|
|
|
|
for (int i = 0; i < key.Length; i++)
|
|
|
|
|
key[i] = reader.ReadUInt32();
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
long InitializeBaseOffs() {
|
2012-12-02 23:20:25 +08:00
|
|
|
|
byte[] buf = new byte[0x1000]; // Must be 4096 bytes
|
|
|
|
|
reader.BaseStream.Position = reader.BaseStream.Length - buf.Length;
|
|
|
|
|
while (true) {
|
|
|
|
|
if (reader.Read(buf, 0, buf.Length) != buf.Length)
|
|
|
|
|
throw new ApplicationException("Could not read");
|
|
|
|
|
|
|
|
|
|
for (int bi = buf.Length - 1; bi > magic.Length; ) {
|
|
|
|
|
int mi = magic.Length - 1;
|
|
|
|
|
if (buf[bi--] != magic[mi--] ||
|
|
|
|
|
buf[bi] != magic[mi--])
|
|
|
|
|
continue;
|
|
|
|
|
while (true) {
|
|
|
|
|
if (buf[--bi] != magic[mi--])
|
|
|
|
|
break;
|
|
|
|
|
if (mi == -1)
|
|
|
|
|
return reader.BaseStream.Position - buf.Length + bi;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reader.BaseStream.Position -= buf.Length * 2 - 0x20;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public override string Decrypt(int stringId) {
|
2012-12-02 23:20:25 +08:00
|
|
|
|
reader.BaseStream.Position = baseOffs + stringId * 4 - fileDispl;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
return DecryptInternal(stringId);
|
2012-12-02 23:20:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-06-06 17:40:48 +08:00
|
|
|
|
|
|
|
|
|
class StringInfo {
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public readonly FieldDef field;
|
2012-06-06 17:40:48 +08:00
|
|
|
|
public readonly int stringId;
|
|
|
|
|
public string decrypted;
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public StringInfo(FieldDef field, int stringId) {
|
2012-06-06 17:40:48 +08:00
|
|
|
|
this.field = field;
|
|
|
|
|
this.stringId = stringId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString() {
|
|
|
|
|
if (decrypted != null)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
return string.Format("{0:X8} - {1}", stringId, Utils.ToCsharpString(decrypted));
|
2012-06-06 17:40:48 +08:00
|
|
|
|
return string.Format("{0:X8}", stringId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-02 23:20:25 +08:00
|
|
|
|
public RummageVersion Version {
|
|
|
|
|
get { return decrypter == null ? RummageVersion.Unknown : decrypter.Version; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public TypeDef Type {
|
2012-12-02 23:20:25 +08:00
|
|
|
|
get { return decrypter != null ? decrypter.Method.DeclaringType : null; }
|
2012-06-06 17:40:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public IEnumerable<TypeDef> OtherTypes {
|
2012-06-06 17:40:48 +08:00
|
|
|
|
get {
|
2012-11-02 22:57:11 +08:00
|
|
|
|
var list = new List<TypeDef>(stringInfos.Count);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
foreach (var info in stringInfos.GetValues())
|
2012-06-06 17:40:48 +08:00
|
|
|
|
list.Add(info.field.DeclaringType);
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Detected {
|
2012-12-02 23:20:25 +08:00
|
|
|
|
get { return decrypter != null; }
|
2012-06-06 17:40:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-07 00:21:56 +08:00
|
|
|
|
public StringDecrypter(ModuleDefMD module) {
|
2012-06-06 17:40:48 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Find() {
|
2012-12-02 23:20:25 +08:00
|
|
|
|
foreach (var type in module.GetTypes()) {
|
|
|
|
|
var cctor = type.FindStaticConstructor();
|
|
|
|
|
if (cctor == null)
|
2012-06-06 17:40:48 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
decrypter = DecrypterV11.Create(cctor);
|
2012-12-02 23:20:25 +08:00
|
|
|
|
if (decrypter != null)
|
|
|
|
|
break;
|
2012-06-06 17:40:48 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
decrypter = DecrypterV21.Create(cctor);
|
2012-12-02 23:20:25 +08:00
|
|
|
|
if (decrypter != null)
|
|
|
|
|
break;
|
2012-06-06 17:40:48 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Initialize() {
|
2012-12-02 23:20:25 +08:00
|
|
|
|
if (decrypter == null)
|
|
|
|
|
return;
|
2012-06-06 17:40:48 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
decrypter.Initialize();
|
2012-06-06 17:40:48 +08:00
|
|
|
|
foreach (var type in module.Types)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
InitType(type);
|
2012-06-06 17:40:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void InitType(TypeDef type) {
|
2012-11-17 06:50:52 +08:00
|
|
|
|
var cctor = type.FindStaticConstructor();
|
2012-06-06 17:40:48 +08:00
|
|
|
|
if (cctor == null)
|
|
|
|
|
return;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var info = GetStringInfo(cctor);
|
2012-06-06 17:40:48 +08:00
|
|
|
|
if (info == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
stringInfos.Add(info.field, info);
|
2012-06-06 17:40:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
StringInfo GetStringInfo(MethodDef method) {
|
2012-06-06 17:40:48 +08:00
|
|
|
|
if (method == null || method.Body == null)
|
|
|
|
|
return null;
|
|
|
|
|
var instrs = method.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count - 2; i++) {
|
|
|
|
|
var ldci4 = instrs[i];
|
2012-11-07 00:21:56 +08:00
|
|
|
|
if (!ldci4.IsLdcI4())
|
2012-06-06 17:40:48 +08:00
|
|
|
|
continue;
|
2012-11-07 00:21:56 +08:00
|
|
|
|
int stringId = ldci4.GetLdcI4Value();
|
2012-06-06 17:40:48 +08:00
|
|
|
|
|
|
|
|
|
var call = instrs[i + 1];
|
|
|
|
|
if (call.OpCode.Code != Code.Call)
|
|
|
|
|
continue;
|
2012-11-07 00:21:56 +08:00
|
|
|
|
var calledMethod = call.Operand as IMethod;
|
2012-12-02 23:20:25 +08:00
|
|
|
|
if (!MethodEqualityComparer.CompareDeclaringTypes.Equals(decrypter.Method, calledMethod))
|
2012-06-06 17:40:48 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var stsfld = instrs[i + 2];
|
|
|
|
|
if (stsfld.OpCode.Code != Code.Stsfld)
|
|
|
|
|
continue;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
var field = stsfld.Operand as FieldDef;
|
2012-06-06 17:40:48 +08:00
|
|
|
|
if (field == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return new StringInfo(field, stringId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Deobfuscate(Blocks blocks) {
|
2012-12-02 23:20:25 +08:00
|
|
|
|
if (decrypter == null)
|
|
|
|
|
return;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
foreach (var block in blocks.MethodBlocks.GetAllBlocks()) {
|
2012-06-06 17:40:48 +08:00
|
|
|
|
var instrs = block.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count; i++) {
|
|
|
|
|
var instr = instrs[i];
|
|
|
|
|
|
|
|
|
|
if (instr.OpCode.Code != Code.Ldsfld)
|
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-07 00:21:56 +08:00
|
|
|
|
var field = instr.Operand as IField;
|
2012-06-06 17:40:48 +08:00
|
|
|
|
if (field == null)
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var info = stringInfos.Find(field);
|
2012-06-06 17:40:48 +08:00
|
|
|
|
if (info == null)
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var decrypted = Decrypt(info);
|
2012-06-06 17:40:48 +08:00
|
|
|
|
|
2012-12-16 07:03:56 +08:00
|
|
|
|
instrs[i] = new Instr(OpCodes.Ldstr.ToInstruction(decrypted));
|
2013-01-19 20:03:57 +08:00
|
|
|
|
Logger.v("Decrypted string: {0}", Utils.ToCsharpString(decrypted));
|
2012-06-06 17:40:48 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
string Decrypt(StringInfo info) {
|
2012-06-06 17:40:48 +08:00
|
|
|
|
if (info.decrypted == null)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
info.decrypted = decrypter.Decrypt(info.stringId);
|
2012-06-06 17:40:48 +08:00
|
|
|
|
|
|
|
|
|
return info.decrypted;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|