- #include <stdio.h>
- #include <windows.h>
- #include <iostream>
- #include <string>
- #include <tlhelp32.h>
- void get_file_size(std::string path)
- {
- struct _stat fileinfo;
- _stat(path.c_str(), &fileinfo);
- std::cout << "file size is " << fileinfo.st_size << std::endl;
- }
- BOOL isElevated(VOID) {
- HANDLE hToken;
- BOOL bResult = FALSE;
- DWORD dwSize;
- TOKEN_ELEVATION te;
- if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) {
- if (GetTokenInformation(hToken, TokenElevation, &te,
- sizeof(TOKEN_ELEVATION), &dwSize)) {
- bResult = te.TokenIsElevated != 0;
- }
- CloseHandle(hToken);
- }
- return bResult;
- }
- BOOL SetPrivilege(HANDLE hInToken, const wchar_t *szPrivilege, BOOL bEnable) {
- HANDLE hToken;
- BOOL bResult;
- LUID luid;
- TOKEN_PRIVILEGES tp;
- if (hInToken) {
- hToken = hInToken;
- bResult = TRUE;
- }
- else {
- bResult = OpenProcessToken(GetCurrentProcess(),
- TOKEN_ADJUST_PRIVILEGES, &hToken);
- }
- if (bResult) {
- bResult = LookupPrivilegeValue(NULL, szPrivilege, &luid);
- if (bResult) {
- tp.PrivilegeCount = 1;
- tp.Privileges[0].Luid = luid;
- tp.Privileges[0].Attributes = (bEnable) ? SE_PRIVILEGE_ENABLED : 0;
- bResult = AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL);
- }
- if (!hInToken)
- CloseHandle(hToken);
- }
- return bResult;
- }
- DWORD GetProcessId(const wchar_t *szName) {
- DWORD dwId = 0;
- HANDLE hSnap;
- BOOL bResult;
- PROCESSENTRY32 pe32;
- hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- if (hSnap != INVALID_HANDLE_VALUE) {
- pe32.dwSize = sizeof(PROCESSENTRY32);
- bResult = Process32First(hSnap, &pe32);
- while (bResult) {
- if (lstrcmpi(pe32.szExeFile, szName) == 0) {
- dwId = pe32.th32ProcessID;
- break;
- }
- bResult = Process32Next(hSnap, &pe32);
- }
- CloseHandle(hSnap);
- }
- return dwId;
- }
- BOOL ImpersonateSystem(VOID) {
- BOOL bImpersonating = FALSE;
- HANDLE hToken, hProcess;
- // get id of a LocalSystem process
- DWORD dwId = GetProcessId(L"lsass.exe");
- if (dwId != 0) {
- // enable debug privilege
- if (SetPrivilege(NULL, SE_DEBUG_NAME, TRUE)) {
- // attempt to open process
- hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwId);
- if (hProcess != NULL) {
- // attempt to open process token
- if (OpenProcessToken(hProcess,
- TOKEN_IMPERSONATE | TOKEN_READ | TOKEN_QUERY | TOKEN_DUPLICATE /* | TOKEN_ADJUST_PRIVILEGES */ , &hToken)) {
- SetPrivilege(hToken, SE_TCB_NAME, TRUE);
- SetPrivilege(hToken, SE_ASSIGNPRIMARYTOKEN_NAME, TRUE);
- SetPrivilege(hToken, SE_INCREASE_QUOTA_NAME, TRUE);
- // attempt to impersonate LocalSystem
- bImpersonating = ImpersonateLoggedOnUser(hToken);
- if (bImpersonating) {
- std::string path = "C:\\cygwin64\\bin\\getent.exe";
- puts("#mark2");
- get_file_size(path);
- char wszCommand[256] = "C:\\cygwin64\\bin\\id.exe";
- STARTUPINFOA si = { sizeof(STARTUPINFO) };
- PROCESS_INFORMATION pi = { 0 };
- /* Unicode version of CreateProcess modifies its command parameter... Ansi doesn't.
- Apparently this is not classed as a bug ???? */
- if (!CreateProcessAsUserA(hToken, NULL, wszCommand, NULL, NULL, TRUE, CREATE_NO_WINDOW | CREATE_PRESERVE_CODE_AUTHZ_LEVEL, NULL, NULL, &si, &pi))
- {
- fprintf(stderr, "CreateProcess returned error %d\n", GetLastError());
- }
- CloseHandle(pi.hProcess);
- CloseHandle(pi.hThread);
- }
- else {
- std::cerr << "ImpersonateLoggedOnUser failed : " << GetLastError() << std::endl;
- }
- CloseHandle(hToken);
- }
- else {
- std::cerr << "OpenProcessToken failed : " << GetLastError() << std::endl;
- }
- CloseHandle(hProcess);
- }
- else {
- std::cerr << "OpenProcess(\"lsass.exe\") failed : " << GetLastError() << std::endl;
- }
- }
- else {
- std::cerr << "SetPrivilege(SE_DEBUG_NAME, TRUE) failed : " << GetLastError() << std::endl;;
- }
- }
- else {
- std::cerr << "GetProcessId(\"lsass.exe\") failed : " << GetLastError() << std::endl;;
- }
- return bImpersonating;
- }
- int main()
- {
- puts("#mark0");
- ImpersonateSystem();
- return 0;
- }
win32: run_something_as_SYSTEM
Posted by Anonymous on Tue 19th Sep 2023 12:25
raw | new post
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.