de4dot-cex/de4dot.code/deobfuscators/MaxtoCode/Deobfuscator.cs

261 lines
6.9 KiB
C#
Raw Normal View History

2012-02-20 10:26:27 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
2012-02-20 10:26:27 +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/>.
*/
2012-07-21 00:59:10 +08:00
using System;
using System.Collections.Generic;
2012-07-21 00:59:10 +08:00
using System.Text;
using dnlib.DotNet;
2012-11-18 14:34:51 +08:00
using de4dot.blocks;
2012-02-20 10:26:27 +08:00
namespace de4dot.code.deobfuscators.MaxtoCode {
public class DeobfuscatorInfo : DeobfuscatorInfoBase {
public const string THE_NAME = "MaxtoCode";
public const string THE_TYPE = "mc";
const string DEFAULT_REGEX = @"!^[oO01l]+$&!^[A-F0-9]{20,}$&" + DeobfuscatorBase.DEFAULT_ASIAN_VALID_NAME_REGEX;
2012-07-21 00:59:10 +08:00
IntOption stringCodePage;
2012-02-20 10:26:27 +08:00
public DeobfuscatorInfo()
: base(DEFAULT_REGEX) {
2013-01-19 20:03:57 +08:00
stringCodePage = new IntOption(null, MakeArgName("cp"), "String code page", 936);
2012-02-20 10:26:27 +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-02-20 10:26:27 +08:00
return new Deobfuscator(new Deobfuscator.Options {
RenameResourcesInCode = false,
2013-11-18 23:28:44 +08:00
ValidNameRegex = validNameRegex.Get(),
StringCodePage = stringCodePage.Get(),
2012-02-20 10:26:27 +08:00
});
}
2012-07-21 00:59:10 +08:00
2013-01-19 20:03:57 +08:00
protected override IEnumerable<Option> GetOptionsInternal() {
2012-07-21 00:59:10 +08:00
return new List<Option>() {
stringCodePage,
};
}
2012-02-20 10:26:27 +08:00
}
class Deobfuscator : DeobfuscatorBase {
Options options;
2012-02-20 11:55:59 +08:00
MainType mainType;
2012-07-21 00:15:40 +08:00
DecrypterInfo decrypterInfo;
2012-07-21 00:59:10 +08:00
StringDecrypter stringDecrypter;
2012-02-20 11:55:59 +08:00
2012-02-20 10:26:27 +08:00
internal class Options : OptionsBase {
2012-07-21 00:59:10 +08:00
public int StringCodePage { get; set; }
2012-02-20 10:26:27 +08:00
}
public override string Type {
get { return DeobfuscatorInfo.THE_TYPE; }
}
public override string TypeLong {
get { return DeobfuscatorInfo.THE_NAME; }
}
public override string Name {
get { return DeobfuscatorInfo.THE_NAME; }
}
internal Deobfuscator(Options options)
: base(options) {
this.options = options;
2012-07-21 00:59:10 +08:00
StringFeatures = StringFeatures.AllowStaticDecryption | StringFeatures.AllowDynamicDecryption;
2012-02-20 10:26:27 +08:00
}
2013-01-19 20:03:57 +08:00
protected override int DetectInternal() {
2012-02-20 11:55:59 +08:00
int val = 0;
if (mainType.Detected)
val = 150;
return val;
2012-02-20 10:26:27 +08:00
}
2013-01-19 20:03:57 +08:00
protected override void ScanForObfuscator() {
2012-02-20 11:55:59 +08:00
mainType = new MainType(module);
2013-01-19 20:03:57 +08:00
mainType.Find();
2012-02-20 10:26:27 +08:00
}
2013-01-19 20:03:57 +08:00
public override bool GetDecryptedModule(int count, ref byte[] newFileData, ref DumpedMethods dumpedMethods) {
if (count != 0 || !mainType.Detected)
return false;
2013-01-19 20:03:57 +08:00
var fileData = DeobUtils.ReadModule(module);
2012-07-21 00:15:40 +08:00
decrypterInfo = new DecrypterInfo(mainType, fileData);
2013-10-21 00:09:38 +08:00
var methodsDecrypter = new MethodsDecrypter(module, decrypterInfo);
2012-07-21 00:15:40 +08:00
2013-01-19 20:03:57 +08:00
if (!methodsDecrypter.Decrypt(ref dumpedMethods))
return false;
newFileData = fileData;
return true;
}
2013-01-19 20:03:57 +08:00
public override IDeobfuscator ModuleReloaded(ModuleDefMD module) {
var newOne = new Deobfuscator(options);
2013-01-19 20:03:57 +08:00
newOne.SetModule(module);
newOne.mainType = new MainType(module, mainType);
2012-07-21 00:15:40 +08:00
newOne.decrypterInfo = decrypterInfo;
decrypterInfo = null;
if (newOne.decrypterInfo != null)
newOne.decrypterInfo.mainType = newOne.mainType;
return newOne;
}
2012-02-21 18:57:47 +08:00
2013-01-19 20:03:57 +08:00
void FreePEImage() {
if (decrypterInfo != null)
decrypterInfo.Dispose();
decrypterInfo = null;
}
2013-01-19 20:03:57 +08:00
public override void DeobfuscateBegin() {
base.DeobfuscateBegin();
2012-02-21 18:57:47 +08:00
2012-07-21 00:59:10 +08:00
stringDecrypter = new StringDecrypter(decrypterInfo);
2013-01-19 20:03:57 +08:00
stringDecrypter.Find();
2012-07-21 00:59:10 +08:00
if (stringDecrypter.Detected) {
2013-01-19 20:03:57 +08:00
stringDecrypter.Initialize(GetEncoding(options.StringCodePage));
staticStringInliner.Add(stringDecrypter.Method, (method, gim, args) => stringDecrypter.Decrypt((uint)args[0]));
DeobfuscatedFile.StringDecryptersAdded();
2012-07-21 00:59:10 +08:00
}
else
2013-01-19 20:03:57 +08:00
FreePEImage();
2012-07-21 00:59:10 +08:00
2012-02-21 18:57:47 +08:00
foreach (var method in mainType.InitMethods)
2013-01-19 20:03:57 +08:00
AddCctorInitCallToBeRemoved(method);
AddTypeToBeRemoved(mainType.Type, "Obfuscator type");
RemoveDuplicateEmbeddedResources();
RemoveInvalidResources();
2012-02-21 18:57:47 +08:00
}
2012-02-27 05:57:55 +08:00
2013-01-19 20:03:57 +08:00
public override void DeobfuscateEnd() {
FreePEImage();
base.DeobfuscateEnd();
}
2013-01-19 20:03:57 +08:00
static Encoding GetEncoding(int cp) {
2012-07-21 00:59:10 +08:00
try {
return Encoding.GetEncoding(cp);
}
catch {
Logger.w("Invalid code page {0}!", cp);
return Encoding.Default;
2012-07-21 00:59:10 +08:00
}
}
2012-07-31 18:50:55 +08:00
class ResourceKey {
readonly EmbeddedResource resource;
public ResourceKey(EmbeddedResource resource) {
this.resource = resource;
}
public override int GetHashCode() {
2012-11-18 14:34:51 +08:00
int hash = 0;
2012-11-20 14:25:10 +08:00
if (resource.Offset != null)
hash ^= resource.Offset.GetHashCode();
2012-11-18 14:34:51 +08:00
hash ^= (int)resource.Data.Position;
hash ^= (int)resource.Data.Length;
return hash;
2012-07-31 18:50:55 +08:00
}
public override bool Equals(object obj) {
var other = obj as ResourceKey;
if (other == null)
return false;
2012-11-18 14:34:51 +08:00
return resource.Data.FileOffset == other.resource.Data.FileOffset &&
resource.Data.Length == other.resource.Data.Length;
2012-07-31 18:50:55 +08:00
}
public override string ToString() {
2012-11-18 14:34:51 +08:00
return resource.Name.String;
2012-07-31 18:50:55 +08:00
}
}
2013-01-19 20:03:57 +08:00
void RemoveDuplicateEmbeddedResources() {
2012-07-31 18:50:55 +08:00
var resources = new Dictionary<ResourceKey, List<EmbeddedResource>>();
2012-07-23 16:22:39 +08:00
foreach (var tmp in module.Resources) {
var rsrc = tmp as EmbeddedResource;
if (rsrc == null)
continue;
2012-11-20 14:25:10 +08:00
if (rsrc.Offset == null)
2012-07-23 16:22:39 +08:00
continue;
List<EmbeddedResource> list;
2012-07-31 18:50:55 +08:00
var key = new ResourceKey(rsrc);
if (!resources.TryGetValue(key, out list))
resources[key] = list = new List<EmbeddedResource>();
2012-07-23 16:22:39 +08:00
list.Add(rsrc);
}
foreach (var list in resources.Values) {
if (list.Count <= 1)
continue;
EmbeddedResource resourceToKeep = null;
foreach (var rsrc in list) {
2012-11-18 14:34:51 +08:00
if (UTF8String.IsNullOrEmpty(rsrc.Name))
2012-07-23 16:22:39 +08:00
continue;
resourceToKeep = rsrc;
break;
}
if (resourceToKeep == null)
continue;
foreach (var rsrc in list) {
if (rsrc == resourceToKeep)
continue;
2013-01-19 20:03:57 +08:00
AddResourceToBeRemoved(rsrc, string.Format("Duplicate of resource {0}", Utils.ToCsharpString(resourceToKeep.Name)));
2012-07-23 16:22:39 +08:00
}
}
}
2013-01-19 20:03:57 +08:00
void RemoveInvalidResources() {
2012-11-20 14:25:10 +08:00
foreach (var tmp in module.Resources) {
var resource = tmp as EmbeddedResource;
if (resource == null)
continue;
if (resource.Offset == null || (resource.Data.FileOffset == 0 && resource.Data.Length == 0))
2013-01-19 20:03:57 +08:00
AddResourceToBeRemoved(resource, "Invalid resource");
2012-11-20 14:25:10 +08:00
}
}
2013-01-19 20:03:57 +08:00
public override IEnumerable<int> GetStringDecrypterMethods() {
2012-07-21 00:59:10 +08:00
var list = new List<int>();
if (stringDecrypter != null && stringDecrypter.Detected)
list.Add(stringDecrypter.Method.MDToken.ToInt32());
2012-07-21 00:59:10 +08:00
return list;
2012-02-27 05:57:55 +08:00
}
protected override void Dispose(bool disposing) {
if (disposing)
2013-01-19 20:03:57 +08:00
FreePEImage();
base.Dispose(disposing);
}
2012-02-20 10:26:27 +08:00
}
}