检查脚本是否具有提升的权限

发布于 2024-08-08 15:19:33 字数 718 浏览 4 评论 0原文

我想检查 VBscript 运行的上下文是否允许我执行管理任务。

要求:

  • 该解决方案应适用于从 Server 2003 开始​​的所有 Windows 操作系统。(这排除了仅检查管理员组中的成员身份的解决方案 - 请记住,有 UAC在 Vista 和 Windows 7 中!)
  • 解决方案应该简单。一个 50 LOC 解决方案,用于检查 Windows 组成员身份(当然是递归的,因为用户可能是某个组的成员,而该组是某个组的成员……这是管理员组的成员),然后对 Vista 进行一些额外的检查UAC 并不简单
  • 该解决方案可能有点脏,所以按照 此解决方案 的方式就可以了。
  • 它不应该太脏。在我看来,将文件写入 C:\Windows 或写入注册表项太脏了,因为它会修改系统。 (编辑:这可能无论如何都不起作用:例如,在 HTA 中使用 VBScript 时,UAC 重定向就会启动。)

相关问题:https:// /stackoverflow.com/questions/301860(我在那里找到的所有答案(a)忽略UAC问题和(b)都是错误的,因为它们忽略了用户拥有管理权限的可能性,尽管不是直接成员管理员组)

I would like to check whether the context in which my VBscript runs allows me to perform administrative tasks.

Requirements:

  • The solution should work on all Windows operating systems starting with Server 2003. (This rules out solutions which just check for membership in the Administrators group -- remember that there's UAC in Vista and Windows 7!)
  • The solution should be simple. A 50 LOC solution that checks the Windows group memberships (recursively, of course, since the user might be member of a groups which is member of a group ... which is member of the Administrators group) and then does some extra checks for Vista UAC is not simple.
  • The solution may be a bit dirty, so something along the lines of this solution would be ok.
  • It should not be too dirty. Writing a file to C:\Windows or writing a registry key is too dirty in my opinion, since it modifies the system. (EDIT: Which might not work anyway: for example, when using VBScript in a HTA, UAC redirection kicks in.)

Related question: https://stackoverflow.com/questions/301860 (all of the answers I found there (a) ignore the UAC issue and (b) are faulty because they ignore the possibility of a user having administrative permissions although not being direct member in the Administrators group)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

递刀给你 2024-08-15 15:19:33

我知道这个帖子很旧并且标记为已回答,但这是一个更简单的方法,一直对我有用。用户 S-1-5-19 是本地 NT 权限,因此访问密钥需要管理员权限。如果通过海拔运行它就可以工作。

Option Explicit 

msgbox isAdmin(), vbOkonly, "Am I an admin?"

Private Function IsAdmin()
    On Error Resume Next
    CreateObject("WScript.Shell").RegRead("HKEY_USERS\S-1-5-19\Environment\TEMP")
    if Err.number = 0 Then 
        IsAdmin = True
    else
        IsAdmin = False
    end if
    Err.Clear
    On Error goto 0
End Function

I know this thread is very old and marked answered but this is a simpler method that has always worked for me. User S-1-5-19 is the Local NT Authority so accessing the key takes admin rights. It works if run via elevation.

Option Explicit 

msgbox isAdmin(), vbOkonly, "Am I an admin?"

Private Function IsAdmin()
    On Error Resume Next
    CreateObject("WScript.Shell").RegRead("HKEY_USERS\S-1-5-19\Environment\TEMP")
    if Err.number = 0 Then 
        IsAdmin = True
    else
        IsAdmin = False
    end if
    Err.Clear
    On Error goto 0
End Function
笑梦风尘 2024-08-15 15:19:33

可能将此(WhoAmI from VBscript)与此(UAC 已打开)。

这是代码,不幸的是 XP 的先决条件是“whoami.exe”,可以在 XP 的资源工具包或支持工具中找到(维基百科) - 我仍然想找到一种没有它的方法。

If UserPerms("Admin") Then
 Message = "Good to go"
Else
 Message = "Non-Admin"
End If

If UACTurnedOn = true Then
 Message = Message & ", UAC Turned On"
Else
 Message = Message & ", UAC Turned Off (Or OS < Vista)"
End If

Wscript.echo Message

Function UserPerms (PermissionQuery)          
 UserPerms = False  ' False unless proven otherwise           
 Dim CheckFor, CmdToRun         

 Select Case Ucase(PermissionQuery)           
 'Setup aliases here           
 Case "ELEVATED"           
   CheckFor =  "S-1-16-12288"           
 Case "ADMIN"           
   CheckFor =  "S-1-5-32-544"           
 Case "ADMINISTRATOR"           
   CheckFor =  "S-1-5-32-544"           
 Case Else                  
   CheckFor = PermissionQuery                  
 End Select           

 CmdToRun = "%comspec% /c whoami /all | findstr /I /C:""" & CheckFor & """"  

 Dim oShell, returnValue        
 Set oShell = CreateObject("WScript.Shell")  
 returnValue = oShell.Run(CmdToRun, 0, true)     
 If returnValue = 0 Then UserPerms = True                   
End Function

Function UACTurnedOn ()
 On Error Resume Next

 Set oShell = CreateObject("WScript.Shell")
 If oShell.RegRead("HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System\EnableLUA") = 0 Then
      UACTurnedOn = false
 Else
      UACTurnedOn = true
 End If
End Function

Possibly combine this (WhoAmI from VBscript) with this (UAC Turned On).

Here is the code, the unfortunate pre-req for XP is "whoami.exe", found in a resource kit or support tools for XP (Wikipedia) - I'd still like to find a way to do without it.

If UserPerms("Admin") Then
 Message = "Good to go"
Else
 Message = "Non-Admin"
End If

If UACTurnedOn = true Then
 Message = Message & ", UAC Turned On"
Else
 Message = Message & ", UAC Turned Off (Or OS < Vista)"
End If

Wscript.echo Message

Function UserPerms (PermissionQuery)          
 UserPerms = False  ' False unless proven otherwise           
 Dim CheckFor, CmdToRun         

 Select Case Ucase(PermissionQuery)           
 'Setup aliases here           
 Case "ELEVATED"           
   CheckFor =  "S-1-16-12288"           
 Case "ADMIN"           
   CheckFor =  "S-1-5-32-544"           
 Case "ADMINISTRATOR"           
   CheckFor =  "S-1-5-32-544"           
 Case Else                  
   CheckFor = PermissionQuery                  
 End Select           

 CmdToRun = "%comspec% /c whoami /all | findstr /I /C:""" & CheckFor & """"  

 Dim oShell, returnValue        
 Set oShell = CreateObject("WScript.Shell")  
 returnValue = oShell.Run(CmdToRun, 0, true)     
 If returnValue = 0 Then UserPerms = True                   
End Function

Function UACTurnedOn ()
 On Error Resume Next

 Set oShell = CreateObject("WScript.Shell")
 If oShell.RegRead("HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System\EnableLUA") = 0 Then
      UACTurnedOn = false
 Else
      UACTurnedOn = true
 End If
End Function
も星光 2024-08-15 15:19:33

上面需要“whoami”的代码来自 CSI-Windows.com/toolkit/ifuserperms 上的 IfUserPerms 脚本。

阅读您的帖子后,我创建了新的脚本代码,用于在 VBS(9 行)和 CMD/BAT(3 行)中使用快速、小型、高效、被动(不更改任何内容)代码检查管理员权限。它还可以与 UAC 配合使用,如果用户未提升权限,则报告错误。

您可以在此处找到代码: http://csi-windows.com/toolkit/csi-isadmin

The code above that requires "whoami" is from our IfUserPerms script at CSI-Windows.com/toolkit/ifuserperms.

After reading your post here, I have created new script code that checks for admin rights with fast, small, efficient, passive (no changing anything) code in both VBS (9 Lines) and CMD/BAT (3 lines). It also works with UAC by reporting false if the user is not elevated.

You can find the code here: http://csi-windows.com/toolkit/csi-isadmin

表情可笑 2024-08-15 15:19:33

我添加了两个额外的脚本工具包,它们极大地增强了上面来自 ifuserperms.vbs 的原始代码。

CSI_IsSession.vbs 可以告诉您几乎所有您想了解的有关 UAC 或当前会话的信息该脚本正在运行。

VBScriptUACKit.vbs(使用 CSI_IsSession.vbs)允许您有选择地在脚本中提示 UAC通过重新启动自身。经过设计和调试,可在多种执行场景下工作。

I have added two additional script kits that dramatically enhance the original code above that came from ifuserperms.vbs.

CSI_IsSession.vbs can tell you almost anything you want to know about UAC or the current session the script is running under.

VBScriptUACKit.vbs (which uses CSI_IsSession.vbs) allows you to selectively prompt for UAC in a script by relaunching itself. Has been designed and debugged to work under many execution scenarios.

浪漫之都 2024-08-15 15:19:33

我还有另一个脚本,甚至可以兼容 Windows 98(尽管他们未打补丁的系统在完整性级别之间没有差异)。

将测试文件写入 *%windir%\system32* 是一个相当肮脏的技巧,但出奇地有效。由于它实际上是微软有史以来为防止对系统文件进行授权访问而制定的最重要的规则,因此它可以被视为当时检查系统访问权限(与游戏/应用程序安装所使用的方式相同)的缩影。

Option Explicit

Dim objShell, objFSO, strSystemFolder, strTestFile, isAdmin

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strSystemFolder = objShell.ExpandEnvironmentStrings("%windir%") & "\system32"
strTestFile = strSystemFolder & "\test_admin.txt"

isAdmin = False

On Error Resume Next
objFSO.CopyFile WScript.ScriptFullName, strTestFile
If Err.Number = 0 Then
    isAdmin = True
    objFSO.DeleteFile strTestFile
End If
On Error GoTo 0

If isAdmin Then
    MsgBox "Skript has admin rights.", vbInformation, "Status: Elevated"
Else
    MsgBox "Skript has NOT admin rights.", vbExclamation, "Status: Non-Elevated"
End If

该脚本已在以下操作系统上成功测试:

  • Windows 98
  • Windows 2000
  • Windows XP
  • Windows 7
  • Windows 11

我们必须知道 VBScript 现在被 Microsoft 称为“已弃用”,并且可能会在未来几年默认删除。我不同意这个想法,但这不属于这个主题。

I've another script that is even compatible down to Windows 98 (though their unpatched system does not differ between integrity levels).

Writing a test file to *%windir%\system32* is a rather dirty trick but surprisingly effective. As it was practically the most important rule ever made by MS to prevent authorized access to system files, it can be seen as the epitome of checking system access (the same way used by games/apps installations) back in the day.

Option Explicit

Dim objShell, objFSO, strSystemFolder, strTestFile, isAdmin

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strSystemFolder = objShell.ExpandEnvironmentStrings("%windir%") & "\system32"
strTestFile = strSystemFolder & "\test_admin.txt"

isAdmin = False

On Error Resume Next
objFSO.CopyFile WScript.ScriptFullName, strTestFile
If Err.Number = 0 Then
    isAdmin = True
    objFSO.DeleteFile strTestFile
End If
On Error GoTo 0

If isAdmin Then
    MsgBox "Skript has admin rights.", vbInformation, "Status: Elevated"
Else
    MsgBox "Skript has NOT admin rights.", vbExclamation, "Status: Non-Elevated"
End If

The script was tested successfully on these OS's:

  • Windows 98
  • Windows 2000
  • Windows XP
  • Windows 7
  • Windows 11

We have to be aware VBScript is called "deprecated" by Microsoft nowadays and probably removed by default in the next years. I do not share the thought, but that does not belong in this topic.

烈酒灼喉 2024-08-15 15:19:33

以下是使脚本文件或任何其他文件以管理员身份运行的最快方法:

首先创建您需要执行的任何操作的 VBS 脚本。
就我而言,它是一个注册表编辑 vbs,允许我自动管理登录
然后当机器重新启动时,运行另一个文件以确保
自动管理登录不再启用。

创建文件后,您需要创建一个 cmd 提示符快捷方式。
接下来“右键单击”快捷方式并更改属性,以便它将以管理员身份运行。

像这样粘贴您的文件路径:
D:\WINDOWS\system32\cmd.exe /c "D:\Dump\Scripts\StartUp.vbs"

其中 'C' 表示完成后将关闭
如果您希望它保持打开状态,请使用“K”

希望这对其他人有帮助。

Here is the fastest way to cause a script file or any other file run as administrator:

First create your VBS script of whatever you need to do.
In my case it was a registry edit vbs to allow me to autoadmin logon
then when the machine was restarted, another file was run to ensure
that autoadmin logon was not enabled any longer.

After you have created your file, then you need to create a cmd prompt shortcut.
Next 'Right click' on the shortcut and change the propeties so that it will run as administrator.

Paste your file path like this:
D:\WINDOWS\system32\cmd.exe /c "D:\Dump\Scripts\StartUp.vbs"

The 'C' means it will close after completion
If you want it to stay open then use 'K'

Hope this helps someone else.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文