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

227 lines
6.0 KiB
C#
Raw Normal View History

2012-02-20 10:26:27 +08:00
/*
Copyright (C) 2011-2012 de4dot@gmail.com
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 dot10.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";
2012-03-08 20:54:07 +08:00
const string DEFAULT_REGEX = @"!^[oO01l]+$&" + DeobfuscatorBase.DEFAULT_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) {
2012-07-21 00:59:10 +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; }
}
public override IDeobfuscator createDeobfuscator() {
return new Deobfuscator(new Deobfuscator.Options {
RenameResourcesInCode = false,
ValidNameRegex = validNameRegex.get(),
2012-07-21 00:59:10 +08:00
StringCodePage = stringCodePage.get(),
2012-02-20 10:26:27 +08:00
});
}
2012-07-21 00:59:10 +08:00
protected override IEnumerable<Option> getOptionsInternal() {
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
}
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
}
protected override void scanForObfuscator() {
2012-02-20 11:55:59 +08:00
mainType = new MainType(module);
mainType.find();
2012-02-20 10:26:27 +08:00
}
public override bool getDecryptedModule(int count, ref byte[] newFileData, ref DumpedMethods dumpedMethods) {
if (count != 0 || !mainType.Detected)
return false;
var fileData = DeobUtils.readModule(module);
2012-07-21 00:15:40 +08:00
decrypterInfo = new DecrypterInfo(mainType, fileData);
2012-07-23 16:08:13 +08:00
var methodsDecrypter = new MethodsDecrypter(decrypterInfo);
2012-07-21 00:15:40 +08:00
2012-07-23 16:08:13 +08:00
if (!methodsDecrypter.decrypt(ref dumpedMethods))
return false;
newFileData = fileData;
return true;
}
2012-11-18 14:34:51 +08:00
public override IDeobfuscator moduleReloaded(ModuleDefMD module) {
var newOne = new Deobfuscator(options);
newOne.setModule(module);
newOne.mainType = new MainType(module, mainType);
2012-07-21 00:15:40 +08:00
newOne.decrypterInfo = decrypterInfo;
2012-11-18 14:34:51 +08:00
newOne.decrypterInfo.mainType = newOne.mainType;
return newOne;
}
2012-02-21 18:57:47 +08:00
public override void deobfuscateBegin() {
base.deobfuscateBegin();
2012-07-21 00:59:10 +08:00
stringDecrypter = new StringDecrypter(decrypterInfo);
stringDecrypter.find();
if (stringDecrypter.Detected) {
stringDecrypter.initialize(getEncoding(options.StringCodePage));
2012-07-28 10:22:17 +08:00
staticStringInliner.add(stringDecrypter.Method, (method, gim, args) => stringDecrypter.decrypt((uint)args[0]));
2012-07-21 00:59:10 +08:00
DeobfuscatedFile.stringDecryptersAdded();
}
2012-02-21 18:57:47 +08:00
foreach (var method in mainType.InitMethods)
addCctorInitCallToBeRemoved(method);
addTypeToBeRemoved(mainType.Type, "Obfuscator type");
2012-07-23 16:22:39 +08:00
removeDuplicateEmbeddedResources();
2012-02-21 18:57:47 +08:00
}
2012-02-27 05:57:55 +08:00
2012-07-21 00:59:10 +08:00
static Encoding getEncoding(int cp) {
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;
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
}
}
2012-07-23 16:22:39 +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-18 14:34:51 +08:00
if (rsrc.Data.FileOffset == 0)
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;
addResourceToBeRemoved(rsrc, string.Format("Duplicate of resource {0}", Utils.toCsharpString(resourceToKeep.Name)));
}
}
}
2012-02-27 05:57:55 +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
}
2012-02-20 10:26:27 +08:00
}
}