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;
|
2012-02-21 00:20:29 +08:00
|
|
|
|
using System.Collections.Generic;
|
2012-07-21 00:59:10 +08:00
|
|
|
|
using System.Text;
|
2012-02-21 00:20:29 +08:00
|
|
|
|
using Mono.Cecil;
|
|
|
|
|
using Mono.MyStuff;
|
|
|
|
|
|
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()
|
2012-02-23 18:52:19 +08:00
|
|
|
|
: 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 {
|
2012-02-21 00:20:29 +08:00
|
|
|
|
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) {
|
2012-02-21 00:20:29 +08:00
|
|
|
|
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
|
|
|
|
}
|
2012-02-21 00:20:29 +08:00
|
|
|
|
|
2012-07-24 23:02:27 +08:00
|
|
|
|
public override bool getDecryptedModule(int count, ref byte[] newFileData, ref DumpedMethods dumpedMethods) {
|
|
|
|
|
if (count != 0 || !mainType.Detected)
|
2012-02-21 00:20:29 +08:00
|
|
|
|
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))
|
2012-02-21 00:20:29 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
newFileData = fileData;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override IDeobfuscator moduleReloaded(ModuleDefinition 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-02-21 00:20:29 +08:00
|
|
|
|
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");
|
|
|
|
|
addModuleReferencesToBeRemoved(mainType.ModuleReferences, "MC runtime module reference");
|
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 {
|
2012-07-23 19:06:49 +08:00
|
|
|
|
Log.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() {
|
|
|
|
|
return resource._GetHashCode();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool Equals(object obj) {
|
|
|
|
|
var other = obj as ResourceKey;
|
|
|
|
|
if (other == null)
|
|
|
|
|
return false;
|
|
|
|
|
return resource._Equals(other.resource);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString() {
|
|
|
|
|
return resource.Name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
if (rsrc.Offset == null)
|
|
|
|
|
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) {
|
|
|
|
|
if (string.IsNullOrEmpty(rsrc.Name))
|
|
|
|
|
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.MetadataToken.ToInt32());
|
|
|
|
|
return list;
|
2012-02-27 05:57:55 +08:00
|
|
|
|
}
|
2012-02-20 10:26:27 +08:00
|
|
|
|
}
|
|
|
|
|
}
|