2012-01-08 03:27:07 +08:00
|
|
|
|
/*
|
2012-01-10 06:04:52 +08:00
|
|
|
|
Copyright (C) 2011-2012 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;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
using Mono.Cecil;
|
2012-01-19 12:38:58 +08:00
|
|
|
|
using Mono.Cecil.Cil;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.Babel_NET {
|
|
|
|
|
class StringDecrypter {
|
|
|
|
|
ModuleDefinition module;
|
|
|
|
|
TypeDefinition decrypterType;
|
|
|
|
|
EmbeddedResource encryptedResource;
|
2012-01-14 17:37:15 +08:00
|
|
|
|
IDecrypterInfo decrypterInfo;
|
|
|
|
|
|
|
|
|
|
interface IDecrypterInfo {
|
|
|
|
|
MethodDefinition Decrypter { get; }
|
|
|
|
|
bool NeedsResource { get; }
|
|
|
|
|
void initialize(ModuleDefinition module, EmbeddedResource resource);
|
|
|
|
|
string decrypt(object[] args);
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-19 12:38:58 +08:00
|
|
|
|
// Babel .NET 2.x
|
2012-01-14 17:37:15 +08:00
|
|
|
|
class DecrypterInfoV1 : IDecrypterInfo {
|
2012-01-19 12:38:58 +08:00
|
|
|
|
public MethodDefinition Decrypter { get; set; }
|
|
|
|
|
public bool NeedsResource {
|
|
|
|
|
get { return false; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void initialize(ModuleDefinition module, EmbeddedResource resource) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string decrypt(object[] args) {
|
|
|
|
|
return decrypt((string)args[0], (int)args[1]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string decrypt(string s, int k) {
|
|
|
|
|
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 MethodDefinition Decrypter { get; set; }
|
|
|
|
|
public bool NeedsResource {
|
|
|
|
|
get { return true; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void initialize(ModuleDefinition module, EmbeddedResource resource) {
|
|
|
|
|
key = resource.GetResourceData();
|
|
|
|
|
if (key.Length != 0x100)
|
|
|
|
|
throw new ApplicationException(string.Format("Unknown key length: {0}", key.Length));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string decrypt(object[] args) {
|
|
|
|
|
return decrypt((string)args[0], (int)args[1]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string decrypt(string s, int k) {
|
|
|
|
|
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>();
|
|
|
|
|
|
|
|
|
|
public MethodDefinition Decrypter { get; set; }
|
|
|
|
|
public bool NeedsResource {
|
|
|
|
|
get { return true; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void initialize(ModuleDefinition module, EmbeddedResource resource) {
|
|
|
|
|
var decrypted = new ResourceDecrypter(module).decrypt(resource.GetResourceData());
|
|
|
|
|
var reader = new BinaryReader(new MemoryStream(decrypted));
|
|
|
|
|
while (reader.BaseStream.Position < reader.BaseStream.Length)
|
|
|
|
|
offsetToString[(int)reader.BaseStream.Position] = reader.ReadString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string decrypt(object[] args) {
|
|
|
|
|
return decrypt((int)args[0]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string decrypt(int offset) {
|
|
|
|
|
return offsetToString[offset];
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-01-08 03:27:07 +08:00
|
|
|
|
|
|
|
|
|
public bool Detected {
|
|
|
|
|
get { return decrypterType != null; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TypeDefinition Type {
|
|
|
|
|
get { return decrypterType; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MethodDefinition 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; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public StringDecrypter(ModuleDefinition module) {
|
|
|
|
|
this.module = module;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void find() {
|
|
|
|
|
foreach (var type in module.Types) {
|
2012-01-14 17:37:15 +08:00
|
|
|
|
var info = checkDecrypterType(type);
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-14 17:37:15 +08:00
|
|
|
|
IDecrypterInfo checkDecrypterType(TypeDefinition 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) {
|
|
|
|
|
var info = checkNested(type, nested);
|
|
|
|
|
if (info != null)
|
|
|
|
|
return info;
|
|
|
|
|
}
|
2012-01-19 12:38:58 +08:00
|
|
|
|
|
|
|
|
|
return checkDecrypterTypeBabel2x(type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IDecrypterInfo checkDecrypterTypeBabel2x(TypeDefinition type) {
|
|
|
|
|
if (type.HasEvents || type.HasProperties || type.HasNestedTypes)
|
|
|
|
|
return null;
|
|
|
|
|
if (type.HasFields || type.Methods.Count != 1)
|
|
|
|
|
return null;
|
|
|
|
|
var decrypter = type.Methods[0];
|
|
|
|
|
if (!checkDecryptMethodBabel2x(decrypter))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return new DecrypterInfoV1 { Decrypter = decrypter };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool checkDecryptMethodBabel2x(MethodDefinition method) {
|
|
|
|
|
if (!method.IsStatic || !method.IsPublic)
|
|
|
|
|
return false;
|
|
|
|
|
if (method.Body == null)
|
|
|
|
|
return false;
|
|
|
|
|
if (method.Name == ".cctor")
|
|
|
|
|
return false;
|
|
|
|
|
if (!DotNetUtils.isMethod(method, "System.String", "(System.String,System.Int32)"))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
int stringLength = 0, stringToCharArray = 0, stringCtor = 0;
|
|
|
|
|
foreach (var instr in method.Body.Instructions) {
|
|
|
|
|
var calledMethod = instr.Operand as MethodReference;
|
|
|
|
|
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
|
|
|
|
|
2012-01-14 17:37:15 +08:00
|
|
|
|
IDecrypterInfo checkNested(TypeDefinition type, TypeDefinition nested) {
|
2012-01-08 03:27:07 +08:00
|
|
|
|
if (nested.HasProperties || nested.HasEvents)
|
2012-01-14 17:37:15 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
if (DotNetUtils.getMethod(nested, ".ctor") == null)
|
|
|
|
|
return null;
|
2012-01-09 12:50:32 +08:00
|
|
|
|
|
|
|
|
|
if (nested.Fields.Count == 1) {
|
2012-01-14 17:37:15 +08:00
|
|
|
|
// 4.0+
|
2012-01-09 12:50:32 +08:00
|
|
|
|
|
|
|
|
|
if (!MemberReferenceHelper.compareTypes(nested.Fields[0].FieldType, nested))
|
2012-01-14 17:37:15 +08:00
|
|
|
|
return null;
|
2012-01-09 12:50:32 +08:00
|
|
|
|
|
|
|
|
|
if (DotNetUtils.getMethod(nested, "System.Reflection.Emit.MethodBuilder", "(System.Reflection.Emit.TypeBuilder)") == null)
|
2012-01-14 17:37:15 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var nestedDecrypter = DotNetUtils.getMethod(nested, "System.String", "(System.Int32)");
|
|
|
|
|
if (nestedDecrypter == null || nestedDecrypter.IsStatic)
|
|
|
|
|
return null;
|
|
|
|
|
var decrypter = DotNetUtils.getMethod(type, "System.String", "(System.Int32)");
|
|
|
|
|
if (decrypter == null || !decrypter.IsStatic)
|
|
|
|
|
return null;
|
|
|
|
|
|
2012-01-19 12:38:58 +08:00
|
|
|
|
return new DecrypterInfoV3 { Decrypter = decrypter };
|
2012-01-09 12:50:32 +08:00
|
|
|
|
}
|
|
|
|
|
else if (nested.Fields.Count == 2) {
|
2012-01-14 17:37:15 +08:00
|
|
|
|
// 3.0 - 3.5
|
|
|
|
|
|
|
|
|
|
if (checkFields(nested, "System.Collections.Hashtable", nested)) {
|
|
|
|
|
// 3.5
|
|
|
|
|
var nestedDecrypter = DotNetUtils.getMethod(nested, "System.String", "(System.Int32)");
|
|
|
|
|
if (nestedDecrypter == null || nestedDecrypter.IsStatic)
|
|
|
|
|
return null;
|
|
|
|
|
var decrypter = DotNetUtils.getMethod(type, "System.String", "(System.Int32)");
|
|
|
|
|
if (decrypter == null || !decrypter.IsStatic)
|
|
|
|
|
return null;
|
|
|
|
|
|
2012-01-19 12:38:58 +08:00
|
|
|
|
return new DecrypterInfoV3 { Decrypter = decrypter };
|
2012-01-14 17:37:15 +08:00
|
|
|
|
}
|
|
|
|
|
else if (checkFields(nested, "System.Byte[]", nested)) {
|
|
|
|
|
// 3.0
|
|
|
|
|
var nestedDecrypter = DotNetUtils.getMethod(nested, "System.String", "(System.String,System.Int32)");
|
|
|
|
|
if (nestedDecrypter == null || nestedDecrypter.IsStatic)
|
|
|
|
|
return null;
|
|
|
|
|
var decrypter = DotNetUtils.getMethod(type, "System.String", "(System.String,System.Int32)");
|
|
|
|
|
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-09 12:50:32 +08:00
|
|
|
|
}
|
2012-01-08 03:27:07 +08:00
|
|
|
|
|
2012-01-14 17:37:15 +08:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool checkFields(TypeDefinition type, string fieldType1, TypeDefinition fieldType2) {
|
|
|
|
|
if (type.Fields.Count != 2)
|
2012-01-08 03:27:07 +08:00
|
|
|
|
return false;
|
2012-01-14 17:37:15 +08:00
|
|
|
|
if (type.Fields[0].FieldType.FullName != fieldType1 &&
|
|
|
|
|
type.Fields[1].FieldType.FullName != fieldType1)
|
|
|
|
|
return false;
|
|
|
|
|
if (!MemberReferenceHelper.compareTypes(type.Fields[0].FieldType, fieldType2) &&
|
|
|
|
|
!MemberReferenceHelper.compareTypes(type.Fields[1].FieldType, fieldType2))
|
2012-01-08 03:27:07 +08:00
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void initialize() {
|
|
|
|
|
if (decrypterType == null)
|
|
|
|
|
return;
|
|
|
|
|
if (encryptedResource != null)
|
|
|
|
|
return;
|
|
|
|
|
|
2012-01-14 17:37:15 +08:00
|
|
|
|
if (decrypterInfo.NeedsResource) {
|
2012-01-18 14:38:35 +08:00
|
|
|
|
encryptedResource = BabelUtils.findEmbeddedResource(module, decrypterType);
|
2012-01-14 17:37:15 +08:00
|
|
|
|
if (encryptedResource == null)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
decrypterInfo.initialize(module, encryptedResource);
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-01-14 17:37:15 +08:00
|
|
|
|
public string decrypt(object[] args) {
|
|
|
|
|
return decrypterInfo.decrypt(args);
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|