2012-04-22 19:43:43 +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;
|
|
|
|
|
using System.IO;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.IO;
|
2012-04-22 19:43:43 +08:00
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators {
|
2012-04-22 20:13:48 +08:00
|
|
|
|
[Serializable]
|
|
|
|
|
class InvalidMethodBody : Exception {
|
|
|
|
|
public InvalidMethodBody() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public InvalidMethodBody(string msg)
|
|
|
|
|
: base(msg) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-23 02:35:01 +08:00
|
|
|
|
class MethodBodyHeader {
|
|
|
|
|
public ushort flags;
|
|
|
|
|
public ushort maxStack;
|
|
|
|
|
public uint codeSize;
|
|
|
|
|
public uint localVarSigTok;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-22 19:43:43 +08:00
|
|
|
|
static class MethodBodyParser {
|
2012-11-21 18:14:20 +08:00
|
|
|
|
public static MethodBodyHeader parseMethodBody(IBinaryReader reader, out byte[] code, out byte[] extraSections) {
|
2012-04-22 20:13:48 +08:00
|
|
|
|
try {
|
2012-04-23 02:35:01 +08:00
|
|
|
|
return parseMethodBody2(reader, out code, out extraSections);
|
2012-04-22 20:13:48 +08:00
|
|
|
|
}
|
|
|
|
|
catch (IOException) {
|
|
|
|
|
throw new InvalidMethodBody();
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-04-22 19:43:43 +08:00
|
|
|
|
|
2012-07-20 20:48:19 +08:00
|
|
|
|
public static bool verify(byte[] data) {
|
2012-11-21 18:14:20 +08:00
|
|
|
|
return verify(MemoryImageStream.Create(data));
|
2012-07-20 20:48:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-21 18:14:20 +08:00
|
|
|
|
public static bool verify(IBinaryReader reader) {
|
2012-07-20 20:48:19 +08:00
|
|
|
|
try {
|
|
|
|
|
byte[] code, extraSections;
|
|
|
|
|
parseMethodBody(reader, out code, out extraSections);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (InvalidMethodBody) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-21 18:14:20 +08:00
|
|
|
|
static MethodBodyHeader parseMethodBody2(IBinaryReader reader, out byte[] code, out byte[] extraSections) {
|
2012-04-23 02:35:01 +08:00
|
|
|
|
var mbHeader = new MethodBodyHeader();
|
|
|
|
|
|
|
|
|
|
uint codeOffset;
|
2012-08-23 17:48:11 +08:00
|
|
|
|
byte b = peek(reader);
|
|
|
|
|
if ((b & 3) == 2) {
|
2012-04-23 02:35:01 +08:00
|
|
|
|
mbHeader.flags = 2;
|
|
|
|
|
mbHeader.maxStack = 8;
|
|
|
|
|
mbHeader.codeSize = (uint)(reader.ReadByte() >> 2);
|
|
|
|
|
mbHeader.localVarSigTok = 0;
|
2012-04-22 19:43:43 +08:00
|
|
|
|
codeOffset = 1;
|
2012-08-23 17:48:11 +08:00
|
|
|
|
}
|
|
|
|
|
else if ((b & 7) == 3) {
|
2012-04-23 02:35:01 +08:00
|
|
|
|
mbHeader.flags = reader.ReadUInt16();
|
|
|
|
|
codeOffset = (uint)(4 * (mbHeader.flags >> 12));
|
2012-04-22 20:13:48 +08:00
|
|
|
|
if (codeOffset != 12)
|
|
|
|
|
throw new InvalidMethodBody();
|
2012-04-23 02:35:01 +08:00
|
|
|
|
mbHeader.maxStack = reader.ReadUInt16();
|
|
|
|
|
mbHeader.codeSize = reader.ReadUInt32();
|
|
|
|
|
if (mbHeader.codeSize > int.MaxValue)
|
2012-04-22 20:13:48 +08:00
|
|
|
|
throw new InvalidMethodBody();
|
2012-04-23 02:35:01 +08:00
|
|
|
|
mbHeader.localVarSigTok = reader.ReadUInt32();
|
|
|
|
|
if (mbHeader.localVarSigTok != 0 && (mbHeader.localVarSigTok >> 24) != 0x11)
|
2012-04-22 20:13:48 +08:00
|
|
|
|
throw new InvalidMethodBody();
|
2012-04-22 19:43:43 +08:00
|
|
|
|
}
|
2012-08-23 17:48:11 +08:00
|
|
|
|
else
|
|
|
|
|
throw new InvalidMethodBody();
|
2012-04-22 19:43:43 +08:00
|
|
|
|
|
2012-11-21 18:14:20 +08:00
|
|
|
|
if (mbHeader.codeSize + codeOffset > reader.Length)
|
2012-04-22 20:13:48 +08:00
|
|
|
|
throw new InvalidMethodBody();
|
2012-04-23 02:35:01 +08:00
|
|
|
|
code = reader.ReadBytes((int)mbHeader.codeSize);
|
2012-04-22 19:43:43 +08:00
|
|
|
|
|
2012-04-23 02:35:01 +08:00
|
|
|
|
if ((mbHeader.flags & 8) != 0)
|
2012-04-26 00:06:27 +08:00
|
|
|
|
extraSections = readExtraSections2(reader);
|
2012-04-22 19:43:43 +08:00
|
|
|
|
else
|
|
|
|
|
extraSections = null;
|
2012-04-23 02:35:01 +08:00
|
|
|
|
|
|
|
|
|
return mbHeader;
|
2012-04-22 19:43:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-21 18:14:20 +08:00
|
|
|
|
static void align(IBinaryReader reader, int alignment) {
|
|
|
|
|
reader.Position = (reader.Position + alignment - 1) & ~(alignment - 1);
|
2012-04-22 19:43:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-21 18:14:20 +08:00
|
|
|
|
public static byte[] readExtraSections(IBinaryReader reader) {
|
2012-04-26 00:06:27 +08:00
|
|
|
|
try {
|
|
|
|
|
return readExtraSections2(reader);
|
|
|
|
|
}
|
|
|
|
|
catch (IOException) {
|
|
|
|
|
throw new InvalidMethodBody();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-21 18:14:20 +08:00
|
|
|
|
static byte[] readExtraSections2(IBinaryReader reader) {
|
2012-04-22 19:43:43 +08:00
|
|
|
|
align(reader, 4);
|
2012-11-21 18:14:20 +08:00
|
|
|
|
int startPos = (int)reader.Position;
|
2012-04-22 19:43:43 +08:00
|
|
|
|
parseSection(reader);
|
2012-11-21 18:14:20 +08:00
|
|
|
|
int size = (int)reader.Position - startPos;
|
|
|
|
|
reader.Position = startPos;
|
2012-04-22 19:43:43 +08:00
|
|
|
|
return reader.ReadBytes(size);
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-21 18:14:20 +08:00
|
|
|
|
static void parseSection(IBinaryReader reader) {
|
2012-04-22 19:43:43 +08:00
|
|
|
|
byte flags;
|
|
|
|
|
do {
|
|
|
|
|
align(reader, 4);
|
|
|
|
|
|
|
|
|
|
flags = reader.ReadByte();
|
|
|
|
|
if ((flags & 1) == 0)
|
2012-04-22 20:13:48 +08:00
|
|
|
|
throw new InvalidMethodBody("Not an exception section");
|
2012-04-22 19:43:43 +08:00
|
|
|
|
if ((flags & 0x3E) != 0)
|
2012-04-22 20:13:48 +08:00
|
|
|
|
throw new InvalidMethodBody("Invalid bits set");
|
2012-04-22 19:43:43 +08:00
|
|
|
|
|
|
|
|
|
if ((flags & 0x40) != 0) {
|
2012-11-21 18:14:20 +08:00
|
|
|
|
reader.Position--;
|
2012-04-22 19:43:43 +08:00
|
|
|
|
int num = (int)(reader.ReadUInt32() >> 8) / 24;
|
2012-11-21 18:14:20 +08:00
|
|
|
|
reader.Position += num * 24;
|
2012-04-22 19:43:43 +08:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
int num = reader.ReadByte() / 12;
|
2012-11-21 18:14:20 +08:00
|
|
|
|
reader.Position += 2 + num * 12;
|
2012-04-22 19:43:43 +08:00
|
|
|
|
}
|
|
|
|
|
} while ((flags & 0x80) != 0);
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-21 18:14:20 +08:00
|
|
|
|
static byte peek(IBinaryReader reader) {
|
2012-04-22 19:43:43 +08:00
|
|
|
|
byte b = reader.ReadByte();
|
2012-11-21 18:14:20 +08:00
|
|
|
|
reader.Position--;
|
2012-04-22 19:43:43 +08:00
|
|
|
|
return b;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|