int _tmain(int argc, _TCHAR* argv[]) { if (!(SetCurrentProcessPrivilege(SE_ASSIGNPRIMARYTOKEN_NAME, true) && SetCurrentProcessPrivilege(SE_INCREASE_QUOTA_NAME, true) && SetCurrentProcessPrivilege(SE_DEBUG_NAME, true))) { ReturnMessage(L"进程调试权限获取失败"); return -1; } PPEB PEB = NtCurrentTeb()->ProcessEnvironmentBlock; return 0; }
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { SetProcessDPIAware(); if (!SetCurrentProcessPrivilege(SE_DEBUG_NAME, true)) { ReturnMessage(L"进程调试权限获取失败"); return -1; } GetModuleFileNameW(NULL, szAppPath, 260); wcsrchr(szAppPath, L'\\')[0] = NULL; wcscpy_s(szShortCutListPath, 260, szAppPath); wcscat_s(szShortCutListPath, 260, L"\\ShortCutList.ini"); DialogBoxParamW(hInstance, MAKEINTRESOURCEW(IDD_NSudoDlg), NULL, NSudoDlgCallBack, 0L); return 0; }
int wmain(int argc, _TCHAR* argv[]) { //******** NSudoInitialize Start ******** NSudoReturnMessage(TextRes.NSudo_AboutText); if (argc == 1) bGUIMode = true; SetProcessDPIAware(); GetModuleFileNameW(NULL, szAppPath, 260); wcsrchr(szAppPath, L'\\')[0] = NULL; wcscpy_s(szShortCutListPath, 260, szAppPath); wcscat_s(szShortCutListPath, 260, L"\\ShortCutList.ini"); if (!SetCurrentProcessPrivilege(SE_DEBUG_NAME, true)) { if (bGUIMode) { wchar_t szExePath[260]; GetModuleFileNameW(NULL, szExePath, 260); ShellExecuteW(NULL, L"runas", szExePath, NULL, NULL, SW_SHOW); return 0; } else { NSudoReturnMessage(TextRes.NSudo_Error_Text1); return -1; } } //******** NSudoInitialize End ******** if (bGUIMode) { FreeConsole(); DialogBoxParamW(GetModuleHandleW(NULL), MAKEINTRESOURCEW(IDD_NSudoDlg), NULL, NSudoDlgCallBack, 0L); } else { bool bUserArgEnable = true; bool bPrivilegeArgEnable = true; bool bIntegrityArgEnable = true; bool bCMDLineArgEnable = true; wchar_t *szBuffer = NULL; HANDLE hUserToken = INVALID_HANDLE_VALUE; for (int i = 1; i < argc; i++) { if (_wcsicmp(argv[i], L"-?") == 0) { NSudoReturnMessage(TextRes.NSudoC_HelpText); return 0; } else if (bUserArgEnable && _wcsicmp(argv[i], L"-U:T") == 0) { NSudoGetTrustedInstallerToken(&hUserToken); bUserArgEnable = false; } else if (bUserArgEnable && _wcsicmp(argv[i], L"-U:S") == 0) { NSudoGetSystemToken(&hUserToken); bUserArgEnable = false; } else if (bUserArgEnable && _wcsicmp(argv[i], L"-U:C") == 0) { NSudoGetCurrentUserToken(&hUserToken); bUserArgEnable = false; } else if (bUserArgEnable && _wcsicmp(argv[i], L"-U:P") == 0) { NSudoGetCurrentProcessToken(&hUserToken); bUserArgEnable = false; } else if (bUserArgEnable && _wcsicmp(argv[i], L"-U:D") == 0) { NSudoCreateLUAToken(&hUserToken); bUserArgEnable = false; } else if (bPrivilegeArgEnable && _wcsicmp(argv[i], L"-P:E") == 0) { NSudoAdjustAllTokenPrivileges(hUserToken, true); bPrivilegeArgEnable = false; } else if (bPrivilegeArgEnable && _wcsicmp(argv[i], L"-P:D") == 0) { NSudoAdjustAllTokenPrivileges(hUserToken, false); bPrivilegeArgEnable = false; } else if (bIntegrityArgEnable && _wcsicmp(argv[i], L"-M:S") == 0) { SetTokenIntegrity(hUserToken, L"S-1-16-16384"); bIntegrityArgEnable = false; } else if (bIntegrityArgEnable && _wcsicmp(argv[i], L"-M:H") == 0) { SetTokenIntegrity(hUserToken, L"S-1-16-12288"); bIntegrityArgEnable = false; } else if (bIntegrityArgEnable && _wcsicmp(argv[i], L"-M:M") == 0) { SetTokenIntegrity(hUserToken, L"S-1-16-8192"); bIntegrityArgEnable = false; } else if (bIntegrityArgEnable && _wcsicmp(argv[i], L"-M:L") == 0) { SetTokenIntegrity(hUserToken, L"S-1-16-4096"); bIntegrityArgEnable = false; } else if (bCMDLineArgEnable) { wchar_t szPath[260]; DWORD dwLength = GetPrivateProfileStringW(argv[i], L"CommandLine", L"", szPath, 260, szShortCutListPath); wcscmp(szPath, L"") != 0 ? szBuffer = szPath : szBuffer = argv[i]; if (szBuffer) bCMDLineArgEnable = false; } } if (bUserArgEnable || bCMDLineArgEnable) { NSudoReturnMessage(TextRes.NSudo_Error_Text4); return -1; } else { if (NSudoImpersonateSystemToken()) { if (!NSudoCreateProcess(hUserToken, szBuffer)) { NSudoReturnMessage(TextRes.NSudo_Error_Text3); } RevertToSelf(); } } CloseHandle(hUserToken); } return 0; }