Bash

使用 bash 從文件中讀取變數時如何避免失去我的轉義字元

  • June 6, 2021

我有一個實驗室ntlm-extract.ntds文件,其中包含以下格式的使用者名和雜湊: domain\username:integer:hash:hash2

例如: somedomain.local\jcricket:5201:0020cfaecd41954fb9c9da8c61ccacd7:0020cfaecd41954fb9c9da8c61ccacd7

我正在將LINE[3]/hash2列中的雜湊值與NTLM HIBP 數據庫中的雜湊值進行比較,然後我想列印出匹配的使用者名,但無論我嘗試什麼都會domain\username失去\,我不確定它是否正在讀取或寫它失去它。

我到目前為止的腳本是:

#!/usr/bin/bash

while read line
do
   IFS=':' read -ra "LINE" <<< ${line}
   HASH=${LINE[3]}
   HASH=${HASH^^}
   printf "Checking for %s\n" $HASH
   found=(`grep "$HASH" "./pwned-passwords-ntlm-ordered-by-hash-v7.txt"`)
   if [ -n $found ]; then
       printf "Match on username %s\n" "${LINE[0]}"
   fi
done < "ntlm-extract.ntds"

根據建議,最終的工作腳本最終成為:

#!/usr/bin/bash

numoflines=(`wc -l ntlm-extract.ntds`)
numcomp=0
while IFS= read -r line; do
   IFS=: read -ra hashline <<< "${line}"
   passhash="${hashline[3]}"
   printf "Checking for %s\n" $passhash
   printf "Line %d of %d\n" $numcomp $numoflines
   numcomp=$((numcomp+1))
   found=''
   found=(`grep -m 1 -i "$passhash" "./pwned-passwords-ntlm-ordered-by-hash-v7.txt"`)
   wait 
   if [ -z "$found" ]; then
       continue
   else
       printf "found return value is %s\n" "$found"
       printf "%s\n" "${hashline[0]}" >> ./hibp-busted.txt
   fi
done < "ntlm-extract.ntds"

你需要-r外在read,而不僅僅是內在read -a。除非您明確想要去除前導空格,否則您還應該引用"${line}"和(可能)想要:IFS=

while IFS= read -r line; do 
 IFS=: read -ra LINE <<< "${line}"; printf '%s\n' "${LINE[@]}"
done < ntlm-extract.ntds
somedomain.local\jcricket
5201
0020cfaecd41954fb9c9da8c61ccacd7
0020cfaecd41954fb9c9da8c61ccacd7

我還建議將變數的名稱更改LINE為並非全部大寫的名稱。

引用自:https://unix.stackexchange.com/questions/653023