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 USE_SHARED_MEM 1
99 #define SHARED_MEM_SIZE 4096
101 static LPVOID pSharedMem = NULL;
102 static HANDLE hFileMapping = NULL;
106 OpenSharedMemory(void) {
108 return (LPSTR)pSharedMem;
111 hFileMapping = CreateFileMapping(
112 INVALID_HANDLE_VALUE, // system paging file
113 NULL, // lpAttributes
114 PAGE_READWRITE, // read/write access
115 0, // dwMaximumSizeHigh
116 SHARED_MEM_SIZE, // dwMaximumSizeLow
117 TEXT("injectfilemap")); // name of map object
118 if (hFileMapping == NULL) {
119 fprintf(stderr, "Failed to create file mapping\n");
123 BOOL bAlreadyExists = (GetLastError() == ERROR_ALREADY_EXISTS);
125 pSharedMem = MapViewOfFile(
127 FILE_MAP_WRITE, // read/write access
128 0, // dwFileOffsetHigh
129 0, // dwFileOffsetLow
130 0); // dwNumberOfBytesToMap (entire file)
131 if (pSharedMem == NULL) {
132 fprintf(stderr, "Failed to map view \n");
136 if (!bAlreadyExists) {
137 memset(pSharedMem, 0, SHARED_MEM_SIZE);
140 return (LPSTR)pSharedMem;
145 CloseSharedMem(void) {
150 UnmapViewOfFile(pSharedMem);
153 CloseHandle(hFileMapping);
159 SetSharedMem(LPCSTR lpszSrc) {
160 LPSTR lpszDst = OpenSharedMemory();
166 while (*lpszSrc && n < SHARED_MEM_SIZE) {
167 *lpszDst++ = *lpszSrc++;
175 GetSharedMem(LPSTR lpszDst, size_t n) {
176 LPCSTR lpszSrc = OpenSharedMemory();
181 while (*lpszSrc && --n) {
182 *lpszDst++ = *lpszSrc++;
188 #endif /* USE_SHARED_MEM */