Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ca4616c5ee | ||
|
|
251b0bf981 | ||
|
|
6daccfe57c |
@@ -4,7 +4,7 @@
|
||||
# if user login >30days, remoe user from sshallow group and write log
|
||||
|
||||
# base folder
|
||||
BASE_FOLDER=$(dirname $(readlink -f $0))"/";
|
||||
BASE_FOLDER=$(dirname "$(readlink -f "$0")")"/";
|
||||
# which groups holds the ssh allowed login users (outside of admin users)
|
||||
ssh_groups=('sshforward' 'sshallow' 'sshreject');
|
||||
ssh_reject_group='sshreject';
|
||||
@@ -68,7 +68,7 @@ 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"` &&
|
||||
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)
|
||||
@@ -76,14 +76,14 @@ 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";
|
||||
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 "Hostname : $(hostname)";
|
||||
echo "Run date : $(date +"%F %T")";
|
||||
echo "Max age last login : ${max_age_login} days";
|
||||
echo "Warn age last login: ${warn_age_login} days";
|
||||
echo "Max age no login : ${max_age_create} days";
|
||||
@@ -197,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 "${user_create_date_string}" ]; then
|
||||
if [ -n "${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
|
||||
@@ -205,24 +205,24 @@ for ssh_group in ${ssh_groups[@]}; do
|
||||
account_age=$(awk '{printf("%.0f\n",($1-$2)/$3)}' <<<"${now} ${user_create_date} ${day}");
|
||||
user_create_date_out=$(echo "${user_create_date_string}" | date +"%F" -f -);
|
||||
fi;
|
||||
if [ ! -z "${found}" ]; then
|
||||
if [ -n "${found}" ]; then
|
||||
last_login_date_string=$(grep "${username};" "${AUTH_LOG}" | cut -d ";" -f 2);
|
||||
last_login_date=$(echo "${last_login_date}" | date +"%s" -f -);
|
||||
last_login_date=$(echo "${last_login_date_string}" | date +"%s" -f -);
|
||||
last_login=$(awk '{printf("%.0f\n",($1-$2)/$3)}' <<<"${now} ${last_login_date} ${day}");
|
||||
if [ ${last_login} -gt ${max_age_login} ]; then
|
||||
out_string="[!] last ssh log in ${last_login} days ago";
|
||||
out_string="[!] Last ssh log in ${last_login} days ago";
|
||||
if [ "${ssh_group}" != "${ssh_reject_group}" ]; then
|
||||
lock_user=1;
|
||||
fi;
|
||||
elif [ ${last_login} -gt ${warn_age_login} ]; then
|
||||
out_string="OK [last ssh login ${last_login} days ago]";
|
||||
out_string="WARN [last ssh login ${last_login} days ago]";
|
||||
else
|
||||
out_string="OK [ssh]";
|
||||
out_string="OK [ssh, ${last_login} days ago]";
|
||||
fi;
|
||||
login_source="ssh";
|
||||
# rewrite to Y-M-D, aka
|
||||
last_login_date="${last_login_date_string}"
|
||||
elif [ ! -z "${last_login_string##*$search*}" ]; then
|
||||
elif [ -n "${last_login_string##*$search*}" ]; then
|
||||
# if we have "** Never logged in**" the user never logged in
|
||||
# find \w{3} \w{3} [\s\d]{2} \d{2}:\d{2}:\d{2} \+\d{4} \d{4}
|
||||
# awk '{for(i=4;i<=NF;++i)printf $i FS}'
|
||||
@@ -230,25 +230,25 @@ for ssh_group in ${ssh_groups[@]}; do
|
||||
# date -d "Wed Nov 2 09:40:35 +0900 2022" +%s
|
||||
last_login=$(awk '{printf("%.0f\n",($1-$2)/$3)}' <<<"${now} ${last_login_date} ${day}");
|
||||
if [ ${last_login} -gt ${max_age_login} ]; then
|
||||
out_string="[!] last terminal log in ${last_login} days ago";
|
||||
out_string="[!] Last terminal log in ${last_login} days ago";
|
||||
if [ "${ssh_group}" != "${ssh_reject_group}" ]; then
|
||||
lock_user=1;
|
||||
fi;
|
||||
elif [ ${last_login} -gt ${warn_age_login} ]; then
|
||||
out_string="OK [last terminal login ${last_login} days ago]";
|
||||
out_string="WARN [last terminal login ${last_login} days ago]";
|
||||
else
|
||||
out_string="OK [lastlog]";
|
||||
out_string="OK [lastlog, ${last_login} days ago]";
|
||||
fi;
|
||||
login_source="lastlog";
|
||||
last_login_date=$(echo "${last_login_string}" | awk '{for(i=4;i<=NF;++i)printf $i FS}' | date +"%F %T" -f -)
|
||||
elif [ ! -z "${user_create_date}" ]; then
|
||||
elif [ -n "${user_create_date}" ]; then
|
||||
if [ ${account_age} -gt ${max_age_create} ]; then
|
||||
out_string="[!] Never logged in: account created ${account_age} days ago";
|
||||
if [ "${ssh_group}" != "${ssh_reject_group}" ]; then
|
||||
lock_user=1;
|
||||
fi;
|
||||
else
|
||||
out_string="OK [Never logged in]";
|
||||
out_string="OK [Never logged in, created ${account_age} days ago]";
|
||||
fi;
|
||||
never_logged_in="true";
|
||||
else
|
||||
@@ -295,13 +295,13 @@ for ssh_group in ${ssh_groups[@]}; do
|
||||
done;
|
||||
done;
|
||||
if [ "${OUTPUT_TARGET}" = "text" ]; then
|
||||
if [ ! -z "${lock_accounts}" ]; then
|
||||
if [ -n "${lock_accounts}" ]; then
|
||||
echo "--------------------->"
|
||||
echo "% Run script below to move users to reject ssh group";
|
||||
echo "";
|
||||
echo "bin/lock_user.sh ${lock_accounts}";
|
||||
fi;
|
||||
if [ ! -z "${unlock_accounts}" ]; then
|
||||
if [ -n "${unlock_accounts}" ]; then
|
||||
echo "--------------------->"
|
||||
echo "% Run script below to move users to allow or forward ssh group";
|
||||
echo "";
|
||||
|
||||
Reference in New Issue
Block a user