de4dot-cex/de4dot.code/deobfuscators/dotNET_Reactor/v3/DecrypterType.cs

199 lines
5.3 KiB
C#
Raw Permalink Normal View History

2011-12-21 07:31:27 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
2011-12-21 07:31: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/>.
*/
using System;
using System.Collections.Generic;
using System.Text;
using dnlib.DotNet;
2011-12-21 07:31:27 +08:00
using de4dot.blocks;
namespace de4dot.code.deobfuscators.dotNET_Reactor.v3 {
2011-12-21 07:31:27 +08:00
// Find the type that decrypts strings and calls the native lib
class DecrypterType {
2012-11-17 22:46:02 +08:00
ModuleDefMD module;
TypeDef decrypterType;
MethodDef stringDecrypter1;
MethodDef stringDecrypter2;
List<MethodDef> initMethods = new List<MethodDef>();
2012-11-22 16:14:51 +08:00
List<ModuleRef> moduleRefs = new List<ModuleRef>();
2011-12-22 01:20:59 +08:00
Resource linkedResource;
2011-12-21 07:31:27 +08:00
public bool Detected {
get { return decrypterType != null; }
}
public TypeDef Type {
2011-12-21 07:31:27 +08:00
get { return decrypterType; }
}
2011-12-22 01:20:59 +08:00
public Resource LinkedResource {
get { return linkedResource; }
}
public MethodDef StringDecrypter1 {
2011-12-21 07:31:27 +08:00
get { return stringDecrypter1; }
}
public MethodDef StringDecrypter2 {
2011-12-21 07:31:27 +08:00
get { return stringDecrypter2; }
}
public IEnumerable<MethodDef> InitMethods {
2011-12-21 07:31:27 +08:00
get { return initMethods; }
}
public IEnumerable<MethodDef> StringDecrypters {
2011-12-21 07:31:27 +08:00
get {
return new List<MethodDef> {
2011-12-21 07:31:27 +08:00
stringDecrypter1,
stringDecrypter2,
};
}
}
2012-11-17 22:46:02 +08:00
public DecrypterType(ModuleDefMD module) {
2011-12-21 07:31:27 +08:00
this.module = module;
}
2012-11-17 22:46:02 +08:00
public DecrypterType(ModuleDefMD module, DecrypterType oldOne) {
2011-12-21 07:31:27 +08:00
this.module = module;
2013-01-19 20:03:57 +08:00
this.decrypterType = Lookup(oldOne.decrypterType, "Could not find decrypterType");
this.stringDecrypter1 = Lookup(oldOne.stringDecrypter1, "Could not find stringDecrypter1");
this.stringDecrypter2 = Lookup(oldOne.stringDecrypter2, "Could not find stringDecrypter2");
2011-12-21 07:31:27 +08:00
foreach (var method in oldOne.initMethods)
2013-01-19 20:03:57 +08:00
initMethods.Add(Lookup(method, "Could not find initMethod"));
UpdateModuleRefs();
2011-12-21 07:31:27 +08:00
}
2013-01-19 20:03:57 +08:00
T Lookup<T>(T def, string errorMessage) where T : class, ICodedToken {
return DeobUtils.Lookup(module, def, errorMessage);
2011-12-21 07:31:27 +08:00
}
2013-01-19 20:03:57 +08:00
public void Find() {
2011-12-21 07:31:27 +08:00
foreach (var type in module.Types) {
if (type.FullName != "<PrivateImplementationDetails>{B4838DC1-AC79-43d1-949F-41B518B904A8}")
continue;
decrypterType = type;
2013-01-19 20:03:57 +08:00
stringDecrypter1 = GetStringDecrypter(type, "CS$0$0004");
stringDecrypter2 = GetStringDecrypter(type, "CS$0$0005");
2011-12-21 07:31:27 +08:00
foreach (var method in type.Methods) {
2013-01-19 20:03:57 +08:00
if (DotNetUtils.IsMethod(method, "System.Void", "()"))
2011-12-21 07:31:27 +08:00
initMethods.Add(method);
}
2013-01-19 20:03:57 +08:00
UpdateModuleRefs();
2011-12-21 07:31:27 +08:00
return;
}
}
2013-01-19 20:03:57 +08:00
void UpdateModuleRefs() {
2011-12-21 07:41:09 +08:00
foreach (var method in decrypterType.Methods) {
2012-11-17 22:46:02 +08:00
if (method.ImplMap != null) {
switch (method.ImplMap.Name.String) {
2011-12-21 07:41:09 +08:00
case "nr_nli":
case "nr_startup":
2012-11-22 16:14:51 +08:00
moduleRefs.Add(method.ImplMap.Module);
2011-12-21 07:41:09 +08:00
break;
}
}
}
2013-01-19 20:03:57 +08:00
UpdateLinkedResource();
2011-12-22 01:20:59 +08:00
}
2013-01-19 20:03:57 +08:00
void UpdateLinkedResource() {
2012-11-22 16:14:51 +08:00
foreach (var modref in moduleRefs) {
2013-01-19 20:03:57 +08:00
var resource = DotNetUtils.GetResource(module, modref.Name.String) as LinkedResource;
2011-12-22 01:20:59 +08:00
if (resource == null)
continue;
linkedResource = resource;
return;
}
2011-12-21 07:41:09 +08:00
}
2013-01-19 20:03:57 +08:00
MethodDef GetStringDecrypter(TypeDef type, string name) {
2012-11-17 22:46:02 +08:00
var method = type.FindMethod(name);
2011-12-21 07:31:27 +08:00
if (method == null)
return null;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.IsMethod(method, "System.String", "(System.String)"))
2011-12-21 07:31:27 +08:00
return null;
return method;
}
2013-01-19 20:03:57 +08:00
public string Decrypt1(string s) {
2011-12-21 07:31:27 +08:00
var sb = new StringBuilder(s.Length);
foreach (var c in s)
sb.Append((char)(0xFF - (byte)c));
return sb.ToString();
}
2013-01-19 20:03:57 +08:00
public string Decrypt2(string s) {
2011-12-21 07:31:27 +08:00
return Encoding.Unicode.GetString(Convert.FromBase64String(s));
}
2011-12-21 23:56:12 +08:00
2013-01-19 20:03:57 +08:00
public bool Patch(byte[] peData) {
2011-12-21 23:56:12 +08:00
try {
2012-11-17 22:46:02 +08:00
using (var peImage = new MyPEImage(peData))
2013-01-19 20:03:57 +08:00
return Patch2(peImage);
2011-12-21 23:56:12 +08:00
}
catch {
Logger.w("Could not patch the file");
2011-12-21 23:56:12 +08:00
return false;
}
}
2013-01-19 20:03:57 +08:00
bool Patch2(MyPEImage peImage) {
uint numPatches = peImage.OffsetReadUInt32(peImage.Length - 4);
2012-11-17 22:46:02 +08:00
uint offset = checked(peImage.Length - 4 - numPatches * 8);
2011-12-21 23:56:12 +08:00
2011-12-23 00:40:21 +08:00
bool startedPatchingBadData = false;
2011-12-21 23:56:12 +08:00
for (uint i = 0; i < numPatches; i++, offset += 8) {
2013-01-19 20:03:57 +08:00
uint rva = GetValue(peImage.OffsetReadUInt32(offset));
var value = peImage.OffsetReadUInt32(offset + 4);
2011-12-21 23:56:12 +08:00
if (value == 4) {
i++;
offset += 8;
2013-01-19 20:03:57 +08:00
rva = GetValue(peImage.OffsetReadUInt32(offset));
value = peImage.OffsetReadUInt32(offset + 4);
2011-12-21 23:56:12 +08:00
}
else
2013-01-19 20:03:57 +08:00
value = GetValue(value);
2011-12-21 23:56:12 +08:00
2011-12-23 00:40:21 +08:00
// Seems there's a bug in their code where they sometimes overwrite valid data
// with invalid data.
if (startedPatchingBadData && value == 0x3115)
continue;
2013-01-19 20:03:57 +08:00
startedPatchingBadData |= !peImage.DotNetSafeWrite(rva, BitConverter.GetBytes(value));
2011-12-21 23:56:12 +08:00
}
return true;
}
2013-01-19 20:03:57 +08:00
static uint GetValue(uint value) {
2011-12-21 23:56:12 +08:00
const uint magic = 2749;
value = checked(value - magic);
if (value % 3 != 0)
throw new Exception();
return value / 3;
}
2011-12-21 07:31:27 +08:00
}
}