2011-11-06 19:19:26 +08:00
|
|
|
|
/*
|
2014-03-12 05:15:43 +08:00
|
|
|
|
Copyright (C) 2011-2014 de4dot@gmail.com
|
2011-11-06 19:19:26 +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.IO;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Text;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.IO;
|
|
|
|
|
using dnlib.DotNet;
|
2011-11-06 19:19:26 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
2012-11-06 23:38:39 +08:00
|
|
|
|
namespace de4dot.code.deobfuscators.Agile_NET {
|
2011-11-06 19:19:26 +08:00
|
|
|
|
class ResourceDecrypter {
|
2012-11-07 12:17:45 +08:00
|
|
|
|
ModuleDefMD module;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
TypeDef rsrcType;
|
|
|
|
|
MethodDef rsrcRrrMethod;
|
|
|
|
|
MethodDef rsrcResolveMethod;
|
2012-04-27 02:50:06 +08:00
|
|
|
|
|
|
|
|
|
public bool Detected {
|
|
|
|
|
get { return rsrcType != null; }
|
|
|
|
|
}
|
2011-11-06 19:19:26 +08:00
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public TypeDef Type {
|
2011-11-06 19:19:26 +08:00
|
|
|
|
get { return rsrcType; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef RsrcRrrMethod {
|
2011-11-06 19:19:26 +08:00
|
|
|
|
get { return rsrcRrrMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-07 12:17:45 +08:00
|
|
|
|
public ResourceDecrypter(ModuleDefMD module) {
|
2011-11-06 19:19:26 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-07 12:17:45 +08:00
|
|
|
|
public ResourceDecrypter(ModuleDefMD module, ResourceDecrypter oldOne) {
|
2012-04-27 02:50:06 +08:00
|
|
|
|
this.module = module;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
rsrcType = Lookup(oldOne.rsrcType, "Could not find rsrcType");
|
|
|
|
|
rsrcRrrMethod = Lookup(oldOne.rsrcRrrMethod, "Could not find rsrcRrrMethod");
|
|
|
|
|
rsrcResolveMethod = Lookup(oldOne.rsrcResolveMethod, "Could not find rsrcResolveMethod");
|
2012-04-27 02:50:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
T Lookup<T>(T def, string errorMessage) where T : class, ICodedToken {
|
|
|
|
|
return DeobUtils.Lookup(module, def, errorMessage);
|
2012-04-27 02:50:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Find() {
|
|
|
|
|
FindResourceType();
|
2011-11-06 19:19:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-27 18:41:18 +08:00
|
|
|
|
static readonly string[] requiredFields1 = new string[] {
|
2012-04-27 02:50:06 +08:00
|
|
|
|
"System.Reflection.Assembly",
|
|
|
|
|
"System.String[]",
|
|
|
|
|
};
|
2014-03-27 18:41:18 +08:00
|
|
|
|
static readonly string[] requiredFields2 = new string[] {
|
|
|
|
|
"System.Reflection.Assembly",
|
|
|
|
|
"System.String[]",
|
|
|
|
|
"System.Collections.Hashtable",
|
|
|
|
|
};
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void FindResourceType() {
|
|
|
|
|
var cctor = DotNetUtils.GetModuleTypeCctor(module);
|
2012-04-27 02:50:06 +08:00
|
|
|
|
if (cctor == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
foreach (var calledMethod in DotNetUtils.GetCalledMethods(module, cctor)) {
|
2012-04-27 02:50:06 +08:00
|
|
|
|
if (!calledMethod.IsStatic || calledMethod.Body == null)
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!DotNetUtils.IsMethod(calledMethod, "System.Void", "()"))
|
2012-04-27 02:50:06 +08:00
|
|
|
|
continue;
|
|
|
|
|
var type = calledMethod.DeclaringType;
|
2014-03-27 18:41:18 +08:00
|
|
|
|
var fieldTypes = new FieldTypes(type);
|
|
|
|
|
if (!fieldTypes.Exactly(requiredFields1) &&
|
|
|
|
|
!fieldTypes.Exactly(requiredFields2))
|
2011-11-06 19:19:26 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var resolveHandler = DotNetUtils.GetMethod(type, "System.Reflection.Assembly", "(System.Object,System.ResolveEventArgs)");
|
|
|
|
|
var decryptMethod = DotNetUtils.GetMethod(type, "System.Byte[]", "(System.IO.Stream)");
|
2012-04-27 02:50:06 +08:00
|
|
|
|
if (resolveHandler == null || !resolveHandler.IsStatic)
|
|
|
|
|
continue;
|
|
|
|
|
if (decryptMethod == null || !decryptMethod.IsStatic)
|
2011-11-06 19:19:26 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
rsrcType = type;
|
2012-04-27 02:50:06 +08:00
|
|
|
|
rsrcRrrMethod = calledMethod;
|
|
|
|
|
rsrcResolveMethod = resolveHandler;
|
2011-11-06 19:19:26 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public EmbeddedResource MergeResources() {
|
2012-04-27 02:50:06 +08:00
|
|
|
|
if (rsrcResolveMethod == null)
|
2011-11-06 19:19:26 +08:00
|
|
|
|
return null;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var resource = DotNetUtils.GetResource(module, DotNetUtils.GetCodeStrings(rsrcResolveMethod)) as EmbeddedResource;
|
2011-11-06 19:19:26 +08:00
|
|
|
|
if (resource == null)
|
|
|
|
|
return null;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
DeobUtils.DecryptAndAddResources(module, resource.Name.String, () => DecryptResource(resource));
|
2011-11-06 19:19:26 +08:00
|
|
|
|
return resource;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
byte[] DecryptResource(EmbeddedResource resource) {
|
2012-11-11 21:45:56 +08:00
|
|
|
|
var reader = resource.Data;
|
|
|
|
|
reader.Position = 0;
|
|
|
|
|
var key = reader.ReadString();
|
|
|
|
|
var data = reader.ReadRemainingBytes();
|
|
|
|
|
var cryptoTransform = new DESCryptoServiceProvider {
|
|
|
|
|
Key = Encoding.ASCII.GetBytes(key),
|
|
|
|
|
IV = Encoding.ASCII.GetBytes(key),
|
|
|
|
|
}.CreateDecryptor();
|
|
|
|
|
var memStream = new MemoryStream(data);
|
|
|
|
|
using (var reader2 = new BinaryReader(new CryptoStream(memStream, cryptoTransform, CryptoStreamMode.Read))) {
|
|
|
|
|
return reader2.ReadBytes((int)memStream.Length);
|
2011-11-06 19:19:26 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|