1 /**************************************************************************
3 * Copyright 2011-2012 Jose Fonseca
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
28 * Code for the DLL that will be injected in the target process.
30 * The injected DLL will manipulate the import tables to hook the
31 * modules/functions of interest.
34 * - http://www.codeproject.com/KB/system/api_spying_hack.aspx
35 * - http://www.codeproject.com/KB/threads/APIHooking.aspx
36 * - http://msdn.microsoft.com/en-us/magazine/cc301808.aspx
43 static inline const char *
44 getSeparator(const char *szFilename) {
51 if (c == '\\' || c == '/' || c == ':') {
59 static inline const char *
60 getBaseName(const char *szFilename) {
61 const char *pSeparator = getSeparator(szFilename);
70 getDirName(char *szFilename) {
71 char *pSeparator = const_cast<char *>(getSeparator(szFilename));
79 getModuleName(char *szModuleName, size_t n, const char *szFilename) {
80 char *p = szModuleName;
81 const char *q = getBaseName(szFilename);
85 if (c == '.' || c == '\0') {
94 #define SHARED_MEM_SIZE 4096
96 static LPVOID pSharedMem = NULL;
97 static HANDLE hFileMapping = NULL;
101 OpenSharedMemory(void) {
103 return (LPSTR)pSharedMem;
106 hFileMapping = CreateFileMapping(
107 INVALID_HANDLE_VALUE, // system paging file
108 NULL, // lpAttributes
109 PAGE_READWRITE, // read/write access
110 0, // dwMaximumSizeHigh
111 SHARED_MEM_SIZE, // dwMaximumSizeLow
112 TEXT("injectfilemap")); // name of map object
113 if (hFileMapping == NULL) {
114 fprintf(stderr, "Failed to create file mapping\n");
118 BOOL bAlreadyExists = (GetLastError() == ERROR_ALREADY_EXISTS);
120 pSharedMem = MapViewOfFile(
122 FILE_MAP_WRITE, // read/write access
123 0, // dwFileOffsetHigh
124 0, // dwFileOffsetLow
125 0); // dwNumberOfBytesToMap (entire file)
126 if (pSharedMem == NULL) {
127 fprintf(stderr, "Failed to map view \n");
131 if (!bAlreadyExists) {
132 memset(pSharedMem, 0, SHARED_MEM_SIZE);
135 return (LPSTR)pSharedMem;
140 CloseSharedMem(void) {
145 UnmapViewOfFile(pSharedMem);
148 CloseHandle(hFileMapping);
154 SetSharedMem(LPCSTR lpszSrc) {
155 LPSTR lpszDst = OpenSharedMemory();
161 while (*lpszSrc && n < SHARED_MEM_SIZE) {
162 *lpszDst++ = *lpszSrc++;
170 GetSharedMem(LPSTR lpszDst, size_t n) {
171 LPCSTR lpszSrc = OpenSharedMemory();
176 while (*lpszSrc && --n) {
177 *lpszDst++ = *lpszSrc++;