@@ -108,7 +108,7 @@ thread_local std::string currentParentExe = ""; // to store the name of our own
108108
109109std::string WideToString (const std::wstring& wstr);
110110
111- void EnsureCurrentParentExe (hSnapshot) {
111+ void EnsureCurrentParentExe (HANDLE hSnapshot) {
112112 if (!currentParentExe.empty ()) return ;
113113
114114
@@ -140,7 +140,7 @@ void EnsureCurrentParentExe(hSnapshot) {
140140 }
141141 }
142142
143- CloseHandle (hSnapshot);
143+
144144}
145145
146146
@@ -310,7 +310,7 @@ std::string GetReadableFileTime(DWORD pid) {
310310 return oss.str ();
311311}
312312
313- void PrintErrorHints (int errorCode, hshot) {
313+ void PrintErrorHints (int errorCode, HANDLE hshot) {
314314 EnsureCurrentParentExe (hshot);
315315 // Use our little lookup table to give hints for specific errors
316316 if (errorHints.find (errorCode) != errorHints.end ()) {
@@ -430,25 +430,22 @@ std::optional<std::wstring> GetUserNameFromProcess(DWORD id)
430430// Permalink: https://stackoverflow.com/a/73242956
431431// Thanks!
432432
433- std::string GetProcessNameFromPid (DWORD pid) {
434- HANDLE snapshot = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS , 0 );
435- if (snapshot == INVALID_HANDLE_VALUE ) {
436- return " " ; // vroken
437- }
433+ std::string GetProcessNameFromPid (DWORD pid, HANDLE snapshot) {
434+
438435
439436 PROCESSENTRY32 pe{};
440437 pe.dwSize = sizeof (PROCESSENTRY32 );
441438
442439 if (Process32First (snapshot, &pe)) {
443440 do {
444441 if (pe.th32ProcessID == pid) {
445- CloseHandle (snapshot);
442+
446443 return WideToString (pe.szExeFile );
447444 }
448445 } while (Process32Next (snapshot, &pe));
449446 }
450447
451- CloseHandle (snapshot);
448+
452449 return " " ;
453450}
454451
@@ -1725,9 +1722,9 @@ void FindProcessPorts(DWORD targetPid) {
17251722
17261723
17271724
1728- void PIDinspect (const std::vector<DWORD >& pids, const std::vector<std::string>& names, hshot) { // ooh guys look i'm in the void
1725+ void PIDinspect (const std::vector<DWORD >& pids, const std::vector<std::string>& names, HANDLE hshot) { // ooh guys look i'm in the void
17291726 DWORD pid = pids[0 ];
1730- std::string procName = GetProcessNameFromPid (pid);
1727+ std::string procName = GetProcessNameFromPid (pid, hshot );
17311728 if (IsVirtualTerminalModeEnabled ()) {
17321729 if (procName == " " ){
17331730 std::cout << " \033 [34mTarget:\033 [0m N/A\n\033 [34mProcess:\033 [0m N/A\n " ;
@@ -1987,7 +1984,7 @@ struct ProcInfos {
19871984 std::vector<int > pids;
19881985};
19891986
1990- ProcInfos findMyProc (const char *procname, hSnapshot) {
1987+ ProcInfos findMyProc (const char *procname, HANDLE hSnapshot) {
19911988
19921989
19931990 PROCESSENTRY32 pe;
@@ -2034,8 +2031,7 @@ ProcInfos findMyProc(const char *procname, hSnapshot) {
20342031 hResult = Process32Next (hSnapshot, &pe);
20352032 }
20362033
2037- // closes an open handle (CreateToolhelp32Snapshot)
2038- CloseHandle (hSnapshot);
2034+
20392035 return result;
20402036}
20412037// The above function is taken from https://cocomelonc.github.io/pentest/2021/09/29/findmyprocess.html, modified simply to use WideToString for the process name comparison among other things.
@@ -2143,6 +2139,7 @@ int main(int argc, char* argv[]) {
21432139 HANDLE hshot = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS , 0 );
21442140 if (INVALID_HANDLE_VALUE == hSnapshot) {return {}};
21452141 PIDinspect (pids, trash, hshot);
2142+ CloseHandle (hshot);
21462143 } else {
21472144 if (IsVirtualTerminalModeEnabled ()) { // ugh i have to do this EVERY SINGLE TIME
21482145 std::cerr << " \033 [1;31mError:\033 [0m --pid option requires an argument." << std::endl;
@@ -2166,6 +2163,7 @@ int main(int argc, char* argv[]) {
21662163 if (!r.pids .empty ()) {
21672164 std::vector<DWORD > dwPids (r.pids .begin (), r.pids .end ());
21682165 PIDinspect (dwPids, r.names , hshot);
2166+ CloseHandle (hshot);
21692167 } else {
21702168 if (IsVirtualTerminalModeEnabled ()) {
21712169 std::cerr << " \033 [1;31mError:\033 [0m Could not find process with name " << procName << " ." << std::endl;
0 commit comments