/
users.cpp
289 lines (254 loc) · 9.07 KB
/
users.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
/**
* Copyright (c) 2014-present, The osquery authors
*
* This source code is licensed as defined by the LICENSE file found in the
* root directory of this source tree.
*
* SPDX-License-Identifier: (Apache-2.0 OR GPL-2.0-only)
*/
#include <osquery/utils/system/system.h>
// clang-format off
#include <LM.h>
// clang-format on
#include <osquery/core/core.h>
#include <osquery/core/tables.h>
#include <osquery/logger/logger.h>
#include "osquery/tables/system/windows/registry.h"
#include "osquery/tables/system/windows/users.h"
#include <osquery/utils/conversions/split.h>
#include <osquery/utils/conversions/tryto.h>
#include <osquery/utils/conversions/windows/strings.h>
#include <osquery/process/process.h>
namespace osquery {
std::string psidToString(PSID sid);
uint32_t getUidFromSid(PSID sid);
uint32_t getGidFromSid(PSID sid);
const std::string kRegProfilePath =
"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows "
"NT\\CurrentVersion\\ProfileList";
const char kRegSep = '\\';
const std::set<std::string> kWellKnownSids = {
"S-1-5-1",
"S-1-5-2",
"S-1-5-3",
"S-1-5-4",
"S-1-5-6",
"S-1-5-7",
"S-1-5-8",
"S-1-5-9",
"S-1-5-10",
"S-1-5-11",
"S-1-5-12",
"S-1-5-13",
"S-1-5-18",
"S-1-5-19",
"S-1-5-20",
"S-1-5-21",
"S-1-5-32",
};
namespace tables {
std::string getUserShell(const std::string& sid) {
// TODO: This column exists for cross-platform consistency, but
// the answer on Windows is arbitrary. %COMSPEC% env variable may
// be the best answer. Currently, hard-coded.
return "C:\\Windows\\system32\\cmd.exe";
}
std::string getUserHomeDir(const std::string& sid) {
QueryData res;
queryKey(kRegProfilePath + kRegSep + sid, res);
for (const auto& kKey : res) {
if (kKey.at("name") == "ProfileImagePath") {
return kKey.at("data");
}
}
return "";
}
// If given a list of UIDs to constrain the results, check if a particular SID
// has a RID that matches any of those UIDs.
bool sidMatchesAnyDesiredUids(const std::set<std::string>& uidStrings,
const std::string& sidString) {
// If there is no constraint of UIDs given, results will not be filtered
if (uidStrings.empty()) {
return true;
}
auto toks = osquery::split(sidString, "-");
auto uid = toks.at(toks.size() - 1);
for (auto desiredUid : uidStrings) {
if (uid.compare(desiredUid) == 0) {
return true;
}
}
return false;
}
// Given a SID, retrieve information about the matching user
void genUser(const std::string& sidString, QueryData& results) {
Row r;
r["uuid"] = sidString;
r["directory"] = getUserHomeDir(sidString);
r["shell"] = getUserShell(sidString);
r["type"] = kWellKnownSids.find(sidString) == kWellKnownSids.end()
? "roaming"
: "special";
PSID sid;
auto ret = ConvertStringSidToSidA(sidString.c_str(), &sid);
if (ret == 0) {
VLOG(1) << "Converting SIDstring to SID failed with " << GetLastError();
return;
} else {
auto uid = getUidFromSid(sid);
auto gid = getGidFromSid(sid);
r["uid"] = BIGINT(uid);
r["gid"] = BIGINT(gid);
r["uid_signed"] = INTEGER(uid);
r["gid_signed"] = INTEGER(gid);
wchar_t accntName[UNLEN] = {0};
wchar_t domName[DNLEN] = {0};
unsigned long accntNameLen = UNLEN;
unsigned long domNameLen = DNLEN;
SID_NAME_USE eUse;
ret = LookupAccountSidW(
nullptr, sid, accntName, &accntNameLen, domName, &domNameLen, &eUse);
r["username"] = ret != 0 ? wstringToString(accntName) : "";
LocalFree(sid);
// Also attempt to get the user account description comment. Move on if
// NetUserGetInfo returns an error, as it will for some system accounts.
unsigned long dwBasicUserInfoLevel = 2;
LPBYTE userLvl2Buff = nullptr;
ret =
NetUserGetInfo(nullptr, accntName, dwBasicUserInfoLevel, &userLvl2Buff);
if (ret == NERR_Success && userLvl2Buff != nullptr) {
r["description"] =
wstringToString(LPUSER_INFO_2(userLvl2Buff)->usri2_comment);
}
if (userLvl2Buff != nullptr) {
NetApiBufferFree(userLvl2Buff);
}
results.push_back(r);
}
}
// Enumerate the users from the profiles key in the Registry, matching only
// the UIDs/RIDs (if any) and skipping any SIDs of local-only users that
// were already processed in the earlier API-based enumeration.
void processRoamingProfiles(const std::set<std::string>& selectedUids,
const std::set<std::string>& processedSids,
QueryData& results) {
QueryData regResults;
queryKey(kRegProfilePath, regResults);
for (const auto& profile : regResults) {
Row r;
if (profile.at("type") != "subkey") {
continue;
}
auto sidString = profile.at("name");
if (sidMatchesAnyDesiredUids(selectedUids, sidString)) {
// Skip this user if already processed
if (processedSids.find(sidString) == processedSids.end()) {
genUser(sidString, results);
}
}
}
}
// Enumerate all local users, constraining results to the list of UIDs if
// any, and recording all enumerated users' SIDs to exclude later from the
// walk of the Roaming Profiles key in the registry.
void processLocalAccounts(const std::set<std::string>& selectedUids,
std::set<std::string>& processedSids,
QueryData& results) {
// Enumerate the users by only the usernames (level 0 struct) and then
// get the desired level of info for each (level 4 struct includes SIDs).
unsigned long dwUserInfoLevel = 0;
unsigned long dwDetailedUserInfoLevel = 4;
unsigned long dwNumUsersRead = 0;
unsigned long dwTotalUsers = 0;
unsigned long resumeHandle = 0;
unsigned long ret = 0;
LPBYTE userBuffer = nullptr;
do {
ret = NetUserEnum(nullptr,
dwUserInfoLevel,
0,
&userBuffer,
MAX_PREFERRED_LENGTH,
&dwNumUsersRead,
&dwTotalUsers,
&resumeHandle);
if ((ret == NERR_Success || ret == ERROR_MORE_DATA) &&
userBuffer != nullptr) {
auto iterBuff = LPUSER_INFO_0(userBuffer);
for (size_t i = 0; i < dwNumUsersRead; i++) {
LPBYTE userLvl4Buff = nullptr;
ret = NetUserGetInfo(nullptr,
iterBuff->usri0_name,
dwDetailedUserInfoLevel,
&userLvl4Buff);
if (ret != NERR_Success || userLvl4Buff == nullptr) {
if (userLvl4Buff != nullptr) {
NetApiBufferFree(userLvl4Buff);
}
VLOG(1) << "Failed to get SID for "
<< wstringToString(iterBuff->usri0_name)
<< " with error code " << ret;
iterBuff++;
continue;
}
// Will return empty string on fail
auto sid = LPUSER_INFO_4(userLvl4Buff)->usri4_user_sid;
auto uid = getUidFromSid(sid);
auto gid = getGidFromSid(sid);
auto sidString = psidToString(sid);
processedSids.insert(sidString);
if (sidMatchesAnyDesiredUids(selectedUids, sidString)) {
Row r;
r["uuid"] = sidString;
r["username"] = wstringToString(iterBuff->usri0_name);
r["uid"] = BIGINT(uid);
r["gid"] = BIGINT(gid);
r["uid_signed"] = INTEGER(uid);
r["gid_signed"] = INTEGER(gid);
r["description"] =
wstringToString(LPUSER_INFO_4(userLvl4Buff)->usri4_comment);
r["directory"] = getUserHomeDir(sidString);
r["shell"] = getUserShell(sidString);
r["type"] = "local";
results.push_back(r);
}
// Free the buffer allocated by NetUserGetInfo()
if (userLvl4Buff != nullptr) {
NetApiBufferFree(userLvl4Buff);
}
iterBuff++; // index to the next record returned by NetUserEnum()
}
} else {
// If there are no local users something may be amiss.
LOG(WARNING) << "NetUserEnum failed with " << ret;
}
// Free the buffer allocated by NetUserEnum()
if (userBuffer != nullptr) {
NetApiBufferFree(userBuffer);
}
} while (ret == ERROR_MORE_DATA);
}
QueryData genUsers(QueryContext& context) {
QueryData results;
std::set<std::string> processedSids;
std::set<std::string> selectedUids;
// implement index on UUID (SID on Windows) column by returning only the
// users in the constraint, bypassing the enumeration step entirely:
if (context.constraints["uuid"].exists(EQUALS)) {
auto sidStrings = context.constraints["uuid"].getAll(EQUALS);
for (const auto& sidString : sidStrings) {
genUser(sidString, results);
}
} else {
// implement index on UID (RID on Windows) column by enumerating all users'
// SIDS and finding matches with the UID portion, then returning matches:
if (context.constraints["uid"].exists(EQUALS)) {
selectedUids = context.constraints["uid"].getAll(EQUALS);
}
processLocalAccounts(selectedUids, processedSids, results);
processRoamingProfiles(selectedUids, processedSids, results);
}
return results;
}
}
}