2012-05-28 12:24:32 +08:00
|
|
|
|
/*
|
2013-01-02 00:03:16 +08:00
|
|
|
|
Copyright (C) 2011-2013 de4dot@gmail.com
|
2012-05-28 12:24:32 +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;
|
2012-07-23 16:42:38 +08:00
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Text;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.PE;
|
|
|
|
|
using dnlib.DotNet;
|
2012-05-28 12:24:32 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.MPRESS {
|
|
|
|
|
public class DeobfuscatorInfo : DeobfuscatorInfoBase {
|
|
|
|
|
public const string THE_NAME = "MPRESS";
|
|
|
|
|
public const string THE_TYPE = "mp";
|
2013-11-18 22:43:08 +08:00
|
|
|
|
const string DEFAULT_REGEX = DeobfuscatorBase.DEFAULT_ASIAN_VALID_NAME_REGEX;
|
|
|
|
|
|
2012-05-28 12:24:32 +08:00
|
|
|
|
public DeobfuscatorInfo()
|
2013-11-18 22:43:08 +08:00
|
|
|
|
: base(DEFAULT_REGEX) {
|
2012-05-28 12:24:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string Name {
|
|
|
|
|
get { return THE_NAME; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string Type {
|
|
|
|
|
get { return THE_TYPE; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public override IDeobfuscator CreateDeobfuscator() {
|
2012-05-28 12:24:32 +08:00
|
|
|
|
return new Deobfuscator(new Deobfuscator.Options {
|
|
|
|
|
ValidNameRegex = validNameRegex.get(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
protected override IEnumerable<Option> GetOptionsInternal() {
|
2012-05-28 12:24:32 +08:00
|
|
|
|
return new List<Option>() {
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Deobfuscator : DeobfuscatorBase {
|
|
|
|
|
Options options;
|
|
|
|
|
Version version;
|
|
|
|
|
string obfuscatorName = DeobfuscatorInfo.THE_NAME;
|
|
|
|
|
|
|
|
|
|
enum Version {
|
|
|
|
|
Unknown,
|
|
|
|
|
V0x, // 0.71 - 0.99
|
|
|
|
|
V1x_217, // 1.x - 2.17
|
|
|
|
|
V218, // 2.18+
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal class Options : OptionsBase {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string Type {
|
|
|
|
|
get { return DeobfuscatorInfo.THE_TYPE; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string TypeLong {
|
|
|
|
|
get { return DeobfuscatorInfo.THE_NAME; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string Name {
|
|
|
|
|
get { return obfuscatorName; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Deobfuscator(Options options)
|
|
|
|
|
: base(options) {
|
|
|
|
|
this.options = options;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
protected override int DetectInternal() {
|
2012-05-28 12:24:32 +08:00
|
|
|
|
int val = 0;
|
|
|
|
|
|
|
|
|
|
if (version != Version.Unknown)
|
|
|
|
|
val += 100;
|
|
|
|
|
|
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
protected override void ScanForObfuscator() {
|
|
|
|
|
version = DetectVersion();
|
2012-05-28 12:24:32 +08:00
|
|
|
|
switch (version) {
|
|
|
|
|
case Version.V0x: obfuscatorName += " v0.71 - v0.99"; break;
|
|
|
|
|
case Version.V1x_217: obfuscatorName += " v1.x - v2.17"; break;
|
|
|
|
|
case Version.V218: obfuscatorName += " v2.18+"; break;
|
|
|
|
|
case Version.Unknown: break;
|
|
|
|
|
default: throw new ApplicationException("Unknown version");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class MethodInfo {
|
|
|
|
|
public readonly string returnType;
|
|
|
|
|
public readonly string parameters;
|
|
|
|
|
public readonly string name;
|
|
|
|
|
|
2012-07-07 13:11:32 +08:00
|
|
|
|
public MethodInfo(string returnType, string parameters)
|
|
|
|
|
: this(returnType, parameters, null) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MethodInfo(string returnType, string parameters, string name) {
|
2012-05-28 12:24:32 +08:00
|
|
|
|
this.returnType = returnType;
|
|
|
|
|
this.parameters = parameters;
|
|
|
|
|
this.name = name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static readonly string[] requiredFields = new string[] {
|
|
|
|
|
"System.Reflection.Assembly",
|
|
|
|
|
};
|
|
|
|
|
static readonly MethodInfo[] methods_v0x = new MethodInfo[] {
|
|
|
|
|
new MethodInfo("System.Void", "()", ".ctor"),
|
|
|
|
|
new MethodInfo("System.Boolean", "(System.String,System.Byte[]&)"),
|
|
|
|
|
new MethodInfo("System.Boolean", "(System.Byte[],System.Byte[]&)"),
|
|
|
|
|
new MethodInfo("System.Int32", "(System.Byte[],System.Int32,System.Int32,System.Byte[],System.Int32,System.Int32)"),
|
|
|
|
|
new MethodInfo("System.Int32", "(System.String[])"),
|
|
|
|
|
};
|
|
|
|
|
static readonly MethodInfo[] methods_v1x = new MethodInfo[] {
|
|
|
|
|
new MethodInfo("System.Void", "()", ".ctor"),
|
|
|
|
|
new MethodInfo("System.Boolean", "(System.String,System.Byte[]&)"),
|
|
|
|
|
new MethodInfo("System.Boolean", "(System.Byte[],System.Byte[]&,System.Int32)"),
|
|
|
|
|
new MethodInfo("System.Int32", "(System.Byte[],System.Byte[],System.Int32)"),
|
|
|
|
|
new MethodInfo("System.Int32", "(System.String[])"),
|
|
|
|
|
};
|
2013-01-19 20:03:57 +08:00
|
|
|
|
Version DetectVersion() {
|
2012-11-07 09:02:38 +08:00
|
|
|
|
var ep = module.EntryPoint;
|
2012-05-28 12:24:32 +08:00
|
|
|
|
if (ep == null || ep.Body == null)
|
|
|
|
|
return Version.Unknown;
|
|
|
|
|
var type = ep.DeclaringType;
|
|
|
|
|
if (type == null)
|
|
|
|
|
return Version.Unknown;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!new FieldTypes(type).Exactly(requiredFields))
|
2012-05-28 12:24:32 +08:00
|
|
|
|
return Version.Unknown;
|
|
|
|
|
if (module.Types.Count != 2)
|
|
|
|
|
return Version.Unknown;
|
|
|
|
|
if (module.Types[1] != type)
|
|
|
|
|
return Version.Unknown;
|
|
|
|
|
if (module.Types[0].Methods.Count != 0)
|
|
|
|
|
return Version.Unknown;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (CheckMethods(type, methods_v0x))
|
2012-05-28 12:24:32 +08:00
|
|
|
|
return Version.V0x;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (CheckMethods(type, methods_v1x)) {
|
|
|
|
|
var lfMethod = DotNetUtils.GetMethod(type, "System.Boolean", "(System.String,System.Byte[]&)");
|
2012-05-28 12:24:32 +08:00
|
|
|
|
if (lfMethod != null) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (DeobUtils.HasInteger(lfMethod, (int)Machine.AMD64))
|
2012-05-28 12:24:32 +08:00
|
|
|
|
return Version.V218;
|
|
|
|
|
return Version.V1x_217;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Version.Unknown;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static bool CheckMethods(TypeDef type, MethodInfo[] requiredMethods) {
|
2012-11-02 22:57:11 +08:00
|
|
|
|
var methods = new List<MethodDef>(type.Methods);
|
2012-05-28 12:24:32 +08:00
|
|
|
|
foreach (var info in requiredMethods) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!CheckMethod(methods, info))
|
2012-05-28 12:24:32 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return methods.Count == 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static bool CheckMethod(List<MethodDef> methods, MethodInfo info) {
|
2012-05-28 12:24:32 +08:00
|
|
|
|
foreach (var method in methods) {
|
|
|
|
|
if (info.name != null && info.name != method.Name)
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!DotNetUtils.IsMethod(method, info.returnType, info.parameters))
|
2012-05-28 12:24:32 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
methods.Remove(method);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public override bool GetDecryptedModule(int count, ref byte[] newFileData, ref DumpedMethods dumpedMethods) {
|
2012-07-24 23:02:27 +08:00
|
|
|
|
if (count != 0 || version == Version.Unknown)
|
2012-05-28 12:24:32 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
byte[] fileData = ModuleBytes ?? DeobUtils.ReadModule(module);
|
2012-11-21 18:14:20 +08:00
|
|
|
|
byte[] decompressed;
|
|
|
|
|
using (var peImage = new MyPEImage(fileData)) {
|
|
|
|
|
var section = peImage.Sections[peImage.Sections.Count - 1];
|
|
|
|
|
var offset = section.PointerToRawData;
|
|
|
|
|
offset += 16;
|
|
|
|
|
|
|
|
|
|
byte[] compressed;
|
|
|
|
|
int compressedLen;
|
|
|
|
|
switch (version) {
|
|
|
|
|
case Version.V0x:
|
|
|
|
|
compressedLen = fileData.Length - (int)offset;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
compressed = peImage.OffsetReadBytes(offset, compressedLen);
|
|
|
|
|
decompressed = Lzmat.DecompressOld(compressed);
|
2012-11-21 18:14:20 +08:00
|
|
|
|
if (decompressed == null)
|
|
|
|
|
throw new ApplicationException("LZMAT decompression failed");
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Version.V1x_217:
|
|
|
|
|
case Version.V218:
|
|
|
|
|
if (peImage.PEImage.ImageNTHeaders.FileHeader.Machine == Machine.AMD64 && version == Version.V218)
|
|
|
|
|
offset = section.PointerToRawData + section.VirtualSize;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
int decompressedLen = (int)peImage.OffsetReadUInt32(offset);
|
2012-11-21 18:14:20 +08:00
|
|
|
|
compressedLen = fileData.Length - (int)offset - 4;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
compressed = peImage.OffsetReadBytes(offset + 4, compressedLen);
|
2012-11-21 18:14:20 +08:00
|
|
|
|
decompressed = new byte[decompressedLen];
|
|
|
|
|
uint decompressedLen2;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (Lzmat.Decompress(decompressed, out decompressedLen2, compressed) != LzmatStatus.OK)
|
2012-11-21 18:14:20 +08:00
|
|
|
|
throw new ApplicationException("LZMAT decompression failed");
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw new ApplicationException("Unknown MPRESS version");
|
|
|
|
|
}
|
2012-05-28 12:24:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newFileData = decompressed;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public override IDeobfuscator ModuleReloaded(ModuleDefMD module) {
|
2012-05-28 12:24:32 +08:00
|
|
|
|
var newOne = new Deobfuscator(options);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
newOne.SetModule(module);
|
2012-05-28 12:24:32 +08:00
|
|
|
|
return newOne;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public override void DeobfuscateBegin() {
|
|
|
|
|
base.DeobfuscateBegin();
|
2012-07-23 16:42:38 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
FixInvalidMvid();
|
2012-07-23 16:42:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void FixInvalidMvid() {
|
2012-07-23 16:42:38 +08:00
|
|
|
|
if (module.Mvid == Guid.Empty) {
|
|
|
|
|
var hash = new SHA1Managed().ComputeHash(Encoding.UTF8.GetBytes(module.ToString()));
|
|
|
|
|
var guid = new Guid(BitConverter.ToInt32(hash, 0),
|
|
|
|
|
BitConverter.ToInt16(hash, 4),
|
|
|
|
|
BitConverter.ToInt16(hash, 6),
|
|
|
|
|
hash[8], hash[9], hash[10], hash[11],
|
|
|
|
|
hash[12], hash[13], hash[14], hash[15]);
|
2012-11-11 12:31:11 +08:00
|
|
|
|
Logger.v("Updating MVID: {0}", guid.ToString("B"));
|
2012-07-23 16:42:38 +08:00
|
|
|
|
module.Mvid = guid;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public override IEnumerable<int> GetStringDecrypterMethods() {
|
2012-05-28 12:24:32 +08:00
|
|
|
|
return new List<int>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|