2012-05-26 06:52:38 +08:00
|
|
|
|
/*
|
2015-10-30 05:45:26 +08:00
|
|
|
|
Copyright (C) 2011-2015 de4dot@gmail.com
|
2012-05-26 06:52:38 +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;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using dnlib.DotNet.Emit;
|
2012-05-26 06:52:38 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.CodeWall {
|
|
|
|
|
class MethodsDecrypter {
|
|
|
|
|
static readonly byte[] newCodeHeader = new byte[6] { 0x2B, 4, 0, 0, 0, 0 };
|
|
|
|
|
static readonly byte[] decryptKey = new byte[10] { 0x8D, 0xB5, 0x2C, 0x3A, 0x1F, 0xC7, 0x31, 0xC3, 0xCD, 0x47 };
|
|
|
|
|
|
2012-11-08 17:40:58 +08:00
|
|
|
|
ModuleDefMD module;
|
|
|
|
|
IMethod initMethod;
|
2012-05-26 06:52:38 +08:00
|
|
|
|
|
|
|
|
|
public bool Detected {
|
|
|
|
|
get { return initMethod != null; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-08 17:40:58 +08:00
|
|
|
|
public MethodsDecrypter(ModuleDefMD module) {
|
2012-05-26 06:52:38 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Find() {
|
|
|
|
|
foreach (var cctor in DeobUtils.GetInitCctors(module, 3)) {
|
|
|
|
|
if (CheckCctor(cctor))
|
2012-05-26 06:52:38 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool CheckCctor(MethodDef method) {
|
2012-05-26 06:52:38 +08:00
|
|
|
|
if (method == null || method.Body == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
foreach (var instr in method.Body.Instructions) {
|
|
|
|
|
if (instr.OpCode.Code != Code.Call)
|
|
|
|
|
continue;
|
2012-11-08 17:40:58 +08:00
|
|
|
|
var calledMethod = instr.Operand as IMethod;
|
2012-05-26 06:52:38 +08:00
|
|
|
|
if (calledMethod == null)
|
|
|
|
|
continue;
|
|
|
|
|
if (calledMethod.DeclaringType.Scope == module)
|
|
|
|
|
return false;
|
|
|
|
|
if (calledMethod.FullName != "System.Void Q::X()")
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
initMethod = calledMethod;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public bool Decrypt(MyPEImage peImage, ref DumpedMethods dumpedMethods) {
|
2012-05-26 06:52:38 +08:00
|
|
|
|
dumpedMethods = new DumpedMethods();
|
|
|
|
|
|
|
|
|
|
bool decrypted = false;
|
|
|
|
|
|
2014-05-23 22:19:25 +08:00
|
|
|
|
var methodDef = peImage.MetaData.TablesStream.MethodTable;
|
2012-11-21 18:14:20 +08:00
|
|
|
|
for (uint rid = 1; rid <= methodDef.Rows; rid++) {
|
2012-05-26 06:52:38 +08:00
|
|
|
|
var dm = new DumpedMethod();
|
2013-01-19 20:03:57 +08:00
|
|
|
|
peImage.ReadMethodTableRowTo(dm, rid);
|
2012-11-21 18:14:20 +08:00
|
|
|
|
|
|
|
|
|
if (dm.mdRVA == 0)
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
uint bodyOffset = peImage.RvaToOffset(dm.mdRVA);
|
2012-05-26 06:52:38 +08:00
|
|
|
|
|
2012-11-21 18:14:20 +08:00
|
|
|
|
peImage.Reader.Position = bodyOffset;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var mbHeader = MethodBodyParser.ParseMethodBody(peImage.Reader, out dm.code, out dm.extraSections);
|
|
|
|
|
peImage.UpdateMethodHeaderInfo(dm, mbHeader);
|
2012-05-26 06:52:38 +08:00
|
|
|
|
|
2012-11-21 18:14:20 +08:00
|
|
|
|
if (dm.code.Length < 6 || dm.code[0] != 0x2A || dm.code[1] != 0x2A)
|
2012-05-26 06:52:38 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-21 18:14:20 +08:00
|
|
|
|
int seed = BitConverter.ToInt32(dm.code, 2);
|
|
|
|
|
Array.Copy(newCodeHeader, dm.code, newCodeHeader.Length);
|
2012-05-26 06:52:38 +08:00
|
|
|
|
if (seed == 0)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
Decrypt(dm.code);
|
2012-05-26 06:52:38 +08:00
|
|
|
|
else
|
2013-01-19 20:03:57 +08:00
|
|
|
|
Decrypt(dm.code, seed);
|
2012-05-26 06:52:38 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
dumpedMethods.Add(dm);
|
2012-05-26 06:52:38 +08:00
|
|
|
|
decrypted = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return decrypted;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void Decrypt(byte[] data) {
|
2012-05-26 06:52:38 +08:00
|
|
|
|
for (int i = 6; i < data.Length; i++)
|
|
|
|
|
data[i] ^= decryptKey[i % decryptKey.Length];
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void Decrypt(byte[] data, int seed) {
|
|
|
|
|
var key = new KeyGenerator(seed).Generate(data.Length);
|
2012-05-26 06:52:38 +08:00
|
|
|
|
for (int i = 6; i < data.Length; i++)
|
|
|
|
|
data[i] ^= key[i];
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Deobfuscate(Blocks blocks) {
|
2012-05-26 06:52:38 +08:00
|
|
|
|
if (initMethod == null)
|
|
|
|
|
return;
|
|
|
|
|
if (blocks.Method.Name != ".cctor")
|
|
|
|
|
return;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
foreach (var block in blocks.MethodBlocks.GetAllBlocks()) {
|
2012-05-26 06:52:38 +08:00
|
|
|
|
var instrs = block.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count; i++) {
|
|
|
|
|
var instr = instrs[i];
|
|
|
|
|
if (instr.OpCode.Code != Code.Call)
|
|
|
|
|
continue;
|
2012-11-08 17:40:58 +08:00
|
|
|
|
var calledMethod = instr.Operand as IMethod;
|
|
|
|
|
if (!MethodEqualityComparer.CompareDeclaringTypes.Equals(calledMethod, initMethod))
|
2012-05-26 06:52:38 +08:00
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
block.Remove(i, 1);
|
2012-05-26 06:52:38 +08:00
|
|
|
|
i--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|