Skip to content

Commit 49d2eeb

Browse files
Updates to pwchange scripts (#6)
- Reworked pwchange scripts to recooperate after a failed password change attempt by using a user-seed.conf to reset password - Converted Windows pwchange script from Batch to Powershell for better flexibility - Added customization for scripts into inputs.conf so values that are unique to an environment can be leveraged without editing the scripts directly
1 parent 8a8e0e9 commit 49d2eeb

File tree

8 files changed

+193
-114
lines changed

8 files changed

+193
-114
lines changed

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,22 @@ index = _internal
8181
sourcetype = pw_change:output
8282
interval = -1
8383
source = pw_change_output
84+
splunkUserName = admin
85+
newPass = auto
86+
oldPass = changeme
87+
printPass = false
8488
85-
[script://.\bin\pwchange.bat]
89+
[powershell://pwchange]
8690
disabled = 1
8791
index = _internal
8892
sourcetype = pw_change:output
8993
interval = -1
9094
source = pw_change_output
95+
script = . "$SplunkHome\etc\apps\SplunkForwarderRepairKit\bin\pwchange.ps1"
96+
splunkUserName = admin
97+
newPass = auto
98+
oldPass = changeme
99+
printPass = false
91100
```
92101

93102
## Use-Cases
@@ -136,12 +145,12 @@ https://docs.splunk.com/Documentation/Splunk/latest/ReleaseNotes/FixDatetimexml2
136145
~~Windows - `dateTimeCorrect.ps1`\
137146
\*Nix - `dateTimeCorrect.sh`~~
138147

139-
###### Update default 'changeme' password on Splunk Forwarders (primary installations before 7.1.0)
148+
###### Update local user password on Splunk Forwarders (primary installations before 7.1.0)
140149
Forwarders deployed before version 7.1.0 didn't require the admin password be changed upon installation. Starting at 7.1.0, the forwarders required either a user-seed file or manual input of the password during first-time run. While the REST API of the forwarder is not configured to allow POST requests until the password is changed on versions prior to 7.1.0, changing the password is still recommended.
141150

142-
This app contains scripts for Windows and Linux forwarders that will allow either a static password or random password to be configured. By default, a random password is generated and printed into the log which is sent back to Splunk.
151+
The variables for the environment can be configured in inputs.conf when the app is deployed such as the Splunk username (default 'admin'), whether to automatically generate a random password or to set it explicitly (default 'auto'), the value of the old password (default 'changeme'), and whether or not to send the new password back in plain-text to Splunk (default 'false').
143152

144-
Windows - `pwchange.bat`\
153+
Windows - `pwchange.ps1`\
145154
\*Nix - `pwchange.sh`
146155

147156
## Restarting the Forwarder

README/inputs.conf.spec

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,47 @@
33
[script:<uniqueName>]
44
deploymentServerUri = [string]
55
* Correct URI that should be configured
6+
* Default value is empty
67

78
deploymentClientApp = [string]
89
* App name that contains the correct deploymentclient.conf configuration
10+
* Default value is empty
11+
12+
splunkUserName = [string]
13+
* Value representing the username configured on the Universal Forwarder
14+
* Default value is 'admin'
15+
16+
newPass = {auto|string}
17+
* Value representing either a specified password to be configured or "auto"
18+
* Default value is 'auto'
19+
20+
oldPass = [string]
21+
* Value representing the old password configured on the Universal Forwarder
22+
* Default is 'changeme'
23+
24+
printPass = {true|false}
25+
* Determines if the new password that is generated will be sent to _internal
26+
* Default value is 'false''. Change to 'true' to print password into _internal
927

1028
[powershell:<uniqueName>]
1129
deploymentServerUri = [string]
1230
* Correct URI that should be configured with port
1331

1432
deploymentClientApp = [string]
1533
* App name that contains the correct deploymentclient.conf configuration
34+
35+
splunkUserName = [string]
36+
* Value representing the username configured on the Universal Forwarder
37+
* Default value is 'admin'
38+
39+
newPass = {auto|string}
40+
* Value representing either a specified password to be configured or "auto"
41+
* Default value is 'auto'
42+
43+
oldPass = [string]
44+
* Value representing the old password configured on the Universal Forwarder
45+
* Default is 'changeme'
46+
47+
printPass = {true|false}
48+
* Determines if the new password that is generated will be sent to _internal
49+
* Default value is 'false''. Change to 'true' to print password into _internal

bin/appContext.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,11 @@ if [ "${SCRIPT_NAME}" = "dsRemove.sh" ]; then
1010
CORRECT_DS=$(${SPLUNK_HOME}/bin/splunk btool --app=$APP_NAME inputs list script://./bin/$SCRIPT_NAME | grep deploymentServerUri | sed "s/deploymentServerUri = //")
1111
CORRECT_APP=$(${SPLUNK_HOME}/bin/splunk btool --app=$APP_NAME inputs list script://./bin/$SCRIPT_NAME | grep deploymentClientApp | sed "s/deploymentClientApp = //")
1212
fi
13+
14+
## Capture the configuration details for changing the local password
15+
if [ "${SCRIPT_NAME}" = "pwchange.sh" ]; then
16+
SPLUNK_USER=$(${SPLUNK_HOME}/bin/splunk btool --app=$APP_NAME inputs list script://./bin/$SCRIPT_NAME | grep newPass | sed "s/splunkUserName = //")
17+
NEWPASS=$(${SPLUNK_HOME}/bin/splunk btool --app=$APP_NAME inputs list script://./bin/$SCRIPT_NAME | grep newPass | sed "s/newPass = //")
18+
OLDPASS=$(${SPLUNK_HOME}/bin/splunk btool --app=$APP_NAME inputs list script://./bin/$SCRIPT_NAME | grep oldPass | sed "s/oldPass = //")
19+
PRINT_PASS=$(${SPLUNK_HOME}/bin/splunk btool --app=$APP_NAME inputs list script://./bin/$SCRIPT_NAME | grep printPass | sed "s/printPass = //")
20+
fi

bin/pwchange.bat

Lines changed: 0 additions & 85 deletions
This file was deleted.

bin/pwchange.ps1

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
### Grab variables from inputs.conf
2+
$BTOOL_INPUT = & $SPLUNKHOME\bin\splunk.exe cmd btool inputs list powershell://pw_change
3+
$SPLUNK_USER_NAME = ($BTOOL_INPUT | findstr splunkUserName).Split(" ")[2]
4+
$NEW_PASS_SETTING = ($BTOOL_INPUT | findstr newPass).Split(" ")[2]
5+
$OLD_PASS_SETTING = ($BTOOL_INPUT | findstr oldPass).Split(" ")[2]
6+
$PRINT_PASS_SETTING = ($BTOOL_INPUT | findstr printPass).Split(" ")[2]
7+
8+
### Configure file paths for the checkpoint
9+
$PW_CHANGED = "$SPLUNKHOME\etc\pw_changed"
10+
$PW_CHECK = (Test-Path -Path "$PW_CHANGED")
11+
12+
### Function for generation of a random password
13+
Function GeneratePassword
14+
{
15+
$MinimumPasswordLength = 12
16+
$MaximumPasswordLength = 16
17+
$PasswordLength = Get-Random -InputObject ($MinimumPasswordLength..$MaximumPasswordLength)
18+
$AllowedPasswordCharacters = [char[]]'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!?@#£$%^&'
19+
$Regex = "(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W)"
20+
21+
do {
22+
$Password = ([string]($AllowedPasswordCharacters |
23+
Get-Random -Count $PasswordLength) -replace ' ')
24+
} until ($Password -cmatch $Regex)
25+
26+
$Password
27+
28+
}
29+
30+
### Filter to attach timestamps where necessary
31+
filter timestamp {"$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss.fff zzz') ${env:COMPUTERNAME}: $_"}
32+
33+
### Check to see if there is a pw_changed file under $SPLUNKHOME\etc and bail out if there is
34+
if ( $PW_CHECK -eq "True" )
35+
{
36+
Write-output "Splunk account password was already changed." | timestamp
37+
Exit
38+
}
39+
40+
### Capture the current password hash to check for a successful change
41+
$PASS_HASH_GET = Get-ChildItem $SPLUNKHOME\etc\passwd | select-string $SPLUNK_USER_NAME
42+
$OLD_HASH = ($PASS_HASH_GET -Split {$_ -eq ":"}) | findstr "\$"
43+
44+
### Set random password if newPass is "auto"
45+
if ( $NEW_PASS_SETTING -eq "auto" )
46+
{
47+
Write-output "Configuring random password..." | timestamp
48+
$NEWPASS = (GeneratePassword)
49+
$OLDPASS = $OLD_PASS_SETTING
50+
if ( $PRINT_PASS_SETTING -eq "true" -or $PRINT_PASS_SETTING -eq "1" )
51+
{
52+
Write-output "New password: ${NEWPASS}" | timestamp
53+
}
54+
& ${SPLUNKHOME}\bin\splunk edit user ${SPLUNK_USER_NAME} -password ${NEWPASS} -auth ${SPLUNK_USER_NAME}:${OLDPASS} 2>$null
55+
$NEW_HASH_GET = Get-ChildItem $SPLUNKHOME\etc\passwd | select-string $SPLUNK_USER_NAME
56+
$NEW_HASH = ($NEW_HASH_GET -Split {$_ -eq ":"}) | findstr "\$"
57+
if ( $NEW_HASH -ne $OLD_HASH ) {
58+
Write-output "Password changed." | timestamp
59+
Out-File -FilePath "$PW_CHANGED"
60+
} else {
61+
Write-output "Password change failed. Creating user-seed.conf." | timestamp
62+
Rename-Item -Path $SPLUNKHOME\etc\passwd -NewName $SPLUNKHOME\etc\passwd.bak
63+
"[user_info]" | Out-File -FilePath $SPLUNKHOME\etc\system\local\user-seed.conf
64+
"USERNAME = ${SPLUNK_USER_NAME}" | Out-File -FilePath $SPLUNKHOME\etc\system\local\user-seed.conf -Append
65+
"PASSWORD = ${NEWPASS}" | Out-File -FilePath $SPLUNKHOME\etc\system\local\user-seed.conf -Append
66+
Out-File -FilePath "$PW_CHANGED"
67+
}
68+
}
69+
70+
### Set configured password if newPass is not "auto"
71+
if ( $NEW_PASS_SETTING -ne "auto" )
72+
{
73+
Write-output "Configuring specific password..." | timestamp
74+
$NEWPASS = $NEW_PASS_SETTING
75+
$OLDPASS = $OLD_PASS_SETTING
76+
if ( $PRINT_PASS_SETTING -eq "true" -or $PRINT_PASS_SETTING -eq "1" )
77+
{
78+
Write-output "New password: '$NEWPASS'" | timestamp
79+
}
80+
& ${SPLUNKHOME}\bin\splunk edit user ${SPLUNK_USER_NAME} -password ${NEWPASS} -auth ${SPLUNK_USER_NAME}:${OLDPASS} | Out-Null
81+
$NEW_HASH_GET = Get-ChildItem $SPLUNKHOME\etc\passwd | select-string $SPLUNK_USER_NAME
82+
$NEW_HASH = $NEW_HASH_GET -Split {$_ -eq ":"} | findstr "\$"
83+
84+
if ( $NEW_HASH -ne $OLD_HASH )
85+
{
86+
Write-output "Password changed." | timestamp
87+
Out-File -FilePath "$PW_CHANGED"
88+
} else {
89+
Write-output "Password change failed. Creating user-seed.conf." | timestamp
90+
Rename-Item -Path $SPLUNKHOME\etc\passwd -NewName $SPLUNKHOME\etc\passwd.bak
91+
"[user_info]" | Out-File -FilePath "$SPLUNKHOME\etc\system\local\user-seed.conf"
92+
"USERNAME = $SPLUNK_USER_NAME" | Out-File -FilePath "$SPLUNKHOME\etc\system\local\user-seed.conf" -Append
93+
"PASSWORD = $NEWPASS" | Out-File -FilePath "$SPLUNKHOME\etc\system\local\user-seed.conf" -Append
94+
Out-File -FilePath "$PW_CHANGED"
95+
}
96+
}

bin/pwchange.sh

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,43 @@
11
#!/bin/bash
2-
# Define the original and new passwords here. To have a password automatically generated, set NEWPASS to 'auto'
3-
OLDPASS=changeme
4-
NEWPASS=auto
5-
6-
# Configure if the random password generated should be printed into the output that is sent back to Splunk. Default is "0" which means it's NOT printed. Change to "1" to print.
7-
PRINT_PASS=0
2+
## Run appContext to capture details from inputs stanza
3+
. $(dirname $0)/appContext.sh
84

95
# Look for the checkpoint file and error out if it exists
10-
if [ -f $SPLUNK_HOME/etc/pwd_changed ]
11-
then
12-
echo $(date -R) $HOSTNAME: Splunk account password was already changed.
13-
exit
6+
if [ -f $SPLUNK_HOME/etc/pwd_changed ]; then
7+
echo $(date -R) $HOSTNAME: Splunk account password was already changed.
8+
exit
149
fi
1510

16-
if [ "$NEWPASS" = "auto" ]
17-
then
11+
# Generate a random password if newPass is set to "auto" in inputs.conf
12+
if [ "$NEWPASS" = "auto" ]; then
1813
NEWPASS=$(head -c 500 /dev/urandom | sha256sum | base64 | head -c 16 ; echo)
19-
if [ "$PRINT_PASS" = "0" ]; then
20-
NEWPASSAUTO=$(echo "Automatic password: $NEWPASS")
21-
else
22-
NEWPASSAUTO=$(echo "Automatic password: **************")
23-
fi
2414
fi
15+
if [ "$PRINT_PASS" = "1" ] || [ "$PRINT_PASS" = "true" ]; then
16+
echo "$(date -R) $HOSTNAME: New password: $NEWPASS"
17+
fi
18+
19+
# Capture current user password hash
20+
OLD_PASS_HASH=$(cat $SPLUNK_HOME/etc/passwd | grep $SPLUNK_USER | sed "s/:/ /g" | awk '{ print $2 }')
2521

26-
# Change the password
27-
$SPLUNK_HOME/bin/splunk edit user admin -password $NEWPASS -auth admin:$OLDPASS > /dev/null 2>&1
22+
# Attempt to change the password with the provided password in inputs.conf
23+
$SPLUNK_HOME/bin/splunk edit user $SPLUNK_USER -password $NEWPASS -auth $SPLUNK_USER:$OLDPASS > /dev/null 2>&1
2824

29-
# Check splunkd.log for any error messages relating to login during the script and determine whether the change was successful or not
30-
CHANGED=$(tail -n 10 $SPLUNK_HOME/var/log/splunk/splunkd.log | grep pwchange | grep Login)
31-
if [ -z "$CHANGED" ]; then
25+
# Capture the user password hash to see if it changed
26+
NEW_PASS_HASH=$(cat $SPLUNK_HOME/etc/passwd | grep $SPLUNK_USER | sed "s/:/ /g" | awk '{ print $2 }')
27+
28+
# Compare the old user hash to the new user hash and generate a user-seed.file if they still match
29+
if [ "$OLD_PASS_HASH" != "$NEW_PASS_HASH" ]; then
3230
echo "$(date -R) $HOSTNAME: Splunk account password successfully changed. $NEWPASSAUTO"
31+
touch $SPLUNK_HOME/etc/pwd_changed
3332
else
34-
echo $(date -R) $HOSTNAME: Splunk account login failed. Old password is not correct for this host.
33+
echo $(date -R) $HOSTNAME: Password change failed. Creating user-seed.conf.
34+
mv "$SPLUNK_HOME/etc/passwd" "$SPLUNK_HOME/etc/password.backup."
35+
cat <<<EOF "$SPLUNK_HOME/etc/system/local/user-seed.conf"
36+
[user-info]
37+
USERNAME = $SPLUNK_USER
38+
PASSWORD = $NEWPASS
39+
40+
EOF
41+
touch $SPLUNK_HOME/etc/pwd_changed
42+
rm $APP_PATH/bin/DeleteMeToRestart
3543
fi

default/app.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ is_visible = false
99
is_manageable = false
1010

1111
[launcher]
12-
version = 2.0.0
12+
version = 2.1.0

default/inputs.conf

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,19 @@ index = _internal
7070
sourcetype = pw_change:output
7171
interval = -1
7272
source = pw_change_output
73+
splunkUserName = admin
74+
newPass = auto
75+
oldPass = changeme
76+
printPass = false
7377

74-
[script://.\bin\pwchange.bat]
78+
[powershell://pwchange]
7579
disabled = 1
7680
index = _internal
7781
sourcetype = pw_change:output
7882
interval = -1
7983
source = pw_change_output
84+
script = . "$SplunkHome\etc\apps\SplunkForwarderRepairKit\bin\pwchange.ps1"
85+
splunkUserName = admin
86+
newPass = auto
87+
oldPass = changeme
88+
printPass = false

0 commit comments

Comments
 (0)