CIS Benchmarks with Batch Scripts

BATCH FILE

@echo off

setlocal enabledelayedexpansion

:: ========================================

:: Multi-File Registry SID Processor Script

:: ========================================

:: This script processes multiple registry files by replacing <***USER_SID***>

:: placeholders with the current user's actual SID and then executes them.

::

:: Usage:

:: process_registry_multi.bat <filename> - Single file

:: process_registry_multi.bat <file1> <file2> <file3> - Multiple files

:: process_registry_multi.bat -list <filelist.txt> - File list

:: process_registry_multi.bat -config <config.ini> - Use config file

::

:: Configuration:

:: Set REGISTRY_SOURCE_PATH to specify the source folder/network location

:: Default: .\registry_files\

:: ========================================

echo Multi-File Registry SID Processor v2.0

echo ========================================

:: Default configuration

set "REGISTRY_SOURCE_PATH=.\registry_files\"

set "CONFIG_FILE=registry_config.ini"

set "TEMP_DIR=%TEMP%\registry_processor_%RANDOM%"

set "PROCESSED_COUNT=0"

set "FAILED_COUNT=0"

:: Check for configuration file and load settings

if exist "%CONFIG_FILE%" (

echo Loading configuration from %CONFIG_FILE%...

call :LoadConfig "%CONFIG_FILE%"

)

:: Check if any parameters provided

if "%~1"=="" (

echo ERROR: No input specified.

echo.

echo Usage:

echo %~nx0 ^<filename^> - Process single file

echo %~nx0 ^<file1^> ^<file2^> ^<file3^> - Process multiple files

echo %~nx0 -list ^<filelist.txt^> - Process files from list

echo %~nx0 -config ^<config.ini^> - Use custom config file

echo.

echo Configuration:

echo Source path: %REGISTRY_SOURCE_PATH%

echo Config file: %CONFIG_FILE%

pause

exit /b 1

)

:: Create temporary directory

mkdir "%TEMP_DIR%" 2>nul

:: Get current user's SID

echo Getting current user SID...

call :GetUserSID

if "!USER_SID!"=="" (

echo ERROR: Could not determine user SID.

echo Please run this script as an administrator or check your permissions.

pause

exit /b 1

)

echo Current user SID: !USER_SID!

echo Source path: %REGISTRY_SOURCE_PATH%

echo.

:: Parse command line arguments

if /i "%~1"=="-config" (

if "%~2"=="" (

echo ERROR: Config file not specified.

goto :Cleanup

)

call :LoadConfig "%~2"

shift

shift

)

if /i "%~1"=="-list" (

if "%~2"=="" (

echo ERROR: File list not specified.

goto :Cleanup

)

call :ProcessFileList "%~2"

) else (

call :ProcessCommandLineFiles %*

)

:: Summary

echo.

echo ========================================

echo Processing Summary:

echo Files processed successfully: !PROCESSED_COUNT!

echo Files failed: !FAILED_COUNT!

echo Total files: !PROCESSED_COUNT! + !FAILED_COUNT! = %PROCESSED_COUNT%!FAILED_COUNT!

echo ========================================

if !FAILED_COUNT! gtr 0 (

echo WARNING: Some files failed to process. Check the output above for details.

set "EXIT_CODE=1"

) else (

echo SUCCESS: All files processed successfully.

set "EXIT_CODE=0"

)

:Cleanup

:: Clean up temporary directory

if exist "%TEMP_DIR%" rmdir /s /q "%TEMP_DIR%" 2>nul

echo.

echo Operation completed.

pause

exit /b !EXIT_CODE!

:: ========================================

:: SUBROUTINES

:: ========================================

:GetUserSID

:: Get current user's SID using multiple methods

for /f "tokens=2 delims==" %%i in ('wmic useraccount where name^="%USERNAME%" get sid /value 2^>nul ^| find "SID="') do (

set "USER_SID=%%i"

)

:: Alternative method if WMIC fails

if "!USER_SID!"=="" (

echo WMIC method failed, trying alternative method...

for /f "tokens=1" %%i in ('whoami /user /fo table /nh 2^>nul ^| findstr /r "S-1-"') do (

set "USER_SID=%%i"

)

)

goto :eof

:LoadConfig

:: Load configuration from INI file

set "CONFIG_PATH=%~1"

if not exist "%CONFIG_PATH%" (

echo WARNING: Config file "%CONFIG_PATH%" not found. Using defaults.

goto :eof

)

for /f "usebackq tokens=1,2 delims==" %%a in ("%CONFIG_PATH%") do (

set "KEY=%%a"

set "VALUE=%%b"

:: Remove leading/trailing spaces

for /f "tokens=* delims= " %%x in ("!KEY!") do set "KEY=%%x"

for /f "tokens=* delims= " %%x in ("!VALUE!") do set "VALUE=%%x"

if /i "!KEY!"=="REGISTRY_SOURCE_PATH" set "REGISTRY_SOURCE_PATH=!VALUE!"

if /i "!KEY!"=="TEMP_DIR" set "TEMP_DIR=!VALUE!"

)

