Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/exploits/CVE-2015-2426/inject/src/GetProcAddressR.c
Views: 11788
//===============================================================================================//1// Copyright (c) 2013, Stephen Fewer of Harmony Security (www.harmonysecurity.com)2// All rights reserved.3//4// Redistribution and use in source and binary forms, with or without modification, are permitted5// provided that the following conditions are met:6//7// * Redistributions of source code must retain the above copyright notice, this list of8// conditions and the following disclaimer.9//10// * Redistributions in binary form must reproduce the above copyright notice, this list of11// conditions and the following disclaimer in the documentation and/or other materials provided12// with the distribution.13//14// * Neither the name of Harmony Security nor the names of its contributors may be used to15// endorse or promote products derived from this software without specific prior written permission.16//17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR18// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND19// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR20// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR21// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR22// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY23// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR24// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE25// POSSIBILITY OF SUCH DAMAGE.26//===============================================================================================//27#include "GetProcAddressR.h"28//===============================================================================================//29// We implement a minimal GetProcAddress to avoid using the native kernel32!GetProcAddress which30// wont be able to resolve exported addresses in reflectivly loaded librarys.31FARPROC WINAPI GetProcAddressR( HANDLE hModule, LPCSTR lpProcName )32{33UINT_PTR uiLibraryAddress = 0;34FARPROC fpResult = NULL;3536if( hModule == NULL )37return NULL;3839// a module handle is really its base address40uiLibraryAddress = (UINT_PTR)hModule;4142__try43{44UINT_PTR uiAddressArray = 0;45UINT_PTR uiNameArray = 0;46UINT_PTR uiNameOrdinals = 0;47PIMAGE_NT_HEADERS pNtHeaders = NULL;48PIMAGE_DATA_DIRECTORY pDataDirectory = NULL;49PIMAGE_EXPORT_DIRECTORY pExportDirectory = NULL;5051// get the VA of the modules NT Header52pNtHeaders = (PIMAGE_NT_HEADERS)(uiLibraryAddress + ((PIMAGE_DOS_HEADER)uiLibraryAddress)->e_lfanew);5354pDataDirectory = (PIMAGE_DATA_DIRECTORY)&pNtHeaders->OptionalHeader.DataDirectory[ IMAGE_DIRECTORY_ENTRY_EXPORT ];5556// get the VA of the export directory57pExportDirectory = (PIMAGE_EXPORT_DIRECTORY)( uiLibraryAddress + pDataDirectory->VirtualAddress );5859// get the VA for the array of addresses60uiAddressArray = ( uiLibraryAddress + pExportDirectory->AddressOfFunctions );6162// get the VA for the array of name pointers63uiNameArray = ( uiLibraryAddress + pExportDirectory->AddressOfNames );6465// get the VA for the array of name ordinals66uiNameOrdinals = ( uiLibraryAddress + pExportDirectory->AddressOfNameOrdinals );6768// test if we are importing by name or by ordinal...69if( ((DWORD)lpProcName & 0xFFFF0000 ) == 0x00000000 )70{71// import by ordinal...7273// use the import ordinal (- export ordinal base) as an index into the array of addresses74uiAddressArray += ( ( IMAGE_ORDINAL( (DWORD)lpProcName ) - pExportDirectory->Base ) * sizeof(DWORD) );7576// resolve the address for this imported function77fpResult = (FARPROC)( uiLibraryAddress + DEREF_32(uiAddressArray) );78}79else80{81// import by name...82DWORD dwCounter = pExportDirectory->NumberOfNames;83while( dwCounter-- )84{85char * cpExportedFunctionName = (char *)(uiLibraryAddress + DEREF_32( uiNameArray ));8687// test if we have a match...88if( strcmp( cpExportedFunctionName, lpProcName ) == 0 )89{90// use the functions name ordinal as an index into the array of name pointers91uiAddressArray += ( DEREF_16( uiNameOrdinals ) * sizeof(DWORD) );9293// calculate the virtual address for the function94fpResult = (FARPROC)(uiLibraryAddress + DEREF_32( uiAddressArray ));9596// finish...97break;98}99100// get the next exported function name101uiNameArray += sizeof(DWORD);102103// get the next exported function name ordinal104uiNameOrdinals += sizeof(WORD);105}106}107}108__except( EXCEPTION_EXECUTE_HANDLER )109{110fpResult = NULL;111}112113return fpResult;114}115//===============================================================================================//116117118