【问题标题】:Passing Admin username & password using shell_exec使用 shell_exec 传递管理员用户名和密码
【发布时间】:2012-12-30 21:24:35
【问题描述】:

我有一个 Powershell 脚本,当我直接在 Web 服务器(Windows 2008R2、IIS)上运行时,它会成功完成。我现在正在尝试从 php 页面启动脚本。脚本启动,我可以在任务管理器中看到该过程。

当我从 php 页面使用 shell_exec 调用脚本时,它失败并出现错误

"Exception calling "FindOne" with "0" argument(s): "An operations error occurred."

我可以从任务管理器中看到该脚本正在以用户 IUSR 的身份运行。我很确定这是脚本失败的原因,因为要成功完成它需要以域管理员身份运行。

我正在使用以下命令调用脚本

$username = 页面上当前登录的用户(针对 AD 进行身份验证)
$newPword1 = 新用户密码

$query = shell_exec("powershell -command $psScriptPath '$username' '$newPword1' < NUL");

Powershell 脚本:

#*=============================================================================
#* PARAMETER DECLARATION
#*=============================================================================

param([string]$username,[string]$pword)

#*=============================================================================
#* INITIALISE VARIABLES
#*=============================================================================
# Increase buffer width/height to avoid Powershell from wrapping the text before
# sending it back to PHP (this results in weird spaces).
$pshost = Get-Host
$pswindow = $pshost.ui.rawui
$newsize = $pswindow.buffersize
$newsize.height = 3000
$newsize.width = 400
$pswindow.buffersize = $newsize


#*=============================================================================
#* SCRIPT BODY
#*=============================================================================

$root = [ADSI]'LDAP://<server>/<DNtoOU>'
$searcher = new-object System.DirectoryServices.DirectorySearcher($root)
$searcher.filter = "(&(objectCategory=person)(sAMAccountName=$username))"
$user = $searcher.findone()

$userDN = $user.path

$user=[ADSI]$($userDN)
$user.SetPassword($pword)
$user.setinfo()
write-host "success"

是否可以像使用上述命令一样传递域管理员用户名/密码来运行 powershell,从而允许脚本成功运行?

【问题讨论】:

  • 您是从我的网站 (theboywonder.co.uk) 获得这个 sn-p 的吗?为什么不使用基本身份验证来登录网站?我们使用域管理员凭据登录 IIS,这会以登录用户身份启动 PowerShell;我们从未遇到过以这种方式使用域管理员凭据的权限问题。无论如何,如果您想遵循我的完整(长)指南,它会告诉您如何操作。

标签: php powershell shell-exec


【解决方案1】:

其中一种方法是使用PowerShellRunAs

编辑:已记录在案,您可以按原样使用 powershell 脚本或对其进行修改以满足您的需要(例如,使用密码文件)。 p>

更详细地说,而不是这个:
shell_exec("powershell -command $psScriptPath …")
你可以使用这个:
shell_exec("powershell -command "Start-Process powershell.exe -argumentlist '-command $psScriptPath …' -Credential 'TheDomain\TheUser'")
但为避免出现密码提示,请按照文档使用 PowerShellRunAs:
shell_exec("powershell -command "Start-Process powershell.exe -argumentlist '-command $psScriptPath …' -Credential (.\PowerShellRunAs.ps1 -get contoso\svc_remoterestart \\fileserver\share\file.pwd)")

或者,您可以将start-process … -credential … 合并到您自己的脚本中。这样您就可以像以前一样保持您的 shell_exec 调用简单,并且只有有限的部分脚本将在具有不同(更高)权限的用户下运行。

如果您按原样使用脚本,请记住首先使用-set 而不是-get 运行它,以便设置密码文件。

【讨论】:

  • 任何想法如何实现它?
  • 这不起作用,因为存储管理员凭据的密码文件只能由创建它的用户访问...您提到这是“几种方法之一”,您能就其他人?
猜你喜欢
  • 2020-06-23
  • 2013-12-10
  • 2012-12-01
  • 2021-08-10
  • 1970-01-01
  • 1970-01-01
  • 2014-02-13
  • 2018-10-23
  • 2020-06-12
相关资源
最近更新 更多