我有一個 PS 腳本,可以下載軟體包以對其進行審核。我想檢查是否有任何 .txt、.json 或 .config 檔案存盤密碼或用戶名。(他們不應該這樣做)。
我正在使用 Get-ChildItem 函式搜索檔案。像下面這樣的檔案就是我要搜索的檔案。我需要澄清它是占位符還是檔案中存盤了實際密碼。
我需要想出一種方法來查看用戶名或密碼是否包含值,或者它是否只是一個占位符。我已解壓縮包并從解壓縮位置獲取物品
$folders = Get-ChildItem $unzipLocation
foreach ($folder in $folders) {
$files = Get-ChildItem $folder.FullName
foreach ($file in $files) {
#Search unzipped file to see if they store a password
$content = Get-Childitem $unzipLocation -Include *.json, *.txt, *.config -Recurse | Select-String -Pattern "password", "pwd", "user", "usr"
內容回傳用戶名或密碼在檔案中的位置:

腳本中的下一行將驗證檔案是否包含密碼欄位中的值。
該檔案如下所示:
{
"info": {
"_postman_id": "70134a48-94c1-45ac-a1f0-4b84ccc7e71c",
"name": "Analytical Report API tests",
"schema": ""
},
"item": [
{
"name": "1. Login using test user",
"event": [
{
"listen": "test",
"script": {
"exec": [
"var jsonData = JSON.parse(responseBody);\r",
"pm.collectionVariables.set(\"token\", jsonData.access_token);\r",
"\r",
"pm.test(\"Login test\", () => {\r",
" pm.response.to.have.status(201);\r",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"partnerCode\": \"{{partner}}\",\n \"password\": \"{{password}}\",\n \"userName\": \"{{userName}}\"\n}"
},
"url": {
"raw": "{{authBaseUrl}}/api/v1/Users/token",
"host": [
"{{authBaseUrl}}"
],
"path": [
"api",
"v1",
"Users",
"token"
]
}
},
"response": []
"variable": [
{
"key": "token",
"value": ""
},
{
"key": "currentARCode",
"value": ""
},
{
"key": "currentAR",
"value": ""
},
{
"key": "authBaseUrl",
"value": ""
},
{
"key": "arBaseUrl",
"value": ""
},
{
"key": "fromNumberOfDaysFilter",
"value": ""
},
{
"key": "userName",
"value": ""
},
{
"key": "password",
"value": ""
},
我可以使用正則運算式函式來驗證這一點嗎?
uj5u.com熱心網友回復:
如果模式是 json(在文本檔案中無關緊要),那么您可以直接使用convertfrom-json而不是正則運算式。我只是采用了相同的方法,可以如下使用:
$a = @'
[
{
"authBaseUrl": "#{AuthAPIServerURL}",
"arBaseUrl": "#{APIServerURL}",
"partner": "EUPLKA",
"fromNumberOfDaysFilter": 90,
"password": "",
"userName": ""
}
]
'@
$inp= ConvertFrom-Json $a
If( ($inp.username.trim().Length -gt 0) -or ($inp.password.trim().Length -gt 0) )
{
echo "yes - either the username or the password field is having value"
}
else
{
echo "false- None of the username of password field is having any value"
}
注意:確保在獲取輸入時,陣列中的宣告必須在行的開頭。我已經考慮了用戶名和密碼欄位的長度,但您可以使用自己的正則運算式來查看是否有任何字符。
uj5u.com熱心網友回復:
這過于復雜,但我沒有看到更簡單的方法。它應該能夠捕獲具有pwd 或密碼以及usr 或 username 的檔案,此外它還應該捕獲具有其中之一的檔案,即:密碼在那里但沒有用戶(請參閱 參考資料Index 4)。
查看regex詳情:https : //regex101.com/r/IsHg8O/1
$re = [regex]'(?msi)(pwd|password).*?\{\{(.*?)\}\}|(usr|username).*?\{\{(.*?)\}\}'
Get-Childitem $unzipLocation -Include *.json, *.txt, *.config -Recurse |
ForEach-Object -Begin { $z = 0 } -Process {
$match = $re.Matches((Get-Content $_ -Raw))
foreach($i in $match)
{
if($group = $i.Groups.Where({$_.Success}))
{
[pscustomobject]@{
Index = $z
Match = $group[1].Value
Value = $group[2].Value
Path = $_.FullName
}
}
}
$z
}
樣本
Index Match Value Path
----- ----- ----- ----
0 password password /home/user/json1.txt
0 userName userName /home/user/json1.txt
1 pwd d]5!bT8**<\fcaY? /home/user/json2.txt
1 usr example.user1 /home/user/json2.txt
2 password h Rq~
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/380572.html
標籤:电源外壳
