欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PowerShell防止腳本注入攻擊分享

  發(fā)布時(shí)間:2011-03-22 21:48:39   作者:佚名   我要評(píng)論
與其他編程語(yǔ)言一樣,PowerShell 腳本容易受到注入攻擊,易受攻擊的函數(shù)運(yùn)行額外的命令,這可能是嚴(yán)重的安全漏洞,了解問(wèn)題后,可通過(guò)多種方式防范注入攻擊

與其他編程語(yǔ)言一樣,PowerShell 腳本容易受到注入攻擊。 當(dāng)用戶(hù)向包含額外命令的易受攻擊的函數(shù)提供輸入時(shí),會(huì)發(fā)生注入攻擊。 易受攻擊的函數(shù)運(yùn)行額外的命令,這可能是嚴(yán)重的安全漏洞。 例如,惡意用戶(hù)可能會(huì)濫用易受攻擊的函數(shù)在遠(yuǎn)程計(jì)算機(jī)上運(yùn)行任意代碼,從而損害該計(jì)算機(jī)并獲取對(duì)網(wǎng)絡(luò)上其他計(jì)算機(jī)的訪問(wèn)權(quán)限。

了解問(wèn)題后,可通過(guò)多種方式防范注入攻擊。

易受攻擊的代碼示例

PowerShell 代碼注入漏洞涉及包含腳本代碼的用戶(hù)輸入。 用戶(hù)輸入添加到易受攻擊的腳本中,由 PowerShell 分析和運(yùn)行。

function Get-ProcessById { param ($ProcId) Invoke-Expression -Command "Get-Process -Id $ProcId" }

Get-ProcessById 函數(shù)按其 ID 值查找本地進(jìn)程。 它采用任何類(lèi)型的 $ProcId 參數(shù)。 然后將 $ProcId 轉(zhuǎn)換為字符串,并插入到使用 Invoke-Expression cmdlet 分析和運(yùn)行的另一個(gè)腳本中。 傳入有效的進(jìn)程 ID 整數(shù)時(shí),此函數(shù)正常工作。

Get-ProcessById $pid NPM(K) PM(M) WS(M) CPU(s) Id SI ProcessName ------ ----- ----- ------ -- -- ----------- 97 50.09 132.72 1.20 12528 3 pwsh

但是,$ProcId 參數(shù)未指定類(lèi)型。 它接受任何可以包含其他命令的任意字符串值。

Get-ProcessById "$pid; Write-Host 'pwnd!'"

在此示例中,函數(shù)正確檢索了由 $pid 標(biāo)識(shí)的進(jìn)程,但也運(yùn)行了注入腳本 Write-Host 'pwnd!'。

NPM(K) PM(M) WS(M) CPU(s) Id SI ProcessName ------ ----- ----- ------ -- -- ----------- 92 45.66 122.52 1.06 21736 3 pwsh pwnd!

防范注入攻擊的方法

可通過(guò)多種方式防范注入攻擊。

使用類(lèi)型化輸入

可以為 $ProcId 參數(shù)指定類(lèi)型。

function Get-ProcessById { param ([int] $ProcId) Invoke-Expression -Command "Get-Process -Id $ProcId" } Get-ProcessById "$pid; Write-Host 'pwnd!'"

Get-ProcessById: Line | 7 | Get-ProcessById "$pid; Write-Host 'pwnd!'" | ~~~~~~~~~~~~~~~~~~~~~~~~~ | Cannot process argument transformation on parameter 'ProcId'. Cannot convert value "8064; Write-Host 'pwnd!'" to type "System.Int32". Error: "The input string '8064; Write-Host 'pwnd!' was not in a correct format."

此處,$ProcId 輸入?yún)?shù)僅限于整數(shù)類(lèi)型,因此,當(dāng)傳入不能轉(zhuǎn)換為整數(shù)的字符串時(shí),會(huì)發(fā)生錯(cuò)誤。

不要使用 Invoke-Expression

無(wú)需使用 Invoke-Expression,直接調(diào)用 Get-Process,讓 PowerShell 的參數(shù)綁定器驗(yàn)證輸入。

function Get-ProcessById { param ($ProcId) Get-Process -Id $ProcId } Get-ProcessById "$pid; Write-Host 'pwnd!'"

Get-Process: Line | 5 | Get-Process -Id $ProcId | ~~~~~~~ | Cannot bind parameter 'Id'. Cannot convert value "8064; Write-Host 'pwnd!'" to type "System.Int32". Error: "The input string '8064; Write-Host 'pwnd!' was not in a correct format."

