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-02-27 05:57:55 +08:00
|
|
|
|
public override bool getDecryptedModule(ref byte[] newFileData, ref DumpedMethods dumpedMethods) {
|
2012-02-21 00:20:29 +08:00
|
|
|
|
if (!mainType.Detected)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var fileData = DeobUtils.readModule(module);
|
2012-07-21 00:15:40 +08:00
|
|
|
|
decrypterInfo = new DecrypterInfo(mainType, fileData);
|
|
|
|
|
var fileDecrypter = new FileDecrypter(decrypterInfo);
|
|
|
|
|
|
|
|
|
|
if (!fileDecrypter.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));
|
|
|
|
|
staticStringInliner.add(stringDecrypter.Method, (method, args) => stringDecrypter.decrypt((uint)args[0]));
|
|
|
|
|
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-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 {
|
|
|
|
|
throw new UserException(string.Format("Code page {0} doesn't exist", cp));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|