我編寫了這個腳本來查找$ WWWUSER具有寫權限的所有文件/目錄。起初,我將其餘的匹配項存儲在一個臨時文件中。我新的必須有一種不使用文件的方式,所以這是我的「解決方案」。它可以工作,但速度很慢。有小費嗎?如何優化bash腳本? (查找文件,忽略白名單,報告其餘)
更新: 在包含大約7K目錄和30K文件(〜8K whitelistings)腳本大約需要15分鐘的目錄結構...(ext3文件系統,UW320 SCSI硬盤)。
#!/usr/bin/env bash
# Checks the webroot for files owned by www daemon and
# writable at the same time. This is only needed by some files
# So we'll check with a whitelist
WWWROOT=/var/www
WWWUSER=www-data
WHITELIST=(/wp-content/uploads
/wp-content/cache
/sitemap.xml
)
OLDIFS=$IFS
IFS=$'\n'
LIST=($(find $WWWROOT -perm /u+w -user $WWWUSER -o -perm /g+w -group $WWWUSER))
IFS=$OLDIFS
arraycount=-1
whitelist_matches=0
for matchedentry in "${LIST[@]}"; do
arraycount=$(($arraycount+1))
for whitelistedentry in "${WHITELIST[@]}"; do
if [ $(echo $matchedentry | grep -c "$whitelistedentry") -gt 0 ]; then
unset LIST[$arraycount]
whitelist_matches=$(($whitelist_matches+1))
fi
done
LISTCOUNT=${#LIST[@]}
done
if [ $(echo $LISTCOUNT) -gt 0 ]; then
for item in "${LIST[@]}"; do
echo -e "$item\r"
done
echo "$LISTCOUNT items are writable by '$WWWUSER' ($whitelist_matches whitelisted)."
else
echo "No writable items found ($whitelist_matches whitelisted)."
fi
是否有可能使用任何外部應用程序來運行該程序Perl的?對於白名單引用,它可以通過使用散列來縮短並加快速度。使用不同的Shebang,它可以運行相同。 – 0xCAFEBABE 2010-11-04 10:59:13
這是可能的,但不是所期望的。但我會檢查TME的想法,然後研究如何實施你的想法。謝謝! – weeheavy 2010-11-04 11:21:45