以指定用户权限运行一个进程

 公司的电脑全部用域进行管理,不相同的域用户有不同的权限,有时在一些用户权限下要以另一个用户的运行一个进程,这时就有了现在这

 

个应用场景,解决方法如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
运行system权限工具psexec 用法:psexec [\\computer[,computer2[,...] | @file][-u user [-p psswd]][-n s][-l][-s|-e][-x][-i [session]][-c [-f|-v]][-w directory][-d][-<priority>][-a n,n,...] cmd [arguments] computer 指示 PsExec 在指定的一台或多台计算机上运行应用程序。如果省略计算机名称,则 PsExec 将在本地系统上运行应用程序;如果输入计算机名称“\\*”,则 PsExec 将在当前域中的所有计算机上运行应用程序。 @file 指示 PsExec 在指定的文本文件中列出的每台计算机上运行命令。 -a 用逗号分隔可以运行应用程序的处理器,CPU 编号最小为 1。例如,要在 CPU 2 和 CPU 4 上运行应用程序,请输入:“-a 2,4” -c 将指定的程序复制到远程系统以便执行。如果省略此选项,则应用程序必须位于远程系统上的系统路径中。 -d 不等待应用程序终止。请只对非交互式应用程序使用此选项。 -e 不加载指定帐户的配置文件。 -f 将指定的程序复制到远程系统,即使远程系统中已存在该文件。 -i 运行程序,以便它与远程系统中指定会话的桌面进行交互。如果未指定会话,则进程将在控制台会话中运行。 -l 以受限用户身份(去除 Administrators 组的权限,并且只允许使用分配给 Users 组的权限)运行进程。在 Windows Vista 上,此进程将以“低完整性”运行。 -n 指定与远程计算机连接的超时(秒)。 -p 指定用户名的密码(可选)。如果省略此选项,系统将提示您输入隐藏密码。 -s 在系统帐户中运行远程进程。 -u 指定用于登录远程计算机的可选用户名。 -v 仅在指定文件具有更高版本号或该文件比远程系统上的文件新时复制该文件。 -w 设置进程的工作目录(相对于远程计算机)。 -x 在 Winlogon 桌面上显示 UI(仅限于本地系统)。 -priority 指定 –low、-belownormal、-abovenormal、-high 或 -realtime 按不同优先级运行进程。 program 要执行的程序的名称。 arguments 要传递的参数(请注意,文件路径必须是目标系统中的绝对路径) 对于其名称中含有空格的应用程序,可以在其两侧加引号,例如,psexec \\marklap "c:\long name\app.exe"。按下 Enter 键时,仅将输入内容传递到远程系统。键入 Ctrl-C 可终止远程进程。 如果省略用户名,则远程进程将以执行 PsExec 时所使用的相同帐户运行,但由于远程进程以模仿方式运行,因此它无权访问远程系统上的网络资源。指定用户名时,远程进程将以指定的帐户执行,并可访问该帐户有权访问的任何网络资源。请注意,密码是以明文形式传递到远程系统的。 当目标系统是本地系统时,由于 PsExec 不需要您具有管理员权限,因此您可以使用当前版本的 PsExec 来取代 Runas。 以下命令可在 \\marklap 上启动交互式命令提示窗口: psexec \\marklap cmd 此命令通过 /all 开关在远程系统上执行 IpConfig,并在本地显示输出结果: psexec \\marklap ipconfig /all 此命令将程序 test.exe 复制到远程系统,并以交互方式执行此程序: psexec \\marklap -c test.exe 如果远程系统中已经安装的程序不在系统路径中,请指定该程序的完整路径: psexec \\marklap c:\bin\test.exe 在系统帐户中以交互方式运行 Regedit,以便查看 SAM 和 SECURITY 注册表项的内容: psexec -i -d -s c:\windows\regedit.exe 要以受限用户权限运行 Internet Explorer,请使用此命令: psexec -l -d "c:\program files\internet explorer\iexplore.exe"
在 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
发出的红包

打赏作者

潘祖记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值