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

116 lines
3.8 KiB
C#
Raw Normal View History

2011-12-22 02:22:23 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
2011-12-22 02:22:23 +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.Collections.Generic;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
2011-12-22 02:22:23 +08:00
using de4dot.blocks;
namespace de4dot.code.deobfuscators.Dotfuscator {
class StringDecrypter {
2012-11-06 23:30:39 +08:00
ModuleDefMD module;
2012-11-22 16:14:51 +08:00
MethodDefAndDeclaringTypeDict<StringDecrypterInfo> stringDecrypterMethods = new MethodDefAndDeclaringTypeDict<StringDecrypterInfo>();
2011-12-22 02:22:23 +08:00
public class StringDecrypterInfo {
public MethodDef method;
2011-12-22 02:22:23 +08:00
public int magic;
public StringDecrypterInfo(MethodDef method, int magic) {
2011-12-22 02:22:23 +08:00
this.method = method;
this.magic = magic;
}
}
public bool Detected {
get { return stringDecrypterMethods.Count > 0; }
}
public IEnumerable<MethodDef> StringDecrypters {
2011-12-22 02:22:23 +08:00
get {
var list = new List<MethodDef>(stringDecrypterMethods.Count);
2013-01-19 20:03:57 +08:00
foreach (var info in stringDecrypterMethods.GetValues())
2012-11-06 23:30:39 +08:00
list.Add(info.method);
2011-12-22 02:22:23 +08:00
return list;
}
}
public IEnumerable<StringDecrypterInfo> StringDecrypterInfos {
2013-01-19 20:03:57 +08:00
get { return stringDecrypterMethods.GetValues(); }
2011-12-22 02:22:23 +08:00
}
2012-11-06 23:30:39 +08:00
public StringDecrypter(ModuleDefMD module) {
2011-12-22 02:22:23 +08:00
this.module = module;
}
2013-01-19 20:03:57 +08:00
public void Find(ISimpleDeobfuscator simpleDeobfuscator) {
2011-12-22 02:22:23 +08:00
foreach (var type in module.GetTypes())
2013-01-19 20:03:57 +08:00
FindStringDecrypterMethods(type, simpleDeobfuscator);
2011-12-22 02:22:23 +08:00
}
2013-01-19 20:03:57 +08:00
void FindStringDecrypterMethods(TypeDef type, ISimpleDeobfuscator simpleDeobfuscator) {
foreach (var method in DotNetUtils.FindMethods(type.Methods, "System.String", new string[] { "System.String", "System.Int32" })) {
2011-12-22 02:22:23 +08:00
if (method.Body.HasExceptionHandlers)
continue;
2013-01-19 20:03:57 +08:00
if (DotNetUtils.GetMethodCalls(method, "System.Char[] System.String::ToCharArray()") != 1)
2011-12-22 02:22:23 +08:00
continue;
2013-01-19 20:03:57 +08:00
if (DotNetUtils.GetMethodCalls(method, "System.String System.String::Intern(System.String)") != 1)
2011-12-22 02:22:23 +08:00
continue;
2013-01-19 20:03:57 +08:00
simpleDeobfuscator.Deobfuscate(method);
2013-10-15 16:12:53 +08:00
var instrs = method.Body.Instructions;
for (int i = 0; i < instrs.Count - 3; i++) {
var ldarg = instrs[i];
if (!ldarg.IsLdarg() || ldarg.GetParameterIndex() != 0)
continue;
var callvirt = instrs[i + 1];
if (callvirt.OpCode.Code != Code.Callvirt)
2011-12-22 02:22:23 +08:00
continue;
2013-10-15 16:12:53 +08:00
var calledMethod = callvirt.Operand as MemberRef;
if (calledMethod == null || calledMethod.FullName != "System.Char[] System.String::ToCharArray()")
2011-12-22 02:22:23 +08:00
continue;
2013-10-15 16:12:53 +08:00
var stloc = instrs[i + 2];
if (!stloc.IsStloc())
continue;
var ldci4 = instrs[i + 3];
if (!ldci4.IsLdcI4())
2011-12-22 02:22:23 +08:00
continue;
2012-11-06 23:30:39 +08:00
var info = new StringDecrypterInfo(method, ldci4.GetLdcI4Value());
2013-01-19 20:03:57 +08:00
stringDecrypterMethods.Add(info.method, info);
Logger.v("Found string decrypter method: {0}, magic: 0x{1:X8}", Utils.RemoveNewlines(info.method), info.magic);
2011-12-22 02:22:23 +08:00
break;
}
}
}
2013-01-19 20:03:57 +08:00
public string Decrypt(IMethod method, string encrypted, int value) {
var info = stringDecrypterMethods.FindAny(method);
2011-12-22 02:22:23 +08:00
char[] chars = encrypted.ToCharArray();
byte key = (byte)(info.magic + value);
for (int i = 0; i < chars.Length; i++) {
char c = chars[i];
byte b1 = (byte)((byte)c ^ key++);
byte b2 = (byte)((byte)(c >> 8) ^ key++);
chars[i] = (char)((b1 << 8) | b2);
}
return new string(chars);
}
}
}