标题: Windows上的sudo 创建: 2020-12-01 12:06 更新: 2021-10-29 14:34 链接: https://scz.617.cn/windows/202012011206.txt 这不是一篇有效技术文档,完全是两个老年程序员回忆往事时的灌水之作。 有天张宇平微信给我发了个链接: gsudo - a sudo for Windows https://github.com/gerardog/gsudo https://github.com/gerardog/gsudo/releases $ gsudo.exe -h gsudo v0.7.2 Copyright(c) 2019-2020 Gerardo Grignoli and GitHub contributors Usage: ------ gsudo [options] Elevates your current shell gsudo [options] {command} [args] Runs {command} with elevated permissions gsudo [-h | --help] Shows this help gsudo [-v | --version] Shows gsudo version gsudo cache [on | off | help] Starts/Stops an elevated cache session. (reduced UAC popups) gsudo config Show current config settings & values. gsudo config {key} [--global] [value] Read or write a user setting gsudo config {key} [--global] --reset Reset config to default value gsudo status Show status about current user, security, integrity level or other gsudo relevant data. General options: -n | --new Starts the command in a new console (and returns immediately). -w | --wait When in new console, force wait for the command to end. -s | --system Run As Local System account ("NT AUTHORITY\SYSTEM"). -i | --integrity {v} Specify integrity level: Untrusted, Low, Medium, MediumPlus, High (default), System -k | --reset-timestamp Kills all cached credentials. The next time gsudo is run a UAC popup will be appear. --copyns Connect network drives to the elevated user. Warning: Verbose, interactive asks for credentials Other options: --loglevel {val} Set minimum log level to display: All, Debug, Info, Warning, Error, None --debug Enable debug mode. --piped (deprecated) Set console mode to piped StdIn/Out/Err. --vt (deprecated) Set console mode to piped VT100 ConPty/PseudoConsole (experimental). --attached (deprecated) Set console mode to attached. --copyev (deprecated) Copy environment variables to the elevated process. (not needed on default console mode) Learn more about security considerations of using gsudo at: https://bit.ly/gsudoSecurity 对"-s"参数有点兴趣。21世纪初有很多奇技淫巧获取SYSTEM权限,打开regedit,可 以直接看SAM。自从"Session 0"出场后,我再没开过SYSTEM的regedit,一是没有这 种刚需,二是相应技术方案各有各的不理想。psexec是不是必须安装个服务啊?随口 一问,不研究这个方向很久了。 简单做些测试: $ gsudo.exe -s cmd # whoami nt authority\system # regedit.exe # reg.exe query "HKLM\SAM\SAM\Domains\Account\Users\000001F4" /v "F" # reg.exe query "HKLM\SAM\SAM\Domains\Account\Users\000001F4" /v "V" 或者 $ gsudo.exe -s cmd.exe /c regedit $ gsudo.exe -s reg.exe query "HKLM\SAM\SAM\Domains\Account\Users\000001F4" /v "F" 若开出SYSTEM的regedit,Process Explorer的Integrity显示System,"User Name" 显示"NT AUTHORITY\SYSTEM",可以查看SAM子键。 诡异的是,如下命令开SYSTEM的regedit,时灵时不灵: $ gsudo.exe -s regedit.exe 所谓不灵是指,没有报错,但开出来的不是SYSTEM的regedit,无法查看SAM子键。应 该是gsudo的BUG。有C#源码,懒得看,因为没有刚需。 张宇平给我这个链接,是人肉分布式备份的目的,他自己完全没有测试过,而我误以 为他在向我推荐这个工具。不过,即使早知道他的动机,也有兴趣测试一下"-s"参数。 并不向诸君推荐使用gsudo,毕竟未做其他横向对比,只是告知。单就查看SAM子键这 个伪需求而言,我甚至没有放狗,想必技术方案如过江之鲫。很多人未做广泛的横向 对比,就敢张口"这是最啥啥"的,不要做这种人。 说些更远的题外话。基本不公开给别人推荐调试器、编辑器之类的工具,除非TA直接 问到我头上,这是一个主观问题,每个人选用哪款有其不为外人所知的先验假设。 对于老年程序员,在某个方向上的历史经验当作茶余饭后的谈资或可一叙,当成别的 什么向年轻人提及,多半是要被拍死在沙滩上。 说起《射雕英雄传》电视剧,不同年代的朋友有自己最爱的版本。人这一生,大概率 觉得自己年轻时第一次接触的那个版本最好、最经典,之后的都差点意思甚至是在抹 黑经典。这种现象是人性的具像化,此处不论是非,吾辈当一日三省,勿做界中人。 最后,喊一句口号,打倒一切人肉分布式备份的反革命份子! 2021-10-29 14:34 scz Run any executable as local SYSTEM account - Pavel Yosifovich [2017-04-12] https://github.com/zodiacon/sysrun (Must run from elevated command window or elevated process) (no service required) sysrun的作者是Pavel Yosifovich,《Windows Internals, 7th Edition》的作者之 一。 https_proxy=socks5://: git clone https://github.com/zodiacon/sysrun.git sysrun 若下列组件之前没有安装,可能需要调整VS 2019组件状态 -------------------------------------------------------------------------- Individual components MSVC v140 - VS 2015 C++ build tools (v14.00) -------------------------------------------------------------------------- 此外,VS 2019已经没有"Windows 8.1 SDK",需要调整工程属性 -------------------------------------------------------------------------- Solution Explorer 右键选中"sysrun (Visual Studio 2015)" Properties Configuration Properties General Target Platform Version 原来是8.1,调整成10.0.16299.0 -------------------------------------------------------------------------- 可能不装"MSVC v140"也成,直接升级用高版本编译工具,但我没试。 必须在管理员级cmd中使用sysrun,否则报错 $ sysrun.exe regedit EnableDebugPrivilege failed: Access denied (are you running elevated?) 在一个管理员级cmd中测试 $ sysrun.exe reg.exe query "HKLM\SAM\SAM\Domains\Account\Users\000001F4" /v "F" $ sysrun.exe cmd.exe /c "start cmd.exe" $ whoami nt authority\system 没啥奇技淫巧,原理上很传统的搞法,没用NativeAPI,直接看sysrun.cpp好了。 EnableDebugPrivilege OpenSystemProcessToken DuplicateTokenEx SetThreadToken OpenProcessToken GetTokenInformation AdjustTokenPrivileges SetTokenInformation CreateProcessAsUser