Skip to main content

Get-keys.bat [OFFICIAL]

:: -------------------------- :: Main scan loop :: -------------------------- pushd "%ROOT%" 2>nul || (echo Cannot access %ROOT% & exit /b 1)

:: -------------------------- :: Defaults and arguments :: -------------------------- set "ROOT=%~1" if "%ROOT%"=="" set "ROOT=%CD%"

:: Normalize paths and build exclude list for findstr set "EXCLUDE_FILTER=" for %%E in (%EXCLUDE:;= %) do ( if defined EXCLUDE_FILTER (set "EXCLUDE_FILTER=!EXCLUDE_FILTER!|%%E") else set "EXCLUDE_FILTER=%%E" ) get-keys.bat

REM build file list using for /R and extension filtering, skipping excludes for /R "%ROOT%" %%F in (%EXT_FILTER%) do ( set "FILE=%%~fF" REM check exclude patterns set "SKIP=0" for %%X in (%EXCLUDE:;= %) do ( echo "!FILE!" | findstr /i /c:"\\%%X\\" >nul if !errorlevel! equ 0 set "SKIP=1" ) if "!SKIP!"=="1" ( REM skip ) else ( REM Read file line by line set "LN=0" for /f "usebackq delims=" %%L in ("%%~fF") do ( set /a LN+=1 set "LINE=%%L" setlocal ENABLEDELAYEDEXPANSION set "L=!LINE!" endlocal & set "L=%L%" REM Quick presence checks for patterns to avoid expensive checks on every line echo "%L%" | findstr /i "AKIA AIza -----BEGIN PRIVATE KEY-----" >nul set "P1=%errorlevel%" echo "%L%" | findstr /r /c:"[A-Fa-f0-9]\8\-[A-Fa-f0-9]\4\-[A-Fa-f0-9]\4\-[A-Fa-f0-9]\4\-[A-Fa-f0-9]\12\" >nul set "P2=%errorlevel%" REM Generic long token heuristic: sequences of 20+ alnum or -_ characters echo "%L%" | findstr /r /c:"[A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-]" >nul set "P3=%errorlevel%" if "%P1%"=="0" (set "MATCHFOUND=1") else if "%P2%"=="0" (set "MATCHFOUND=1") else if "%P3%"=="0" (set "MATCHFOUND=1") else set "MATCHFOUND=0"

if "%MATCHFOUND%"=="1" ( REM Determine match types - simple checks set "MT=Unknown" echo "%L%" | findstr /i "AKIA" >nul if %errorlevel% equ 0 set "MT=AWS_Access_Key" echo "%L%" | findstr /i "AIza" >nul if %errorlevel% equ 0 set "MT=Google_API_Key" echo "%L%" | findstr /i "-----BEGIN PRIVATE KEY-----" >nul if %errorlevel% equ 0 set "MT=Private_Key" echo "%L%" | findstr /r /c:"[A-Fa-f0-9]\8\-[A-Fa-f0-9]\4\-[A-Fa-f0-9]\4\-[A-Fa-f0-9]\4\-[A-Fa-f0-9]\12\" >nul if %errorlevel% equ 0 set "MT=UUID" if "%MT%"=="Unknown" ( set "MT=Generic_Token" ) REM Extract a candidate token (best-effort): we will pick the longest contiguous alnum/_/- sequence for /f "tokens=1-*" %%A in ('echo "%L%" ^| findstr /o /r "[A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-][A-Za-z0-9_-]"') do ( REM findstr /o prints the position of match; we can't easily extract substring in pure batch reliably for arbitrary position, so fallback to output the whole line as context and label the match type set "MATCHVAL=%L%" ) echo Scanning root: %ROOT% echo Extensions: %EXTS% echo

:: Write CSV header set "CSV_HDR=File,LineNumber,Context,MatchType,MatchValue" if "%DRY%"=="0" ( echo %CSV_HDR%> "%OUTFILE%" )

REM findstr in Windows supports limited regex; some syntax above may not be portable. REM We'll use simpler multiple findstr searches per pattern below. MatchValue" if "%DRY%"=="0" ( echo %CSV_HDR%&gt

echo Scanning root: %ROOT% echo Extensions: %EXTS% echo Excludes: %EXCLUDE% if "%MASK%"=="1" echo Masking enabled if "%DRY%"=="1" echo Dry-run (no report written)