簡體   English   中英

appcmd.exe set config 不檢查用戶名或密碼是否無效並設置它

[英]appcmd.exe set config doesn't check if username or password is invalid and sets it anyways

我正在使用后端 api 中的 winexe 在 Windows 域服務器上運行命令。 我想將 IIS App Pool Identity 設置為 Active Directory 中的帳戶。 問題是在使用此命令時:

%windir%\system32\inetsrv\appcmd.exe set config /section:applicationPools ^
/[name='POOLNAME'].processModel.identityType:SpecificUser ^
/[name='POOLNAME'].processModel.userName:DOMAIN\USER ^
/[name='POOLNAME'].processModel.password:PASSWORD

即使用戶名和密碼不正確,它也每次都能成功運行。 甚至池也以錯誤的密碼啟動。 但是通過 GUI 設置錯誤的密碼失敗。

我想確定密碼或用戶名何時設置錯誤。

PS:我什至嘗試在 powershell 上使用Set-ItemProperty ,結果是一樣的。

您無法使用 AppPool 測試您的憑據,但您絕對可以測試它們。

# Service Principal credentials
$username = 'Username'
$password = 'Password' | ConvertTo-SecureString -AsPlainText -Force
$credential = New-Object -TypeName 'System.Management.Automation.PSCredential' -ArgumentList $username, $password


if (Test-Credential -Credential $credential) {
    Write-Verbose "Credentials for $($credential.UserName) are valid..."
    # do the appcmd stuff
}
else {
    Write-Warning 'Credentials are not valid or some other logic'
}

只需在腳本頂部添加Test-Credential function 定義

function Test-Credential {
    [CmdletBinding()]
    Param
    (
        # Specifies the user account credentials to use when performing this task.
        [Parameter()]
        [ValidateNotNull()]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]
        $Credential = [System.Management.Automation.PSCredential]::Empty
    )
   
    Add-Type -AssemblyName System.DirectoryServices.AccountManagement
    $DS = $null
    $Username = $Credential.UserName
    $SplitUser = $Username.Split('\')
    if ($SplitUser.Count -eq 2 ) {$Username = $SplitUser[1]}
    
    if ($SplitUser.Count -eq 1 -or $SplitUser[0] -eq $env:COMPUTERNAME ) {
        $DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext('machine', $env:COMPUTERNAME)
    }
    else {
        try {
            $DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext('domain')
        }
        catch {
            return $false
        }
    }
        
    $DS.ValidateCredentials($Username, $Credential.GetNetworkCredential().Password)
   
}

(PS:代碼是有效的,即使修飾符用反斜杠引用語法中斷)

令人驚訝的是,我很困惑你可以這樣做 - 但它仍然無法驗證

appcmd set apppool junkapp /processmodel.password:junkpassword

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM