Skip to content

Commit c9b63f9

Browse files
committed
Quiet false errors, commit missing 1.29 changes
Changes for 1.29 that somehow didn't get committed. Better checking for impersonation handle value (and quieting false errors) in Remote.cpp (v1.30)
1 parent 954f83e commit c9b63f9

8 files changed

+175
-90
lines changed

InteractiveSession.cpp

+90-40
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ BOOL RunningAsLocalSystem();
5151

5252
void CleanUpInteractiveProcess(CleanupInteractive* pCI)
5353
{
54-
SetTokenInformation(pCI->hUser, TokenSessionId, &pCI->origSessionID, sizeof(pCI->origSessionID));
54+
//SetTokenInformation(pCI->hUser, TokenSessionId, &pCI->origSessionID, sizeof(pCI->origSessionID));
5555

5656
//// Allow logon SID full access to interactive window station.
5757
//RemoveAceFromWindowStation(hwinsta, pSid);
@@ -74,47 +74,83 @@ void CleanUpInteractiveProcess(CleanupInteractive* pCI)
7474
//hdesk = NULL;
7575
}
7676

77-
BOOL PrepForInteractiveProcess(Settings& settings, CleanupInteractive* pCI, DWORD sessionID)
77+
BOOL CALLBACK EnumWindowStationsProc(LPWSTR lpszWindowStation, LPARAM lParam)
7878
{
79-
pCI->bPreped = true;
80-
//settings.hUser is set as the -u user, Local System (from -s) or as the account the user originally launched PAExec with
79+
Log(StrFormat(L"Seen winstation: %s", lpszWindowStation), (DWORD)0);
80+
return TRUE;
81+
}
8182

82-
//figure out which session we need to go into
83-
Duplicate(settings.hUser, __FILE__, __LINE__);
84-
pCI->hUser = settings.hUser;
8583

86-
DWORD targetSessionID = sessionID;
84+
BOOL PrepForInteractiveProcess(Settings& settings, CleanupInteractive* pCI)
85+
{
86+
EnablePrivilege(SE_TCB_NAME, NULL);
87+
88+
pCI->bPreped = true;
89+
90+
//settings.hUser is already set as the -u user, Local System (from -s) or as the account the user originally launched PAExec with
8791

92+
//figure out which session we need to go into
8893
if((DWORD)-1 == settings.sessionToInteractWith)
8994
{
90-
targetSessionID = GetInteractiveSessionID();
91-
Log(StrFormat(L"Using SessionID %u (interactive session)", targetSessionID), false);
95+
settings.sessionToInteractWith = GetInteractiveSessionID();
96+
Log(StrFormat(L"Using SessionID %u (interactive session)", settings.sessionToInteractWith), false);
9297
}
9398
else
94-
Log(StrFormat(L"Using SessionID %u from params", targetSessionID), false);
99+
Log(StrFormat(L"Using SessionID %u from params", settings.sessionToInteractWith), false);
95100

96-
//if(FALSE == WTSQueryUserToken(targetSessionID, &settings.hUser))
97-
// Log(L"Failed to get user from session ", GetLastError());
101+
if (settings.user.IsEmpty())
102+
{
103+
if(settings.bUseSystemAccount)
104+
{
105+
HANDLE hProcessToken = INVALID_HANDLE_VALUE;
106+
OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED, &hProcessToken);
107+
DuplicateTokenToIncreaseRights(hProcessToken, __FILE__, __LINE__);
108+
SetTokenInformation(hProcessToken, TokenSessionId, &settings.sessionToInteractWith, sizeof(DWORD));
109+
settings.hUser = hProcessToken;
110+
return TRUE;
111+
}
98112

99-
//Duplicate(settings.hUser, __FILE__, __LINE__);
113+
//no user given, but want interactive, so run as the currently logged in user
114+
HANDLE hTmp = INVALID_HANDLE_VALUE;
115+
if ((FALSE == WTSQueryUserToken(settings.sessionToInteractWith, &hTmp)) || (INVALID_HANDLE_VALUE == hTmp))
116+
Log(StrFormat(L"WTSQueryUserToken failed for session ID %d", settings.sessionToInteractWith), GetLastError());
100117

101-
DWORD returnedLen = 0;
102-
GetTokenInformation(settings.hUser, TokenSessionId, &pCI->origSessionID, sizeof(pCI->origSessionID), &returnedLen);
118+
if (INVALID_HANDLE_VALUE != hTmp)
119+
{
120+
Log(L"Using user from WTSQueryUserToken", (DWORD)0);
121+
settings.hUser = hTmp;
122+
DuplicateTokenToIncreaseRights(settings.hUser, __FILE__, __LINE__);
123+
}
124+
return TRUE;
125+
}
103126

104-
EnablePrivilege(SE_TCB_NAME, settings.hUser);
127+
//This is the hard case - interactive with a specific user. Not sure why it doesn't work better
128+
129+
DuplicateTokenToIncreaseRights(settings.hUser, __FILE__, __LINE__);
130+
pCI->hUser = settings.hUser;
105131

