PowerShell防止腳本注入攻擊分享

與其他編程語言一樣,PowerShell 腳本容易受到注入攻擊。 當(dāng)用戶向包含額外命令的易受攻擊的函數(shù)提供輸入時,會發(fā)生注入攻擊。 易受攻擊的函數(shù)運行額外的命令,這可能是嚴重的安全漏洞。 例如,惡意用戶可能會濫用易受攻擊的函數(shù)在遠程計算機上運行任意代碼,從而損害該計算機并獲取對網(wǎng)絡(luò)上其他計算機的訪問權(quán)限。
了解問題后,可通過多種方式防范注入攻擊。
易受攻擊的代碼示例
PowerShell 代碼注入漏洞涉及包含腳本代碼的用戶輸入。 用戶輸入添加到易受攻擊的腳本中,由 PowerShell 分析和運行。
function Get-ProcessById { param ($ProcId) Invoke-Expression -Command "Get-Process -Id $ProcId" }
Get-ProcessById 函數(shù)按其 ID 值查找本地進程。 它采用任何類型的 $ProcId 參數(shù)。 然后將 $ProcId 轉(zhuǎn)換為字符串,并插入到使用 Invoke-Expression cmdlet 分析和運行的另一個腳本中。 傳入有效的進程 ID 整數(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ù)未指定類型。 它接受任何可以包含其他命令的任意字符串值。
Get-ProcessById "$pid; Write-Host 'pwnd!'"
在此示例中,函數(shù)正確檢索了由 $pid 標識的進程,但也運行了注入腳本 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!
防范注入攻擊的方法
可通過多種方式防范注入攻擊。
使用類型化輸入
可以為 $ProcId 參數(shù)指定類型。
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ù)類型,因此,當(dāng)傳入不能轉(zhuǎn)換為整數(shù)的字符串時,會發(fā)生錯誤。
不要使用 Invoke-Expression
無需使用 Invoke-Expression,直接調(diào)用 Get-Process,讓 PowerShell 的參數(shù)綁定器驗證輸入。
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,尤其是在處理用戶輸入時。 Invoke-Expression 很危險,因為它會分析和運行你提供的任何字符串內(nèi)容,使其容易受到注入攻擊。 最好依賴于 PowerShell 參數(shù)綁定。
將字符串包含在單引號中
但是,有時使用 Invoke-Expression 是不可避免的,你還需要處理用戶字符串輸入。 可以使用每個字符串輸入變量周圍的單引號安全地處理用戶輸入。 單引號可確保 PowerShell 分析程序?qū)⒂脩糨斎胍暈閱蝹€字符串文本。
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ù)對于防范注入攻擊還不是完全安全的。 惡意用戶仍然可以在其輸入中使用單引號來注入代碼。
Get-ProcessById "$pid'; Write-Host 'pwnd!';'"
此示例在用戶輸入中使用單引號來強制函數(shù)運行三個單獨的語句,其中一個語句是由用戶注入的任意代碼。
NPM(K) PM(M) WS(M) CPU(s) Id SI ProcessName ------ ----- ----- ------ -- -- ----------- 97 46.08 183.10 1.08 2524 3 pwsh pwnd!
使用 EscapeSingleQuotedStringContent() 方法
若要防止用戶插入自己的單引號字符來利用函數(shù),必須使用 EscapeSingleQuotedStringContent() API。 這是 PowerShell System.Management.Automation.Language.CodeGeneration 類的靜態(tài)公共方法。 此方法通過轉(zhuǎn)義用戶輸入中包含的任何單引號來確保用戶輸入的安全性。
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 檢測易受攻擊的代碼
Injection Hunter 是 Lee Holmes 編寫的模塊,其中包含用于檢測代碼注入漏洞的 PowerShell 腳本分析器規(guī)則。 使用以下命令之一從 PowerShell 庫安裝模塊:
# Use PowerShellGet v2.x Install-Module InjectionHunter # Use PowerShellGet v3.x Install-PSResource InjectionHunter
可以使用此模塊在生成、持續(xù)集成過程、部署和其他方案中自動執(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)文章
thinkphp代碼執(zhí)行g(shù)etshell的漏洞解決
本文來介紹一下thinkphp官方修復(fù)的一個getshell漏洞,框架對控制器沒有進行足夠的檢測導(dǎo)致的一處getshell,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨2018-12-12記 FineUI 官方論壇discuz所遭受的一次真實網(wǎng)絡(luò)攻擊
這篇文章主要介紹了記 FineUI 官方論壇discuz所遭受的一次真實網(wǎng)絡(luò)攻擊,需要的朋友可以參考下2018-11-30- 這篇文章主要介紹了Linux 下多種反彈 shell 方法,需要的朋友可以參考下2017-09-06
- 這篇文章主要為大家介紹了基于反射的XSS攻擊,主要依靠站點服務(wù)端返回腳本,在客戶端觸發(fā)執(zhí)行從而發(fā)起Web攻擊,需要的朋友可以參考下2017-05-20
- 這篇文章主要介紹了SQL注入黑客防線網(wǎng)站實例分析,需要的朋友可以參考下2017-05-19
- 這里為大家分享一下sql注入的一些語句,很多情況下由于程序員的安全意識薄弱或基本功不足就容易導(dǎo)致sql注入安全問題,建議大家多看一下網(wǎng)上的安全文章,最好的防范就是先學(xué)2017-05-19
- 對于目前流行的sql注入,程序員在編寫程序時,都普遍的加入防注入程序,有些防注入程序只要在我們提交一些非法的參數(shù)后,就會自動的記錄下你的IP地址,提交的非法參數(shù)和動作等,2017-04-29
XSS繞過技術(shù) XSS插入繞過一些方式總結(jié)
我們友情進行XSS檢查,偶然跳出個小彈窗,其中我們總結(jié)了一些平時可能用到的XSS插入方式,方便我們以后進行快速檢查,也提供了一定的思路,其中XSS有反射、存儲、DOM這三類2016-12-27- 這篇文章主要介紹了Python 爬蟲使用動態(tài)切換ip防止封殺的相關(guān)資料,需要的朋友可以參考下2016-10-08
- 這篇文章主要介紹了使用爬蟲采集網(wǎng)站時,解決被封IP的幾種方法的相關(guān)資料,需要的朋友可以參考下2016-10-08