PowerShell 版本5.x 系统 wIn10
PowerShell Visual Code 环境配置 https://docs.microsoft.com/zh-cn/powershell/scripting/components/vscode/using-vscode?view=powershell-7
Visual Code 中文乱码解决办法 https://blog.csdn.net/weixin_45033342/article/details/90574434
直接上代码
$keyWordList = "md5","download","zip>","rar>","checkupdate","更新","远程","解压","压缩","下载","支付","downurl","ToSelector","performSelector","method_exchangeIm","dlopen","dlsym" if (Test-Path -Path keyWordPosition.txt) { Remove-item -Path keyWordPosition.txt } $millisec = [Timespan]::FromMilliseconds([Environment]::TickCount) Write-Host -NoNewline "start match $(Get-Date)" foreach($word in $keyWordList) { Write-Host -NoNewline " $($word) " } Write-Host $totalLineCount = 0 $totalFileCount = 0 $totalCheckLine = 0 Get-ChildItem -Path .sourcecode -Exclude *.a,*.png,*.jpg,*.so,*log,*.obj,*pbxproj,*make,*pch,*filters,*xcuserstate,*vcxproj,*mk,*js,*project,*.py,*lib,*pdb,*dll,*txt,*mp3,*tmx,*mdown,*ttf -File -recurse | ForEach-Object { $lines = Get-Content $_.FullName -Encoding UTF8 $idx = 1 $keyCount = 0 foreach($lineInfo in $lines) { foreach($word in $keyWordList) { if($lineInfo -match $word) { Write-Host "find [$($word)] at line $($idx) in file $($_.Name)" Start-Sleep -Milliseconds 11 Add-Content -Path keyWordPosition.txt -Encoding UTF8 -Value "find [$($word)] at $($idx) in file $($_.Name)" $keyCount ++ $totalLineCount ++ } $totalCheckLine ++ } $idx ++ } if ( $keyCount -gt 0 ) { Write-Host "$($_.Name) has $($keyCount) sensitive word total $($lines.Count) line" $totalFileCount ++ } } $spendTime = [Timespan]::FromMilliseconds([Environment]::TickCount) - $millisec $usetime = "total spend time [$($spendTime)] total sensitive count [$($totalLineCount)] total check line [$($totalCheckLine)] total check file $($totalFileCount) " Write-Host "$($usetime)" Add-Content -Path keyWordPosition.txt -Encoding UTF8 -Value "$($usetime)" $ignoretype = "*.a,*.png,*.jpg,*.so,*log,*.obj,*pbxproj,*make,*pch,*filters,*xcuserstate,*vcxproj,*mk,*js,*project,*.py,*lib,*pdb,*dll,*txt,*mp3,*tmx,*mdown,*ttf" Write-Host "过滤文件类型 $($ignoretype)" Add-Content -Path keyWordPosition.txt -Encoding UTF8 -Value "过滤文件类型 $($ignoretype)" $keyStr = "" foreach($word in $keyWordList) { $keyStr = $keyStr + " " + $word } Write-Host "比对包含的敏感词 $($keyStr)" Add-Content -Path keyWordPosition.txt -Encoding UTF8 -Value "比对包含的敏感词 $($keyStr)" Write-Host Pause
运行结果
当前脚本和被检索的文件夹放在一个目录 会遍历文件夹里面所有的子文件