CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/external/source/exploits/CVE-2015-2426/inject/src/GetProcAddressR.c
Views: 11788
1
//===============================================================================================//
2
// Copyright (c) 2013, Stephen Fewer of Harmony Security (www.harmonysecurity.com)
3
// All rights reserved.
4
//
5
// Redistribution and use in source and binary forms, with or without modification, are permitted
6
// provided that the following conditions are met:
7
//
8
// * Redistributions of source code must retain the above copyright notice, this list of
9
// conditions and the following disclaimer.
10
//
11
// * Redistributions in binary form must reproduce the above copyright notice, this list of
12
// conditions and the following disclaimer in the documentation and/or other materials provided
13
// with the distribution.
14
//
15
// * Neither the name of Harmony Security nor the names of its contributors may be used to
16
// endorse or promote products derived from this software without specific prior written permission.
17
//
18
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
// POSSIBILITY OF SUCH DAMAGE.
27
//===============================================================================================//
28
#include "GetProcAddressR.h"
29
//===============================================================================================//
30
// We implement a minimal GetProcAddress to avoid using the native kernel32!GetProcAddress which
31
// wont be able to resolve exported addresses in reflectivly loaded librarys.
32
FARPROC WINAPI GetProcAddressR( HANDLE hModule, LPCSTR lpProcName )
33
{
34
UINT_PTR uiLibraryAddress = 0;
35
FARPROC fpResult = NULL;
36
37
if( hModule == NULL )
38
return NULL;
39
40
// a module handle is really its base address
41
uiLibraryAddress = (UINT_PTR)hModule;
42
43
__try
44
{
45
UINT_PTR uiAddressArray = 0;
46
UINT_PTR uiNameArray = 0;
47
UINT_PTR uiNameOrdinals = 0;
48
PIMAGE_NT_HEADERS pNtHeaders = NULL;
49
PIMAGE_DATA_DIRECTORY pDataDirectory = NULL;
50
PIMAGE_EXPORT_DIRECTORY pExportDirectory = NULL;
51
52
// get the VA of the modules NT Header
53
pNtHeaders = (PIMAGE_NT_HEADERS)(uiLibraryAddress + ((PIMAGE_DOS_HEADER)uiLibraryAddress)->e_lfanew);
54
55
pDataDirectory = (PIMAGE_DATA_DIRECTORY)&pNtHeaders->OptionalHeader.DataDirectory[ IMAGE_DIRECTORY_ENTRY_EXPORT ];
56
57
// get the VA of the export directory
58
pExportDirectory = (PIMAGE_EXPORT_DIRECTORY)( uiLibraryAddress + pDataDirectory->VirtualAddress );
59
60
// get the VA for the array of addresses
61
uiAddressArray = ( uiLibraryAddress + pExportDirectory->AddressOfFunctions );
62
63
// get the VA for the array of name pointers
64
uiNameArray = ( uiLibraryAddress + pExportDirectory->AddressOfNames );
65
66
// get the VA for the array of name ordinals
67
uiNameOrdinals = ( uiLibraryAddress + pExportDirectory->AddressOfNameOrdinals );
68
69
// test if we are importing by name or by ordinal...
70
if( ((DWORD)lpProcName & 0xFFFF0000 ) == 0x00000000 )
71
{
72
// import by ordinal...
73
74
// use the import ordinal (- export ordinal base) as an index into the array of addresses
75
uiAddressArray += ( ( IMAGE_ORDINAL( (DWORD)lpProcName ) - pExportDirectory->Base ) * sizeof(DWORD) );
76
77
// resolve the address for this imported function
78
fpResult = (FARPROC)( uiLibraryAddress + DEREF_32(uiAddressArray) );
79
}
80
else
81
{
82
// import by name...
83
DWORD dwCounter = pExportDirectory->NumberOfNames;
84
while( dwCounter-- )
85
{
86
char * cpExportedFunctionName = (char *)(uiLibraryAddress + DEREF_32( uiNameArray ));
87
88
// test if we have a match...
89
if( strcmp( cpExportedFunctionName, lpProcName ) == 0 )
90
{
91
// use the functions name ordinal as an index into the array of name pointers
92
uiAddressArray += ( DEREF_16( uiNameOrdinals ) * sizeof(DWORD) );
93
94
// calculate the virtual address for the function
95
fpResult = (FARPROC)(uiLibraryAddress + DEREF_32( uiAddressArray ));
96
97
// finish...
98
break;
99
}
100
101
// get the next exported function name
102
uiNameArray += sizeof(DWORD);
103
104
// get the next exported function name ordinal
105
uiNameOrdinals += sizeof(WORD);
106
}
107
}
108
}
109
__except( EXCEPTION_EXECUTE_HANDLER )
110
{
111
fpResult = NULL;
112
}
113
114
return fpResult;
115
}
116
//===============================================================================================//
117
118