-
Notifications
You must be signed in to change notification settings - Fork 24
/
resolve.c
142 lines (122 loc) · 3.51 KB
/
resolve.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* API Set Lookup
* Copyright (c) 2018-2019 Aidan Khoury. All rights reserved.
*
* @file resolve.c
* @authors Aidan Khoury (ajkhoury)
* @date 11/22/2018
*/
#include "apiset.h"
#include "ntrtl.h"
static
NTSTATUS
ApiSetpResolve(
_In_ HANDLE Process,
_In_ PCUNICODE_STRING Name,
_In_opt_ PCUNICODE_STRING BaseName,
_Out_ PUNICODE_STRING ResolvedName
)
{
NTSTATUS Status;
PROCESS_BASIC_INFORMATION BasicInfo;
ULONG ReturnLength;
PPEB Peb;
PVOID PebWow64;
BOOLEAN Resolved;
PAPI_SET_NAMESPACE ApiSetMap;
//
// Retrieve PEB address for given process.
//
Status = NtQueryInformationProcess(Process,
ProcessBasicInformation,
&BasicInfo,
sizeof(BasicInfo),
&ReturnLength);
if (!NT_SUCCESS(Status)) {
return Status;
}
Peb = (PPEB)BasicInfo.PebBaseAddress;
Status = NtQueryInformationProcess(Process,
ProcessWow64Information,
&PebWow64,
sizeof(PebWow64),
&ReturnLength);
if (!NT_SUCCESS(Status)) {
return Status;
}
if (PebWow64 != NULL) {
ApiSetMap = (PAPI_SET_NAMESPACE)(ULONG_PTR)((PPEB32)PebWow64)->ApiSetMap;
} else {
ApiSetMap = (PAPI_SET_NAMESPACE)Peb->ApiSetMap;
}
if (!ApiSetMap) {
return STATUS_NOT_FOUND;
}
switch (ApiSetMap->Version) {
//
// API set schema version 2
//
case API_SET_SCHEMA_VERSION_V2:
Status = ApiSetResolveToHostV2(ApiSetMap, Name, BaseName, &Resolved, ResolvedName);
if (NT_SUCCESS(Status) && !Resolved) {
Status = STATUS_NOT_FOUND;
}
break;
//
// API set schema version 3
//
case API_SET_SCHEMA_VERSION_V3:
Status = ApiSetResolveToHostV3(ApiSetMap, Name, BaseName, &Resolved, ResolvedName);
if (NT_SUCCESS(Status) && !Resolved) {
Status = STATUS_NOT_FOUND;
}
break;
//
// API set schema version 4
//
case API_SET_SCHEMA_VERSION_V4:
Status = ApiSetResolveToHostV4(ApiSetMap, Name, BaseName, &Resolved, ResolvedName);
if (NT_SUCCESS(Status) && !Resolved) {
Status = STATUS_NOT_FOUND;
}
break;
//
// API set schema version 6
//
case API_SET_SCHEMA_VERSION_V6:
Status = ApiSetResolveToHostV6(ApiSetMap, Name, BaseName, &Resolved, ResolvedName);
if (NT_SUCCESS(Status) && !Resolved) {
Status = STATUS_NOT_FOUND;
}
break;
default:
Status = STATUS_NOT_SUPPORTED;
break;
}
return Status;
}
BOOLEAN
APISETAPI
ApiSetResolve(
_In_z_ PWCHAR ApiSetName,
_In_opt_z_ PWCHAR BaseDllName,
_Out_ PUNICODE_STRING ResolvedHostLibrary
)
{
NTSTATUS Status;
UNICODE_STRING ApiToResolve;
UNICODE_STRING BaseName;
RtlInitUnicodeString(&ApiToResolve, ApiSetName);
if (BaseDllName != NULL) {
RtlInitUnicodeString(&BaseName, BaseDllName);
}
Status = ApiSetpResolve(GetCurrentProcess(),
&ApiToResolve,
BaseDllName ? &BaseName : NULL,
ResolvedHostLibrary
);
if (NT_SUCCESS(Status)) {
return TRUE;
}
return FALSE;
}