2011-10-22 23:13:28 +08:00
|
|
|
|
/*
|
2015-10-30 05:45:26 +08:00
|
|
|
|
Copyright (C) 2011-2015 de4dot@gmail.com
|
2011-10-22 23:13:28 +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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2011-12-15 23:16:21 +08:00
|
|
|
|
using System;
|
2011-10-22 23:13:28 +08:00
|
|
|
|
using System.Text;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.DotNet;
|
2011-10-22 23:13:28 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
2011-12-09 16:02:06 +08:00
|
|
|
|
namespace de4dot.code.deobfuscators.CryptoObfuscator {
|
2011-10-22 23:13:28 +08:00
|
|
|
|
class StringDecrypter {
|
2012-11-09 05:24:13 +08:00
|
|
|
|
ModuleDefMD module;
|
2011-10-22 23:13:28 +08:00
|
|
|
|
EmbeddedResource stringResource;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
TypeDef stringDecrypterType;
|
|
|
|
|
MethodDef stringDecrypterMethod;
|
2011-10-22 23:13:28 +08:00
|
|
|
|
byte[] decryptedData;
|
|
|
|
|
|
|
|
|
|
public bool Detected {
|
|
|
|
|
get { return stringDecrypterType != null; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public TypeDef Type {
|
2011-10-23 15:07:47 +08:00
|
|
|
|
get { return stringDecrypterType; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef Method {
|
2011-10-22 23:13:28 +08:00
|
|
|
|
get { return stringDecrypterMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-06 22:24:30 +08:00
|
|
|
|
public EmbeddedResource Resource {
|
2011-10-22 23:29:49 +08:00
|
|
|
|
get { return stringResource; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-09 05:24:13 +08:00
|
|
|
|
public StringDecrypter(ModuleDefMD module) {
|
2011-10-22 23:13:28 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Find() {
|
2012-11-02 22:57:11 +08:00
|
|
|
|
TypeDef type;
|
|
|
|
|
MethodDef method;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!FindStringDecrypterType(out type, out method))
|
2011-10-22 23:13:28 +08:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
stringDecrypterType = type;
|
|
|
|
|
stringDecrypterMethod = method;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Initialize(ResourceDecrypter resourceDecrypter) {
|
2012-11-20 11:42:19 +08:00
|
|
|
|
if (decryptedData != null || stringDecrypterType == null)
|
2011-10-22 23:13:28 +08:00
|
|
|
|
return;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var resourceName = GetResourceName();
|
|
|
|
|
stringResource = DotNetUtils.GetResource(module, resourceName) as EmbeddedResource;
|
2011-10-22 23:13:28 +08:00
|
|
|
|
if (stringResource == null)
|
|
|
|
|
return;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
Logger.v("Adding string decrypter. Resource: {0}", Utils.ToCsharpString(stringResource.Name));
|
2011-10-22 23:13:28 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
decryptedData = resourceDecrypter.Decrypt(stringResource.GetResourceStream());
|
2011-10-22 23:13:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
string GetResourceName() {
|
2012-11-09 05:24:13 +08:00
|
|
|
|
var defaultName = module.Assembly.Name.String + module.Assembly.Name.String;
|
2011-12-15 23:16:21 +08:00
|
|
|
|
|
2012-11-17 06:50:52 +08:00
|
|
|
|
var cctor = stringDecrypterType.FindStaticConstructor();
|
2011-12-15 23:16:21 +08:00
|
|
|
|
if (cctor == null)
|
|
|
|
|
return defaultName;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
foreach (var s in DotNetUtils.GetCodeStrings(cctor)) {
|
|
|
|
|
if (DotNetUtils.GetResource(module, s) != null)
|
2012-03-16 05:36:23 +08:00
|
|
|
|
return s;
|
2011-12-15 23:16:21 +08:00
|
|
|
|
try {
|
|
|
|
|
return Encoding.UTF8.GetString(Convert.FromBase64String(s));
|
|
|
|
|
}
|
|
|
|
|
catch {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return defaultName;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public string Decrypt(int index) {
|
|
|
|
|
int len = DeobUtils.ReadVariableLengthInt32(decryptedData, ref index);
|
2011-10-22 23:13:28 +08:00
|
|
|
|
return Encoding.Unicode.GetString(decryptedData, index, len);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool FindStringDecrypterType(out TypeDef theType, out MethodDef theMethod) {
|
2011-10-22 23:13:28 +08:00
|
|
|
|
theType = null;
|
|
|
|
|
theMethod = null;
|
|
|
|
|
|
|
|
|
|
foreach (var type in module.Types) {
|
|
|
|
|
if (type.IsPublic)
|
|
|
|
|
continue;
|
|
|
|
|
if (type.Fields.Count != 1)
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (DotNetUtils.FindFieldType(type, "System.Byte[]", true) == null)
|
2011-10-22 23:13:28 +08:00
|
|
|
|
continue;
|
2012-03-16 05:36:23 +08:00
|
|
|
|
if (type.Methods.Count != 2 && type.Methods.Count != 3)
|
2011-10-22 23:13:28 +08:00
|
|
|
|
continue;
|
2012-01-14 18:39:49 +08:00
|
|
|
|
if (type.NestedTypes.Count > 0)
|
|
|
|
|
continue;
|
2011-10-22 23:13:28 +08:00
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
MethodDef method = null;
|
2011-10-22 23:13:28 +08:00
|
|
|
|
foreach (var m in type.Methods) {
|
|
|
|
|
if (m.Name == ".ctor" || m.Name == ".cctor")
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (DotNetUtils.IsMethod(m, "System.String", "(System.Int32)")) {
|
2011-10-22 23:13:28 +08:00
|
|
|
|
method = m;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (method == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
theType = type;
|
|
|
|
|
theMethod = method;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|