Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
694f04313c | ||
|
|
50e28c7cfd | ||
|
|
65b7a6ad43 |
@@ -35,6 +35,14 @@ if [ ! -d "${LOG}" ]; then
|
||||
echo "log folder ${LOG} not found";
|
||||
exit;
|
||||
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;
|
||||
# option 1 in list
|
||||
case "${1,,}" in
|
||||
text)
|
||||
@@ -45,18 +53,28 @@ 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://instance-data/latest/meta-data/
|
||||
instance_id=$(curl -s http://instance-data/latest/meta-data/instance-id)
|
||||
account_id=$(curl -s http://instance-data/latest/meta-data/identity-credentials/ec2/info/ | jq -r .AccountId)
|
||||
region=$(curl -s http://instance-data/latest/meta-data/placement/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 +82,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 +127,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 +152,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 +190,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 +282,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;
|
||||
|
||||
Reference in New Issue
Block a user