英文:
Check if application is running as administrator in golang
问题
在Google Go应用程序中,检查当前进程是否以管理员身份运行的最简单方法是什么?
英文:
In a Google Go application what is the easiest way to check if the current process is running as an administrator?
答案1
得分: 3
简短的回答是使用user.Current
。然而...
你需要找到一种方法,通过给定的user.User
结构体,确定用户是否为root。这个问题你需要自己解决。更重要的是,你不应该将其用于安全目的。在不被运行程序的人欺骗的情况下,确定当前用户是否为root非常棘手。例如,在Linux上使用setuid二进制文件的天真实现将容易受到用户使用LD_PRELOAD来欺骗二进制文件以为自己正在以root身份运行的攻击。确保你要么a) 理解了这个方法的确切语义,并且确信用户无法绕过它(如果选择这条路线,你应该看看其他人是否已经找到了解决方法),要么b) 不要基于这个信息做出任何安全关键的决策。
英文:
The short answer is to use user.Current
. HOWEVER...
You will need to find a way to, given the user.User
struct, figure out whether the user is root. You'll have to figure that one out on your own. More importantly, you should not use this for security purposes. Figuring out whether the current user is root without being lied to by the person running the program is very tricky. For example, a naive implementation of this on Linux with a setuid binary would be vulnerable to the user using LD_PRELOAD to trick the binary into thinking it was running as root. Make sure that you either, a) understand the exact semantics of this and are convinced there is no way to circumvent it by the user (and if you choose this route, you should probably just see if others have figured it out), or, b) do not make any security-critical decisions based on this information.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论