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

316 lines
8.5 KiB
C#
Raw Normal View History

2011-12-31 20:14:02 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
2011-12-31 20:14:02 +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.Globalization;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
2011-12-31 20:14:02 +08:00
using de4dot.blocks;
namespace de4dot.code.deobfuscators.Skater_NET {
class StringDecrypter {
2012-11-07 00:15:11 +08:00
ModuleDefMD module;
TypeDef decrypterType;
MethodDef decrypterCctor;
2012-11-22 16:14:51 +08:00
FieldDefAndDeclaringTypeDict<string> fieldToDecryptedString = new FieldDefAndDeclaringTypeDict<string>();
bool canRemoveType;
2012-03-27 21:25:33 +08:00
IDecrypter decrypter;
interface IDecrypter {
2013-01-19 20:03:57 +08:00
string Decrypt(string encrypted);
2012-03-27 21:25:33 +08:00
}
class DecrypterV1 : IDecrypter {
byte[] key;
byte[] iv;
public DecrypterV1(byte[] key, byte[] iv) {
this.key = key;
this.iv = iv;
}
2013-01-19 20:03:57 +08:00
public string Decrypt(string encrypted) {
2012-03-27 21:25:33 +08:00
try {
2013-01-19 20:03:57 +08:00
return Encoding.Unicode.GetString(DeobUtils.Des3Decrypt(Convert.FromBase64String(encrypted), key, iv));
2012-03-27 21:25:33 +08:00
}
catch (FormatException) {
return "";
}
}
}
class DecrypterV2 : IDecrypter {
2013-01-19 20:03:57 +08:00
public string Decrypt(string encrypted) {
2012-03-27 21:25:33 +08:00
var ints = encrypted.Split(' ');
if (ints.Length % 3 != 0)
throw new ApplicationException("Invalid encrypted string");
var sb = new StringBuilder(ints.Length / 3);
for (int i = 0; i < ints.Length; i += 3) {
int val1 = int.Parse(ints[i]);
int val2 = int.Parse(ints[i + 1]);
if ((double)val2 / 2.0 == Math.Round((double)val2 / 2.0))
val1 += val1;
sb.Append((char)val1);
}
return sb.ToString();
}
}
2011-12-31 20:14:02 +08:00
public bool Detected {
get { return decrypterType != null; }
}
public bool CanRemoveType {
get { return canRemoveType; }
}
public TypeDef Type {
2011-12-31 20:14:02 +08:00
get { return decrypterType; }
}
2012-11-07 00:15:11 +08:00
public StringDecrypter(ModuleDefMD module) {
2011-12-31 20:14:02 +08:00
this.module = module;
}
2013-01-19 20:03:57 +08:00
public void Find() {
2011-12-31 20:14:02 +08:00
foreach (var type in module.Types) {
if (type.HasProperties || type.HasEvents)
continue;
2012-11-17 06:50:52 +08:00
var cctor = type.FindStaticConstructor();
2011-12-31 20:14:02 +08:00
if (cctor == null)
continue;
2013-01-19 20:03:57 +08:00
if (CheckType(type)) {
canRemoveType = true;
2011-12-31 20:14:02 +08:00
decrypterType = type;
decrypterCctor = cctor;
return;
}
}
}
2013-01-19 20:03:57 +08:00
public void Initialize(ISimpleDeobfuscator deobfuscator) {
2011-12-31 20:14:02 +08:00
if (decrypterCctor == null)
return;
2013-01-19 20:03:57 +08:00
deobfuscator.Deobfuscate(decrypterCctor);
2011-12-31 20:14:02 +08:00
var instrs = decrypterCctor.Body.Instructions;
for (int i = 0; i < instrs.Count - 4; i++) {
var ldstr = instrs[i];
if (ldstr.OpCode.Code != Code.Ldstr)
continue;
var encryptedString = ldstr.Operand as string;
if (encryptedString == null)
continue;
if (instrs[i + 1].OpCode.Code != Code.Stsfld)
continue;
if (instrs[i + 2].OpCode.Code != Code.Ldsfld)
continue;
if (instrs[i + 3].OpCode.Code != Code.Call)
continue;
if (instrs[i + 4].OpCode.Code != Code.Stsfld)
continue;
var field = instrs[i + 4].Operand as FieldDef;
2011-12-31 20:14:02 +08:00
if (field == null)
continue;
2012-11-07 00:15:11 +08:00
if (!new SigComparer().Equals(field.DeclaringType, decrypterType))
2011-12-31 20:14:02 +08:00
continue;
2013-01-19 20:03:57 +08:00
fieldToDecryptedString.Add(field, decrypter.Decrypt(encryptedString));
2011-12-31 20:14:02 +08:00
}
}
2013-01-19 20:03:57 +08:00
bool CheckType(TypeDef type) {
2011-12-31 20:14:02 +08:00
foreach (var method in type.Methods) {
if (!method.IsStatic || method.Body == null)
continue;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.IsMethod(method, "System.String", "(System.String)"))
2011-12-31 20:14:02 +08:00
continue;
2013-01-19 20:03:57 +08:00
if (CheckMethodV1(method))
2012-03-27 21:25:33 +08:00
return true;
2013-01-19 20:03:57 +08:00
if (CheckMethodV2(method))
2012-03-27 21:25:33 +08:00
return true;
}
2011-12-31 20:14:02 +08:00
2012-03-27 21:25:33 +08:00
return false;
}
2013-01-19 20:03:57 +08:00
bool CheckMethodV1(MethodDef method) {
var salt = GetSalt(method);
2012-03-27 21:25:33 +08:00
if (salt == null)
return false;
2013-01-19 20:03:57 +08:00
var password = GetPassword(method);
2012-03-27 21:25:33 +08:00
if (string.IsNullOrEmpty(password))
return false;
2011-12-31 20:14:02 +08:00
2012-03-27 21:25:33 +08:00
var passwordBytes = new PasswordDeriveBytes(password, salt);
var key = passwordBytes.GetBytes(16);
var iv = passwordBytes.GetBytes(8);
decrypter = new DecrypterV1(key, iv);
return true;
}
static string[] callsMethodsV2 = new string[] {
"System.String[] System.String::Split(System.Char[])",
"System.Int32 System.Array::GetUpperBound(System.Int32)",
"System.String Microsoft.VisualBasic.CompilerServices.Conversions::ToString(System.Char)",
"System.Int32 Microsoft.VisualBasic.CompilerServices.Conversions::ToInteger(System.String)",
"System.String System.String::Concat(System.String,System.String)",
"System.Char Microsoft.VisualBasic.Strings::Chr(System.Int32)",
};
2013-01-19 20:03:57 +08:00
bool CheckMethodV2(MethodDef method) {
if (!DeobUtils.HasInteger(method, ' '))
2012-03-27 21:25:33 +08:00
return false;
foreach (var calledMethodName in callsMethodsV2) {
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.CallsMethod(method, calledMethodName))
2012-03-27 21:25:33 +08:00
return false;
2011-12-31 20:14:02 +08:00
}
2012-03-27 21:25:33 +08:00
decrypter = new DecrypterV2();
return true;
2011-12-31 20:14:02 +08:00
}
2013-01-19 20:03:57 +08:00
static byte[] GetSalt(MethodDef method) {
foreach (var s in DotNetUtils.GetCodeStrings(method)) {
var saltAry = FixSalt(s);
2011-12-31 20:14:02 +08:00
if (saltAry != null)
return saltAry;
}
return null;
}
2013-01-19 20:03:57 +08:00
static byte[] FixSalt(string s) {
2011-12-31 20:14:02 +08:00
if (s.Length < 10 || s.Length > 30 || s.Length / 2 * 2 != s.Length)
return null;
var ary = s.ToCharArray();
Array.Reverse(ary);
for (int i = 0; i < ary.Length; i++)
ary[i]--;
var s2 = new string(ary);
var saltAry = new byte[(int)Math.Round((double)s2.Length / 2 - 1) + 1];
for (int i = 0; i < saltAry.Length; i++) {
int result;
if (!int.TryParse(s2.Substring(i * 2, 2), NumberStyles.AllowHexSpecifier, null, out result))
return null;
saltAry[i] = (byte)result;
}
return saltAry;
}
2013-01-19 20:03:57 +08:00
string GetPassword(MethodDef decryptMethod) {
foreach (var method in DotNetUtils.GetCalledMethods(module, decryptMethod)) {
2011-12-31 20:14:02 +08:00
if (!method.IsStatic || method.Body == null)
continue;
2012-11-07 00:15:11 +08:00
if (!new SigComparer().Equals(method.DeclaringType, decryptMethod.DeclaringType))
2011-12-31 20:14:02 +08:00
continue;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.IsMethod(method, "System.String", "()"))
2011-12-31 20:14:02 +08:00
continue;
2013-01-19 20:03:57 +08:00
var hexChars = GetPassword2(method);
2011-12-31 20:14:02 +08:00
if (string.IsNullOrEmpty(hexChars))
continue;
2013-01-19 20:03:57 +08:00
var password = FixPassword(hexChars);
2011-12-31 20:14:02 +08:00
if (string.IsNullOrEmpty(password))
continue;
return password;
}
return null;
}
2013-01-19 20:03:57 +08:00
string FixPassword(string hexChars) {
2011-12-31 20:14:02 +08:00
var ary = hexChars.Trim().Split(' ');
string password = "";
for (int i = 0; i < ary.Length; i++) {
int result;
if (!int.TryParse(ary[i], NumberStyles.AllowHexSpecifier, null, out result))
return null;
password += (char)result;
}
return password;
}
2013-01-19 20:03:57 +08:00
string GetPassword2(MethodDef method) {
2011-12-31 20:14:02 +08:00
string password = "";
2013-01-19 20:03:57 +08:00
foreach (var calledMethod in DotNetUtils.GetCalledMethods(module, method)) {
var s = GetPassword3(calledMethod);
2011-12-31 20:14:02 +08:00
if (string.IsNullOrEmpty(s))
return null;
password += s;
}
return password;
}
2013-01-19 20:03:57 +08:00
string GetPassword3(MethodDef method) {
var strings = new List<string>(DotNetUtils.GetCodeStrings(method));
2011-12-31 20:14:02 +08:00
if (strings.Count != 1)
return null;
var s = strings[0];
if (!Regex.IsMatch(s, @"^[a-fA-F0-9]{2} $"))
return null;
return s;
}
2013-01-19 20:03:57 +08:00
public void Deobfuscate(Blocks blocks) {
foreach (var block in blocks.MethodBlocks.GetAllBlocks()) {
2011-12-31 20:14:02 +08:00
var instrs = block.Instructions;
for (int i = 0; i < instrs.Count; i++) {
var instr = instrs[i];
if (instr.OpCode.Code == Code.Call || instr.OpCode.Code == Code.Callvirt) {
if (blocks.Method.DeclaringType == decrypterType)
continue;
2012-11-07 00:15:11 +08:00
var calledMethod = instr.Operand as IMethod;
if (calledMethod != null && calledMethod.DeclaringType == decrypterType)
canRemoveType = false;
}
else if (instr.OpCode.Code == Code.Ldsfld) {
if (instr.OpCode.Code != Code.Ldsfld)
continue;
2012-11-07 00:15:11 +08:00
var field = instr.Operand as IField;
if (field == null)
continue;
2013-01-19 20:03:57 +08:00
var decrypted = fieldToDecryptedString.Find(field);
if (decrypted == null)
continue;
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));
}
2011-12-31 20:14:02 +08:00
}
}
}
}
}