goto :eof

:ProcessCommandLineFiles

:: Process files specified on command line

:ProcessLoop

if "%~1"=="" goto :eof

call :ProcessSingleFile "%~1"

shift

goto :ProcessLoop

:ProcessFileList

:: Process files from a list file

set "LIST_FILE=%~1"

if not exist "%LIST_FILE%" (

echo ERROR: File list "%LIST_FILE%" not found.

set /a FAILED_COUNT+=1

goto :eof

)

echo Processing files from list: %LIST_FILE%

for /f "usebackq tokens=* delims=" %%a in ("%LIST_FILE%") do (

set "FILENAME=%%a"

:: Skip empty lines and comments

if not "!FILENAME!"=="" (

if not "!FILENAME:~0,1!"=="#" (

call :ProcessSingleFile "!FILENAME!"

)

)

)

goto :eof

:ProcessSingleFile

:: Process a single registry file

set "FILENAME=%~1"

set "SOURCE_FILE=%REGISTRY_SOURCE_PATH%%FILENAME%"

set "TEMP_FILE=%TEMP_DIR%\processed_%RANDOM%_%FILENAME%"

echo.

echo Processing: %FILENAME%

echo Source: %SOURCE_FILE%

:: Check if source file exists

if not exist "%SOURCE_FILE%" (

echo ERROR: File "%SOURCE_FILE%" not found.

set /a FAILED_COUNT+=1

goto :eof

)

:: Process the file - replace SID placeholders

echo Replacing SID placeholders...

(

for /f "usebackq delims=" %%a in ("%SOURCE_FILE%") do (

set "line=%%a"

set "line=!line:<***USER_SID***>=!USER_SID!!"

echo !line!

)

) > "%TEMP_FILE%"

:: Verify the temporary file was created

if not exist "%TEMP_FILE%" (

echo ERROR: Failed to create processed file for "%FILENAME%".

set /a FAILED_COUNT+=1

goto :eof

)

echo Processed file created: %TEMP_FILE%

:: Apply the registry changes

echo Applying registry changes...

regedit /s "%TEMP_FILE%"

if !ERRORLEVEL! equ 0 (

echo SUCCESS: Registry changes applied for "%FILENAME%".

set /a PROCESSED_COUNT+=1

) else (

echo ERROR: Failed to apply registry changes for "%FILENAME%". Error code: !ERRORLEVEL!

set /a FAILED_COUNT+=1

)

goto :eof

:ShowPreview

:: Show preview of all files to be processed

set "PREVIEW_FILE=%TEMP_DIR%\preview_all.reg"

echo Creating preview of all changes...

echo Windows Registry Editor Version 5.00 > "%PREVIEW_FILE%"

echo. >> "%PREVIEW_FILE%"

echo ; ======================================== >> "%PREVIEW_FILE%"

echo ; PREVIEW OF ALL REGISTRY CHANGES >> "%PREVIEW_FILE%"

echo ; ======================================== >> "%PREVIEW_FILE%"

echo. >> "%PREVIEW_FILE%"

:: Append all processed files to preview

for %%f in ("%TEMP_DIR%\processed_*.reg") do (

echo ; File: %%~nxf >> "%PREVIEW_FILE%"

echo ; ---------------------------------------- >> "%PREVIEW_FILE%"

type "%%f" >> "%PREVIEW_FILE%"

echo. >> "%PREVIEW_FILE%"

)

echo Preview file created: %PREVIEW_FILE%

echo.

echo Would you like to view the preview? (Y/N):

set /p "VIEW_PREVIEW="

if /i "!VIEW_PREVIEW!"=="Y" (

notepad "%PREVIEW_FILE%"

)

echo.

set /p "CONFIRM_ALL=Do you want to apply ALL these registry changes? (Y/N): "

if /i not "!CONFIRM_ALL!"=="Y" (

echo Operation cancelled by user.

goto :Cleanup

)

goto :eof

CONFIG FILE

# Registry Processor Configuration File

# =====================================

# This file contains configuration settings for the multi-file registry processor

# Source path for registry files (can be local folder or network path)

# Examples:

# REGISTRY_SOURCE_PATH=.\registry_files\

# REGISTRY_SOURCE_PATH=C:\CompanyPolicies\Registry\

# REGISTRY_SOURCE_PATH=\\server\share\registry_policies\

REGISTRY_SOURCE_PATH=.\registry_files\

# Temporary directory for processing (optional)

# If not specified, uses system temp directory

# TEMP_DIR=C:\Temp\RegistryProcessor\

FILE LIST EXAMPLE

# Registry Files List

# ===================

# This file contains a list of registry files to be processed

# Lines starting with # are comments and will be ignored

# Empty lines are also ignored

# Core security and policy files

screensaver_policy.reg

windows_update_policy.reg

security_policy.reg

# User interface customizations

explorer_settings.reg

# Additional files can be added here

# desktop_wallpaper.reg

# network_settings.reg

Next
Next

CIS Benchmarks with Powershell Scripts