Skip to content

Commit 1301a05

Browse files
Merge pull request #53 from supervoidcoder/show-user
feat: added user entry on output. Now it shows what user started a specific process
2 parents d8e5820 + cd1c3c7 commit 1301a05

2 files changed

Lines changed: 95 additions & 2 deletions

File tree

main.cpp

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,80 @@ void PrintErrorHints(int errorCode) {
303303
}
304304
}
305305

306+
std::optional<std::wstring> GetUserNameFromProcess(DWORD id)
307+
{
308+
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, id);
309+
310+
311+
if (!hProcess && GetLastError() == ERROR_ACCESS_DENIED) {
312+
hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, id); // cute fallback
313+
}
314+
std::wstring endUser = L"";
315+
std::wstring endDomain = L"";
316+
317+
if (hProcess != NULL)
318+
{
319+
HANDLE hToken = NULL;
320+
321+
if (OpenProcessToken(hProcess, TOKEN_QUERY, &hToken)) // 2- OpenProcessToken
322+
{
323+
DWORD tokenSize = 0;
324+
if (!GetTokenInformation(hToken, TokenUser, nullptr, 0, &tokenSize) &&
325+
GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
326+
CloseHandle(hToken);
327+
CloseHandle(hProcess);
328+
return {};
329+
}
330+
331+
if (tokenSize > 0)
332+
{
333+
std::vector<BYTE> data(tokenSize);
334+
if (!GetTokenInformation(hToken, TokenUser, data.data(), tokenSize, &tokenSize)) {
335+
CloseHandle(hToken);
336+
CloseHandle(hProcess);
337+
return {};
338+
}
339+
TOKEN_USER* pUser = reinterpret_cast<TOKEN_USER*>(data.data());
340+
PSID pSID = pUser->User.Sid;
341+
DWORD userSize = 0;
342+
DWORD domainSize = 0;
343+
SID_NAME_USE sidName;
344+
if (!LookupAccountSidW(nullptr, pSID, nullptr, &userSize, nullptr, &domainSize, &sidName) &&
345+
GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
346+
CloseHandle(hToken);
347+
CloseHandle(hProcess);
348+
return {};
349+
}
350+
std::wstring user(userSize, L'\0');
351+
std::wstring domain(domainSize, L'\0');
352+
if (!LookupAccountSidW(nullptr, pSID, user.data(), &userSize, domain.data(), &domainSize, &sidName)) {
353+
CloseHandle(hToken);
354+
CloseHandle(hProcess);
355+
return {};
356+
}
357+
user.resize(userSize);
358+
domain.resize(domainSize);
359+
endUser = user;
360+
endDomain = domain;
361+
}
362+
363+
364+
CloseHandle(hToken);
365+
}
366+
367+
CloseHandle(hProcess);
368+
369+
if (endUser != L"")
370+
return endUser;
371+
}
372+
373+
return {};
374+
}
375+
// I just straight up stole this function from Stack Overflow lol
376+
// https://stackoverflow.com/questions/2686096/c-get-username-from-process
377+
// Permalink: https://stackoverflow.com/a/73242956
378+
// Thanks!
379+
306380

307381
void PrintAncestry(DWORD pid) {
308382

@@ -558,7 +632,25 @@ void PIDinspect(DWORD pid) { // ooh guys look i'm in the void
558632
}
559633

560634
// Use our little lookup table to give hints for specific errors
561-
635+
auto user = GetUserNameFromProcess(pid); // dang it dude it feels like such a war crime using auto in c++ 😭✌️
636+
if (user.has_value()) {
637+
if (IsVirtualTerminalModeEnabled()) {
638+
std::cout << "\033[1;34mUser\033[0m: " << WideToString(user.value());
639+
} else {
640+
std::cout << "User: " << WideToString(user.value());
641+
}
642+
643+
} else {
644+
if (IsVirtualTerminalModeEnabled()) {
645+
std::cout << "\033[1;34mUser\033[0m: \033[1;31mN/A (Failed to access info)\033[0m";
646+
} else {
647+
std::cout << "User: N/A (Failed to access info)";
648+
}
649+
}
650+
651+
// literally very rough start i just rushed to get this done
652+
// still needs lots of error handling, some code modifying
653+
// so far i dont even know if the function works due to how rushed i did this
562654

563655

564656

tests/process/process.bat

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
win-witr winlogon.exe
22
win-witr lsass.exe
33
win-witr win-witr.exe
4-
win-witr wininit.exe
4+
win-witr wininit.exe
5+

0 commit comments

Comments
 (0)