2012-05-26 06:52:38 +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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
using dot10.DotNet;
|
|
|
|
|
using dot10.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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void find() {
|
|
|
|
|
foreach (var cctor in DeobUtils.getInitCctors(module, 3)) {
|
|
|
|
|
if (checkCctor(cctor))
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +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;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-21 18:14:20 +08:00
|
|
|
|
public bool decrypt(MyPEImage peImage, ref DumpedMethods dumpedMethods) {
|
2012-05-26 06:52:38 +08:00
|
|
|
|
dumpedMethods = new DumpedMethods();
|
|
|
|
|
|
|
|
|
|
bool decrypted = false;
|
|
|
|
|
|
2012-11-21 18:14:20 +08:00
|
|
|
|
var methodDef = peImage.DotNetFile.MetaData.TablesStream.MethodTable;
|
|
|
|
|
for (uint rid = 1; rid <= methodDef.Rows; rid++) {
|
2012-05-26 06:52:38 +08:00
|
|
|
|
var dm = new DumpedMethod();
|
2012-11-21 18:14:20 +08:00
|
|
|
|
peImage.readMethodTableRowTo(dm, rid);
|
|
|
|
|
|
|
|
|
|
if (dm.mdRVA == 0)
|
|
|
|
|
continue;
|
|
|
|
|
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;
|
|
|
|
|
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)
|
2012-11-21 18:14:20 +08:00
|
|
|
|
decrypt(dm.code);
|
2012-05-26 06:52:38 +08:00
|
|
|
|
else
|
2012-11-21 18:14:20 +08:00
|
|
|
|
decrypt(dm.code, seed);
|
2012-05-26 06:52:38 +08:00
|
|
|
|
|
|
|
|
|
dumpedMethods.add(dm);
|
|
|
|
|
decrypted = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return decrypted;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void decrypt(byte[] data) {
|
|
|
|
|
for (int i = 6; i < data.Length; i++)
|
|
|
|
|
data[i] ^= decryptKey[i % decryptKey.Length];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void decrypt(byte[] data, int seed) {
|
|
|
|
|
var key = new KeyGenerator(seed).generate(data.Length);
|
|
|
|
|
for (int i = 6; i < data.Length; i++)
|
|
|
|
|
data[i] ^= key[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void deobfuscate(Blocks blocks) {
|
|
|
|
|
if (initMethod == null)
|
|
|
|
|
return;
|
|
|
|
|
if (blocks.Method.Name != ".cctor")
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
foreach (var block in blocks.MethodBlocks.getAllBlocks()) {
|
|
|
|
|
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;
|
|
|
|
|
block.remove(i, 1);
|
|
|
|
|
i--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|