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

587 lines
17 KiB
C#
Raw Normal View History

2012-01-08 03:27:07 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
2012-01-08 03:27:07 +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/>.
*/
2012-01-14 17:37:15 +08:00
using System;
2012-01-08 03:27:07 +08:00
using System.Collections.Generic;
using System.IO;
2012-01-14 17:37:15 +08:00
using System.Text;
using dnlib.IO;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
2012-01-08 03:27:07 +08:00
using de4dot.blocks;
2012-06-12 16:37:51 +08:00
using de4dot.blocks.cflow;
2012-01-08 03:27:07 +08:00
namespace de4dot.code.deobfuscators.Babel_NET {
class StringDecrypter {
2012-11-08 14:06:46 +08:00
ModuleDefMD module;
2012-06-12 16:37:51 +08:00
ResourceDecrypter resourceDecrypter;
2012-06-07 03:16:18 +08:00
ISimpleDeobfuscator simpleDeobfuscator;
TypeDef decrypterType;
2012-01-08 03:27:07 +08:00
EmbeddedResource encryptedResource;
2012-01-14 17:37:15 +08:00
IDecrypterInfo decrypterInfo;
interface IDecrypterInfo {
MethodDef Decrypter { get; }
2012-01-14 17:37:15 +08:00
bool NeedsResource { get; }
2013-01-19 20:03:57 +08:00
void Initialize(ModuleDefMD module, EmbeddedResource resource);
string Decrypt(object[] args);
2012-01-14 17:37:15 +08:00
}
2012-01-19 12:38:58 +08:00
// Babel .NET 2.x
2012-01-14 17:37:15 +08:00
class DecrypterInfoV1 : IDecrypterInfo {
public MethodDef Decrypter { get; set; }
2012-01-19 12:38:58 +08:00
public bool NeedsResource {
get { return false; }
}
2013-01-19 20:03:57 +08:00
public void Initialize(ModuleDefMD module, EmbeddedResource resource) {
2012-01-19 12:38:58 +08:00
}
2013-01-19 20:03:57 +08:00
public string Decrypt(object[] args) {
return Decrypt((string)args[0], (int)args[1]);
2012-01-19 12:38:58 +08:00
}
2013-01-19 20:03:57 +08:00
string Decrypt(string s, int k) {
2012-01-19 12:38:58 +08:00
var sb = new StringBuilder(s.Length);
foreach (var c in s)
sb.Append((char)(c ^ k));
return sb.ToString();
}
}
class DecrypterInfoV2 : IDecrypterInfo {
2012-01-14 17:37:15 +08:00
byte[] key;
public MethodDef Decrypter { get; set; }
2012-01-14 17:37:15 +08:00
public bool NeedsResource {
get { return true; }
}
2013-01-19 20:03:57 +08:00
public void Initialize(ModuleDefMD module, EmbeddedResource resource) {
2012-11-08 14:06:46 +08:00
key = resource.Data.ReadAllBytes();
2012-01-14 17:37:15 +08:00
if (key.Length != 0x100)
throw new ApplicationException(string.Format("Unknown key length: {0}", key.Length));
}
2013-01-19 20:03:57 +08:00
public string Decrypt(object[] args) {
return Decrypt((string)args[0], (int)args[1]);
2012-01-14 17:37:15 +08:00
}
2013-01-19 20:03:57 +08:00
string Decrypt(string s, int k) {
2012-01-14 17:37:15 +08:00
var sb = new StringBuilder(s.Length);
byte b = key[(byte)k];
foreach (var c in s)
sb.Append((char)(c ^ (b | k)));
return sb.ToString();
}
}
2012-01-19 12:38:58 +08:00
class DecrypterInfoV3 : IDecrypterInfo {
2012-01-14 17:37:15 +08:00
Dictionary<int, string> offsetToString = new Dictionary<int, string>();
2012-06-12 16:37:51 +08:00
ResourceDecrypter resourceDecrypter;
InstructionEmulator emulator = new InstructionEmulator();
2012-01-14 17:37:15 +08:00
2012-06-12 16:37:51 +08:00
public IList<Instruction> OffsetCalcInstructions { get; set; }
public MethodDef Decrypter { get; set; }
2012-01-14 17:37:15 +08:00
public bool NeedsResource {
get { return true; }
}
2012-06-12 16:37:51 +08:00
public DecrypterInfoV3(ResourceDecrypter resourceDecrypter) {
this.resourceDecrypter = resourceDecrypter;
}
2013-01-19 20:03:57 +08:00
public void Initialize(ModuleDefMD module, EmbeddedResource resource) {
var decrypted = resourceDecrypter.Decrypt(resource.Data.ReadAllBytes());
2012-01-14 17:37:15 +08:00
var reader = new BinaryReader(new MemoryStream(decrypted));
while (reader.BaseStream.Position < reader.BaseStream.Length)
2013-01-19 20:03:57 +08:00
offsetToString[GetOffset((int)reader.BaseStream.Position)] = reader.ReadString();
2012-06-12 16:37:51 +08:00
}
MethodDef dummyMethod;
2013-01-19 20:03:57 +08:00
int GetOffset(int offset) {
2012-06-12 16:37:51 +08:00
if (OffsetCalcInstructions == null || OffsetCalcInstructions.Count == 0)
return offset;
if (dummyMethod == null) {
2012-11-08 14:06:46 +08:00
dummyMethod = new MethodDefUser();
dummyMethod.Body = new CilBody();
2012-06-12 16:37:51 +08:00
}
2013-01-19 20:03:57 +08:00
emulator.Initialize(dummyMethod);
emulator.Push(new Int32Value(offset));
2012-06-12 16:37:51 +08:00
foreach (var instr in OffsetCalcInstructions)
2013-01-19 20:03:57 +08:00
emulator.Emulate(instr);
return ((Int32Value)emulator.Pop()).Value;
2012-01-14 17:37:15 +08:00
}
2013-01-19 20:03:57 +08:00
public string Decrypt(object[] args) {
return Decrypt((int)args[0]);
2012-01-14 17:37:15 +08:00
}
2013-01-19 20:03:57 +08:00
string Decrypt(int offset) {
2012-06-12 16:37:51 +08:00
return offsetToString[offset];
2012-01-14 17:37:15 +08:00
}
}
2012-01-08 03:27:07 +08:00
public bool Detected {
get { return decrypterType != null; }
}
public TypeDef Type {
2012-01-08 03:27:07 +08:00
get { return decrypterType; }
}
public MethodDef DecryptMethod {
2012-01-14 17:37:15 +08:00
get { return decrypterInfo == null ? null : decrypterInfo.Decrypter; }
2012-01-08 03:27:07 +08:00
}
public EmbeddedResource Resource {
get { return encryptedResource; }
}
2012-11-08 14:06:46 +08:00
public StringDecrypter(ModuleDefMD module, ResourceDecrypter resourceDecrypter) {
2012-01-08 03:27:07 +08:00
this.module = module;
2012-06-12 16:37:51 +08:00
this.resourceDecrypter = resourceDecrypter;
2012-01-08 03:27:07 +08:00
}
2013-01-19 20:03:57 +08:00
public void Find(ISimpleDeobfuscator simpleDeobfuscator) {
2012-06-07 03:16:18 +08:00
this.simpleDeobfuscator = simpleDeobfuscator;
2012-01-08 03:27:07 +08:00
foreach (var type in module.Types) {
2013-01-19 20:03:57 +08:00
var info = CheckDecrypterType(type);
2012-01-14 17:37:15 +08:00
if (info == null)
2012-01-08 03:27:07 +08:00
continue;
decrypterType = type;
2012-01-14 17:37:15 +08:00
decrypterInfo = info;
2012-01-08 03:27:07 +08:00
return;
}
}
2013-01-19 20:03:57 +08:00
IDecrypterInfo CheckDecrypterType(TypeDef type) {
2012-01-08 03:27:07 +08:00
if (type.HasEvents)
2012-01-14 17:37:15 +08:00
return null;
if (type.NestedTypes.Count > 2)
return null;
if (type.Fields.Count > 1)
return null;
foreach (var nested in type.NestedTypes) {
2013-01-19 20:03:57 +08:00
var info = CheckNested(type, nested);
2012-01-14 17:37:15 +08:00
if (info != null)
return info;
}
2012-01-19 12:38:58 +08:00
2013-01-19 20:03:57 +08:00
return CheckDecrypterTypeBabel2x(type);
2012-01-19 12:38:58 +08:00
}
2013-01-19 20:03:57 +08:00
IDecrypterInfo CheckDecrypterTypeBabel2x(TypeDef type) {
2012-01-19 12:38:58 +08:00
if (type.HasEvents || type.HasProperties || type.HasNestedTypes)
return null;
if (type.HasFields || type.Methods.Count != 1)
return null;
var decrypter = type.Methods[0];
2013-01-19 20:03:57 +08:00
if (!CheckDecryptMethodBabel2x(decrypter))
2012-01-19 12:38:58 +08:00
return null;
return new DecrypterInfoV1 { Decrypter = decrypter };
}
2013-01-19 20:03:57 +08:00
bool CheckDecryptMethodBabel2x(MethodDef method) {
2012-01-19 12:38:58 +08:00
if (!method.IsStatic || !method.IsPublic)
return false;
if (method.Body == null)
return false;
if (method.Name == ".cctor")
return false;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.IsMethod(method, "System.String", "(System.String,System.Int32)"))
2012-01-19 12:38:58 +08:00
return false;
int stringLength = 0, stringToCharArray = 0, stringCtor = 0;
foreach (var instr in method.Body.Instructions) {
2012-11-08 14:06:46 +08:00
var calledMethod = instr.Operand as IMethod;
2012-01-19 12:38:58 +08:00
if (calledMethod == null)
continue;
switch (instr.OpCode.Code) {
case Code.Call:
case Code.Callvirt:
if (calledMethod.FullName == "System.Int32 System.String::get_Length()")
stringLength++;
else if (calledMethod.FullName == "System.Char[] System.String::ToCharArray()")
stringToCharArray++;
else
return false;
break;
case Code.Newobj:
if (calledMethod.FullName == "System.Void System.String::.ctor(System.Char[])")
stringCtor++;
else
return false;
break;
default:
continue;
}
}
return stringLength == 1 && stringToCharArray == 1 && stringCtor == 1;
2012-01-14 17:37:15 +08:00
}
2012-01-08 03:27:07 +08:00
2013-01-19 20:03:57 +08:00
IDecrypterInfo CheckNested(TypeDef type, TypeDef nested) {
2012-01-08 03:27:07 +08:00
if (nested.HasProperties || nested.HasEvents)
2012-01-14 17:37:15 +08:00
return null;
2012-11-08 14:06:46 +08:00
if (nested.FindMethod(".ctor") == null)
2012-01-14 17:37:15 +08:00
return null;
2012-06-12 16:37:51 +08:00
if (nested.Fields.Count == 1 || nested.Fields.Count == 3) {
2012-01-14 17:37:15 +08:00
// 4.0+
2013-01-19 20:03:57 +08:00
if (!HasFieldType(nested.Fields, nested))
2012-01-14 17:37:15 +08:00
return null;
2013-01-19 20:03:57 +08:00
var decrypterBuilderMethod = DotNetUtils.GetMethod(nested, "System.Reflection.Emit.MethodBuilder", "(System.Reflection.Emit.TypeBuilder)");
2012-06-07 03:16:18 +08:00
if (decrypterBuilderMethod == null)
2012-01-14 17:37:15 +08:00
return null;
2013-01-19 20:03:57 +08:00
resourceDecrypter.DecryptMethod = ResourceDecrypter.FindDecrypterMethod(nested.FindMethod(".ctor"));
2012-06-12 16:37:51 +08:00
2013-01-19 20:03:57 +08:00
var nestedDecrypter = DotNetUtils.GetMethod(nested, "System.String", "(System.Int32)");
2012-01-14 17:37:15 +08:00
if (nestedDecrypter == null || nestedDecrypter.IsStatic)
return null;
2013-01-19 20:03:57 +08:00
var decrypter = DotNetUtils.GetMethod(type, "System.String", "(System.Int32)");
2012-01-14 17:37:15 +08:00
if (decrypter == null || !decrypter.IsStatic)
return null;
2013-01-19 20:03:57 +08:00
simpleDeobfuscator.Deobfuscate(decrypterBuilderMethod);
2012-06-12 16:37:51 +08:00
return new DecrypterInfoV3(resourceDecrypter) {
2012-06-07 03:16:18 +08:00
Decrypter = decrypter,
2013-01-19 20:03:57 +08:00
OffsetCalcInstructions = GetOffsetCalcInstructions(decrypterBuilderMethod),
2012-06-07 03:16:18 +08:00
};
}
else if (nested.Fields.Count == 2) {
2012-01-14 17:37:15 +08:00
// 3.0 - 3.5
2013-01-19 20:03:57 +08:00
if (CheckFields(nested, "System.Collections.Hashtable", nested)) {
2012-06-12 16:37:51 +08:00
// 3.0 - 3.5
2013-01-19 20:03:57 +08:00
var nestedDecrypter = DotNetUtils.GetMethod(nested, "System.String", "(System.Int32)");
2012-01-14 17:37:15 +08:00
if (nestedDecrypter == null || nestedDecrypter.IsStatic)
return null;
2013-01-19 20:03:57 +08:00
var decrypter = DotNetUtils.GetMethod(type, "System.String", "(System.Int32)");
2012-01-14 17:37:15 +08:00
if (decrypter == null || !decrypter.IsStatic)
return null;
2013-01-19 20:03:57 +08:00
resourceDecrypter.DecryptMethod = ResourceDecrypter.FindDecrypterMethod(nested.FindMethod(".ctor"));
2012-06-12 16:37:51 +08:00
return new DecrypterInfoV3(resourceDecrypter) { Decrypter = decrypter };
2012-01-14 17:37:15 +08:00
}
2013-01-19 20:03:57 +08:00
else if (CheckFields(nested, "System.Byte[]", nested)) {
2012-01-14 17:37:15 +08:00
// 3.0
2013-01-19 20:03:57 +08:00
var nestedDecrypter = DotNetUtils.GetMethod(nested, "System.String", "(System.String,System.Int32)");
2012-01-14 17:37:15 +08:00
if (nestedDecrypter == null || nestedDecrypter.IsStatic)
return null;
2013-01-19 20:03:57 +08:00
var decrypter = DotNetUtils.GetMethod(type, "System.String", "(System.String,System.Int32)");
2012-01-14 17:37:15 +08:00
if (decrypter == null || !decrypter.IsStatic)
return null;
2012-01-19 12:38:58 +08:00
return new DecrypterInfoV2 { Decrypter = decrypter };
2012-01-14 17:37:15 +08:00
}
else
return null;
}
2012-01-08 03:27:07 +08:00
2012-01-14 17:37:15 +08:00
return null;
}
2013-01-19 20:03:57 +08:00
class ReflectionToDNLibMethodCreator {
MethodDef method;
2012-06-12 16:37:51 +08:00
List<Instruction> instructions = new List<Instruction>();
InstructionEmulator emulator;
int index;
class UserValue : UnknownValue {
public readonly object obj;
public UserValue(object obj) {
this.obj = obj;
}
public override string ToString() {
if (obj == null)
return "<null>";
return obj.ToString();
}
}
public List<Instruction> Instructions {
get { return instructions; }
}
2013-01-19 20:03:57 +08:00
public ReflectionToDNLibMethodCreator(MethodDef method) {
2012-06-12 16:37:51 +08:00
this.method = method;
this.emulator = new InstructionEmulator(method);
}
2013-01-19 20:03:57 +08:00
public bool Create() {
2012-06-12 16:37:51 +08:00
int arrayIndex;
Value array;
object value;
while (true) {
var instr = method.Body.Instructions[index];
switch (instr.OpCode.Code) {
case Code.Ret:
return true;
case Code.Newarr:
2012-11-08 14:06:46 +08:00
var arrayType = (ITypeDefOrRef)instr.Operand;
int arrayCount = ((Int32Value)emulator.Pop()).Value;
2012-06-12 16:37:51 +08:00
if (arrayType.FullName == "System.Char")
2013-01-19 20:03:57 +08:00
emulator.Push(new UserValue(new char[arrayCount]));
2012-06-12 16:37:51 +08:00
else
2013-01-19 20:03:57 +08:00
emulator.Push(new UnknownValue());
2012-06-12 16:37:51 +08:00
break;
case Code.Call:
case Code.Callvirt:
2013-01-19 20:03:57 +08:00
if (!DoCall(instr))
2012-06-12 16:37:51 +08:00
return false;
break;
case Code.Ldelem_U1:
arrayIndex = ((Int32Value)emulator.Pop()).Value;
2013-01-19 20:03:57 +08:00
array = (Value)emulator.Pop();
2012-06-12 16:37:51 +08:00
if (array is UserValue)
2013-01-19 20:03:57 +08:00
emulator.Push(new Int32Value(((byte[])((UserValue)array).obj)[arrayIndex]));
2012-06-12 16:37:51 +08:00
else
2013-01-19 20:03:57 +08:00
emulator.Push(Int32Value.CreateUnknownUInt8());
2012-06-12 16:37:51 +08:00
break;
case Code.Stelem_I1:
2013-01-19 20:03:57 +08:00
value = emulator.Pop();
arrayIndex = ((Int32Value)emulator.Pop()).Value;
2013-01-19 20:03:57 +08:00
array = (Value)emulator.Pop();
2012-06-12 16:37:51 +08:00
if (array is UserValue)
((byte[])((UserValue)array).obj)[arrayIndex] = (byte)((Int32Value)value).Value;
2012-06-12 16:37:51 +08:00
break;
case Code.Stelem_I2:
2013-01-19 20:03:57 +08:00
value = emulator.Pop();
arrayIndex = ((Int32Value)emulator.Pop()).Value;
2013-01-19 20:03:57 +08:00
array = (Value)emulator.Pop();
2012-06-12 16:37:51 +08:00
if (array is UserValue)
((char[])((UserValue)array).obj)[arrayIndex] = (char)((Int32Value)value).Value;
2012-06-12 16:37:51 +08:00
break;
case Code.Ldelem_Ref:
arrayIndex = ((Int32Value)emulator.Pop()).Value;
2013-01-19 20:03:57 +08:00
array = (Value)emulator.Pop();
2012-06-12 16:37:51 +08:00
var userValue = array as UserValue;
if (userValue != null && userValue.obj is string[])
2013-01-19 20:03:57 +08:00
emulator.Push(new StringValue(((string[])userValue.obj)[arrayIndex]));
2012-06-12 16:37:51 +08:00
else
2013-01-19 20:03:57 +08:00
emulator.Push(new UnknownValue());
2012-06-12 16:37:51 +08:00
break;
case Code.Ldsfld:
2013-01-19 20:03:57 +08:00
emulator.Push(new UserValue((IField)instr.Operand));
2012-06-12 16:37:51 +08:00
break;
default:
2013-01-19 20:03:57 +08:00
emulator.Emulate(instr);
2012-06-12 16:37:51 +08:00
break;
}
index++;
}
}
2013-01-19 20:03:57 +08:00
bool DoCall(Instruction instr) {
2012-11-08 14:06:46 +08:00
var calledMethod = (IMethod)instr.Operand;
var sig = calledMethod.MethodSig;
var fn = calledMethod.FullName;
if (fn == "System.Byte[] System.Convert::FromBase64String(System.String)") {
2013-01-19 20:03:57 +08:00
emulator.Push(new UserValue(Convert.FromBase64String(((StringValue)emulator.Pop()).value)));
2012-06-12 16:37:51 +08:00
return true;
}
2012-11-08 14:06:46 +08:00
else if (fn == "System.String System.Text.Encoding::GetString(System.Byte[])") {
2013-01-19 20:03:57 +08:00
emulator.Push(new StringValue(Encoding.UTF8.GetString((byte[])((UserValue)emulator.Pop()).obj)));
2012-06-12 16:37:51 +08:00
return true;
}
2012-11-08 14:06:46 +08:00
else if (fn == "System.Int32 System.Int32::Parse(System.String)") {
2013-01-19 20:03:57 +08:00
emulator.Push(new Int32Value(int.Parse(((StringValue)emulator.Pop()).value)));
2012-06-12 16:37:51 +08:00
return true;
}
2012-11-08 14:06:46 +08:00
else if (fn == "System.String[] System.String::Split(System.Char[])") {
2013-01-19 20:03:57 +08:00
var ary = (char[])((UserValue)emulator.Pop()).obj;
var s = ((StringValue)emulator.Pop()).value;
emulator.Push(new UserValue(s.Split(ary)));
2012-06-12 16:37:51 +08:00
return true;
}
2012-11-08 14:06:46 +08:00
else if (sig != null && sig.HasThis && calledMethod.DeclaringType.FullName == "System.Reflection.Emit.ILGenerator" && calledMethod.Name == "Emit") {
2012-06-12 16:37:51 +08:00
Value operand = null;
2012-11-08 14:06:46 +08:00
if (calledMethod.MethodSig.GetParamCount() == 2)
2013-01-19 20:03:57 +08:00
operand = emulator.Pop();
var opcode = ReflectionToOpCode((IField)((UserValue)emulator.Pop()).obj);
emulator.Pop(); // the this ptr
AddInstruction(new Instruction {
2012-06-12 16:37:51 +08:00
OpCode = opcode,
2013-01-19 20:03:57 +08:00
Operand = CreateDNLibOperand(opcode, operand),
2012-06-12 16:37:51 +08:00
});
return true;
}
else {
2013-01-19 20:03:57 +08:00
emulator.Emulate(instr);
2012-06-12 16:37:51 +08:00
return true;
}
}
2013-01-19 20:03:57 +08:00
object CreateDNLibOperand(OpCode opcode, Value op) {
2012-06-12 16:37:51 +08:00
if (op is Int32Value)
return ((Int32Value)op).Value;
2012-06-12 16:37:51 +08:00
if (op is StringValue)
return ((StringValue)op).value;
return null;
}
2013-01-19 20:03:57 +08:00
void AddInstruction(Instruction instr) {
2012-06-12 16:37:51 +08:00
instructions.Add(instr);
}
2013-01-19 20:03:57 +08:00
static OpCode ReflectionToOpCode(IField reflectionField) {
2012-11-08 14:06:46 +08:00
var field = typeof(OpCodes).GetField(reflectionField.Name.String);
2012-06-12 16:37:51 +08:00
if (field == null || field.FieldType != typeof(OpCode))
return null;
return (OpCode)field.GetValue(null);
}
}
2013-01-19 20:03:57 +08:00
static List<Instruction> GetOffsetCalcInstructions(MethodDef method) {
var creator = new ReflectionToDNLibMethodCreator(method);
creator.Create();
2012-06-12 16:37:51 +08:00
var instrs = creator.Instructions;
int index = 0;
2013-01-19 20:03:57 +08:00
index = FindInstruction(instrs, index, OpCodes.Conv_I4);
2012-06-12 16:37:51 +08:00
if (index < 0)
return null;
int startInstr = ++index;
2013-01-19 20:03:57 +08:00
index = FindInstruction(instrs, index, OpCodes.Box);
2012-06-12 16:37:51 +08:00
if (index < 0)
return null;
int endInstr = index - 1;
var transformInstructions = new List<Instruction>();
for (int i = startInstr; i <= endInstr; i++)
transformInstructions.Add(instrs[i]);
return transformInstructions;
}
2013-01-19 20:03:57 +08:00
static int FindInstruction(IList<Instruction> instrs, int index, OpCode opcode) {
2012-06-12 16:37:51 +08:00
if (index < 0)
return -1;
for (int i = index; i < instrs.Count; i++) {
if (instrs[i].OpCode == opcode)
return i;
}
return -1;
}
2013-01-19 20:03:57 +08:00
static bool HasFieldType(IEnumerable<FieldDef> fields, TypeDef fieldType) {
2012-06-12 16:37:51 +08:00
foreach (var field in fields) {
2012-11-08 14:06:46 +08:00
if (new SigComparer().Equals(field.FieldSig.GetFieldType(), fieldType))
2012-06-12 16:37:51 +08:00
return true;
}
return false;
}
2013-01-19 20:03:57 +08:00
static int GetOffsetMagic(MethodDef method) {
2012-06-07 03:16:18 +08:00
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count - 4; i++) {
int index = i;
var ldsfld1 = instrs[index++];
if (ldsfld1.OpCode.Code != Code.Ldsfld)
continue;
var ldci4 = instrs[index++];
2012-11-08 14:06:46 +08:00
if (!ldci4.IsLdcI4())
2012-06-07 03:16:18 +08:00
continue;
var callvirt = instrs[index++];
if (callvirt.OpCode.Code != Code.Callvirt)
continue;
2012-11-08 14:06:46 +08:00
var calledMethod = callvirt.Operand as IMethod;
2012-06-07 03:16:18 +08:00
if (calledMethod == null)
continue;
if (calledMethod.FullName != "System.Void System.Reflection.Emit.ILGenerator::Emit(System.Reflection.Emit.OpCode,System.Int32)")
continue;
2012-11-08 14:06:46 +08:00
if (!instrs[index++].IsLdloc())
2012-06-07 03:16:18 +08:00
continue;
var ldsfld2 = instrs[index++];
if (ldsfld2.OpCode.Code != Code.Ldsfld)
continue;
2012-11-08 14:06:46 +08:00
var field = ldsfld2.Operand as IField;
2012-06-07 03:16:18 +08:00
if (field == null)
continue;
if (field.FullName != "System.Reflection.Emit.OpCode System.Reflection.Emit.OpCodes::Xor")
continue;
// Here if Babel.NET 5.5
2012-11-08 14:06:46 +08:00
return ldci4.GetLdcI4Value();
2012-06-07 03:16:18 +08:00
}
// Here if Babel.NET <= 5.0
return 0;
}
2013-01-19 20:03:57 +08:00
bool CheckFields(TypeDef type, string fieldType1, TypeDef fieldType2) {
2012-01-14 17:37:15 +08:00
if (type.Fields.Count != 2)
2012-01-08 03:27:07 +08:00
return false;
2012-11-08 14:06:46 +08:00
if (type.Fields[0].FieldSig.GetFieldType().GetFullName() != fieldType1 &&
type.Fields[1].FieldSig.GetFieldType().GetFullName() != fieldType1)
2012-01-14 17:37:15 +08:00
return false;
2012-11-08 14:06:46 +08:00
if (!new SigComparer().Equals(type.Fields[0].FieldSig.GetFieldType(), fieldType2) &&
!new SigComparer().Equals(type.Fields[1].FieldSig.GetFieldType(), fieldType2))
2012-01-08 03:27:07 +08:00
return false;
return true;
}
2013-01-19 20:03:57 +08:00
public void Initialize() {
2012-01-08 03:27:07 +08:00
if (decrypterType == null)
return;
if (encryptedResource != null)
return;
2012-01-14 17:37:15 +08:00
if (decrypterInfo.NeedsResource) {
2013-01-19 20:03:57 +08:00
encryptedResource = BabelUtils.FindEmbeddedResource(module, decrypterType);
2012-01-14 17:37:15 +08:00
if (encryptedResource == null)
return;
}
2013-01-19 20:03:57 +08:00
decrypterInfo.Initialize(module, encryptedResource);
2012-01-08 03:27:07 +08:00
}
2013-01-19 20:03:57 +08:00
public string Decrypt(object[] args) {
return decrypterInfo.Decrypt(args);
2012-01-08 03:27:07 +08:00
}
}
}