2009-11-16 20:19:13 -07:00
|
|
|
/*
|
|
|
|
www.sourceforge.net/projects/dfhack
|
|
|
|
Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any
|
|
|
|
damages arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any
|
|
|
|
purpose, including commercial applications, and to alter it and
|
|
|
|
redistribute it freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must
|
|
|
|
not claim that you wrote the original software. If you use this
|
|
|
|
software in a product, an acknowledgment in the product documentation
|
|
|
|
would be appreciated but is not required.
|
|
|
|
|
|
|
|
2. Altered source versions must be plainly marked as such, and
|
|
|
|
must not be misrepresented as being the original software.
|
|
|
|
|
|
|
|
3. This notice may not be removed or altered from any source
|
|
|
|
distribution.
|
|
|
|
*/
|
2010-05-26 04:24:45 -06:00
|
|
|
#include "Internal.h"
|
2010-05-26 00:42:09 -06:00
|
|
|
#include "dfhack/DFProcess.h"
|
2010-08-20 06:10:05 -06:00
|
|
|
#include "dfhack/VersionInfo.h"
|
2010-05-26 00:42:09 -06:00
|
|
|
#include "dfhack/DFError.h"
|
2011-02-13 19:58:32 -07:00
|
|
|
#include <string.h>
|
2009-11-16 20:19:13 -07:00
|
|
|
using namespace DFHack;
|
|
|
|
|
2009-12-22 14:19:39 -07:00
|
|
|
class NormalProcess::Private
|
2009-11-16 20:19:13 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
Private()
|
|
|
|
{
|
|
|
|
my_descriptor = NULL;
|
|
|
|
my_handle = NULL;
|
|
|
|
my_main_thread = NULL;
|
|
|
|
my_pid = 0;
|
|
|
|
attached = false;
|
|
|
|
suspended = false;
|
2011-02-13 19:58:32 -07:00
|
|
|
base = 0;
|
|
|
|
sections = 0;
|
2009-11-16 20:19:13 -07:00
|
|
|
};
|
|
|
|
~Private(){};
|
2010-08-20 06:10:05 -06:00
|
|
|
VersionInfo * my_descriptor;
|
2010-05-01 17:20:31 -06:00
|
|
|
HANDLE my_handle;
|
2009-11-16 20:19:13 -07:00
|
|
|
HANDLE my_main_thread;
|
|
|
|
uint32_t my_pid;
|
|
|
|
string memFile;
|
|
|
|
bool attached;
|
|
|
|
bool suspended;
|
|
|
|
bool identified;
|
2010-09-17 20:36:14 -06:00
|
|
|
uint32_t STLSTR_buf_off;
|
|
|
|
uint32_t STLSTR_size_off;
|
|
|
|
uint32_t STLSTR_cap_off;
|
2011-02-13 19:58:32 -07:00
|
|
|
IMAGE_NT_HEADERS32 pe_header;
|
|
|
|
IMAGE_SECTION_HEADER * sections;
|
|
|
|
uint32_t base;
|
2009-11-16 20:19:13 -07:00
|
|
|
};
|
|
|
|
|
2010-08-20 06:10:05 -06:00
|
|
|
NormalProcess::NormalProcess(uint32_t pid, vector <VersionInfo *> & known_versions)
|
2009-11-16 20:19:13 -07:00
|
|
|
: d(new Private())
|
|
|
|
{
|
|
|
|
HMODULE hmod = NULL;
|
2011-02-12 02:26:36 -07:00
|
|
|
DWORD needed;
|
2009-11-16 20:19:13 -07:00
|
|
|
HANDLE hProcess;
|
|
|
|
bool found = false;
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2009-11-16 20:19:13 -07:00
|
|
|
d->identified = false;
|
|
|
|
// open process
|
|
|
|
hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pid );
|
|
|
|
if (NULL == hProcess)
|
|
|
|
return;
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2009-11-16 20:19:13 -07:00
|
|
|
// try getting the first module of the process
|
2011-02-12 02:26:36 -07:00
|
|
|
if(EnumProcessModules(hProcess, &hmod, sizeof(hmod), &needed) == 0)
|
2009-11-16 20:19:13 -07:00
|
|
|
{
|
|
|
|
CloseHandle(hProcess);
|
2010-03-09 07:15:15 -07:00
|
|
|
// cout << "EnumProcessModules fail'd" << endl;
|
2009-11-17 20:35:43 -07:00
|
|
|
return; //if enumprocessModules fails, give up
|
2009-11-16 20:19:13 -07:00
|
|
|
}
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2009-11-16 20:19:13 -07:00
|
|
|
// got base ;)
|
2011-02-13 19:58:32 -07:00
|
|
|
d->base = (uint32_t)hmod;
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2009-12-22 14:19:39 -07:00
|
|
|
// temporarily assign this to allow some checks
|
|
|
|
d->my_handle = hProcess;
|
2010-04-13 06:18:09 -06:00
|
|
|
d->my_main_thread = 0;
|
2009-11-16 20:19:13 -07:00
|
|
|
// read from this process
|
2010-04-13 06:18:09 -06:00
|
|
|
try
|
|
|
|
{
|
2011-02-13 19:58:32 -07:00
|
|
|
uint32_t pe_offset = readDWord(d->base+0x3C);
|
|
|
|
read(d->base + pe_offset , sizeof(d->pe_header), (uint8_t *)&d->pe_header);
|
|
|
|
const size_t sectionsSize = sizeof(IMAGE_SECTION_HEADER) * d->pe_header.FileHeader.NumberOfSections;
|
|
|
|
d->sections = (IMAGE_SECTION_HEADER *) malloc(sectionsSize);
|
|
|
|
read(d->base + pe_offset + sizeof(d->pe_header), sectionsSize, (uint8_t *)d->sections);
|
2010-04-13 06:18:09 -06:00
|
|
|
d->my_handle = 0;
|
|
|
|
}
|
|
|
|
catch (exception &)
|
|
|
|
{
|
|
|
|
CloseHandle(hProcess);
|
|
|
|
d->my_handle = 0;
|
|
|
|
return;
|
|
|
|
}
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2009-11-16 20:19:13 -07:00
|
|
|
// see if there's a version entry that matches this process
|
2010-08-20 06:10:05 -06:00
|
|
|
vector<VersionInfo*>::iterator it;
|
2009-11-16 20:19:13 -07:00
|
|
|
for ( it=known_versions.begin() ; it < known_versions.end(); it++ )
|
|
|
|
{
|
|
|
|
// filter by OS
|
2011-02-08 14:55:40 -07:00
|
|
|
if(OS_WINDOWS != (*it)->getOS())
|
2009-11-16 20:19:13 -07:00
|
|
|
continue;
|
2010-02-28 11:59:07 -07:00
|
|
|
uint32_t pe_timestamp;
|
|
|
|
// filter by timestamp, skip entries without a timestamp
|
|
|
|
try
|
|
|
|
{
|
2010-08-23 08:16:58 -06:00
|
|
|
pe_timestamp = (*it)->getPE();
|
2010-02-28 11:59:07 -07:00
|
|
|
}
|
2010-11-07 16:10:59 -07:00
|
|
|
catch(Error::AllMemdef&)
|
2010-02-28 11:59:07 -07:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2011-02-13 19:58:32 -07:00
|
|
|
if (pe_timestamp != d->pe_header.FileHeader.TimeDateStamp)
|
2009-11-16 20:19:13 -07:00
|
|
|
continue;
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2009-11-16 20:19:13 -07:00
|
|
|
// all went well
|
|
|
|
{
|
2010-02-22 15:34:20 -07:00
|
|
|
printf("Match found! Using version %s.\n", (*it)->getVersion().c_str());
|
2009-11-16 20:19:13 -07:00
|
|
|
d->identified = true;
|
|
|
|
// give the process a data model and memory layout fixed for the base of first module
|
2010-08-20 06:10:05 -06:00
|
|
|
VersionInfo *m = new VersionInfo(**it);
|
2011-02-13 19:58:32 -07:00
|
|
|
m->RebaseAll(d->base);
|
2009-11-16 20:19:13 -07:00
|
|
|
// keep track of created memory_info object so we can destroy it later
|
|
|
|
d->my_descriptor = m;
|
2010-04-18 13:30:02 -06:00
|
|
|
m->setParentProcess(this);
|
2009-11-16 20:19:13 -07:00
|
|
|
// process is responsible for destroying its data model
|
|
|
|
d->my_pid = pid;
|
|
|
|
d->my_handle = hProcess;
|
|
|
|
d->identified = true;
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2009-11-16 20:19:13 -07:00
|
|
|
// TODO: detect errors in thread enumeration
|
|
|
|
vector<uint32_t> threads;
|
|
|
|
getThreadIDs( threads );
|
|
|
|
d->my_main_thread = OpenThread(THREAD_ALL_ACCESS, FALSE, (DWORD) threads[0]);
|
2010-09-17 20:36:14 -06:00
|
|
|
OffsetGroup * strGrp = m->getGroup("string")->getGroup("MSVC");
|
|
|
|
d->STLSTR_buf_off = strGrp->getOffset("buffer");
|
|
|
|
d->STLSTR_size_off = strGrp->getOffset("size");
|
|
|
|
d->STLSTR_cap_off = strGrp->getOffset("capacity");
|
2009-11-16 20:19:13 -07:00
|
|
|
found = true;
|
|
|
|
break; // break the iterator loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// close handle of processes that aren't DF
|
|
|
|
if(!found)
|
|
|
|
{
|
|
|
|
CloseHandle(hProcess);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
*/
|
|
|
|
|
2009-12-22 14:19:39 -07:00
|
|
|
NormalProcess::~NormalProcess()
|
2009-11-16 20:19:13 -07:00
|
|
|
{
|
|
|
|
if(d->attached)
|
|
|
|
{
|
|
|
|
detach();
|
|
|
|
}
|
2010-01-10 23:27:59 -07:00
|
|
|
// destroy our rebased copy of the memory descriptor
|
2009-11-16 20:19:13 -07:00
|
|
|
delete d->my_descriptor;
|
|
|
|
if(d->my_handle != NULL)
|
|
|
|
{
|
|
|
|
CloseHandle(d->my_handle);
|
|
|
|
}
|
|
|
|
if(d->my_main_thread != NULL)
|
|
|
|
{
|
|
|
|
CloseHandle(d->my_main_thread);
|
|
|
|
}
|
2011-02-13 19:58:32 -07:00
|
|
|
if(d->sections != NULL)
|
|
|
|
free(d->sections);
|
2009-11-24 03:34:42 -07:00
|
|
|
delete d;
|
2009-11-16 20:19:13 -07:00
|
|
|
}
|
|
|
|
|
2010-08-20 06:10:05 -06:00
|
|
|
VersionInfo * NormalProcess::getDescriptor()
|
2009-11-16 20:19:13 -07:00
|
|
|
{
|
|
|
|
return d->my_descriptor;
|
|
|
|
}
|
|
|
|
|
2009-12-22 14:19:39 -07:00
|
|
|
int NormalProcess::getPID()
|
|
|
|
{
|
|
|
|
return d->my_pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NormalProcess::isSuspended()
|
2009-11-16 20:19:13 -07:00
|
|
|
{
|
|
|
|
return d->suspended;
|
|
|
|
}
|
2009-12-22 14:19:39 -07:00
|
|
|
bool NormalProcess::isAttached()
|
2009-11-16 20:19:13 -07:00
|
|
|
{
|
|
|
|
return d->attached;
|
|
|
|
}
|
|
|
|
|
2009-12-22 14:19:39 -07:00
|
|
|
bool NormalProcess::isIdentified()
|
2009-11-16 20:19:13 -07:00
|
|
|
{
|
|
|
|
return d->identified;
|
|
|
|
}
|
|
|
|
|
2010-01-01 19:25:21 -07:00
|
|
|
bool NormalProcess::asyncSuspend()
|
|
|
|
{
|
|
|
|
return suspend();
|
|
|
|
}
|
|
|
|
|
2009-12-22 14:19:39 -07:00
|
|
|
bool NormalProcess::suspend()
|
2009-11-16 20:19:13 -07:00
|
|
|
{
|
|
|
|
if(!d->attached)
|
|
|
|
return false;
|
|
|
|
if(d->suspended)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
SuspendThread(d->my_main_thread);
|
|
|
|
d->suspended = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-12-22 14:19:39 -07:00
|
|
|
bool NormalProcess::forceresume()
|
2009-11-18 18:48:18 -07:00
|
|
|
{
|
|
|
|
if(!d->attached)
|
|
|
|
return false;
|
|
|
|
while (ResumeThread(d->my_main_thread) > 1);
|
|
|
|
d->suspended = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-22 14:19:39 -07:00
|
|
|
bool NormalProcess::resume()
|
2009-11-16 20:19:13 -07:00
|
|
|
{
|
|
|
|
if(!d->attached)
|
|
|
|
return false;
|
|
|
|
if(!d->suspended)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
ResumeThread(d->my_main_thread);
|
|
|
|
d->suspended = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-12-22 14:19:39 -07:00
|
|
|
bool NormalProcess::attach()
|
2009-11-16 20:19:13 -07:00
|
|
|
{
|
2010-04-18 13:30:02 -06:00
|
|
|
if(d->attached)
|
2009-11-16 20:19:13 -07:00
|
|
|
{
|
2010-04-18 13:30:02 -06:00
|
|
|
if(!d->suspended)
|
|
|
|
return suspend();
|
|
|
|
return true;
|
2009-11-16 20:19:13 -07:00
|
|
|
}
|
|
|
|
d->attached = true;
|
|
|
|
suspend();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-22 14:19:39 -07:00
|
|
|
bool NormalProcess::detach()
|
2009-11-16 20:19:13 -07:00
|
|
|
{
|
2010-08-13 17:25:18 -06:00
|
|
|
if(!d->attached) return true;
|
2009-11-16 20:19:13 -07:00
|
|
|
resume();
|
|
|
|
d->attached = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-12-22 14:19:39 -07:00
|
|
|
bool NormalProcess::getThreadIDs(vector<uint32_t> & threads )
|
2009-11-16 20:19:13 -07:00
|
|
|
{
|
2010-08-20 06:10:05 -06:00
|
|
|
HANDLE AllThreads = INVALID_HANDLE_VALUE;
|
2009-11-16 20:19:13 -07:00
|
|
|
THREADENTRY32 te32;
|
2010-08-20 06:10:05 -06:00
|
|
|
|
|
|
|
AllThreads = CreateToolhelp32Snapshot( TH32CS_SNAPTHREAD, 0 );
|
|
|
|
if( AllThreads == INVALID_HANDLE_VALUE )
|
2009-11-16 20:19:13 -07:00
|
|
|
{
|
2010-08-20 06:10:05 -06:00
|
|
|
return false;
|
2009-11-16 20:19:13 -07:00
|
|
|
}
|
2010-08-20 06:10:05 -06:00
|
|
|
te32.dwSize = sizeof(THREADENTRY32 );
|
|
|
|
|
|
|
|
if( !Thread32First( AllThreads, &te32 ) )
|
2009-11-16 20:19:13 -07:00
|
|
|
{
|
|
|
|
CloseHandle( AllThreads );
|
|
|
|
return false;
|
|
|
|
}
|
2010-08-20 06:10:05 -06:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2009-11-16 20:19:13 -07:00
|
|
|
if( te32.th32OwnerProcessID == d->my_pid )
|
|
|
|
{
|
|
|
|
threads.push_back(te32.th32ThreadID);
|
|
|
|
}
|
2010-08-20 06:10:05 -06:00
|
|
|
} while( Thread32Next(AllThreads, &te32 ) );
|
|
|
|
|
2009-11-16 20:19:13 -07:00
|
|
|
CloseHandle( AllThreads );
|
|
|
|
return true;
|
|
|
|
}
|
2011-02-12 02:26:36 -07:00
|
|
|
/*
|
2011-02-08 07:34:34 -07:00
|
|
|
typedef struct _MEMORY_BASIC_INFORMATION
|
2009-11-16 20:19:13 -07:00
|
|
|
{
|
2011-02-08 07:34:34 -07:00
|
|
|
void * BaseAddress;
|
|
|
|
void * AllocationBase;
|
|
|
|
uint32_t AllocationProtect;
|
|
|
|
size_t RegionSize;
|
|
|
|
uint32_t State;
|
|
|
|
uint32_t Protect;
|
|
|
|
uint32_t Type;
|
|
|
|
} MEMORY_BASIC_INFORMATION, *PMEMORY_BASIC_INFORMATION;
|
2011-02-12 02:26:36 -07:00
|
|
|
*/
|
2011-02-13 19:58:32 -07:00
|
|
|
/*
|
|
|
|
//Internal structure used to store heap block information.
|
|
|
|
struct HeapBlock
|
|
|
|
{
|
|
|
|
PVOID dwAddress;
|
|
|
|
DWORD dwSize;
|
|
|
|
DWORD dwFlags;
|
|
|
|
ULONG reserved;
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
void HeapNodes(DWORD pid, map<uint64_t, unsigned int> & heaps)
|
|
|
|
{
|
|
|
|
// Create debug buffer
|
|
|
|
PDEBUG_BUFFER db = RtlCreateQueryDebugBuffer(0, FALSE);
|
|
|
|
// Get process heap data
|
|
|
|
RtlQueryProcessDebugInformation( pid, PDI_HEAPS/* | PDI_HEAP_BLOCKS*/, db);
|
|
|
|
ULONG heapNodeCount = db->HeapInformation ? *PULONG(db->HeapInformation):0;
|
|
|
|
PDEBUG_HEAP_INFORMATION heapInfo = PDEBUG_HEAP_INFORMATION(PULONG(db-> HeapInformation) + 1);
|
|
|
|
// Go through each of the heap nodes and dispaly the information
|
|
|
|
for (unsigned int i = 0; i < heapNodeCount; i++)
|
|
|
|
{
|
|
|
|
heaps[heapInfo[i].Base] = i;
|
|
|
|
}
|
|
|
|
// Clean up the buffer
|
|
|
|
RtlDestroyQueryDebugBuffer( db );
|
|
|
|
}
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2011-02-08 07:34:34 -07:00
|
|
|
// FIXME: NEEDS TESTING!
|
2009-12-22 14:19:39 -07:00
|
|
|
void NormalProcess::getMemRanges( vector<t_memrange> & ranges )
|
2009-11-16 20:19:13 -07:00
|
|
|
{
|
2011-02-08 07:34:34 -07:00
|
|
|
MEMORY_BASIC_INFORMATION MBI;
|
2011-02-13 19:58:32 -07:00
|
|
|
map<uint64_t, unsigned int> heaps;
|
|
|
|
uint64_t movingStart = 0;
|
|
|
|
map <uint64_t, string> nameMap;
|
|
|
|
|
2011-02-12 02:26:36 -07:00
|
|
|
// get page size
|
|
|
|
SYSTEM_INFO si;
|
|
|
|
GetSystemInfo(&si);
|
|
|
|
uint64_t PageSize = si.dwPageSize;
|
2011-02-13 19:58:32 -07:00
|
|
|
// enumerate heaps
|
|
|
|
HeapNodes(d->my_pid, heaps);
|
2011-02-12 02:26:36 -07:00
|
|
|
// go through all the VM regions, convert them to our internal format
|
2011-02-13 19:58:32 -07:00
|
|
|
while (VirtualQueryEx(this->d->my_handle, (const void*) (movingStart), &MBI, sizeof(MBI)) == sizeof(MBI))
|
2011-02-08 06:34:43 -07:00
|
|
|
{
|
2011-02-13 19:58:32 -07:00
|
|
|
movingStart = ((uint64_t)MBI.BaseAddress + MBI.RegionSize);
|
|
|
|
if(movingStart % PageSize != 0)
|
|
|
|
movingStart = (movingStart / PageSize + 1) * PageSize;
|
|
|
|
// skip empty regions and regions we share with other processes (DLLs)
|
|
|
|
if( !(MBI.State & MEM_COMMIT) /*|| !(MBI.Type & MEM_PRIVATE)*/ )
|
2011-02-08 07:34:34 -07:00
|
|
|
continue;
|
|
|
|
t_memrange temp;
|
2011-02-13 19:58:32 -07:00
|
|
|
temp.start = (uint64_t) MBI.BaseAddress;
|
|
|
|
temp.end = ((uint64_t)MBI.BaseAddress + (uint64_t)MBI.RegionSize);
|
|
|
|
temp.read = MBI.Protect & PAGE_EXECUTE_READ || MBI.Protect & PAGE_EXECUTE_READWRITE || MBI.Protect & PAGE_READONLY || MBI.Protect & PAGE_READWRITE;
|
|
|
|
temp.write = MBI.Protect & PAGE_EXECUTE_READWRITE || MBI.Protect & PAGE_READWRITE;
|
2011-02-12 02:26:36 -07:00
|
|
|
temp.execute = MBI.Protect & PAGE_EXECUTE_READ || MBI.Protect & PAGE_EXECUTE_READWRITE || MBI.Protect & PAGE_EXECUTE;
|
2011-02-13 19:58:32 -07:00
|
|
|
temp.valid = true;
|
|
|
|
if(!GetModuleBaseName(this->d->my_handle, (HMODULE) temp.start, temp.name, 1024))
|
|
|
|
{
|
|
|
|
if(nameMap.count(temp.start))
|
|
|
|
{
|
|
|
|
// potential buffer overflow...
|
|
|
|
strcpy(temp.name, nameMap[temp.start].c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// filter away shared segments without a name.
|
|
|
|
if( !(MBI.Type & MEM_PRIVATE) )
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// could be a heap?
|
|
|
|
if(heaps.count(temp.start))
|
|
|
|
{
|
|
|
|
sprintf(temp.name,"HEAP %d",heaps[temp.start]);
|
|
|
|
}
|
|
|
|
else temp.name[0]=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// this is our executable! (could be generalized to pull segments from libs, but whatever)
|
|
|
|
if(d->base == temp.start)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < d->pe_header.FileHeader.NumberOfSections; i++)
|
|
|
|
{
|
|
|
|
char sectionName[9];
|
|
|
|
memcpy(sectionName,d->sections[i].Name,8);
|
|
|
|
sectionName[8] = 0;
|
|
|
|
string nm;
|
|
|
|
nm.append(temp.name);
|
|
|
|
nm.append(" : ");
|
|
|
|
nm.append(sectionName);
|
|
|
|
nameMap[temp.start + d->sections[i].VirtualAddress] = nm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
}
|
2011-02-08 07:34:34 -07:00
|
|
|
ranges.push_back(temp);
|
2011-02-08 06:34:43 -07:00
|
|
|
}
|
2009-11-16 20:19:13 -07:00
|
|
|
}
|
2009-12-22 14:19:39 -07:00
|
|
|
|
|
|
|
uint8_t NormalProcess::readByte (const uint32_t offset)
|
|
|
|
{
|
|
|
|
uint8_t result;
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!ReadProcessMemory(d->my_handle, (int*) offset, &result, sizeof(uint8_t), NULL))
|
|
|
|
throw Error::MemoryAccessDenied();
|
2009-12-22 14:19:39 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NormalProcess::readByte (const uint32_t offset,uint8_t &result)
|
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!ReadProcessMemory(d->my_handle, (int*) offset, &result, sizeof(uint8_t), NULL))
|
|
|
|
throw Error::MemoryAccessDenied();
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t NormalProcess::readWord (const uint32_t offset)
|
|
|
|
{
|
|
|
|
uint16_t result;
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!ReadProcessMemory(d->my_handle, (int*) offset, &result, sizeof(uint16_t), NULL))
|
|
|
|
throw Error::MemoryAccessDenied();
|
2009-12-22 14:19:39 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NormalProcess::readWord (const uint32_t offset, uint16_t &result)
|
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!ReadProcessMemory(d->my_handle, (int*) offset, &result, sizeof(uint16_t), NULL))
|
|
|
|
throw Error::MemoryAccessDenied();
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t NormalProcess::readDWord (const uint32_t offset)
|
|
|
|
{
|
|
|
|
uint32_t result;
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!ReadProcessMemory(d->my_handle, (int*) offset, &result, sizeof(uint32_t), NULL))
|
|
|
|
throw Error::MemoryAccessDenied();
|
2009-12-22 14:19:39 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NormalProcess::readDWord (const uint32_t offset, uint32_t &result)
|
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!ReadProcessMemory(d->my_handle, (int*) offset, &result, sizeof(uint32_t), NULL))
|
|
|
|
throw Error::MemoryAccessDenied();
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
|
2010-04-26 16:12:22 -06:00
|
|
|
uint64_t NormalProcess::readQuad (const uint32_t offset)
|
|
|
|
{
|
|
|
|
uint64_t result;
|
|
|
|
if(!ReadProcessMemory(d->my_handle, (int*) offset, &result, sizeof(uint64_t), NULL))
|
|
|
|
throw Error::MemoryAccessDenied();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NormalProcess::readQuad (const uint32_t offset, uint64_t &result)
|
|
|
|
{
|
|
|
|
if(!ReadProcessMemory(d->my_handle, (int*) offset, &result, sizeof(uint64_t), NULL))
|
|
|
|
throw Error::MemoryAccessDenied();
|
|
|
|
}
|
|
|
|
|
2010-04-20 10:13:00 -06:00
|
|
|
float NormalProcess::readFloat (const uint32_t offset)
|
|
|
|
{
|
|
|
|
float result;
|
|
|
|
if(!ReadProcessMemory(d->my_handle, (int*) offset, &result, sizeof(float), NULL))
|
|
|
|
throw Error::MemoryAccessDenied();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NormalProcess::readFloat (const uint32_t offset, float &result)
|
|
|
|
{
|
|
|
|
if(!ReadProcessMemory(d->my_handle, (int*) offset, &result, sizeof(float), NULL))
|
|
|
|
throw Error::MemoryAccessDenied();
|
|
|
|
}
|
|
|
|
|
2009-12-22 14:19:39 -07:00
|
|
|
void NormalProcess::read (const uint32_t offset, uint32_t size, uint8_t *target)
|
|
|
|
{
|
2010-04-13 06:18:09 -06:00
|
|
|
if(!ReadProcessMemory(d->my_handle, (int*) offset, target, size, NULL))
|
2010-04-12 19:11:26 -06:00
|
|
|
throw Error::MemoryAccessDenied();
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// WRITING
|
2010-04-26 16:12:22 -06:00
|
|
|
void NormalProcess::writeQuad (const uint32_t offset, uint64_t data)
|
|
|
|
{
|
|
|
|
if(!WriteProcessMemory(d->my_handle, (int*) offset, &data, sizeof(data), NULL))
|
|
|
|
throw Error::MemoryAccessDenied();
|
|
|
|
}
|
|
|
|
|
2009-12-22 14:19:39 -07:00
|
|
|
void NormalProcess::writeDWord (const uint32_t offset, uint32_t data)
|
|
|
|
{
|
2010-04-26 16:12:22 -06:00
|
|
|
if(!WriteProcessMemory(d->my_handle, (int*) offset, &data, sizeof(data), NULL))
|
2010-04-12 19:11:26 -06:00
|
|
|
throw Error::MemoryAccessDenied();
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// using these is expensive.
|
|
|
|
void NormalProcess::writeWord (uint32_t offset, uint16_t data)
|
|
|
|
{
|
2010-04-26 16:12:22 -06:00
|
|
|
if(!WriteProcessMemory(d->my_handle, (int*) offset, &data, sizeof(data), NULL))
|
2010-04-12 19:11:26 -06:00
|
|
|
throw Error::MemoryAccessDenied();
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void NormalProcess::writeByte (uint32_t offset, uint8_t data)
|
|
|
|
{
|
2010-04-26 16:12:22 -06:00
|
|
|
if(!WriteProcessMemory(d->my_handle, (int*) offset, &data, sizeof(data), NULL))
|
2010-04-12 19:11:26 -06:00
|
|
|
throw Error::MemoryAccessDenied();
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
|
2010-01-01 19:25:21 -07:00
|
|
|
void NormalProcess::write (uint32_t offset, uint32_t size, uint8_t *source)
|
2009-12-22 14:19:39 -07:00
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!WriteProcessMemory(d->my_handle, (int*) offset, source, size, NULL))
|
|
|
|
throw Error::MemoryAccessDenied();
|
2009-12-22 14:19:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///FIXME: reduce use of temporary objects
|
|
|
|
const string NormalProcess::readCString (const uint32_t offset)
|
|
|
|
{
|
|
|
|
string temp;
|
|
|
|
char temp_c[256];
|
2010-05-25 15:52:04 -06:00
|
|
|
SIZE_T read;
|
2010-04-12 19:11:26 -06:00
|
|
|
if(!ReadProcessMemory(d->my_handle, (int *) offset, temp_c, 254, &read))
|
|
|
|
throw Error::MemoryAccessDenied();
|
|
|
|
// needs to be 254+1 byte for the null term
|
2009-12-22 14:19:39 -07:00
|
|
|
temp_c[read+1] = 0;
|
2010-03-07 19:00:31 -07:00
|
|
|
temp.assign(temp_c);
|
2009-12-22 14:19:39 -07:00
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
2010-02-15 16:04:15 -07:00
|
|
|
size_t NormalProcess::readSTLString (uint32_t offset, char * buffer, size_t bufcapacity)
|
|
|
|
{
|
2010-09-17 20:36:14 -06:00
|
|
|
uint32_t start_offset = offset + d->STLSTR_buf_off;
|
|
|
|
size_t length = readDWord(offset + d->STLSTR_size_off);
|
|
|
|
size_t capacity = readDWord(offset + d->STLSTR_cap_off);
|
2010-02-15 16:04:15 -07:00
|
|
|
size_t read_real = min(length, bufcapacity-1);// keep space for null termination
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2010-02-15 16:04:15 -07:00
|
|
|
// read data from inside the string structure
|
|
|
|
if(capacity < 16)
|
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
read(start_offset, read_real , (uint8_t *)buffer);
|
2010-02-15 16:04:15 -07:00
|
|
|
}
|
|
|
|
else // read data from what the offset + 4 dword points to
|
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
start_offset = readDWord(start_offset);// dereference the start offset
|
|
|
|
read(start_offset, read_real, (uint8_t *)buffer);
|
2010-02-15 16:04:15 -07:00
|
|
|
}
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2010-02-15 16:04:15 -07:00
|
|
|
buffer[read_real] = 0;
|
|
|
|
return read_real;
|
|
|
|
}
|
|
|
|
|
|
|
|
const string NormalProcess::readSTLString (uint32_t offset)
|
|
|
|
{
|
2010-09-17 20:36:14 -06:00
|
|
|
uint32_t start_offset = offset + d->STLSTR_buf_off;
|
|
|
|
size_t length = readDWord(offset + d->STLSTR_size_off);
|
|
|
|
size_t capacity = readDWord(offset + d->STLSTR_cap_off);
|
2010-02-15 16:04:15 -07:00
|
|
|
char * temp = new char[capacity+1];
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2010-02-15 16:04:15 -07:00
|
|
|
// read data from inside the string structure
|
|
|
|
if(capacity < 16)
|
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
read(start_offset, capacity, (uint8_t *)temp);
|
2010-02-15 16:04:15 -07:00
|
|
|
}
|
|
|
|
else // read data from what the offset + 4 dword points to
|
|
|
|
{
|
2010-04-12 19:11:26 -06:00
|
|
|
start_offset = readDWord(start_offset);// dereference the start offset
|
|
|
|
read(start_offset, capacity, (uint8_t *)temp);
|
2010-02-15 16:04:15 -07:00
|
|
|
}
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2010-02-15 16:04:15 -07:00
|
|
|
temp[length] = 0;
|
|
|
|
string ret = temp;
|
|
|
|
delete temp;
|
|
|
|
return ret;
|
2010-02-18 18:48:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
string NormalProcess::readClassName (uint32_t vptr)
|
|
|
|
{
|
|
|
|
int rtti = readDWord(vptr - 0x4);
|
|
|
|
int typeinfo = readDWord(rtti + 0xC);
|
|
|
|
string raw = readCString(typeinfo + 0xC); // skips the .?AV
|
2010-04-09 06:44:00 -06:00
|
|
|
raw.resize(raw.length() - 2);// trim @@ from end
|
2010-02-18 18:48:03 -07:00
|
|
|
return raw;
|
2010-04-20 10:13:00 -06:00
|
|
|
}
|
2010-09-12 19:36:31 -06:00
|
|
|
string NormalProcess::getPath()
|
|
|
|
{
|
|
|
|
HMODULE hmod;
|
|
|
|
DWORD junk;
|
|
|
|
char String[255];
|
|
|
|
EnumProcessModules(d->my_handle, &hmod, 1 * sizeof(HMODULE), &junk); //get the module from the handle
|
|
|
|
GetModuleFileNameEx(d->my_handle,hmod,String,sizeof(String)); //get the filename from the module
|
2010-09-16 19:44:38 -06:00
|
|
|
string out(String);
|
|
|
|
return(out.substr(0,out.find_last_of("\\")));
|
2010-09-12 19:36:31 -06:00
|
|
|
}
|