106-
if(FALSE == SetTokenInformation(settings.hUser, TokenSessionId, &targetSessionID, sizeof(targetSessionID)))
132+
//DWORD returnedLen = 0;
133+
//if(FALSE == GetTokenInformation(settings.hUser, TokenSessionId, &pCI->origSessionID, sizeof(pCI->origSessionID), &returnedLen))
134+
// Log(L"GetTokenInformation failed", GetLastError());
135+
136+
if(false == EnablePrivilege(SE_TCB_NAME, settings.hUser))
137+
Log(L"EnablePrivilege failed", GetLastError());
138+
139+
if(FALSE == SetTokenInformation(settings.hUser, TokenSessionId, &settings.sessionToInteractWith, sizeof(settings.sessionToInteractWith)))
107140
Log(L"Failed to set interactive token", GetLastError());
108141

109142
return TRUE;
110-
////START FUNKY STUFF
143+
144+
////START FUNKY STUFF - probably doesn't work because OpenWindowStation will get PAExec's Window Station in session 0, which is not what we want
145+
111146
// BOOL bResult = FALSE;
112147
//
113148
// HDESK hdesk = NULL;
114149
// HWINSTA hwinsta = NULL;
115150
// PSID pSid = NULL;
116151
// HWINSTA hwinstaSave = NULL;
117-
//
152+
// USEROBJECTFLAGS uof = { 0 };
153+
// DWORD needed = 0;
118154
//
119155
// // Save a handle to the caller's current window station.
120156
// if ((hwinstaSave = GetProcessWindowStation()) == NULL)
@@ -123,18 +159,28 @@ BOOL PrepForInteractiveProcess(Settings& settings, CleanupInteractive* pCI, DWOR
123159
// goto Cleanup;
124160
// }
125161
//
162+
// LPCWSTR winStaToUse = L"WinSta0";
163+
//
126164
// // Get a handle to the interactive window station.
127165
// hwinsta = OpenWindowStation(
128-
// _T("winsta0"), // the interactive window station
166+
// winStaToUse, // the interactive window station
129167
// FALSE, // handle is not inheritable
130-
// READ_CONTROL | WRITE_DAC); // rights to read/write the DACL
168+
// READ_CONTROL | WRITE_DAC | WINSTA_READATTRIBUTES); // rights to read/write the DACL
131169
//
132170
// if (BAD_HANDLE(hwinsta))
133171
// {
134-
// Log(L"Failed to open winsta0.", GetLastError());
172+
// Log(StrFormat(L"Failed to open WinStation %s.", winStaToUse), GetLastError());
173+
// EnumWindowStations(EnumWindowStationsProc, NULL);
174+
//
135175
// goto Cleanup;
136176
// }
137177
//
178+
// //Some logging
179+
// if(GetUserObjectInformation(hwinsta, UOI_FLAGS, &uof, sizeof(uof), &needed))
180+
// Log(StrFormat(L"WinStation visible: %d", uof.dwFlags), (DWORD)0);
181+
// else
182+
// Log(L"GetUserObjectInformation failed", GetLastError());
183+
//
138184
// // To get the correct default desktop, set the caller's
139185
// // window station to the interactive window station.
140186
// if (!SetProcessWindowStation(hwinsta))
@@ -195,8 +241,8 @@ BOOL PrepForInteractiveProcess(Settings& settings, CleanupInteractive* pCI, DWOR
195241
// bResult = TRUE;
196242
//
197243
//Cleanup:
198-
// if (!BAD_HANDLE(hwinstaSave))
199-
// SetProcessWindowStation (hwinstaSave);
244+
//// if (!BAD_HANDLE(hwinstaSave))
245+
//// SetProcessWindowStation (hwinstaSave);
200246
//
201247
// return bResult;
202248
}
@@ -1127,7 +1173,7 @@ typedef DWORD (WINAPI *WTSGetActiveConsoleSessionIdProc)(void);
11271173
DWORD GetInteractiveSessionID()
11281174
{
11291175
// Get the active session ID.
1130-
DWORD SessionId = 0;
1176+
DWORD SessionId = (DWORD)-1;
11311177
PWTS_SESSION_INFO pSessionInfo;
11321178
DWORD Count = 0;
11331179

@@ -1136,30 +1182,34 @@ DWORD GetInteractiveSessionID()
11361182
for (DWORD i = 0; i < Count; i ++)
11371183
{
11381184
if (pSessionInfo [i].State == WTSActive) //Here is
1139-
{
1140-
SessionId = pSessionInfo [i].SessionId;
1141-
}
1185+
SessionId = pSessionInfo[i].SessionId;
11421186
}
11431187
WTSFreeMemory (pSessionInfo);
11441188
}
11451189

1146-
if(0 == SessionId)
1190+
static WTSGetActiveConsoleSessionIdProc pWTSGetActiveConsoleSessionId = NULL;
1191+
if(NULL == pWTSGetActiveConsoleSessionId)
11471192
{
1148-
static WTSGetActiveConsoleSessionIdProc pWTSGetActiveConsoleSessionId = NULL;
1149-
if(NULL == pWTSGetActiveConsoleSessionId)
1193+
HMODULE hMod = LoadLibrary(L"Kernel32.dll"); //GLOK
1194+
if(NULL != hMod)
11501195
{
1151-
HMODULE hMod = LoadLibrary(L"Kernel32.dll"); //GLOK
1152-
if(NULL != hMod)
1153-
{
1154-
pWTSGetActiveConsoleSessionId = (WTSGetActiveConsoleSessionIdProc)GetProcAddress(hMod, "WTSGetActiveConsoleSessionId");
1155-
}
1196+
pWTSGetActiveConsoleSessionId = (WTSGetActiveConsoleSessionIdProc)GetProcAddress(hMod, "WTSGetActiveConsoleSessionId");
11561197
}
1198+
}
11571199

1158-
if(NULL != pWTSGetActiveConsoleSessionId) //not supported on Win2K
1159-
SessionId = pWTSGetActiveConsoleSessionId(); //we fall back on this if needed since it apparently doesn't always work
1200+
if(NULL != pWTSGetActiveConsoleSessionId) //not supported on Win2K
1201+
{
1202+
DWORD tmp = pWTSGetActiveConsoleSessionId(); //we fall back on this if needed since it apparently doesn't always work
1203+
if(0 == SessionId)
1204+
SessionId = tmp;
11601205
else
1161-
Log(L"WTSGetActiveConsoleSessionId not supported on this OS", false);
1206+
{
1207+
if(tmp != SessionId)
1208+
Log(StrFormat(L"WTSEnumerateSessions found session ID %u, but WTSGetActiveConsoleSessionId returned %u. Using %u.", SessionId, tmp, SessionId), (DWORD)0);
1209+
}
11621210
}
1211+
else
1212+
Log(L"WTSGetActiveConsoleSessionId not supported on this OS", false);
11631213

11641214
return SessionId;
11651215
}

PAExec.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919

2020
////////////////////////////////////////////////////////////////////////
2121
//
22+
//v1.30 - Feb 2, 2023
23+
// Tried to make interactive work better. Works OK if no username/password is given, and if running as the system account is wanted, but
24+
// still doesn't work well when credentials are given.
25+
//
2226
//v1.29 - April 14, 2021
2327
// will connect with given credentials before automatically trying file copy and service installation
2428
// support redirected input from file
@@ -335,7 +339,7 @@ void PrintCopyright()
335339
verInfo = NULL;
336340
}
337341

338-
Log(StrFormat(L"\r\nPAExec %s - Execute Programs Remotely\r\nCopyright (c) 2012-2021 Power Admin LLC\r\nwww.poweradmin.com/PAExec\r\n", ver), false);
342+
Log(StrFormat(L"\r\nPAExec %s - Execute Programs Remotely\r\nCopyright (c) 2012-2023 Power Admin LLC\r\nwww.poweradmin.com/PAExec\r\n", ver), false);
339343
}
340344

341345

PAExec.rc

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ BEGIN
6969
BEGIN
7070
VALUE "CompanyName", "Power Admin LLC"
7171
VALUE "FileDescription", "PAExec Application"
72-
VALUE "FileVersion", "1.29.0.0"
72+
VALUE "FileVersion", "1.30.0.0"
7373
VALUE "InternalName", "PAExec"
74-
VALUE "LegalCopyright", "Copyright (c) 2012-2021 Power Admin LLC"
74+
VALUE "LegalCopyright", "Copyright (c) 2012-2023 Power Admin LLC"
7575
VALUE "OriginalFilename", "PAExec.exe"
7676
VALUE "ProductName", "PAExec Application"
77-
VALUE "ProductVersion", "1.29.0.0"
77+
VALUE "ProductVersion", "1.30.0.0"
7878
END
7979
END
8080
BLOCK "VarFileInfo"

PAExec.vcxproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@
1414
<ProjectGuid>{2FEB96F5-08E6-48A3-B306-794277650A08}</ProjectGuid>
1515
<RootNamespace>PAExec</RootNamespace>
1616
<Keyword>Win32Proj</Keyword>
17-
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
17+
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
1818
</PropertyGroup>
1919
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
2020
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
2121
<ConfigurationType>Application</ConfigurationType>
22-
<PlatformToolset>v141_xp</PlatformToolset>
22+
<PlatformToolset>v143</PlatformToolset>
2323
<UseOfMfc>false</UseOfMfc>
2424
<CharacterSet>Unicode</CharacterSet>
2525
<WholeProgramOptimization>true</WholeProgramOptimization>
2626
</PropertyGroup>
2727
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
2828
<ConfigurationType>Application</ConfigurationType>
29-
<PlatformToolset>v141_xp</PlatformToolset>
29+
<PlatformToolset>v143</PlatformToolset>
3030
<UseOfMfc>false</UseOfMfc>
3131
<CharacterSet>Unicode</CharacterSet>
3232
</PropertyGroup>

0 commit comments

Comments
 (0)