2012-04-10 22:17:45 +08:00
|
|
|
|
/*
|
2013-01-02 00:03:16 +08:00
|
|
|
|
Copyright (C) 2011-2013 de4dot@gmail.com
|
2012-04-10 22:17:45 +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.IO;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
2013-11-08 18:05:17 +08:00
|
|
|
|
using System.Runtime.ExceptionServices;
|
2012-04-10 22:17:45 +08:00
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Reflection;
|
2013-11-08 18:05:17 +08:00
|
|
|
|
using System.Security;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using dnlib.DotNet.MD;
|
2013-11-08 18:05:17 +08:00
|
|
|
|
using dnlib.PE;
|
2012-04-10 22:17:45 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
2013-11-08 18:05:17 +08:00
|
|
|
|
namespace System.Runtime.ExceptionServices {
|
|
|
|
|
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
|
|
|
|
|
class HandleProcessCorruptedStateExceptionsAttribute : Attribute {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-10 22:17:45 +08:00
|
|
|
|
namespace de4dot.mdecrypt {
|
|
|
|
|
public class DynamicMethodsDecrypter {
|
|
|
|
|
static DynamicMethodsDecrypter instance;
|
|
|
|
|
DecryptMethodsInfo decryptMethodsInfo;
|
|
|
|
|
|
|
|
|
|
struct FuncPtrInfo<D> {
|
|
|
|
|
public D del;
|
|
|
|
|
public IntPtr ptr;
|
|
|
|
|
public IntPtr ptrInDll;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Prepare(Delegate del) {
|
2012-04-10 22:17:45 +08:00
|
|
|
|
RuntimeHelpers.PrepareDelegate(del);
|
|
|
|
|
ptr = Marshal.GetFunctionPointerForDelegate(del);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
|
|
|
|
struct IMAGE_SECTION_HEADER {
|
|
|
|
|
public ulong name;
|
|
|
|
|
public uint VirtualSize;
|
|
|
|
|
public uint VirtualAddress;
|
|
|
|
|
public uint SizeOfRawData;
|
|
|
|
|
public uint PointerToRawData;
|
|
|
|
|
public uint PointerToRelocations;
|
|
|
|
|
public uint PointerToLinenumbers;
|
|
|
|
|
public ushort NumberOfRelocations;
|
|
|
|
|
public ushort NumberOfLinenumbers;
|
|
|
|
|
public uint Characteristics;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack=1, Size=0x88)]
|
|
|
|
|
struct CORINFO_METHOD_INFO {
|
|
|
|
|
public IntPtr ftn;
|
|
|
|
|
public IntPtr scope;
|
|
|
|
|
public IntPtr ILCode;
|
|
|
|
|
public uint ILCodeSize;
|
|
|
|
|
public ushort maxStack;
|
|
|
|
|
public ushort EHCount;
|
|
|
|
|
// 0x64 other bytes here...
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class DecryptContext {
|
|
|
|
|
public DumpedMethod dm;
|
2012-11-01 16:33:02 +08:00
|
|
|
|
public MethodDef method;
|
2012-04-10 22:17:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FuncPtrInfo<CompileMethod> ourCompileMethodInfo = new FuncPtrInfo<CompileMethod>();
|
|
|
|
|
FuncPtrInfo<ReturnMethodToken> returnMethodTokenInfo = new FuncPtrInfo<ReturnMethodToken>();
|
|
|
|
|
FuncPtrInfo<ReturnNameOfMethod> returnNameOfMethodInfo = new FuncPtrInfo<ReturnNameOfMethod>();
|
|
|
|
|
|
|
|
|
|
IntPtr origCompileMethod;
|
|
|
|
|
IntPtr jitterTextFreeMem;
|
|
|
|
|
|
|
|
|
|
IntPtr callMethod;
|
|
|
|
|
CallMethod callMethodDelegate;
|
|
|
|
|
|
|
|
|
|
IntPtr jitterInstance;
|
|
|
|
|
IntPtr jitterVtbl;
|
|
|
|
|
Module moduleToDecrypt;
|
|
|
|
|
IntPtr hInstModule;
|
|
|
|
|
IntPtr ourCompMem;
|
|
|
|
|
bool compileMethodIsThisCall;
|
2012-07-23 00:37:14 +08:00
|
|
|
|
IntPtr ourCodeAddr;
|
2012-04-10 22:17:45 +08:00
|
|
|
|
|
2012-11-21 18:14:20 +08:00
|
|
|
|
MDTable methodDefTable;
|
2012-04-10 22:17:45 +08:00
|
|
|
|
IntPtr methodDefTablePtr;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
ModuleDefMD dnlibModule;
|
2012-11-01 16:33:02 +08:00
|
|
|
|
MethodDef moduleCctor;
|
2012-04-10 22:17:45 +08:00
|
|
|
|
uint moduleCctorCodeRva;
|
|
|
|
|
IntPtr moduleToDecryptScope;
|
|
|
|
|
|
|
|
|
|
DecryptContext ctx = new DecryptContext();
|
|
|
|
|
|
|
|
|
|
public static DynamicMethodsDecrypter Instance {
|
|
|
|
|
get {
|
|
|
|
|
if (instance != null)
|
|
|
|
|
return instance;
|
|
|
|
|
return instance = new DynamicMethodsDecrypter();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-23 11:12:10 +08:00
|
|
|
|
static Version VersionNet45DevPreview = new Version(4, 0, 30319, 17020);
|
|
|
|
|
static Version VersionNet45Rtm = new Version(4, 0, 30319, 17929);
|
2012-04-10 22:17:45 +08:00
|
|
|
|
DynamicMethodsDecrypter() {
|
|
|
|
|
if (UIntPtr.Size != 4)
|
|
|
|
|
throw new ApplicationException("Only 32-bit dynamic methods decryption is supported");
|
|
|
|
|
|
2012-11-23 11:12:10 +08:00
|
|
|
|
// .NET 4.5 beta/preview/RC compileMethod has thiscall calling convention, but they
|
|
|
|
|
// switched back to stdcall in .NET 4.5 RTM
|
|
|
|
|
compileMethodIsThisCall = Environment.Version >= VersionNet45DevPreview &&
|
|
|
|
|
Environment.Version < VersionNet45Rtm;
|
2012-04-10 22:17:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DllImport("kernel32", CharSet = CharSet.Ansi)]
|
|
|
|
|
static extern IntPtr GetModuleHandle(string name);
|
|
|
|
|
|
|
|
|
|
[DllImport("kernel32", CharSet = CharSet.Ansi)]
|
|
|
|
|
static extern IntPtr GetProcAddress(IntPtr hModule, string name);
|
|
|
|
|
|
|
|
|
|
[DllImport("kernel32")]
|
|
|
|
|
static extern bool VirtualProtect(IntPtr addr, int size, uint newProtect, out uint oldProtect);
|
|
|
|
|
const uint PAGE_EXECUTE_READWRITE = 0x40;
|
|
|
|
|
|
|
|
|
|
[DllImport("kernel32")]
|
2012-07-23 00:37:14 +08:00
|
|
|
|
static extern IntPtr VirtualAlloc(IntPtr lpAddress, UIntPtr dwSize, uint flAllocationType, uint flProtect);
|
2012-04-10 22:17:45 +08:00
|
|
|
|
|
2013-11-08 18:05:17 +08:00
|
|
|
|
[DllImport("kernel32")]
|
|
|
|
|
static extern bool GetModuleHandleEx(uint dwFlags, IntPtr lpModuleName, out IntPtr phModule);
|
|
|
|
|
|
2012-04-10 22:17:45 +08:00
|
|
|
|
delegate IntPtr GetJit();
|
|
|
|
|
delegate int CompileMethod(IntPtr jitter, IntPtr comp, IntPtr info, uint flags, IntPtr nativeEntry, IntPtr nativeSizeOfCode, out bool handled);
|
|
|
|
|
delegate int ReturnMethodToken();
|
|
|
|
|
delegate string ReturnNameOfMethod();
|
|
|
|
|
delegate int CallMethod(IntPtr compileMethod, IntPtr jitter, IntPtr comp, IntPtr info, uint flags, IntPtr nativeEntry, IntPtr nativeSizeOfCode);
|
|
|
|
|
|
|
|
|
|
public DecryptMethodsInfo DecryptMethodsInfo {
|
|
|
|
|
set { decryptMethodsInfo = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public unsafe Module Module {
|
|
|
|
|
set {
|
|
|
|
|
if (moduleToDecrypt != null)
|
|
|
|
|
throw new ApplicationException("Module has already been initialized");
|
|
|
|
|
|
|
|
|
|
moduleToDecrypt = value;
|
|
|
|
|
hInstModule = Marshal.GetHINSTANCE(moduleToDecrypt);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
moduleToDecryptScope = GetScope(moduleToDecrypt);
|
2012-04-10 22:17:45 +08:00
|
|
|
|
|
2012-12-20 09:06:09 +08:00
|
|
|
|
dnlibModule = ModuleDefMD.Load(hInstModule);
|
|
|
|
|
methodDefTable = dnlibModule.TablesStream.MethodTable;
|
|
|
|
|
methodDefTablePtr = new IntPtr((byte*)hInstModule + (uint)dnlibModule.MetaData.PEImage.ToRVA(methodDefTable.StartOffset));
|
2012-04-10 22:17:45 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
InitializeDNLibMethods();
|
2012-04-10 22:17:45 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static IntPtr GetScope(Module module) {
|
|
|
|
|
var obj = GetFieldValue(module.ModuleHandle, "m_ptr");
|
2012-04-10 22:17:45 +08:00
|
|
|
|
if (obj is IntPtr)
|
|
|
|
|
return (IntPtr)obj;
|
|
|
|
|
if (obj.GetType().ToString() == "System.Reflection.RuntimeModule")
|
2013-01-19 20:03:57 +08:00
|
|
|
|
return (IntPtr)GetFieldValue(obj, "m_pData");
|
2012-04-10 22:17:45 +08:00
|
|
|
|
|
|
|
|
|
throw new ApplicationException(string.Format("m_ptr is an invalid type: {0}", obj.GetType()));
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static object GetFieldValue(object obj, string fieldName) {
|
2012-04-10 22:17:45 +08:00
|
|
|
|
var field = obj.GetType().GetField(fieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
|
|
|
|
|
if (field == null)
|
|
|
|
|
throw new ApplicationException(string.Format("Could not get field {0}::{1}", obj.GetType(), fieldName));
|
|
|
|
|
return field.GetValue(obj);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
unsafe void InitializeDNLibMethods() {
|
2012-12-20 09:06:09 +08:00
|
|
|
|
moduleCctor = dnlibModule.GlobalType.FindStaticConstructor();
|
2012-04-10 22:17:45 +08:00
|
|
|
|
if (moduleCctor == null)
|
|
|
|
|
moduleCctorCodeRva = 0;
|
|
|
|
|
else {
|
2012-11-01 16:33:02 +08:00
|
|
|
|
byte* p = (byte*)hInstModule + (uint)moduleCctor.RVA;
|
2012-04-10 22:17:45 +08:00
|
|
|
|
if ((*p & 3) == 2)
|
|
|
|
|
moduleCctorCodeRva = (uint)moduleCctor.RVA + 1;
|
|
|
|
|
else
|
2012-11-01 16:33:02 +08:00
|
|
|
|
moduleCctorCodeRva = (uint)((uint)moduleCctor.RVA + (p[1] >> 4) * 4);
|
2012-04-10 22:17:45 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public unsafe void InstallCompileMethod() {
|
|
|
|
|
var hJitterDll = GetJitterDllHandle();
|
|
|
|
|
jitterTextFreeMem = GetEndOfText(hJitterDll);
|
2012-04-10 22:17:45 +08:00
|
|
|
|
|
|
|
|
|
var getJitPtr = GetProcAddress(hJitterDll, "getJit");
|
|
|
|
|
var getJit = (GetJit)Marshal.GetDelegateForFunctionPointer(getJitPtr, typeof(GetJit));
|
|
|
|
|
jitterInstance = getJit();
|
|
|
|
|
jitterVtbl = *(IntPtr*)jitterInstance;
|
|
|
|
|
origCompileMethod = *(IntPtr*)jitterVtbl;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
PrepareMethods();
|
|
|
|
|
InitializeDelegateFunctionPointers();
|
|
|
|
|
CreateOurCode();
|
2012-04-10 22:17:45 +08:00
|
|
|
|
callMethodDelegate = (CallMethod)Marshal.GetDelegateForFunctionPointer(callMethod, typeof(CallMethod));
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
WriteCompileMethod(ourCompileMethodInfo.ptrInDll);
|
2012-07-23 00:37:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
unsafe void WriteCompileMethod(IntPtr newCompileMethod) {
|
2012-04-10 22:17:45 +08:00
|
|
|
|
uint oldProtect;
|
|
|
|
|
if (!VirtualProtect(jitterVtbl, IntPtr.Size, PAGE_EXECUTE_READWRITE, out oldProtect))
|
|
|
|
|
throw new ApplicationException("Could not enable write access to jitter vtbl");
|
2012-07-23 00:37:14 +08:00
|
|
|
|
*(IntPtr*)jitterVtbl = newCompileMethod;
|
2012-04-10 22:17:45 +08:00
|
|
|
|
VirtualProtect(jitterVtbl, IntPtr.Size, oldProtect, out oldProtect);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void InitializeDelegateFunctionPointers() {
|
|
|
|
|
ourCompileMethodInfo.Prepare(ourCompileMethodInfo.del = TheCompileMethod);
|
|
|
|
|
returnMethodTokenInfo.Prepare(returnMethodTokenInfo.del = ReturnMethodToken2);
|
|
|
|
|
returnNameOfMethodInfo.Prepare(returnNameOfMethodInfo.del = ReturnNameOfMethod2);
|
2012-04-10 22:17:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void LoadObfuscator() {
|
2012-04-10 22:17:45 +08:00
|
|
|
|
RuntimeHelpers.RunModuleConstructor(moduleToDecrypt.ModuleHandle);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public unsafe bool CanDecryptMethods() {
|
2012-04-10 22:17:45 +08:00
|
|
|
|
return *(IntPtr*)jitterVtbl != ourCompileMethodInfo.ptrInDll &&
|
|
|
|
|
*(IntPtr*)jitterVtbl != origCompileMethod;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
unsafe static IntPtr GetEndOfText(IntPtr hDll) {
|
2012-04-10 22:17:45 +08:00
|
|
|
|
byte* p = (byte*)hDll;
|
|
|
|
|
p += *(uint*)(p + 0x3C); // add DOSHDR.e_lfanew
|
|
|
|
|
p += 4;
|
|
|
|
|
int numSections = *(ushort*)(p + 2);
|
|
|
|
|
int sizeOptionalHeader = *(ushort*)(p + 0x10);
|
|
|
|
|
p += 0x14;
|
|
|
|
|
uint sectionAlignment = *(uint*)(p + 0x20);
|
|
|
|
|
p += sizeOptionalHeader;
|
|
|
|
|
|
|
|
|
|
var textName = new byte[8] { (byte)'.', (byte)'t', (byte)'e', (byte)'x', (byte)'t', 0, 0, 0 };
|
|
|
|
|
var name = new byte[8];
|
|
|
|
|
var pSection = (IMAGE_SECTION_HEADER*)p;
|
|
|
|
|
for (int i = 0; i < numSections; i++, pSection++) {
|
|
|
|
|
Marshal.Copy(new IntPtr(pSection), name, 0, name.Length);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!CompareName(textName, name, name.Length))
|
2012-04-10 22:17:45 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
uint size = pSection->VirtualSize;
|
|
|
|
|
uint rva = pSection->VirtualAddress;
|
2012-06-27 16:27:16 +08:00
|
|
|
|
int displ = -8;
|
2012-06-02 13:26:21 +08:00
|
|
|
|
return new IntPtr((byte*)hDll + rva + size + displ);
|
2012-04-10 22:17:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new ApplicationException("Could not find .text section");
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static bool CompareName(byte[] b1, byte[] b2, int len) {
|
2012-04-10 22:17:45 +08:00
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
|
if (b1[i] != b2[i])
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void PrepareMethods() {
|
2012-04-10 22:17:45 +08:00
|
|
|
|
Marshal.PrelinkAll(GetType());
|
|
|
|
|
foreach (var methodInfo in GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance))
|
|
|
|
|
RuntimeHelpers.PrepareMethod(methodInfo.MethodHandle);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
unsafe void CreateOurCode() {
|
2012-04-10 22:17:45 +08:00
|
|
|
|
var code = new NativeCodeGenerator();
|
|
|
|
|
|
|
|
|
|
// our compileMethod() func
|
|
|
|
|
int compileMethodOffset = code.Size;
|
|
|
|
|
|
|
|
|
|
int numPushedArgs = compileMethodIsThisCall ? 5 : 6;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
code.WriteByte(0x51); // push ecx
|
|
|
|
|
code.WriteByte(0x50); // push eax
|
|
|
|
|
code.WriteByte(0x54); // push esp
|
2012-04-10 22:17:45 +08:00
|
|
|
|
for (int i = 0; i < 5; i++)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
WritePushDwordPtrEspDispl(code, (sbyte)(0xC + numPushedArgs * 4)); // push dword ptr [esp+XXh]
|
2012-04-10 22:17:45 +08:00
|
|
|
|
if (!compileMethodIsThisCall)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
WritePushDwordPtrEspDispl(code, (sbyte)(0xC + numPushedArgs * 4)); // push dword ptr [esp+XXh]
|
2012-04-10 22:17:45 +08:00
|
|
|
|
else
|
2013-01-19 20:03:57 +08:00
|
|
|
|
code.WriteByte(0x51); // push ecx
|
|
|
|
|
code.WriteCall(ourCompileMethodInfo.ptr);
|
|
|
|
|
code.WriteByte(0x5A); // pop edx
|
|
|
|
|
code.WriteByte(0x59); // pop ecx
|
|
|
|
|
code.WriteBytes(0x84, 0xD2); // test dl, dl
|
|
|
|
|
code.WriteBytes(0x74, 0x03); // jz $+5
|
|
|
|
|
code.WriteBytes(0xC2, (ushort)(numPushedArgs * 4)); // retn 14h/18h
|
2012-04-10 22:17:45 +08:00
|
|
|
|
for (int i = 0; i < numPushedArgs; i++)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
WritePushDwordPtrEspDispl(code, (sbyte)(numPushedArgs * 4)); // push dword ptr [esp+XXh]
|
|
|
|
|
code.WriteCall(origCompileMethod);
|
|
|
|
|
code.WriteBytes(0xC2, (ushort)(numPushedArgs * 4)); // retn 14h/18h
|
2012-04-10 22:17:45 +08:00
|
|
|
|
|
|
|
|
|
// Our callMethod() code. 1st arg is the method to call. stdcall calling convention.
|
|
|
|
|
int callMethodOffset = code.Size;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
code.WriteByte(0x58); // pop eax (ret addr)
|
|
|
|
|
code.WriteByte(0x5A); // pop edx (method to call)
|
2012-04-10 22:17:45 +08:00
|
|
|
|
if (compileMethodIsThisCall)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
code.WriteByte(0x59); // pop ecx (this ptr)
|
|
|
|
|
code.WriteByte(0x50); // push eax (ret addr)
|
|
|
|
|
code.WriteBytes(0xFF, 0xE2); // jmp edx
|
2012-04-10 22:17:45 +08:00
|
|
|
|
|
|
|
|
|
// Returns token of method
|
|
|
|
|
int getMethodTokenOffset = code.Size;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
code.WriteCall(returnMethodTokenInfo.ptr);
|
|
|
|
|
code.WriteBytes(0xC2, (ushort)(IntPtr.Size * 2));
|
2012-04-10 22:17:45 +08:00
|
|
|
|
|
|
|
|
|
// Returns name of method
|
|
|
|
|
int getMethodNameOffset = code.Size;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
code.WriteCall(returnNameOfMethodInfo.ptr);
|
|
|
|
|
code.WriteBytes(0xC2, (ushort)(IntPtr.Size * 3));
|
2012-04-10 22:17:45 +08:00
|
|
|
|
|
2012-07-23 00:37:14 +08:00
|
|
|
|
ourCodeAddr = VirtualAlloc(IntPtr.Zero, new UIntPtr((ulong)code.Size), 0x00001000, PAGE_EXECUTE_READWRITE);
|
|
|
|
|
IntPtr baseAddr = ourCodeAddr;
|
2012-04-10 22:17:45 +08:00
|
|
|
|
ourCompileMethodInfo.ptrInDll = new IntPtr((byte*)baseAddr + compileMethodOffset);
|
|
|
|
|
callMethod = new IntPtr((byte*)baseAddr + callMethodOffset);
|
|
|
|
|
returnMethodTokenInfo.ptrInDll = new IntPtr((byte*)baseAddr + getMethodTokenOffset);
|
|
|
|
|
returnNameOfMethodInfo.ptrInDll = new IntPtr((byte*)baseAddr + getMethodNameOffset);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
byte[] theCode = code.GetCode(baseAddr);
|
2012-07-23 00:37:14 +08:00
|
|
|
|
Marshal.Copy(theCode, 0, baseAddr, theCode.Length);
|
2012-04-10 22:17:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Writes push dword ptr [esp+displ]
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static void WritePushDwordPtrEspDispl(NativeCodeGenerator code, sbyte displ) {
|
|
|
|
|
code.WriteBytes(0xFF, 0x74);
|
|
|
|
|
code.WriteBytes(0x24, (byte)displ);
|
2012-04-10 22:17:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static IntPtr GetJitterDllHandle() {
|
2012-04-10 22:17:45 +08:00
|
|
|
|
var hJitterDll = GetModuleHandle("mscorjit");
|
|
|
|
|
if (hJitterDll == IntPtr.Zero)
|
|
|
|
|
hJitterDll = GetModuleHandle("clrjit");
|
|
|
|
|
if (hJitterDll == IntPtr.Zero)
|
|
|
|
|
throw new ApplicationException("Could not get a handle to the jitter DLL");
|
|
|
|
|
return hJitterDll;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
unsafe int TheCompileMethod(IntPtr jitter, IntPtr comp, IntPtr info, uint flags, IntPtr nativeEntry, IntPtr nativeSizeOfCode, out bool handled) {
|
2012-04-10 22:17:45 +08:00
|
|
|
|
if (ourCompMem != IntPtr.Zero && comp == ourCompMem) {
|
|
|
|
|
// We're decrypting methods
|
|
|
|
|
var info2 = (CORINFO_METHOD_INFO*)info;
|
|
|
|
|
ctx.dm.code = new byte[info2->ILCodeSize];
|
2012-04-11 03:51:45 +08:00
|
|
|
|
|
2012-04-10 22:17:45 +08:00
|
|
|
|
Marshal.Copy(info2->ILCode, ctx.dm.code, 0, ctx.dm.code.Length);
|
2012-04-11 03:51:45 +08:00
|
|
|
|
ctx.dm.mhMaxStack = info2->maxStack;
|
|
|
|
|
ctx.dm.mhCodeSize = info2->ILCodeSize;
|
|
|
|
|
if ((ctx.dm.mhFlags & 8) != 0)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
ctx.dm.extraSections = ReadExtraSections((byte*)info2->ILCode + info2->ILCodeSize);
|
2012-04-10 22:17:45 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
UpdateFromMethodDefTableRow();
|
2012-04-10 22:17:45 +08:00
|
|
|
|
|
|
|
|
|
handled = true;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// We're not decrypting methods
|
|
|
|
|
|
|
|
|
|
var info2 = (CORINFO_METHOD_INFO*)info;
|
2013-11-08 18:05:17 +08:00
|
|
|
|
if (info2->scope != moduleToDecryptScope ||
|
|
|
|
|
decryptMethodsInfo.moduleCctorBytes == null ||
|
|
|
|
|
moduleCctorCodeRva == 0) {
|
2012-04-10 22:17:45 +08:00
|
|
|
|
handled = false;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint codeRva = (uint)((byte*)info2->ILCode - (byte*)hInstModule);
|
2013-11-08 18:05:17 +08:00
|
|
|
|
if (moduleCctorCodeRva == codeRva) {
|
2012-04-10 22:17:45 +08:00
|
|
|
|
fixed (byte* newIlCodeBytes = &decryptMethodsInfo.moduleCctorBytes[0]) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
WriteCompileMethod(origCompileMethod);
|
2012-04-10 22:17:45 +08:00
|
|
|
|
info2->ILCode = new IntPtr(newIlCodeBytes);
|
|
|
|
|
info2->ILCodeSize = (uint)decryptMethodsInfo.moduleCctorBytes.Length;
|
|
|
|
|
handled = true;
|
|
|
|
|
return callMethodDelegate(origCompileMethod, jitter, comp, info, flags, nativeEntry, nativeSizeOfCode);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handled = false;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
unsafe static byte* Align(byte* p, int alignment) {
|
2012-04-11 03:51:45 +08:00
|
|
|
|
return (byte*)new IntPtr((long)((ulong)(p + alignment - 1) & ~(ulong)(alignment - 1)));
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
unsafe static byte[] ReadExtraSections(byte* p) {
|
|
|
|
|
p = Align(p, 4);
|
2012-04-11 03:51:45 +08:00
|
|
|
|
byte* startPos = p;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
p = ParseSection(p);
|
2012-04-11 03:51:45 +08:00
|
|
|
|
int size = (int)(p - startPos);
|
|
|
|
|
var sections = new byte[size];
|
|
|
|
|
Marshal.Copy(new IntPtr(startPos), sections, 0, sections.Length);
|
|
|
|
|
return sections;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
unsafe static byte* ParseSection(byte* p) {
|
2012-04-11 03:51:45 +08:00
|
|
|
|
byte flags;
|
|
|
|
|
do {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
p = Align(p, 4);
|
2012-04-11 03:51:45 +08:00
|
|
|
|
|
|
|
|
|
flags = *p++;
|
|
|
|
|
if ((flags & 1) == 0)
|
|
|
|
|
throw new ApplicationException("Not an exception section");
|
|
|
|
|
if ((flags & 0x3E) != 0)
|
|
|
|
|
throw new ApplicationException("Invalid bits set");
|
|
|
|
|
|
|
|
|
|
if ((flags & 0x40) != 0) {
|
|
|
|
|
p--;
|
|
|
|
|
int num = (int)(*(uint*)p >> 8) / 24;
|
|
|
|
|
p += 4 + num * 24;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
int num = *p++ / 12;
|
|
|
|
|
p += 2 + num * 12;
|
|
|
|
|
}
|
|
|
|
|
} while ((flags & 0x80) != 0);
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
unsafe void UpdateFromMethodDefTableRow() {
|
2012-11-21 18:14:20 +08:00
|
|
|
|
uint methodIndex = ctx.dm.token - 0x06000001;
|
|
|
|
|
byte* row = (byte*)methodDefTablePtr + methodIndex * methodDefTable.RowSize;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
ctx.dm.mdRVA = Read(row, methodDefTable.Columns[0]);
|
|
|
|
|
ctx.dm.mdImplFlags = (ushort)Read(row, methodDefTable.Columns[1]);
|
|
|
|
|
ctx.dm.mdFlags = (ushort)Read(row, methodDefTable.Columns[2]);
|
|
|
|
|
ctx.dm.mdName = Read(row, methodDefTable.Columns[3]);
|
|
|
|
|
ctx.dm.mdSignature = Read(row, methodDefTable.Columns[4]);
|
|
|
|
|
ctx.dm.mdParamList = Read(row, methodDefTable.Columns[5]);
|
2012-04-11 03:51:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static unsafe uint Read(byte* row, ColumnInfo colInfo) {
|
2012-11-21 18:14:20 +08:00
|
|
|
|
switch (colInfo.Size) {
|
|
|
|
|
case 1: return *(row + colInfo.Offset);
|
|
|
|
|
case 2: return *(ushort*)(row + colInfo.Offset);
|
|
|
|
|
case 4: return *(uint*)(row + colInfo.Offset);
|
|
|
|
|
default: throw new ApplicationException(string.Format("Unknown size: {0}", colInfo.Size));
|
2012-04-10 22:17:45 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
string ReturnNameOfMethod2() {
|
2012-11-01 16:33:02 +08:00
|
|
|
|
return ctx.method.Name.String;
|
2012-04-10 22:17:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
int ReturnMethodToken2() {
|
2012-11-01 16:33:02 +08:00
|
|
|
|
return ctx.method.MDToken.ToInt32();
|
2012-04-10 22:17:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public DumpedMethods DecryptMethods() {
|
|
|
|
|
if (!CanDecryptMethods())
|
2012-07-23 00:37:14 +08:00
|
|
|
|
throw new ApplicationException("Can't decrypt methods since compileMethod() isn't hooked yet");
|
2013-01-19 20:03:57 +08:00
|
|
|
|
InstallCompileMethod2();
|
2012-07-23 00:37:14 +08:00
|
|
|
|
|
2012-04-10 22:17:45 +08:00
|
|
|
|
var dumpedMethods = new DumpedMethods();
|
|
|
|
|
|
|
|
|
|
if (decryptMethodsInfo.methodsToDecrypt == null) {
|
2012-11-21 18:14:20 +08:00
|
|
|
|
for (uint rid = 1; rid <= methodDefTable.Rows; rid++)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
dumpedMethods.Add(DecryptMethod(0x06000000 + rid));
|
2012-04-10 22:17:45 +08:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
foreach (var token in decryptMethodsInfo.methodsToDecrypt)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
dumpedMethods.Add(DecryptMethod(token));
|
2012-04-10 22:17:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dumpedMethods;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
unsafe DumpedMethod DecryptMethod(uint token) {
|
|
|
|
|
if (!CanDecryptMethods())
|
2012-04-10 22:17:45 +08:00
|
|
|
|
throw new ApplicationException("Can't decrypt methods since compileMethod() isn't hooked yet");
|
|
|
|
|
|
|
|
|
|
ctx = new DecryptContext();
|
|
|
|
|
ctx.dm = new DumpedMethod();
|
|
|
|
|
ctx.dm.token = token;
|
|
|
|
|
|
2012-12-20 09:06:09 +08:00
|
|
|
|
ctx.method = dnlibModule.ResolveMethod(MDToken.ToRID(token));
|
2012-04-10 22:17:45 +08:00
|
|
|
|
if (ctx.method == null)
|
|
|
|
|
throw new ApplicationException(string.Format("Could not find method {0:X8}", token));
|
|
|
|
|
|
2012-11-01 16:33:02 +08:00
|
|
|
|
byte* mh = (byte*)hInstModule + (uint)ctx.method.RVA;
|
2012-04-10 22:17:45 +08:00
|
|
|
|
byte* code;
|
|
|
|
|
if (mh == (byte*)hInstModule) {
|
|
|
|
|
ctx.dm.mhMaxStack = 0;
|
|
|
|
|
ctx.dm.mhCodeSize = 0;
|
|
|
|
|
ctx.dm.mhFlags = 0;
|
|
|
|
|
ctx.dm.mhLocalVarSigTok = 0;
|
|
|
|
|
code = null;
|
|
|
|
|
}
|
|
|
|
|
else if ((*mh & 3) == 2) {
|
|
|
|
|
uint headerSize = 1;
|
|
|
|
|
ctx.dm.mhMaxStack = 8;
|
|
|
|
|
ctx.dm.mhCodeSize = (uint)(*mh >> 2);
|
|
|
|
|
ctx.dm.mhFlags = 2;
|
|
|
|
|
ctx.dm.mhLocalVarSigTok = 0;
|
|
|
|
|
code = mh + headerSize;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
uint headerSize = (uint)((mh[1] >> 4) * 4);
|
|
|
|
|
ctx.dm.mhMaxStack = *(ushort*)(mh + 2);
|
|
|
|
|
ctx.dm.mhCodeSize = *(uint*)(mh + 4);
|
|
|
|
|
ctx.dm.mhFlags = *(ushort*)mh;
|
|
|
|
|
ctx.dm.mhLocalVarSigTok = *(uint*)(mh + 8);
|
|
|
|
|
code = mh + headerSize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CORINFO_METHOD_INFO info = default(CORINFO_METHOD_INFO);
|
|
|
|
|
info.ILCode = new IntPtr(code);
|
|
|
|
|
info.ILCodeSize = ctx.dm.mhCodeSize;
|
|
|
|
|
info.maxStack = ctx.dm.mhMaxStack;
|
2012-04-11 03:51:45 +08:00
|
|
|
|
info.scope = moduleToDecryptScope;
|
2012-04-10 22:17:45 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
InitializeOurComp();
|
2012-04-11 03:51:45 +08:00
|
|
|
|
if (code == null) {
|
2012-04-10 22:17:45 +08:00
|
|
|
|
ctx.dm.code = new byte[0];
|
2013-01-19 20:03:57 +08:00
|
|
|
|
UpdateFromMethodDefTableRow();
|
2012-04-11 03:51:45 +08:00
|
|
|
|
}
|
2012-04-10 22:17:45 +08:00
|
|
|
|
else
|
|
|
|
|
callMethodDelegate(*(IntPtr*)jitterVtbl, jitterInstance, ourCompMem, new IntPtr(&info), 0, new IntPtr(0x12345678), new IntPtr(0x3ABCDEF0));
|
|
|
|
|
|
|
|
|
|
var dm = ctx.dm;
|
|
|
|
|
ctx = null;
|
|
|
|
|
return dm;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
unsafe void InitializeOurComp() {
|
2012-04-10 22:17:45 +08:00
|
|
|
|
const int numIndexes = 15;
|
|
|
|
|
if (ourCompMem == IntPtr.Zero)
|
|
|
|
|
ourCompMem = Marshal.AllocHGlobal(numIndexes * IntPtr.Size);
|
|
|
|
|
if (ourCompMem == IntPtr.Zero)
|
|
|
|
|
throw new ApplicationException("Could not allocate memory");
|
|
|
|
|
|
|
|
|
|
IntPtr* mem = (IntPtr*)ourCompMem;
|
|
|
|
|
for (int i = 0; i < numIndexes; i++)
|
|
|
|
|
mem[i] = IntPtr.Zero;
|
|
|
|
|
|
|
|
|
|
mem[1] = new IntPtr(mem + 2);
|
|
|
|
|
mem[3] = new IntPtr(IntPtr.Size * 5);
|
|
|
|
|
mem[5] = new IntPtr(IntPtr.Size * 7);
|
|
|
|
|
mem[6] = new IntPtr(mem + 7);
|
|
|
|
|
mem[7] = returnNameOfMethodInfo.ptrInDll;
|
|
|
|
|
mem[8] = new IntPtr(mem);
|
|
|
|
|
mem[13] = returnMethodTokenInfo.ptrInDll; // .NET 2.0
|
|
|
|
|
mem[14] = returnMethodTokenInfo.ptrInDll; // .NET 4.0
|
|
|
|
|
}
|
2012-07-23 00:37:14 +08:00
|
|
|
|
|
|
|
|
|
bool hasInstalledCompileMethod2 = false;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
unsafe void InstallCompileMethod2() {
|
2012-07-23 00:37:14 +08:00
|
|
|
|
if (hasInstalledCompileMethod2)
|
|
|
|
|
return;
|
|
|
|
|
|
2013-11-08 18:05:17 +08:00
|
|
|
|
if (!PatchCM(*(IntPtr*)jitterVtbl, origCompileMethod, ourCompileMethodInfo.ptrInDll))
|
2012-07-23 00:37:14 +08:00
|
|
|
|
throw new ApplicationException("Couldn't patch compileMethod");
|
|
|
|
|
|
|
|
|
|
hasInstalledCompileMethod2 = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-08 18:05:17 +08:00
|
|
|
|
static IntPtr GetModuleHandle(IntPtr addr) {
|
|
|
|
|
IntPtr hModule;
|
|
|
|
|
if (!GetModuleHandleEx(4, addr, out hModule))
|
|
|
|
|
throw new ApplicationException("GetModuleHandleEx() failed");
|
|
|
|
|
return hModule;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-24 05:00:13 +08:00
|
|
|
|
class PatchInfo {
|
|
|
|
|
public int RVA;
|
|
|
|
|
public byte[] Data;
|
|
|
|
|
public byte[] Orig;
|
|
|
|
|
|
|
|
|
|
public PatchInfo(int rva, byte[] data, byte[] orig) {
|
|
|
|
|
this.RVA = rva;
|
|
|
|
|
this.Data = data;
|
|
|
|
|
this.Orig = orig;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
static readonly PatchInfo[] patches = new PatchInfo[] {
|
|
|
|
|
new PatchInfo(0x000110A5, new byte[] { 0x33, 0xC0, 0xC2, 0x04, 0x00 }, new byte[] { 0xE9, 0x36, 0x3A, 0x00, 0x00 }),
|
|
|
|
|
new PatchInfo(0x000110AF, new byte[] { 0x33, 0xC0, 0xC2, 0x04, 0x00 }, new byte[] { 0xE9, 0x4C, 0x3C, 0x00, 0x00 }),
|
|
|
|
|
new PatchInfo(0x000110AA, new byte[] { 0x33, 0xC0, 0xC2, 0x04, 0x00 }, new byte[] { 0xE9, 0xF1, 0x3A, 0x00, 0x00 }),
|
|
|
|
|
};
|
|
|
|
|
|
2013-11-08 18:05:17 +08:00
|
|
|
|
static unsafe bool PatchCM(IntPtr addr, IntPtr origValue, IntPtr newValue) {
|
|
|
|
|
var baseAddr = GetModuleHandle(addr);
|
|
|
|
|
IntPtr patchAddr;
|
|
|
|
|
using (var peImage = new PEImage(baseAddr))
|
|
|
|
|
patchAddr = FindCMAddress(peImage, baseAddr, origValue);
|
|
|
|
|
if (patchAddr == IntPtr.Zero)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
*(IntPtr*)patchAddr = newValue;
|
2013-11-24 05:00:13 +08:00
|
|
|
|
PatchRT(baseAddr);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HandleProcessCorruptedStateExceptions, SecurityCritical] // Req'd on .NET 4.0
|
|
|
|
|
static unsafe bool PatchRT(IntPtr baseAddr) {
|
|
|
|
|
foreach (var info in patches) {
|
|
|
|
|
try {
|
|
|
|
|
var addr = new IntPtr(baseAddr.ToInt64() + info.RVA);
|
|
|
|
|
|
|
|
|
|
var data = new byte[info.Orig.Length];
|
|
|
|
|
Marshal.Copy(addr, data, 0, data.Length);
|
|
|
|
|
if (!Equals(data, info.Orig))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
uint oldProtect;
|
|
|
|
|
if (!VirtualProtect(addr, info.Data.Length, PAGE_EXECUTE_READWRITE, out oldProtect))
|
|
|
|
|
throw new ApplicationException("Could not enable write access");
|
|
|
|
|
Marshal.Copy(info.Data, 0, addr, info.Data.Length);
|
|
|
|
|
VirtualProtect(addr, info.Data.Length, oldProtect, out oldProtect);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool Equals(byte[] a, byte[] b) {
|
|
|
|
|
if (a == b)
|
|
|
|
|
return true;
|
|
|
|
|
if (a == null || b == null)
|
|
|
|
|
return false;
|
|
|
|
|
if (a.Length != b.Length)
|
|
|
|
|
return false;
|
|
|
|
|
for (int i = 0; i < a.Length; i++) {
|
|
|
|
|
if (a[i] != b[i])
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-11-08 18:05:17 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HandleProcessCorruptedStateExceptions, SecurityCritical] // Req'd on .NET 4.0
|
|
|
|
|
static unsafe IntPtr FindCMAddress(PEImage peImage, IntPtr baseAddr, IntPtr origValue) {
|
2013-11-14 01:54:06 +08:00
|
|
|
|
int offset = Environment.Version.Major == 2 ? 0x10 : 0x28;
|
2013-11-08 18:05:17 +08:00
|
|
|
|
|
|
|
|
|
foreach (var section in peImage.ImageSectionHeaders) {
|
|
|
|
|
const uint RW = 0x80000000 | 0x40000000;
|
|
|
|
|
if ((section.Characteristics & RW) != RW)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
byte* p = (byte*)baseAddr + (uint)section.VirtualAddress + ((section.VirtualSize + IntPtr.Size - 1) & ~(IntPtr.Size - 1)) - IntPtr.Size;
|
|
|
|
|
for (; p >= (byte*)baseAddr; p -= IntPtr.Size) {
|
|
|
|
|
try {
|
|
|
|
|
byte* p2 = (byte*)*(IntPtr**)p;
|
|
|
|
|
if ((ulong)p2 >= 0x10000) {
|
2013-11-14 01:54:06 +08:00
|
|
|
|
if (*(IntPtr*)(p2 + 0x74) == origValue)
|
|
|
|
|
return new IntPtr(p2 + 0x74);
|
|
|
|
|
if (*(IntPtr*)(p2 + 0x78) == origValue)
|
|
|
|
|
return new IntPtr(p2 + 0x78);
|
2012-07-23 00:37:14 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-08 18:05:17 +08:00
|
|
|
|
catch {
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
byte* p2 = (byte*)*(IntPtr**)p;
|
|
|
|
|
if ((ulong)p2 >= 0x10000) {
|
2013-11-14 01:54:06 +08:00
|
|
|
|
p2 += offset;
|
2013-11-08 18:05:17 +08:00
|
|
|
|
if (*(IntPtr*)p2 == origValue)
|
|
|
|
|
return new IntPtr(p2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch {
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
if (*(IntPtr*)p == origValue)
|
|
|
|
|
return new IntPtr(p);
|
|
|
|
|
}
|
|
|
|
|
catch {
|
|
|
|
|
}
|
2012-07-23 00:37:14 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-08 18:05:17 +08:00
|
|
|
|
return IntPtr.Zero;
|
2012-07-23 00:37:14 +08:00
|
|
|
|
}
|
2012-04-10 22:17:45 +08:00
|
|
|
|
}
|
|
|
|
|
}
|