Windows 10家庭版笔记本如何为进程开启SE_DEBUG_NAME权限?
如何在Windows 10家庭版为进程赋予SE_DEBUG_NAME权限
问题场景
我需要让C++程序以全权限打开其他进程,因此尝试为当前进程赋予SE_DEBUG_NAME权限,核心代码如下:
HANDLE hProc = GetCurrentProcess(); HANDLE hToken = NULL; DWORD pid; if (!OpenProcessToken(hProc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) printf("Failed to open access token\n"); if (!SetPrivilege(hToken, SE_DEBUG_NAME, TRUE)) printf("Failed to set debug privilege\n");
使用MSDN提供的权限激活函数SetPrivilege:
BOOL SetPrivilege( HANDLE hToken, // access token handle LPCTSTR lpszPrivilege, // name of privilege to enable/disable BOOL bEnablePrivilege // to enable or disable privilege ) { TOKEN_PRIVILEGES tp; LUID luid; if ( !LookupPrivilegeValue( NULL, // lookup privilege on local system lpszPrivilege, // privilege to lookup &luid ) ) // receives LUID of privilege { printf("LookupPrivilegeValue error: %u\n", GetLastError() ); return FALSE; } tp.PrivilegeCount = 1; tp.Privileges[0].Luid = luid; if (bEnablePrivilege) tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; else tp.Privileges[0].Attributes = 0; // Enable the privilege or disable all privileges. if ( !AdjustTokenPrivileges( hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL) ) { printf("AdjustTokenPrivileges error: %u\n", GetLastError() ); return FALSE; } if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) { printf("The token does not have the specified privilege. \n"); return FALSE; } return TRUE; }
运行后触发ERROR_NOT_ALL_ASSIGNED错误,通过GetTokenInformation确认当前进程未持有SE_DEBUG_NAME权限。已以管理员身份运行程序,但Windows 10家庭版无本地安全策略编辑器(secpol.msc),无法通过常规途径配置权限。
解决方案(Windows 10家庭版专属)
Windows 10家庭版默认缺失本地安全策略编辑器,可通过以下三种方式配置SE_DEBUG_NAME权限:
方法1:修改注册表关闭UAC(快速但有安全风险)
- 打开注册表编辑器(执行
regedit),导航至:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System - 右键点击
System项,选择新建 > DWORD(32位)值,命名为EnableLUA,设置数值数据为0。 - 重启电脑后,以管理员身份运行程序,进程将自动拥有
SE_DEBUG_NAME权限。
注意:关闭UAC会降低系统安全防护级别,测试完成后建议将
EnableLUA改回1。
方法2:使用ntrights.exe命令行工具
- 获取
ntrights.exe(可从Windows Server Resource Kit工具包中提取),将其放入C:\Windows\System32目录。 - 以管理员身份打开命令提示符,执行命令(替换为你的系统用户名):
ntrights +r SeDebugPrivilege -u 你的用户名 - 重启电脑后,权限配置生效。
方法3:PowerShell脚本配置(无需额外工具)
- 以管理员身份启动PowerShell,执行以下脚本(替换为你的系统用户名):
$accountName = "你的用户名" $account = New-Object System.Security.Principal.NTAccount($accountName) $sid = $account.Translate([System.Security.Principal.SecurityIdentifier]).Value # 禁用管理员令牌过滤 $regPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" New-ItemProperty -Path $regPath -Name "FilterAdministratorToken" -Value 0 -PropertyType DWORD -Force | Out-Null # 导出并修改本地安全策略配置 $tempCfg = "$env:TEMP\secpol_temp.cfg" secedit /export /cfg $tempCfg | Out-Null (Get-Content $tempCfg) -replace "^SeDebugPrivilege\s*=.*", "SeDebugPrivilege = *$sid" | Set-Content $tempCfg -Encoding ASCII secedit /configure /db "$env:windir\security\local.sdb" /cfg $tempCfg /areas USER_RIGHTS | Out-Null Remove-Item $tempCfg -Force - 重启电脑后,权限配置生效。
内容的提问来源于stack exchange,提问作者Spring Logger
相关产品推荐
相关产品推荐

