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

99 lines
2.5 KiB
C#
Raw Normal View History

2012-05-30 01:34:51 +08:00
/*
2014-03-12 05:15:43 +08:00
Copyright (C) 2011-2014 de4dot@gmail.com
2012-05-30 01:34:51 +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.Text;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
2012-05-30 01:34:51 +08:00
using de4dot.blocks;
namespace de4dot.code.deobfuscators.CodeFort {
class StringDecrypter {
2012-11-08 14:43:57 +08:00
ModuleDefMD module;
MethodDef decryptMethod;
2012-05-30 01:34:51 +08:00
public bool Detected {
get { return decryptMethod != null; }
}
public MethodDef Method {
2012-05-30 01:34:51 +08:00
get { return decryptMethod; }
}
public TypeDef Type {
2012-05-30 01:34:51 +08:00
get { return decryptMethod == null ? null : decryptMethod.DeclaringType; }
}
2012-11-08 14:43:57 +08:00
public StringDecrypter(ModuleDefMD module) {
2012-05-30 01:34:51 +08:00
this.module = module;
}
2013-01-19 20:03:57 +08:00
public void Find() {
2012-05-30 01:34:51 +08:00
foreach (var type in module.Types) {
2013-01-19 20:03:57 +08:00
var method = CheckType(type);
2012-05-30 01:34:51 +08:00
if (method == null)
continue;
decryptMethod = method;
}
}
2013-01-19 20:03:57 +08:00
static MethodDef CheckType(TypeDef type) {
2012-05-30 01:34:51 +08:00
if (type.HasFields)
return null;
2013-01-19 20:03:57 +08:00
return CheckMethods(type);
2012-05-30 01:34:51 +08:00
}
2013-01-19 20:03:57 +08:00
static MethodDef CheckMethods(TypeDef type) {
MethodDef decryptMethod = null;
2012-05-30 01:34:51 +08:00
foreach (var method in type.Methods) {
if (method.Name == ".cctor")
continue;
if (!method.IsStatic || method.Body == null)
return null;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.IsMethod(method, "System.String", "(System.String)"))
2012-05-30 01:34:51 +08:00
return null;
2013-01-19 20:03:57 +08:00
if (!HasDouble(method, 3992.0))
2012-05-30 01:34:51 +08:00
return null;
decryptMethod = method;
}
return decryptMethod;
}
2013-01-19 20:03:57 +08:00
static bool HasDouble(MethodDef method, double value) {
2012-05-30 01:34:51 +08:00
if (method == null || method.Body == null)
return false;
foreach (var instr in method.Body.Instructions) {
if (instr.OpCode.Code != Code.Ldc_R8)
continue;
if ((double)instr.Operand == value)
return true;
}
return false;
}
2013-01-19 20:03:57 +08:00
public string Decrypt(string s) {
2012-05-30 01:34:51 +08:00
var bytes = new byte[s.Length];
for (int i = 0; i < s.Length; i++)
bytes[i] = (byte)(s[i] ^ 0x3F);
return Encoding.UTF8.GetString(bytes);
}
}
}