2016-12-06 198 views
0

TL;博士 如何在這些情況下使用正則表達式用********替換的單詞的祕密:隱藏密碼字符串

Password="secret" 
PWD="secret" 
-P secret -i args 

我想製作一個正則表達式即會檢測字符串中的密碼並將其替換爲*(在日誌記錄功能中)。

我現在有這個PowerShell的功能:

function ScrubPasswords { 
param(
    [string]$DirtyText 
) 
    # match any case of password="" (or PWD="") (or -P .*) 
    $PwdArgRegex=[regex]'(([Pp][Aa][Ss][Ss][Ww][Oo][Rr][Dd]|PWD)=)("[^"]+")' 
    $PwdArgRegex.replace($DirtyText,'$1********') 
} 

其中一期工程好了,它取代8 *的引號之間的密碼。

但我發現另一個字符串,標識密碼即將到來。對於sqlcmd.exe,參數列表是-U username -P password,我想從那裏擦除密碼。我嘗試在正則表達式中添加|(-P \ s([^ \ s] +)\ s),但是它將*替換了整個匹配字符串(-P thispassword -i otherargs變成了**** **** - 我otherargs;不必要地刪除-P \和尾隨\ s)。

我正在做我的正則表達式設計/測試https://regex101.com/,然後當我有一些看起來不錯的東西時,我通過Powershell_ISE運行它。

我想我的問題是我在第二個或「|」的兩邊有不同數量的組。我會繼續擺弄對手,看看我能否讓比賽小組在雙方都能比賽。

我想了解一些幫助,或者有更好的方法來實現我的目標。

這是到目前爲止我最大的努力:

((([Pp][Aa][Ss][Ss][Ww][Oo][Rr][Dd]|PWD)=)("[^"]+")|(-P\s([^\s]+)\s)) 

有了這些結果:

Match 1 
Full match 23-38 `-P @@@[email protected]@@ ` 
Group 1. 23-38 `-P @@@[email protected]@@ ` 
Group 2. 787-787 `` 
Group 3. 787-787 `` 
Group 4. 787-787 `` 
Group 5. 23-38 `-P @@@[email protected]@@ ` 
Group 6. 26-37 `@@@[email protected]@@` 
Match 2 
Full match 235-250 `-P @@@[email protected]@@ ` 
Group 1. 235-250 `-P @@@[email protected]@@ ` 
Group 2. 787-787 `` 
Group 3. 787-787 `` 
Group 4. 787-787 `` 
Group 5. 235-250 `-P @@@[email protected]@@ ` 
Group 6. 238-249 `@@@[email protected]@@` 
Match 3 
Full match 459-487 `Password="@@@[email protected]@@"` 
Group 1. 459-487 `Password="@@@[email protected]@@"` 
Group 2. 459-468 `Password=` 
Group 3. 459-467 `Password` 
Group 4. 468-487 `"@@@[email protected]@@"` 
Group 5. 787-787 `` 
Group 6. 787-787 `` 
Match 4 
Full match 652-669 `PWD="@@@[email protected]@@"` 
Group 1. 652-669 `PWD="@@@[email protected]@@"` 
Group 2. 652-656 `PWD=` 
Group 3. 652-655 `PWD` 
Group 4. 656-669 `"@@@[email protected]@@"` 
Group 5. 787-787 `` 
Group 6. 787-787 `` 

使用這種測試數據

=begin data= 

Result of sqlcmd -U sa -P @@@[email protected]@@ -i C:\scripts\BridgepointScriptingLibrary\tmp\StandupScripts\common\000_SQLServer_Post_Install_Script.sql = Changed database context to 'master'. (5 rows affected) 

Preparing to run sqlcmd -U sa -P @@@[email protected]@@ -i 000_SQLServer_Post_Install_Script.sql. 

Auth User="@@@[email protected]@@" Password="@@@[email protected]@@" 
PathFromBinRoot="SQL Server 2008 R2 SP1 (x64bit).exe" PWD="@@@[email protected]@@" 

=end data= 
+0

它看起來像你的正則表達式是匹配。也許替換應該是這樣的:'$ DirtyText.replace($ 1,'*********');'但我並不真正瞭解Powershell。 – varlogtim

回答

1

此正則表達式應該這樣做( regex101):

(?:(?<=Password=")\S+(?="))|(?:(?<=-P\s)\S+)|(?:(?<=PWD=")\S+(?=")) 

代碼:

function ScrubPasswords { 
param(
    [string]$DirtyText 
) 
    # match any case of password="" (or PWD="") (or -P .*) 
    $DirtyText -replace '(?:(?<=Password=")\S+(?="))|(?:(?<=-P\s)\S+)|(?:(?<=PWD=")\S+(?="))', '********' 
} 


$testData = 
@' 
=begin data= 

Result of sqlcmd -U sa -P @@@[email protected]@@ -i C:\scripts\BridgepointScriptingLibrary\tmp\StandupScripts\common\000_SQLServer_Post_Install_Script.sql = Changed database context to 'master'. (5 rows affected) 

Preparing to run sqlcmd -U sa -P @@@[email protected]@@ -i 000_SQLServer_Post_Install_Script.sql. 

Auth User="@@@[email protected]@@" Password="@@@[email protected]@@" PathFromBinRoot="SQL Server 2008 R2 SP1 (x64bit).exe" PWD="@@@[email protected]@@" 

=end data= 
'@ 

ScrubPasswords $testData 

輸出:

=begin data= 

Result of sqlcmd -U sa -P ******** -i C:\scripts\BridgepointScriptingLibrary\tmp\StandupScripts\common\000_SQLServer_Post_Install_Script.sql = Changed database context to 'master'. (5 rows affected 
) 

Preparing to run sqlcmd -U sa -P ******** -i 000_SQLServer_Post_Install_Script.sql. 

Auth User="@@@[email protected]@@" Password="********" PathFromBinRoot="SQL Server 2008 R2 SP1 (x64bit).exe" PWD="********" 

=end data= 
+0

我不是一個正則表達式專家 - **可能有更好的解決方案**。然而,在使用密碼或PWD時,我使用了三個未加入組的OR組和一些正面的後視組,以獲得正確的位置和一個正面的前瞻以在使用密碼或PWD時留下引號。只要看看每個人就像第一個人一樣。 '(?:?(<=密碼= 「)\ S +(=?」))'。也許@WiktorStribiżew可以進一步改進:-) –