绕过UAC提示以管理员身份运行程序

在windows 7下,我们可能经常需要以管理员权限运行某个程序,但每次运行一个程序都得:先找到那个程序,右键鼠标,在弹出菜单中选择以管理员身份运行,而且还会弹出以下窗口,让你点击确认才行。

1

 

有些人可能会选择禁用UAC,但这毕竟对系统的安全存在影响。下面就用一个简单的方法解决这个问题。

这个简单的方法就是利用系统自带的任务计划程序。

 

第一步:创建一个任务来运行你想要运行的程序

任务计划程序在开始菜单->附件->系统工具这个目录下,或者你直接在开始菜单的搜索栏中输入Task Scheduler也可以找到。

tsk_sched1

 

我们在右边点击创建一个新的任务。

task_sched2

 

按照下图设置好新建的任务。

ts_uac1

 

最主要的就是记得勾选Run with highest privileges,任务的名字可以随便你,但是后面还会用到它。

下面在Actions这个Tab目录下,我们还要新建一个Action,这里的Program就是选择你自己需要启动的程序了,如下图

ts_uac2

 

最后,还要再设定一下,让任务能够直接运行,至于能不能重复运行,就看你自己程序的要求了。

ts_uac3

 

OK,我们的任务创建完成,我们可以在任务计划程序里试一下。

找到你创建的任务,右键运行。

ts_uac4

 

你将看到以管理员身份运行出来的cmd,并且没有UAC的提示。

ts_uac5

 

接下来第二步。

 

第二步:为任务创建一个快捷方式

我们不可能每次都得打开任务计划程序再去运行一个程序,所以我们可以创建一个快捷方式,直接双击就能运行了。

随便在你需要创建快捷方式的地方,右键鼠标,选择New->Shortcut

输入以下命令:schtasks /run /tn “CommandPrompt”

这里的CommandPrompt就是你新建的任务名字。

ts_uac6

 

点击Next就创建好了。

现在双击一下你自己创建的快捷方式试试?(提示:你如果依然使用CMD程序的话可能会一闪而过,不过它的确是在管理员权限下的了)

 

 

 

文章引用:

