Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
83f84abd46 | ||
|
|
090d6f9cec | ||
|
|
5659cc010f | ||
|
|
0bd40cdd73 | ||
|
|
5bf30a8b2f | ||
|
|
26c007dba6 | ||
|
|
785e3c116d | ||
|
|
adbfeb0074 | ||
|
|
8c7ef32894 | ||
|
|
70ef7a3fc5 | ||
|
|
89252af50b | ||
|
|
8fb833d3c4 | ||
|
|
694f04313c | ||
|
|
50e28c7cfd | ||
|
|
65b7a6ad43 |
2
.shellcheckrc
Normal file
2
.shellcheckrc
Normal file
@@ -0,0 +1,2 @@
|
||||
shell=bash
|
||||
external-sources=true
|
||||
25
Readme.md
25
Readme.md
@@ -26,6 +26,31 @@ Inside the base folder there are
|
||||
- ssh-keygen for temporary holding the PEM/PUB files
|
||||
- zip file which holds the created user list, password and PEM/PUB files
|
||||
|
||||
## Config
|
||||
|
||||
### create_user.sh: create_user.cfg
|
||||
|
||||
A `create_user.cfg` can be created to set a differen HOME_LOCATION and PASSWORD_LENGTH values
|
||||
|
||||
eg:
|
||||
|
||||
```ini
|
||||
HOME_LOCATION="/storage"
|
||||
PASSWORD_LENGTH=14
|
||||
```
|
||||
|
||||
### authorized_key_location_change.sh: authorized_key_location_change.ignore
|
||||
|
||||
For this script a `authorized_key_location_change.ignore` with a list of user names to ignore for the
|
||||
move
|
||||
|
||||
eg:
|
||||
|
||||
```ini
|
||||
foo_user
|
||||
bar_user
|
||||
```
|
||||
|
||||
## Options
|
||||
|
||||
### -g (go)
|
||||
|
||||
@@ -27,12 +27,24 @@ LOG="${BASE_FOLDER}/../log";
|
||||
# auth log file user;date from collect_login_data script
|
||||
AUTH_LOG="${BASE_FOLDER}/../auth-log/user_auth.log";
|
||||
|
||||
error=0;
|
||||
if [ $(whoami) != "root" ]; then
|
||||
echo "Script must be run as root user";
|
||||
exit;
|
||||
error=1;
|
||||
fi;
|
||||
if [ ! -d "${LOG}" ]; then
|
||||
echo "log folder ${LOG} not found";
|
||||
error=1;
|
||||
fi;
|
||||
if [ -z $(command -v curl) ]; then
|
||||
echo "Missing curl application, aborting";
|
||||
error=1;
|
||||
fi;
|
||||
if [ -z $(command -v jq) ]; then
|
||||
echo "Missing jq application, aborting";
|
||||
error=1;
|
||||
fi;
|
||||
if [ $error -eq 1 ]; then
|
||||
exit;
|
||||
fi;
|
||||
# option 1 in list
|
||||
@@ -45,18 +57,31 @@ case "${1,,}" in
|
||||
echo "{";
|
||||
;;
|
||||
csv)
|
||||
CSV_LINE="%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n";
|
||||
OUTPUT_TARGET="csv";
|
||||
echo "Hostname,Username,Main Group,SSH Group,Account Created Date,Account Age,Last Login Date,Last Login Age,Never Logged In,Login Source,Status";
|
||||
echo "Account ID,Region,Instance ID,Hostname,Username,Main Group,SSH Group,Account Created Date,Account Age,Last Login Date,Last Login Age,Never Logged In,Login Source,Status";
|
||||
;;
|
||||
*)
|
||||
OUTPUT_TARGET="text";
|
||||
;;
|
||||
esac;
|
||||
|
||||
# collect info via: curl http://169.254.169.254/latest/meta-data/
|
||||
instance_data=$(
|
||||
TOKEN=`curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` &&
|
||||
curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/dynamic/instance-identity/document
|
||||
)
|
||||
instance_id=$(echo "${instance_data}" | jq .instanceId)
|
||||
account_id=$(echo "${instance_data}" | jq .accountId)
|
||||
region=$(echo "${instance_data}" | jq .region)
|
||||
|
||||
if [ "${OUTPUT_TARGET}" = "text" ]; then
|
||||
LOG="${LOG}/check_ssh_user."$(date +"%F_%H%m%S")".log";
|
||||
exec &> >(tee -a "${LOG}");
|
||||
echo "[START] =============>";
|
||||
echo "AWS ID : ${account_id}";
|
||||
echo "Region : ${region}";
|
||||
echo "Instance ID : ${instance_id}";
|
||||
echo "Hostname : "$(hostname);
|
||||
echo "Run date : "$(date +"%F %T");
|
||||
echo "Max age last login : ${max_age_login} days";
|
||||
@@ -64,6 +89,9 @@ if [ "${OUTPUT_TARGET}" = "text" ]; then
|
||||
echo "Max age no login : ${max_age_create} days";
|
||||
elif [ "${OUTPUT_TARGET}" = "json" ]; then
|
||||
echo '"Info": {'
|
||||
echo '"AccountId": '${account_id}',';
|
||||
echo '"Region": '${region}',';
|
||||
echo '"InstanceId": '${instance_id}',';
|
||||
echo '"Hostname": "'$(hostname)'",';
|
||||
echo '"Date": "'$(date +"%F %T")'",';
|
||||
echo '"MaxAgeLogin": '${max_age_login}',';
|
||||
@@ -106,7 +134,7 @@ for ssh_group in ${ssh_groups[@]}; do
|
||||
echo "}";
|
||||
;;
|
||||
csv)
|
||||
printf "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n" "$(hostname)" "${username}" "" "${ssh_group}" "" "" "" "" "true" "${out_string}"
|
||||
printf "${CSV_LINE}" "${account_id}" "${region}" "${instance_id}" "$(hostname)" "${username}" "" "${ssh_group}" "" "" "" "" "true" "${out_string}"
|
||||
;;
|
||||
esac;
|
||||
continue;
|
||||
@@ -131,14 +159,25 @@ for ssh_group in ${ssh_groups[@]}; do
|
||||
# check user create time, if we have set it in comment
|
||||
user_create_date_string=$(cat /etc/passwd | grep "${username}:" | cut -d ":" -f 5);
|
||||
# if empty try last password set time
|
||||
if [ -z "${user_create_date_string}" ]; then
|
||||
if ! [[ "${user_create_date_string}" =~ ^\d{4}-\d{2}-\{2} ]]; then
|
||||
# user L 11/09/2020 0 99999 7 -1
|
||||
user_create_date_string=$(passwd -S ${username} | cut -d " " -f 3);
|
||||
fi;
|
||||
# last try is user home .bash_logout
|
||||
if [ -z "${user_create_date_string}" ]; then
|
||||
home_dir=$(cat /etc/passwd | grep "${username}:" | cut -d ":" -f 6)"/.bash_logout";
|
||||
user_create_date_string=$(stat -c %Z "${home_dir}");
|
||||
if ! [[ "${user_create_date_string}" =~ ^\d{4}-\d{2}-\{2} ]]; then
|
||||
# try logout or bash history
|
||||
home_dir_bl=$(cat /etc/passwd | grep "${username}:" | cut -d ":" -f 6)"/.bash_logout";
|
||||
home_dir_bh=$(cat /etc/passwd | grep "${username}:" | cut -d ":" -f 6)"/.bash_history";
|
||||
# check that this file exists
|
||||
if [ -f "${home_dir_bl}" ]; then
|
||||
user_create_date_string=$(stat -c %Z "${home_dir_bl}");
|
||||
elif [ -f "${home_dir_bh}" ]; then
|
||||
user_create_date_string=$(stat -c %Z "${home_dir_bh}");
|
||||
fi;
|
||||
fi;
|
||||
# still no date -> set empty
|
||||
if ! [[ "${user_create_date_string}" =~ ^\d{4}-\d{2}-\{2} ]]; then
|
||||
user_create_date_string="";
|
||||
fi;
|
||||
|
||||
# below only works if the user logged in, a lot of them are just file upload
|
||||
@@ -158,7 +197,7 @@ for ssh_group in ${ssh_groups[@]}; do
|
||||
found=$(grep "${username};" "${AUTH_LOG}");
|
||||
fi;
|
||||
# always pre work account dates if they exist, but output only if text
|
||||
if [ -z "${found}" ] && [ ! -z "${user_create_date_string}" ]; then
|
||||
if [ ! -z "${user_create_date_string}" ]; then
|
||||
user_create_date=$(echo "${user_create_date_string}" | date +"%s" -f -);
|
||||
# if all empty, we continue with only check if user has last login date
|
||||
# else get days since creation
|
||||
@@ -250,7 +289,7 @@ for ssh_group in ${ssh_groups[@]}; do
|
||||
echo "}";
|
||||
;;
|
||||
csv)
|
||||
printf "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n" "$(hostname)" "${username}" "${main_group}" "${ssh_group}" "${user_create_date_out}" "${account_age}" "${last_login_date}" "${last_login}" "${never_logged_in}" "${login_source}" "${out_string}"
|
||||
printf "${CSV_LINE}" "${account_id}" "${region}" "${instance_id}" "$(hostname)" "${username}" "${main_group}" "${ssh_group}" "${user_create_date_out}" "${account_age}" "${last_login_date}" "${last_login}" "${never_logged_in}" "${login_source}" "${out_string}"
|
||||
;;
|
||||
esac;
|
||||
done;
|
||||
|
||||
@@ -87,6 +87,13 @@ if [ ! -d "${HOME_FOLDER}" ]; then
|
||||
echo "Home folder location not found: ${HOME_FOLDER}";
|
||||
error=1;
|
||||
fi;
|
||||
# allow 10 to 39 length for password
|
||||
if [ ! -z "${PASSWORD_LENGTH}" ] && ! [[ "${PASSWORD_LENGTH}" =~ ^[13][0-9]$ ]]; then
|
||||
echo "Password length set error, can only be a value between 10 and 39";
|
||||
error=1;
|
||||
elif [ -z ${PASSWORD_LENGTH} ]; then
|
||||
PASSWORD_LENGTH=14;
|
||||
fi;
|
||||
# home dir error abort
|
||||
if [ $error -eq 1 ]; then
|
||||
exit;
|
||||
@@ -309,7 +316,11 @@ while read i; do
|
||||
# Note we only create a password if we need it
|
||||
# password + store pwgen 10 1 -1
|
||||
if [ -z "${_password}" ]; then
|
||||
password=$(printf "%s" $(pwgen 14 1));
|
||||
password=$(printf "%s" $(pwgen ${PASSWORD_LENGTH} 1));
|
||||
elif [ "${_password}" = "SET_NO_PASSWORD" ]; then
|
||||
# set empty
|
||||
echo "* No password set";
|
||||
password="";
|
||||
else
|
||||
echo "! Override password set";
|
||||
password=${_password};
|
||||
@@ -410,14 +421,24 @@ done;
|
||||
if [ ${INFO} -eq 1 ]; then
|
||||
exit;
|
||||
fi;
|
||||
# check if there are any files in the SSH_KEYGEN_FOLDER, else skip zip file creation and file move
|
||||
has_pem_files=0;
|
||||
if (shopt -s nullglob dotglob; f=("${SSH_KEYGEN_FOLDER}"*".pem"*); ((${#f[@]}))); then
|
||||
has_pem_files=1;
|
||||
fi;
|
||||
# zip everything and remove data in ssh key folder, delete output file with passwords
|
||||
if [ ${TEST} -eq 0 ]; then
|
||||
zip -r \
|
||||
"${ROOT_FOLDER}${output_zip_folder}${output_zip}" \
|
||||
"${input_file}" \
|
||||
"${output_file}" \
|
||||
"${SSH_KEYGEN_FOLDER}" \
|
||||
-x\*.gitignore;
|
||||
if [ "${has_pem_files}" -eq 1 ]; then
|
||||
zip -r \
|
||||
"${ROOT_FOLDER}${output_zip_folder}${output_zip}" \
|
||||
"${input_file}" \
|
||||
"${output_file}" \
|
||||
"${SSH_KEYGEN_FOLDER}" \
|
||||
-x\*.gitignore;
|
||||
echo "Download: ${ROOT_FOLDER}${output_zip_folder}${output_zip}";
|
||||
else
|
||||
echo "Skip ZIP file creation, no pem files";
|
||||
fi;
|
||||
else
|
||||
echo "zip -r \\"
|
||||
echo "${ROOT_FOLDER}${output_zip_folder}${output_zip} \\"
|
||||
@@ -425,15 +446,19 @@ else
|
||||
echo "${output_file} \\"
|
||||
echo "${SSH_KEYGEN_FOLDER} \\"
|
||||
echo "-x\*.gitignore;"
|
||||
echo "Download: ${ROOT_FOLDER}${output_zip_folder}${output_zip}";
|
||||
fi;
|
||||
echo "Download: ${ROOT_FOLDER}${output_zip_folder}${output_zip}";
|
||||
# cleam up user log file and ssh keys
|
||||
if [ ${TEST} -eq 0 ]; then
|
||||
# move pub to created folders
|
||||
mv "${ROOT_FOLDER}${SSH_KEYGEN_FOLDER}"*.pub "${ROOT_FOLDER}${SSH_KEYGEN_FOLDER_CREATED_PUB}";
|
||||
# delete the rest
|
||||
rm "${ROOT_FOLDER}${output_file}";
|
||||
rm "${ROOT_FOLDER}${SSH_KEYGEN_FOLDER}"*;
|
||||
if [ "${has_pem_files}" -eq 1 ]; then
|
||||
# move pub to created folders
|
||||
mv "${ROOT_FOLDER}${SSH_KEYGEN_FOLDER}"*.pub "${ROOT_FOLDER}${SSH_KEYGEN_FOLDER_CREATED_PUB}";
|
||||
# delete the rest
|
||||
rm "${ROOT_FOLDER}${output_file}";
|
||||
rm "${ROOT_FOLDER}${SSH_KEYGEN_FOLDER}"*;
|
||||
else
|
||||
echo "Skip pub file move and cleanup, no pem files";
|
||||
fi;
|
||||
else
|
||||
echo "$> mv ${ROOT_FOLDER}${SSH_KEYGEN_FOLDER}*.pub ${ROOT_FOLDER}${SSH_KEYGEN_FOLDER_CREATED_PUB};";
|
||||
echo "$> rm ${ROOT_FOLDER}${output_file}";
|
||||
|
||||
Reference in New Issue
Block a user