作為最佳做法,應(yīng)避免使用 Invoke-Expression,尤其是在處理用戶(hù)輸入時(shí)。 Invoke-Expression 很危險(xiǎn),因?yàn)樗鼤?huì)分析和運(yùn)行你提供的任何字符串內(nèi)容,使其容易受到注入攻擊。 最好依賴(lài)于 PowerShell 參數(shù)綁定。

將字符串包含在單引號(hào)中

但是,有時(shí)使用 Invoke-Expression 是不可避免的,你還需要處理用戶(hù)字符串輸入。 可以使用每個(gè)字符串輸入變量周?chē)膯我?hào)安全地處理用戶(hù)輸入。 單引號(hào)可確保 PowerShell 分析程序?qū)⒂脩?hù)輸入視為單個(gè)字符串文本。

function Get-ProcessById { param ($ProcId) Invoke-Expression -Command "Get-Process -Id '$ProcId'" } Get-ProcessById "$pid; Write-Host 'pwnd!'"

Get-Process: Cannot bind parameter 'Id'. Cannot convert value "8064; Write-Host " to type "System.Int32". Error: "The input string '8064; Write-Host' was not in a correct format."

但是,此版本函數(shù)對(duì)于防范注入攻擊還不是完全安全的。 惡意用戶(hù)仍然可以在其輸入中使用單引號(hào)來(lái)注入代碼。

Get-ProcessById "$pid'; Write-Host 'pwnd!';'"

此示例在用戶(hù)輸入中使用單引號(hào)來(lái)強(qiáng)制函數(shù)運(yùn)行三個(gè)單獨(dú)的語(yǔ)句,其中一個(gè)語(yǔ)句是由用戶(hù)注入的任意代碼。

NPM(K) PM(M) WS(M) CPU(s) Id SI ProcessName ------ ----- ----- ------ -- -- ----------- 97 46.08 183.10 1.08 2524 3 pwsh pwnd!

使用 EscapeSingleQuotedStringContent() 方法

若要防止用戶(hù)插入自己的單引號(hào)字符來(lái)利用函數(shù),必須使用 EscapeSingleQuotedStringContent() API。 這是 PowerShell System.Management.Automation.Language.CodeGeneration 類(lèi)的靜態(tài)公共方法。 此方法通過(guò)轉(zhuǎn)義用戶(hù)輸入中包含的任何單引號(hào)來(lái)確保用戶(hù)輸入的安全性。

function Get-ProcessById { param ($ProcId) $ProcIdClean = [System.Management.Automation.Language.CodeGeneration]:: EscapeSingleQuotedStringContent("$ProcId") Invoke-Expression -Command "Get-Process -Id '$ProcIdClean'" } Get-ProcessById "$pid'; Write-Host 'pwnd!';'"

Get-Process: Cannot bind parameter 'Id'. Cannot convert value "8064'; Write-Host 'pwnd!';'" to type "System.Int32". Error: "The input string '8064'; Write-Host 'pwnd!';'' was not in a correct format."

使用 Injection Hunter 檢測(cè)易受攻擊的代碼

Injection Hunter 是 Lee Holmes 編寫(xiě)的模塊,其中包含用于檢測(cè)代碼注入漏洞的 PowerShell 腳本分析器規(guī)則。 使用以下命令之一從 PowerShell 庫(kù)安裝模塊:

# Use PowerShellGet v2.x Install-Module InjectionHunter # Use PowerShellGet v3.x Install-PSResource InjectionHunter

可以使用此模塊在生成、持續(xù)集成過(guò)程、部署和其他方案中自動(dòng)執(zhí)行安全分析。

$RulePath = (Get-Module -list InjectionHunter).Path Invoke-ScriptAnalyzer -CustomRulePath $RulePath -Path .\Invoke-Dangerous.ps1

RuleName Severity ScriptName Line Message -------- -------- ---------- ---- ------- InjectionRisk.InvokeExpression Warning Invoke-Dan 3 Possible script injection risk via the gerous.ps1 Invoke-Expression cmdlet. Untrusted input can cause arbitrary PowerShell expressions to be run. Variables may be used directly for dynamic parameter arguments, splatting can be used for dynamic parameter names, and the invocation operator can be used for dynamic command names. If content escaping is truly needed, PowerShell has several valid quote characters, so [System.Management.Automation.Languag e.CodeGeneration]::Escape* should be used.

相關(guān)文章

最新評(píng)論