http://www.7tutorials.com/use-task-scheduler-launch-programs-without-uac-prompts

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Windows 中,要以管理员权限运行一个进程并绕过 UAC 提示,可以使用以下方法: 1. 使用 ShellExecuteEx 函数启动进程。该函数可以在指定的进程上下文中启动应用程序。 2. 在启动进程之前,需要创建一个管理员特权的进程令牌。可以使用 OpenProcessToken 和 DuplicateTokenEx 函数来创建一个管理员特权的令牌。 3. 使用 CreateProcessAsUser 函数以管理员权限运行指定的进程。这个函数会将进程启动在指定的用户上下文中。 下面是一个简单的 C++ 代码示例: ``` #include <windows.h> #include <sddl.h> #include <userenv.h> #include <shlobj.h> #include <iostream> void RunElevated(const wchar_t* cmdLine) { SHELLEXECUTEINFO sei = { sizeof(sei) }; sei.lpVerb = L"runas"; sei.lpFile = cmdLine; sei.hwnd = NULL; sei.nShow = SW_NORMAL; if (!ShellExecuteEx(&sei)) { DWORD error = GetLastError(); if (error == ERROR_CANCELLED) { // The user refused to allow privileges elevation. std::cerr << "User refused to allow privileges elevation.\n"; } else { std::cerr << "Failed to elevate privileges. Error code: " << error << "\n"; } } } int main() { // Get the current process token. HANDLE tokenHandle; if (!OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE | TOKEN_QUERY | TOKEN_ASSIGN_PRIMARY, &tokenHandle)) { std::cerr << "Failed to open process token. Error code: " << GetLastError() << "\n"; return 1; } // Duplicate the token with admin privileges. HANDLE elevatedTokenHandle; if (!DuplicateTokenEx(tokenHandle, TOKEN_ALL_ACCESS, NULL, SecurityIdentification, TokenPrimary, &elevatedTokenHandle)) { std::cerr << "Failed to duplicate process token. Error code: " << GetLastError() << "\n"; CloseHandle(tokenHandle); return 1; } // Set the elevation level of the new token to full. TOKEN_ELEVATION_TYPE elevationType; DWORD elevationSize = sizeof(TOKEN_ELEVATION_TYPE); if (!GetTokenInformation(elevatedTokenHandle, TokenElevationType, &elevationType, sizeof(elevationType), &elevationSize)) { std::cerr << "Failed to get token information. Error code: " << GetLastError() << "\n"; CloseHandle(tokenHandle); CloseHandle(elevatedTokenHandle); return 1; } if (elevationType != TokenElevationTypeFull) { TOKEN_ELEVATION elevation = { 0 }; elevation.TokenIsElevated = 1; if (!SetTokenInformation(elevatedTokenHandle, TokenElevation, &elevation, sizeof(elevation))) { std::cerr << "Failed to set token information. Error code: " << GetLastError() << "\n"; CloseHandle(tokenHandle); CloseHandle(elevatedTokenHandle); return 1; } } // Get the user name and domain name for the current user. DWORD size = 0; GetUserNameEx(NameSamCompatible, NULL, &size); std::wstring userName(size, L'\0'); GetUserNameEx(NameSamCompatible, &userName[0], &size); size = 0; GetComputerNameEx(ComputerNameDnsDomain, NULL, &size); std::wstring domainName(size, L'\0'); GetComputerNameEx(ComputerNameDnsDomain, &domainName[0], &size); // Create the user profile for the new user token. PROFILEINFO profileInfo = { sizeof(profileInfo) }; profileInfo.dwFlags = PI_NOUI; profileInfo.lpUserName = &userName[0]; profileInfo.lpProfilePath = L""; profileInfo.lpDefaultPath = L""; profileInfo.lpServerName = &domainName[0]; profileInfo.lpPolicyPath = L""; profileInfo.hProfile = NULL; DWORD sessionId; HANDLE userTokenHandle; if (!CreateProfile(&profileInfo, &userTokenHandle)) { std::cerr << "Failed to create user profile. Error code: " << GetLastError() << "\n"; CloseHandle(tokenHandle); CloseHandle(elevatedTokenHandle); return 1; } if (!ProcessIdToSessionId(GetCurrentProcessId(), &sessionId)) { std::cerr << "Failed to get session ID. Error code: " << GetLastError() << "\n"; CloseHandle(tokenHandle); CloseHandle(elevatedTokenHandle); CloseHandle(userTokenHandle); return 1; } if (!ImpersonateLoggedOnUser(userTokenHandle)) { std::cerr << "Failed to impersonate user. Error code: " << GetLastError() << "\n"; CloseHandle(tokenHandle); CloseHandle(elevatedTokenHandle); CloseHandle(userTokenHandle); return 1; } // Get the path to the executable. wchar_t exePath[MAX_PATH]; if (GetModuleFileName(NULL, exePath, MAX_PATH) == 0) { std::cerr << "Failed to get executable path. Error code: " << GetLastError() << "\n"; CloseHandle(tokenHandle); CloseHandle(elevatedTokenHandle); CloseHandle(userTokenHandle); return 1; } RunElevated(exePath); // Restore the original token and close the elevated token. if (!RevertToSelf()) { std::cerr << "Failed to revert to self. Error code: " << GetLastError() << "\n"; CloseHandle(tokenHandle); CloseHandle(elevatedTokenHandle); CloseHandle(userTokenHandle); return 1; } CloseHandle(tokenHandle); CloseHandle(elevatedTokenHandle); CloseHandle(userTokenHandle); return 0; } ``` 这个代码示例使用了 ShellExecuteEx 函数来以管理员权限运行指定的进程。如果进程启动失败,将会输出错误信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值