|
3 | 3 | #include <assert.h>
|
4 | 4 | #include <Psapi.h>
|
5 | 5 | #include <tlhelp32.h>
|
6 |
| - |
| 6 | +#pragma comment(lib,"Psapi.lib") |
7 | 7 | namespace CODELIB
|
8 | 8 | {
|
9 | 9 | CProcessImpl::CProcessImpl(void): m_dwPID(-1), m_hProcess(NULL)
|
@@ -171,120 +171,135 @@ namespace CODELIB
|
171 | 171 | return dwPID;
|
172 | 172 | }
|
173 | 173 |
|
174 |
| - BOOL CProcessImpl::EnumProcess( std::vector<PROCESSENTRY32>& proVec ) |
175 |
| - { |
176 |
| - PROCESSENTRY32 pe32 = {sizeof(pe32)}; |
177 |
| - HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL); |
178 |
| - |
179 |
| - if(INVALID_HANDLE_VALUE == hProcessSnap) return FALSE; |
180 |
| - |
181 |
| - if(Process32First(hProcessSnap, &pe32)) |
182 |
| - { |
183 |
| - do |
184 |
| - { |
185 |
| - proVec.push_back(pe32); |
186 |
| - } |
187 |
| - while(Process32Next(hProcessSnap, &pe32)); |
188 |
| - } |
189 |
| - |
190 |
| - CloseHandle(hProcessSnap); |
191 |
| - hProcessSnap = NULL; |
192 |
| - return TRUE; |
193 |
| - } |
194 |
| - |
195 |
| - BOOL CProcessImpl::CreateLowIntegrityProcess(PWSTR pszCommandLine) |
196 |
| - { |
197 |
| - DWORD dwError = ERROR_SUCCESS; |
198 |
| - HANDLE hToken = NULL; |
199 |
| - HANDLE hNewToken = NULL; |
200 |
| - SID_IDENTIFIER_AUTHORITY MLAuthority = SECURITY_MANDATORY_LABEL_AUTHORITY; |
201 |
| - PSID pIntegritySid = NULL; |
202 |
| - TOKEN_MANDATORY_LABEL tml = { 0 }; |
203 |
| - STARTUPINFO si = { sizeof(si) }; |
204 |
| - PROCESS_INFORMATION pi = { 0 }; |
205 |
| - |
206 |
| - // Open the primary access token of the process. |
207 |
| - if (!OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE | TOKEN_QUERY | |
208 |
| - TOKEN_ADJUST_DEFAULT | TOKEN_ASSIGN_PRIMARY, &hToken)) |
209 |
| - { |
210 |
| - dwError = GetLastError(); |
211 |
| - goto Cleanup; |
212 |
| - } |
213 |
| - |
214 |
| - // Duplicate the primary token of the current process. |
215 |
| - if (!DuplicateTokenEx(hToken, 0, NULL, SecurityImpersonation, |
216 |
| - TokenPrimary, &hNewToken)) |
217 |
| - { |
218 |
| - dwError = GetLastError(); |
219 |
| - goto Cleanup; |
220 |
| - } |
221 |
| - |
222 |
| - // Create the low integrity SID. |
223 |
| - if (!AllocateAndInitializeSid(&MLAuthority, 1, SECURITY_MANDATORY_LOW_RID, |
224 |
| - 0, 0, 0, 0, 0, 0, 0, &pIntegritySid)) |
225 |
| - { |
226 |
| - dwError = GetLastError(); |
227 |
| - goto Cleanup; |
228 |
| - } |
229 |
| - |
230 |
| - tml.Label.Attributes = SE_GROUP_INTEGRITY; |
231 |
| - tml.Label.Sid = pIntegritySid; |
232 |
| - |
233 |
| - // Set the integrity level in the access token to low. |
234 |
| - if (!SetTokenInformation(hNewToken, TokenIntegrityLevel, &tml, |
235 |
| - (sizeof(tml) + GetLengthSid(pIntegritySid)))) |
236 |
| - { |
237 |
| - dwError = GetLastError(); |
238 |
| - goto Cleanup; |
239 |
| - } |
240 |
| - |
241 |
| - // Create the new process at the Low integrity level. |
242 |
| - if (!CreateProcessAsUser(hNewToken, NULL, pszCommandLine, NULL, NULL, |
243 |
| - FALSE, 0, NULL, NULL, &si, &pi)) |
244 |
| - { |
245 |
| - dwError = GetLastError(); |
246 |
| - goto Cleanup; |
247 |
| - } |
| 174 | + BOOL CProcessImpl::EnumProcess(std::vector<PROCESSENTRY32>& proVec) |
| 175 | + { |
| 176 | + PROCESSENTRY32 pe32 = {sizeof(pe32)}; |
| 177 | + HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL); |
| 178 | + |
| 179 | + if(INVALID_HANDLE_VALUE == hProcessSnap) return FALSE; |
| 180 | + |
| 181 | + if(Process32First(hProcessSnap, &pe32)) |
| 182 | + { |
| 183 | + do |
| 184 | + { |
| 185 | + proVec.push_back(pe32); |
| 186 | + } |
| 187 | + while(Process32Next(hProcessSnap, &pe32)); |
| 188 | + } |
| 189 | + |
| 190 | + CloseHandle(hProcessSnap); |
| 191 | + hProcessSnap = NULL; |
| 192 | + return TRUE; |
| 193 | + } |
| 194 | + |
| 195 | + BOOL CProcessImpl::CreateLowIntegrityProcess(PWSTR pszCommandLine) |
| 196 | + { |
| 197 | + DWORD dwError = ERROR_SUCCESS; |
| 198 | + HANDLE hToken = NULL; |
| 199 | + HANDLE hNewToken = NULL; |
| 200 | + SID_IDENTIFIER_AUTHORITY MLAuthority = SECURITY_MANDATORY_LABEL_AUTHORITY; |
| 201 | + PSID pIntegritySid = NULL; |
| 202 | + TOKEN_MANDATORY_LABEL tml = { 0 }; |
| 203 | + STARTUPINFO si = { sizeof(si) }; |
| 204 | + PROCESS_INFORMATION pi = { 0 }; |
| 205 | + |
| 206 | + // Open the primary access token of the process. |
| 207 | + if(!OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE | TOKEN_QUERY | |
| 208 | + TOKEN_ADJUST_DEFAULT | TOKEN_ASSIGN_PRIMARY, &hToken)) |
| 209 | + { |
| 210 | + dwError = GetLastError(); |
| 211 | + goto Cleanup; |
| 212 | + } |
| 213 | + |
| 214 | + // Duplicate the primary token of the current process. |
| 215 | + if(!DuplicateTokenEx(hToken, 0, NULL, SecurityImpersonation, |
| 216 | + TokenPrimary, &hNewToken)) |
| 217 | + { |
| 218 | + dwError = GetLastError(); |
| 219 | + goto Cleanup; |
| 220 | + } |
| 221 | + |
| 222 | + // Create the low integrity SID. |
| 223 | + if(!AllocateAndInitializeSid(&MLAuthority, 1, SECURITY_MANDATORY_LOW_RID, |
| 224 | + 0, 0, 0, 0, 0, 0, 0, &pIntegritySid)) |
| 225 | + { |
| 226 | + dwError = GetLastError(); |
| 227 | + goto Cleanup; |
| 228 | + } |
| 229 | + |
| 230 | + tml.Label.Attributes = SE_GROUP_INTEGRITY; |
| 231 | + tml.Label.Sid = pIntegritySid; |
| 232 | + |
| 233 | + // Set the integrity level in the access token to low. |
| 234 | + if(!SetTokenInformation(hNewToken, TokenIntegrityLevel, &tml, |
| 235 | + (sizeof(tml) + GetLengthSid(pIntegritySid)))) |
| 236 | + { |
| 237 | + dwError = GetLastError(); |
| 238 | + goto Cleanup; |
| 239 | + } |
| 240 | + |
| 241 | + // Create the new process at the Low integrity level. |
| 242 | + if(!CreateProcessAsUser(hNewToken, NULL, pszCommandLine, NULL, NULL, |
| 243 | + FALSE, 0, NULL, NULL, &si, &pi)) |
| 244 | + { |
| 245 | + dwError = GetLastError(); |
| 246 | + goto Cleanup; |
| 247 | + } |
248 | 248 |
|
249 | 249 | Cleanup:
|
250 |
| - // Centralized cleanup for all allocated resources. |
251 |
| - if (hToken) |
252 |
| - { |
253 |
| - CloseHandle(hToken); |
254 |
| - hToken = NULL; |
255 |
| - } |
256 |
| - if (hNewToken) |
257 |
| - { |
258 |
| - CloseHandle(hNewToken); |
259 |
| - hNewToken = NULL; |
260 |
| - } |
261 |
| - if (pIntegritySid) |
262 |
| - { |
263 |
| - FreeSid(pIntegritySid); |
264 |
| - pIntegritySid = NULL; |
265 |
| - } |
266 |
| - if (pi.hProcess) |
267 |
| - { |
268 |
| - CloseHandle(pi.hProcess); |
269 |
| - pi.hProcess = NULL; |
270 |
| - } |
271 |
| - if (pi.hThread) |
272 |
| - { |
273 |
| - CloseHandle(pi.hThread); |
274 |
| - pi.hThread = NULL; |
275 |
| - } |
276 |
| - |
277 |
| - if (ERROR_SUCCESS != dwError) |
278 |
| - { |
279 |
| - // Make sure that the error code is set for failure. |
280 |
| - SetLastError(dwError); |
281 |
| - return FALSE; |
282 |
| - } |
283 |
| - else |
284 |
| - { |
285 |
| - return TRUE; |
286 |
| - } |
287 |
| - } |
| 250 | + |
| 251 | + // Centralized cleanup for all allocated resources. |
| 252 | + if(hToken) |
| 253 | + { |
| 254 | + CloseHandle(hToken); |
| 255 | + hToken = NULL; |
| 256 | + } |
| 257 | + |
| 258 | + if(hNewToken) |
| 259 | + { |
| 260 | + CloseHandle(hNewToken); |
| 261 | + hNewToken = NULL; |
| 262 | + } |
| 263 | + |
| 264 | + if(pIntegritySid) |
| 265 | + { |
| 266 | + FreeSid(pIntegritySid); |
| 267 | + pIntegritySid = NULL; |
| 268 | + } |
| 269 | + |
| 270 | + if(pi.hProcess) |
| 271 | + { |
| 272 | + CloseHandle(pi.hProcess); |
| 273 | + pi.hProcess = NULL; |
| 274 | + } |
| 275 | + |
| 276 | + if(pi.hThread) |
| 277 | + { |
| 278 | + CloseHandle(pi.hThread); |
| 279 | + pi.hThread = NULL; |
| 280 | + } |
| 281 | + |
| 282 | + if(ERROR_SUCCESS != dwError) |
| 283 | + { |
| 284 | + // Make sure that the error code is set for failure. |
| 285 | + SetLastError(dwError); |
| 286 | + return FALSE; |
| 287 | + } |
| 288 | + else |
| 289 | + { |
| 290 | + return TRUE; |
| 291 | + } |
| 292 | + } |
| 293 | + |
| 294 | + SIZE_T CProcessImpl::VirtualQueryEx(LPCVOID lpAddress, PMEMORY_BASIC_INFORMATION lpBuffer, SIZE_T dwLength) |
| 295 | + { |
| 296 | + return ::VirtualQueryEx(m_hProcess, lpAddress, lpBuffer, dwLength); |
| 297 | + } |
| 298 | + |
| 299 | + BOOL CProcessImpl::IsOpened() |
| 300 | + { |
| 301 | + return (NULL != m_hProcess); |
| 302 | + } |
288 | 303 |
|
289 | 304 | }
|
290 | 305 |
|
0 commit comments