2016-12-06 199 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可以进一步改进:-) –