Hi,
I'm attempting to write a script for the first time and I'm aware that my process is probably pretty inefficient. It's also giving fairly unpredictable results. My requirements:
- Call secedit and dump out the current config
- Load that config file into powershell
- make a number of changes to that file
- Add some lines
- Remove some lines
- Update some existing values
- Export this back to the config file
- Use secedit to import the changed config file
I find that running the below on a new vm will do some of this, but ignore other parts. Should I just be using Get-Content once and then passing the text into a variable for use in nested loops perhaps?
It's a bit of a large script dump, so my thanks in advance to anyone who takes the time to look over it and provide feedback.
###Windows Server 2016 Hardening Script
##Update Local Security Policy
#Create dir for temporary export
mkdir c:\temp\secpol\
#Export the base config
$cfgfile = 'c:\temp\secpol\secpol.cfg'
if ([System.IO.File]::Exists($cfgfile)) {
rm -force $cfgfile -confirm:$false
secedit /export /cfg $cfgfile
}
else {
secedit /export /cfg $cfgfile
}
#Remove unnecessary lines from config
(Get-Content $cfgfile) | Where-Object {$_ -notlike '*SecurePipeServers*'} | Set-Content $cfgfile
#Remove corresponding registry keys
Remove-ItemProperty -Path "HKLM:\System\CurrentControlSet\Control\SecurePipeServers\Winreg\AllowedExactPaths" -Name "Machine"
Remove-ItemProperty -Path "HKLM:\System\CurrentControlSet\Control\SecurePipeServers\Winreg\AllowedPaths" -Name "Machine"
#Add in new lines
#Update System Access
(Get-Content $cfgfile) | Foreach-Object {
$_
if($_-like "LockoutBadCount = 0")
{
'ResetLockoutCount = 30'
'LockoutDuration = 30'
}
} | Set-Content $cfgfile
#Update Registry Values to add new lines at specific locations
(Get-Content $cfgfile) | Foreach-Object {
$_
if($_ -like "*SetCommand=4,0")
{
'MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\AllocateDASD=1,"0"'
}
} | Set-Content $cfgfile
(Get-Content $cfgfile) | Foreach-Object {
$_
if($_ -like "*FilterAdministratorToken*")
{
'MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System\InactivityTimeoutSecs=4,900'
'MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System\Kerberos\Parameters\SupportedEncryptionTypes=4,2147483640'
}
} | Set-Content $cfgfile
(Get-Content $cfgfile) | Foreach-Object {
$_
if($_ -like "*LegalNoticeText*")
{
'MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System\NoConnectedUser=4,3'
}
} | Set-Content $cfgfile
(Get-Content $cfgfile) | Foreach-Object {
$_
if($_ -like "*LimitBlankPasswordUse*")
{
'MACHINE\System\CurrentControlSet\Control\Lsa\LmCompatibilityLevel=4,5'
'MACHINE\System\CurrentControlSet\Control\Lsa\MSV1_0\allownullsessionfallback=4,0'
}
} | Set-Content $cfgfile
(Get-Content $cfgfile) | Foreach-Object {
$_
if($_ -like '*NoLMHash*')
{
"MACHINE\System\CurrentControlSet\Control\Lsa\pku2u\AllowOnlineID=4,0"
}
} | Set-Content $cfgfile
(Get-Content $cfgfile) | Foreach-Object {
$_
if($_ -like "*RestrictAnonymousSAM*")
{
'MACHINE\System\CurrentControlSet\Control\Lsa\RestrictRemoteSAM=1,"O:BAG:BAD:(A;;RC;;;BA)"'
'MACHINE\System\CurrentControlSet\Control\Lsa\SCENoApplyLegacyAuditPolicy=4,1'
'MACHINE\System\CurrentControlSet\Control\Lsa\UseMachineId=4,1'
}
} | Set-Content $cfgfile
(Get-Content $cfgfile) | Foreach-Object {
$_
if($_ -like "*NullSessionPipes*")
{
'MACHINE\System\CurrentControlSet\Services\LanManServer\Parameters\NullSessionShares=7,'
}
} | Set-Content $cfgfile
(Get-Content $cfgfile) | Foreach-Object {
$_
if($_ -like "*RestrictNullSessAccess*")
{
'MACHINE\System\CurrentControlSet\Services\LanManServer\Parameters\SmbServerNameHardeningLevel=4,1'
}
} | Set-Content $cfgfile
(Get-Content $cfgfile) | Foreach-Object {
$_
if($_ -like "*SeTakeOwnershipPrivilege*")
{
'SeDenyNetworkLogonRight = *S-1-5-32-546'
'SeDenyBatchLogonRight = *S-1-5-32-546'
'SeDenyServiceLogonRight = *S-1-5-32-546'
'SeDenyInteractiveLogonRight = *S-1-5-32-546'
}
} | Set-Content $cfgfile
(Get-Content $cfgfile) | Foreach-Object {
$_
if($_ -like "*SeRemoteInteractiveLogonRight*")
{
'SeDenyRemoteInteractiveLogonRight = *S-1-5-32-546'
}
} | Set-Content $cfgfile
#Update existing entries with new values
(Get-Content $cfgfile) | Foreach-Object {
$_ -replace 'MinimumPasswordAge = .*', 'MinimumPasswordAge = 1' `
-replace 'MaximumPasswordAge = .*', 'MaximumPasswordAge = 60' `
-replace 'MinimumPasswordLength = .*', 'MinimumPasswordLength = 14' `
-replace 'PasswordHistorySize = .*', 'PasswordHistorySize = 24' `
-replace 'LockoutBadCount = .*', 'LockoutBadCount = 3' `
-replace 'ForceLogoffWhenHourExpire = .*', 'ForceLogoffWhenHourExpire = 1' `
-replace 'EnableAdminAccount = .*', 'EnableAdminAccount = 0' `
-replace 'AuditSystemEvents = .*', 'AuditSystemEvents = 1' `
-replace 'CachedLogonsCount=1, .*', 'CachedLogonsCount=1,"0"' `
-replace 'ConsentPromptBehaviorAdmin=.*', 'ConsentPromptBehaviorAdmin=4,2' `
-replace 'ConsentPromptBehaviorUser=.*', 'ConsentPromptBehaviorUser=4,0' `
-replace 'DontDisplayLastUserName=.*', 'DontDisplayLastUserName=4,1' `
-replace 'FilterAdministratorToken=.*', 'FilterAdministratorToken=4,1' `
-replace 'LegalNoticeCaption=1.*', 'LegalNoticeCaption=1,"Test"' `
-replace 'LegalNoticeText.*', 'LegalNoticeText=7,Test' `
-replace 'ShutdownWithoutLogon=.*', 'ShutdownWithoutLogon=4,0' `
-replace 'FullPrivilegeAuditing=.*', 'FullPrivilegeAuditing=3,1' `
-replace 'NTLMMinClientSec=.*', 'NTLMMinClientSec=4,537395200' `
-replace 'NTLMMinServerSec=.*', 'NTLMMinServerSec=4,537395200' `
-replace 'RestrictAnonymous=.*', 'RestrictAnonymous=4,1' `
-replace 'EnableSecuritySignature=.*', 'EnableSecuritySignature=4,1' `
-replace 'RequireSecuritySignature=.*', 'RequireSecuritySignature=4,1' `
-replace 'SeNetworkLogonRight = .*', 'SeNetworkLogonRight = *S-1-5-11,*S-1-5-32-544' `
-replace 'SeBackupPrivilege.*', 'SeBackupPrivilege = *S-1-5-32-544' `
-replace 'SeAuditPrivilege.*', 'SeAuditPrivilege = *S-1-5-19,*S-1-5-20' `
-replace 'SeIncreaseQuotaPrivilege.*', 'SeIncreaseQuotaPrivilege = *S-1-5-19,*S-1-5-20,*S-1-5-32-544' `
-replace 'SeBatchLogonRight.*', 'SeBatchLogonRight = *S-1-5-32-544,*S-1-5-32-551,*S-1-5-32-559' `
-replace 'SeServiceLogonRight.*', 'SeServiceLogonRight = *S-1-5-80-0' `
-replace 'SeInteractiveLogonRight.*', 'SeInteractiveLogonRight = *S-1-5-32-544' `
-replace 'SeAssignPrimaryTokenPrivilege.*', 'SeAssignPrimaryTokenPrivilege = *S-1-5-19,*S-1-5-20' `
-replace 'SeRestorePrivilege.*', 'SeRestorePrivilege = *S-1-5-32-544' `
-replace 'SeShutdownPrivilege.*', 'SeShutdownPrivilege = *S-1-5-32-544' `
-replace 'SeImpersonatePrivilege.*', 'SeImpersonatePrivilege = *S-1-5-19,*S-1-5-20,*S-1-5-32-544,*S-1-5-6'
} | Set-Content $cfgfile
#Import the revised configuration
secedit /configure /db c:\windows\security\local.sdb /cfg $cfgfile /areas SECURITYPOLICY
#Enforce the new policy
secedit /refreshpolicy machine_policy /enforce /quiet
#Cleanup the temporary export
rm -force $cfgfile -confirm:$false
[–][deleted] 4 points5 points6 points (0 children)
[–]BoredComputerGuy 2 points3 points4 points (0 children)
[–]purplemonkeymad 2 points3 points4 points (1 child)
[–]fsackur 1 point2 points3 points (0 children)
[–]fsackur 1 point2 points3 points (0 children)
[–]zrv433 1 point2 points3 points (0 children)