-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPwdLdLibrary.cpp
292 lines (231 loc) · 7.67 KB
/
PwdLdLibrary.cpp
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#ifdef _LEAKDETECT
#ifdef _DEBUG
#ifndef DBG_NEW
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
#define new DBG_NEW
#endif
#endif // _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif
#include <windows.h>
#include <npapi.h>
#include <ntsecapi.h>
#include <tchar.h>
#include <wchar.h>
#include <string.h>
#include <Strsafe.h>
#include <stdio.h>
#include <time.h>
#ifndef _LEAKDETECT
#include <stdlib.h>
#endif
#include <vector>
#include "PwdCommon.h"
#include "PwdLdLibrary.h"
#ifndef STATUS_SUCCESS
#define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
#endif
typedef int (__cdecl *PPWD_FILT)(PUNICODE_STRING UserName, PUNICODE_STRING FullName, PUNICODE_STRING Password, BOOL SetOperation);
HMODULE hModuleDll;
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
hModuleDll = hModule;
return TRUE;
}
BOOL NTAPI InitializeChangeNotify(void)
{
return TRUE;
}
NTSTATUS NTAPI PasswordChangeNotify(PUNICODE_STRING UserName, ULONG RelativeId, PUNICODE_STRING Password)
{
return STATUS_SUCCESS;
}
BOOL NTAPI PasswordFilter(PUNICODE_STRING UserName, PUNICODE_STRING FullName, PUNICODE_STRING Password, BOOL SetOperation)
{
log_evt_func_call(Debug,AT_FILE_LINE,_TEXT(__FUNCTION__));
PWLLConfig pwll_config; //Config structure for PwdLdLibrary
PWCHAR pwll_config_str = NULL; //String representation of PWLLConfig structure. Used for logging
PWCHAR c_username = NULL; //Username as a PWCHAR
HINSTANCE hLib = NULL; //Handle to library (.DLL)
PPWD_FILT pwd_filt = NULL; //Pointer to pwd_filt function in remote library
BOOL pwd_status = FALSE; //Return value from pwd_filt from remote library
int result = 0; //Temp var used for getting return value from func calls
PWCHAR printf_format_str = NULL; //String sent to any printf funcs (printf,swprintf,etc)
size_t printf_char_count = 0; //Used for storing the character count for buffer used w/ printf funcs
//Default debug level before config is parsed
debug_level = Debug;
//Convert username to PCHAR for logging purposes
c_username = PUNICODE_STRING_to_PWCHAR(UserName);
//Global User variable - used for associating log messages w/ particular user
USER = c_username;
//Log Passwd Change Recieved
log_evt(Info,AT_FILE_LINE,_TEXT("Password change received."));
if(!pwll_config.load_config_from_registry()){
log_evt(Error,AT_FILE_LINE,_TEXT("Failed to parse configuration from registry. Aborting..."));
}
else{
//Set debug level
debug_level = pwll_config.debug_lvl;
//Log configuration
pwll_config_str = pwll_config.to_string();
if(pwll_config_str != NULL) {
log_evt(Debug, AT_FILE_LINE, pwll_config_str);
delete[] pwll_config_str;
}
//Check if user is excluded from being processed further
if(pwll_config.exclude_users.size() != 0){
for(std::vector<PWCHAR>::size_type i = 0; i != pwll_config.exclude_users.size(); i++) {
if(_wcsicmp(pwll_config.exclude_users[i], c_username) == 0){
log_evt(Debug, AT_FILE_LINE, _TEXT("User matches exclusion list. Password change permitted but further processing will not be performed."));
pwd_status = TRUE;
break;
}
}
}
if(pwd_status != TRUE && pwll_config.libraries.size() != 0){
for(std::vector<PWCHAR>::size_type i = 0; i != pwll_config.libraries.size(); i++) {
size_t dll_len = wcslen(pwll_config.libraries[i]) + 1;
WCHAR* abs_lib_path = new WCHAR[dll_len];
wcscpy_s(abs_lib_path, dll_len, pwll_config.libraries[i]);
hLib = LoadLibrary(abs_lib_path);
if(hLib == NULL) {
log_evt(Error, AT_FILE_LINE, _TEXT("Failed to load library %s. Aborting..."), abs_lib_path);
pwd_status = FALSE;
}
else {
//Log loading of library
log_evt(Info, AT_FILE_LINE, _TEXT("Successfully loaded library %s."), abs_lib_path);
pwd_filt = (PPWD_FILT)GetProcAddress(hLib, "pwd_filt");
if(pwd_filt == NULL) {
log_evt(Error, AT_FILE_LINE, _TEXT("Failed to load pointer to function pwd_filt for library %s. Aborting..."), abs_lib_path);
pwd_status = FALSE;
}
else {
pwd_status = pwd_filt(UserName, FullName, Password, SetOperation);
//Log result of pwd_filt for this particular library
log_evt(Info, AT_FILE_LINE, _TEXT("Function pwd_filt returned %d for library %s."), pwd_status, pwll_config.libraries[i]);
}
FreeLibrary(hLib);
hLib = NULL;
}
delete[] abs_lib_path;
if(!pwd_status)
break;
}
}
}
//Cleanup
USER = NULL;
delete[] c_username;
log_evt_func_exit(Info,AT_FILE_LINE,_TEXT(__FUNCTION__),pwd_status);
return pwd_status;
}
int PWLLConfig::load_config_from_registry()
{
log_evt_func_call(Debug,AT_FILE_LINE,_TEXT(__FUNCTION__));
int result = 1;
int tmp = 0;
PWCHAR tmp_str = NULL;
PWCHAR read_error_msg = _TEXT("simple_reg_read failed to open or read HKLM\\%s %s");
//Debug level
result = simple_reg_read_int(
HKEY_LOCAL_MACHINE,
REG_KEY_CONF,
REG_NAME_DBG_LVL,
sizeof(WCHAR) * 2,
tmp);
if(!result){
log_evt(Error,AT_FILE_LINE,read_error_msg,REG_KEY_CONF,REG_NAME_DBG_LVL);
}
else
debug_lvl = (DEBUG_LEVEL) tmp;
//Exclude users
if(result){
result = simple_reg_read_wstring(
HKEY_LOCAL_MACHINE,
REG_KEY_CONF,
REG_EXCLUDE_USERS,
sizeof(WCHAR)*1024,
tmp_str);
if(!result){
log_evt(Error, AT_FILE_LINE, read_error_msg, REG_KEY_CONF, REG_EXCLUDE_USERS);
}
else{
if(tokenize_string(tmp_str, ' ', exclude_users) < 1){
log_evt(
Warn,
AT_FILE_LINE,
_TEXT("Failed to parse HKLM\\%s %s into list of user exclusions. Proceeding w/ empty user exclusion list."),
REG_KEY_CONF,
REG_EXCLUDE_USERS);
}
}
delete[] tmp_str;
}
//Libs
if(result){
result = simple_reg_read_wstring(
HKEY_LOCAL_MACHINE,
REG_KEY_CONF,
REG_NAME_LIBS,
sizeof(WCHAR)*1024,
tmp_str);
if(!result){
log_evt(Error,AT_FILE_LINE,read_error_msg,REG_KEY_CONF,REG_NAME_LIBS);
}
else {
if(tokenize_string(tmp_str, ' ', libraries) < 1){
log_evt(
Warn,
AT_FILE_LINE,
_TEXT("Failed to parse HKLM\\%s %s into list of libraries. Proceeding w/ empty library list."),
REG_KEY_CONF,
REG_NAME_LIBS);
}
delete[] tmp_str;
}
}
log_evt_func_exit(Debug,AT_FILE_LINE,_TEXT(__FUNCTION__),result);
return result;
}
PWCHAR PWLLConfig::to_string()
{
log_evt_func_call(Debug,AT_FILE_LINE,_TEXT(__FUNCTION__));
size_t config_str_len = 0;
PWCHAR config_str = NULL;
PWCHAR libs = NULL;
PWCHAR excludes = NULL;
WCHAR config_templ[] =
L"Settings {"
L" Debug Level: %d;"
L" Exclude Users: %s;"
L" Libraries: %s; };";
config_str_len += wcslen(config_templ);
config_str_len += 2; //Debug level should never be more than 2 digits
//Libs
untokenize_string_array(libraries, ' ', &libs);
config_str_len += wcslen(libs);
config_str_len++;
//User exclusions
untokenize_string_array(exclude_users, ' ', &excludes);
config_str_len += wcslen(excludes);
config_str_len++;
config_str = new WCHAR[config_str_len];
int result = swprintf_s(config_str, config_str_len, config_templ, debug_lvl, excludes, libs);
if(result < 0)
return NULL;
delete[] libs;
delete[] excludes;
log_evt_func_exit(Debug,AT_FILE_LINE,_TEXT(__FUNCTION__),result);
return config_str;
}
PWLLConfig::~PWLLConfig() {
for(std::vector<PWCHAR>::size_type i = 0; i != exclude_users.size(); i++) {
delete[] exclude_users[i];
}
for(std::vector<PWCHAR>::size_type i = 0; i != libraries.size(); i++) {
delete[] libraries[i];
}
}