From bd99c61fb2dc5a221f3bff687261fe7298edd8cb Mon Sep 17 00:00:00 2001 From: Bhikesh Khute Date: Wed, 10 Sep 2025 20:54:38 +0530 Subject: [PATCH 01/10] shell scripting project #1 --- day02/pam-main.sh | 153 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100755 day02/pam-main.sh diff --git a/day02/pam-main.sh b/day02/pam-main.sh new file mode 100755 index 0000000..3b384bd --- /dev/null +++ b/day02/pam-main.sh @@ -0,0 +1,153 @@ +#/bin/bash +#!/usr/bin/bash +# Date: 10/09/2025 +# This is script for user & group management in linux. +# Usage: pam-main.sh +# Author: Bhikesh Khute +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +WHITE='\033[1;37m' +PURPLE='\033[0;35m' +CYAN='\033[0;36m' +NC='\033[0m' +echo "------------------------------------------------------" +echo -e "=======\033[45m Welcome to Login Management System \033[0m=======" +echo "------------------------------------------------------" +while true; do +echo -e "${RED}1. Add user${NC}" +echo -e "${GREEN}2. Delete user${NC}" +echo -e "${PURPLE}3. Modify user - Add user to multiple groups${NC}" +echo -e "${CYAN}4. Modify user - Changing Login Shell${NC}" +echo -e "${YELLOW}5. Modify user - Disable Login${NC}" +echo -e "${GREEN}6. Modify user - Enable Login${NC}" +echo -e "${CYAN}7. Modify user - Add user to sudo group${NC}" +echo -e "${RED}8. Add group${NC}" +echo -e "${GREEN}9. Backup directories${NC}" +echo -e "${PURPLE}10. Exit${NC}" +read -p "Select the option: " choice +case $choice in + 1) + read -p "Enter first and last name of the user(Example - Ramesh Sippy): " fullname #Example - Ramesh Sippy + username=`echo $fullname | awk '{print tolower($1 $2)}'` #Output - rameshsippy + useradd -c "$fullname" -m -s /bin/bash $username > /dev/null #Redirects output to blackhole instead of screen + echo $username:R@nd0mDig!t | chpasswd #Adding Default Password; To be changed after login for security reasons. + passwd --expire $username > /dev/null #Password is expired so that on first login itself it can be changed. + echo "$username is successfully created in the system" + echo "-----------------------------------------------" + unset username + ;; + 2) + awk -F: '$3 >= 1000 {print $1}' /etc/passwd | nl -w1 -s". " #Filtering only normal users + read -p "Select user to be deleted: " choice + username=$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p") + deluser --remove-home -q $username + echo "$username is successfully deleted from the system" + echo "-------------------------------------------------" + unset username + unset choice + ;; + 3) + echo "------------------Normal Users List------------------" + awk -F: '$3 >= 1000 {print $1}' /etc/passwd | nl -w1 -s". " #Filtering only normal users + echo "------------------Group List-------------------------" + awk -F: '$3 >= 1000 {print $1}' /etc/group | nl -w1 -s". " + read -p "Select the existing user: " choice + read -p "Select the group name to be added: " gchoice + username=$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p") + grpname=$(awk -F: '$3 >= 1000 {print $1}' /etc/group | sed -n "${gchoice}p") + usermod -aG $grpname $username + echo "$username is successfully added to group $grpname" + echo "-------------------------------------------------" + unset grpname + unset username + unset choice + unset gchoice + ;; + 4) + echo "------------------Normal Users List------------------" + awk -F: '$3 >= 1000 {print $1}' /etc/passwd | nl -w1 -s". " #Filtering only normal users + read -p "Select the existing username: " choice + username=$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p") + curr_shell=$(getent passwd "$username" | cut -d: -f7) + if [ "$curr_shell" = "/bin/sh" ]; then + chsh -s /bin/bash "$username" + echo "$username's shell is successfully changed to bash" + echo "-------------------------------------------------" + elif [ "$curr_shell" = "/bin/bash" ]; then + chsh -s /bin/sh "$username" + echo "$username's shell is successfully changed to sh" + echo "-----------------------------------------------" + fi + unset username + unset curr_shell + unset choice + ;; + 5) + echo "------------------Normal Users List------------------" + awk -F: '$3 >= 1000 {print $1}' /etc/passwd | nl -w1 -s". " #Filtering only normal users + read -p "Select the existing username: " choice + username=$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p") + if getent passwd "$username" > /dev/null; then + echo "Changing $username shell to /usr/sbin/nologin" + chsh -s /usr/sbin/nologin "$username" + echo "$username's disabled to nologin" + echo "-------------------------------" + else + echo "User $username not found!" + fi + unset username + unset choice + ;; + 6) + echo "------------------Normal Users List------------------" + awk -F: '$3 >= 1000 {print $1}' /etc/passwd | nl -w1 -s". " #Filtering only normal users + read -p "Select the existing username: " choice + username=$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p") + if getent passwd "$username" > /dev/null; then + echo "Changing $username shell to /bin/bash" + chsh -s /bin/bash "$username" + echo "$username's shell is successfully activated to bash" + echo "---------------------------------------------------" + else + echo "User $username not found!" + fi + unset username + unset choice + ;; + 7) + echo "------------------Normal Users List------------------" + awk -F: '$3 >= 1000 {print $1}' /etc/passwd | nl -w1 -s". " #Filtering only normal users + read -p "Enter the existing username: " choice + username=$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p") + usermod -aG sudo $username + echo "$username is successfully added to sudo group. Enjoy super-privileges!" + echo "----------------------------------------------------------------------" + unset username + unset choice + ;; + 8) + read -p "Enter the group name to be added: " grpname + addgroup $grpname > /dev/null + echo "--------------------$grpname is added in the system.------------------" + echo " " + ;; + 9) + mkdir -p /opt/dir_backups + read -p "Select the username to intiate the backup: " choice + username=$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p") + read -p "Specify the path of the directory to be backuped up: " dirback + tar -cvzf /opt/dir_backups/$username.tar.gz $dirback > /dev/null + echo "Backup completed successfully" + echo "-----------------------------" + unset username + unset dirback + unset choice + ;; + 10) + break; + ;; + *) + echo "Invalid option! Exiting the program" +esac +done From 89b33c2d32701d73b8efa792c98e3554e8378bc9 Mon Sep 17 00:00:00 2001 From: Bhikesh Khute Date: Wed, 10 Sep 2025 21:57:27 +0530 Subject: [PATCH 02/10] Add pam-main.sh with recommended changes --- day02/pam-main.sh | 110 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 76 insertions(+), 34 deletions(-) diff --git a/day02/pam-main.sh b/day02/pam-main.sh index 3b384bd..44aed35 100755 --- a/day02/pam-main.sh +++ b/day02/pam-main.sh @@ -1,5 +1,4 @@ #/bin/bash -#!/usr/bin/bash # Date: 10/09/2025 # This is script for user & group management in linux. # Usage: pam-main.sh @@ -15,6 +14,10 @@ echo "------------------------------------------------------" echo -e "=======\033[45m Welcome to Login Management System \033[0m=======" echo "------------------------------------------------------" while true; do +if [[ $EUID -ne 0 ]]; then + echo "This script must be run as root (sudo)."; exit 1 +fi +while true; do echo -e "${RED}1. Add user${NC}" echo -e "${GREEN}2. Delete user${NC}" echo -e "${PURPLE}3. Modify user - Add user to multiple groups${NC}" @@ -28,21 +31,41 @@ echo -e "${PURPLE}10. Exit${NC}" read -p "Select the option: " choice case $choice in 1) - read -p "Enter first and last name of the user(Example - Ramesh Sippy): " fullname #Example - Ramesh Sippy - username=`echo $fullname | awk '{print tolower($1 $2)}'` #Output - rameshsippy - useradd -c "$fullname" -m -s /bin/bash $username > /dev/null #Redirects output to blackhole instead of screen - echo $username:R@nd0mDig!t | chpasswd #Adding Default Password; To be changed after login for security reasons. - passwd --expire $username > /dev/null #Password is expired so that on first login itself it can be changed. - echo "$username is successfully created in the system" + read -rp "Enter first and last name of the user(Example - Ramesh Sippy): " fullname #Example - Ramesh Sippy + username="$(awk '{print tolower($1 $2)}' <<<"$fullname")" + if id -u "$username" >/dev/null 2>&1; then + echo "User '$username' already exists."; echo "-----------------------------------------------"; unset username; break + fi + if useradd -c "$fullname" -m -s /bin/bash "$username" >/dev/null 2>&1; then + randpass="$(openssl rand -base64 14 2>/dev/null || tr -dc 'A-Za-z0-9!@#%+=' /dev/null + echo "$username created. Temporary password: $randpass (will be forced to change on first login)" + else + echo "Failed to create user '$username'." + fi echo "-----------------------------------------------" unset username + unset randpass ;; 2) - awk -F: '$3 >= 1000 {print $1}' /etc/passwd | nl -w1 -s". " #Filtering only normal users - read -p "Select user to be deleted: " choice - username=$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p") - deluser --remove-home -q $username - echo "$username is successfully deleted from the system" + awk -F: '$3 >= 1000 {print $1}' /etc/passwd | nl -w1 -s". " + read -r -p "Select user NUMBER to be deleted: " choice + username="$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p")" + if [[ -z "${username:-}" ]]; then echo "Invalid selection."; else + if [[ "$username" == "$USER" ]]; then echo "Refusing to delete the current user '$USER'."; else + read -r -p "Type the username '$username' to confirm deletion: " confirm + if [[ "$confirm" == "$username" ]]; then + if userdel -r "$username" >/dev/null 2>&1; then + echo "$username deleted." + else + echo "Failed to delete '$username'." + fi + else + echo "Confirmation mismatch. Aborting." + fi + fi + fi echo "-------------------------------------------------" unset username unset choice @@ -51,13 +74,16 @@ case $choice in echo "------------------Normal Users List------------------" awk -F: '$3 >= 1000 {print $1}' /etc/passwd | nl -w1 -s". " #Filtering only normal users echo "------------------Group List-------------------------" - awk -F: '$3 >= 1000 {print $1}' /etc/group | nl -w1 -s". " - read -p "Select the existing user: " choice - read -p "Select the group name to be added: " gchoice + getent group | awk -F: '{print $1}' | nl -w1 -s". " + read -p "Select the existing user NUMBER: " choice + read -p "Select the group NUMBER to be added: " gchoice username=$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p") grpname=$(awk -F: '$3 >= 1000 {print $1}' /etc/group | sed -n "${gchoice}p") - usermod -aG $grpname $username - echo "$username is successfully added to group $grpname" + if [[ -n "${username:-}" && -n "${grpname:-}" ]]; then + usermod -aG "$grpname" "$username" && echo "$username added to group $grpname" + else + echo "Invalid selection." + fi echo "-------------------------------------------------" unset grpname unset username @@ -89,20 +115,22 @@ case $choice in read -p "Select the existing username: " choice username=$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p") if getent passwd "$username" > /dev/null; then - echo "Changing $username shell to /usr/sbin/nologin" - chsh -s /usr/sbin/nologin "$username" - echo "$username's disabled to nologin" + nologin_path="$(command -v nologin || echo /sbin/nologin)" + echo "Changing $username shell to $nologin_path" + usermod -s "$nologin_path" "$username" + echo "$username's login disabled." echo "-------------------------------" else echo "User $username not found!" fi unset username unset choice + unset nologin_path ;; 6) echo "------------------Normal Users List------------------" awk -F: '$3 >= 1000 {print $1}' /etc/passwd | nl -w1 -s". " #Filtering only normal users - read -p "Select the existing username: " choice + read -p "Select the existing user: " choice username=$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p") if getent passwd "$username" > /dev/null; then echo "Changing $username shell to /bin/bash" @@ -120,34 +148,48 @@ case $choice in awk -F: '$3 >= 1000 {print $1}' /etc/passwd | nl -w1 -s". " #Filtering only normal users read -p "Enter the existing username: " choice username=$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p") - usermod -aG sudo $username - echo "$username is successfully added to sudo group. Enjoy super-privileges!" + admin_group="$(getent group sudo >/dev/null 2>&1 && echo sudo || echo wheel)" + usermod -aG "$admin_group" "$username" + echo "$username added to $admin_group group." echo "----------------------------------------------------------------------" unset username unset choice + unset admin_group ;; 8) - read -p "Enter the group name to be added: " grpname - addgroup $grpname > /dev/null - echo "--------------------$grpname is added in the system.------------------" - echo " " + read -r -p "Enter the group name to be added: " grpname + if getent group "$grpname" >/dev/null 2>&1; then + echo "Group '$grpname' already exists." + elif groupadd "$grpname" >/dev/null 2>&1; then + echo "--------------------$grpname added.------------------" + else + echo "Failed to add group '$grpname'." + fi ;; 9) - mkdir -p /opt/dir_backups - read -p "Select the username to intiate the backup: " choice - username=$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p") - read -p "Specify the path of the directory to be backuped up: " dirback - tar -cvzf /opt/dir_backups/$username.tar.gz $dirback > /dev/null - echo "Backup completed successfully" + mkdir -p /opt/dir_backups + awk -F: '$3 >= 1000 {print $1}' /etc/passwd | nl -w1 -s". " + read -r -p "Select the user NUMBER to associate with the backup: " choice + username="$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p")" + read -r -p "Specify the absolute path of the directory to be backed up: " dirback + if [[ -d "$dirback" ]]; then + ts="$(date +%F_%H-%M-%S)" + tar -czf "/opt/dir_backups/${username}_$ts.tar.gz" -C "$dirback" . >/dev/null 2>&1 && \ + echo "Backup completed successfully" || echo "Backup failed" + else + echo "Directory '$dirback' not found." + fi echo "-----------------------------" unset username unset dirback unset choice + unset ts ;; 10) break; ;; *) - echo "Invalid option! Exiting the program" + echo "Invalid option. Try again." + ;; esac done From 8a74275ea90a729fc8493c7bf49a611b3755540b Mon Sep 17 00:00:00 2001 From: Bhikesh Khute Date: Wed, 10 Sep 2025 22:15:08 +0530 Subject: [PATCH 03/10] Fixed while loop validation and safer delete guard in pam-main.sh --- day02/pam-main.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/day02/pam-main.sh b/day02/pam-main.sh index 44aed35..41fd73f 100755 --- a/day02/pam-main.sh +++ b/day02/pam-main.sh @@ -17,6 +17,7 @@ while true; do if [[ $EUID -ne 0 ]]; then echo "This script must be run as root (sudo)."; exit 1 fi +done while true; do echo -e "${RED}1. Add user${NC}" echo -e "${GREEN}2. Delete user${NC}" @@ -51,9 +52,11 @@ case $choice in 2) awk -F: '$3 >= 1000 {print $1}' /etc/passwd | nl -w1 -s". " read -r -p "Select user NUMBER to be deleted: " choice + [[ "$choice" =~ ^[0-9]+$ ]] || { echo "Invalid selection."; break; } username="$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p")" if [[ -z "${username:-}" ]]; then echo "Invalid selection."; else - if [[ "$username" == "$USER" ]]; then echo "Refusing to delete the current user '$USER'."; else + current="${SUDO_USER:-$USER}" + if [[ "$username" == "$current" ]]; then echo "Refusing to delete the current user '$current'."; else read -r -p "Type the username '$username' to confirm deletion: " confirm if [[ "$confirm" == "$username" ]]; then if userdel -r "$username" >/dev/null 2>&1; then @@ -69,6 +72,7 @@ case $choice in echo "-------------------------------------------------" unset username unset choice + unset current ;; 3) echo "------------------Normal Users List------------------" From 115dd6a16a5ef0ae0f2ca5183cbd55a0c48ea8b6 Mon Sep 17 00:00:00 2001 From: Bhikesh Khute Date: Wed, 10 Sep 2025 22:48:52 +0530 Subject: [PATCH 04/10] Fixed break to continue in pam-main.sh --- day02/pam-main.sh | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/day02/pam-main.sh b/day02/pam-main.sh index 41fd73f..22bec97 100755 --- a/day02/pam-main.sh +++ b/day02/pam-main.sh @@ -52,7 +52,7 @@ case $choice in 2) awk -F: '$3 >= 1000 {print $1}' /etc/passwd | nl -w1 -s". " read -r -p "Select user NUMBER to be deleted: " choice - [[ "$choice" =~ ^[0-9]+$ ]] || { echo "Invalid selection."; break; } + [[ "$choice" =~ ^[0-9]+$ ]] || { echo "Invalid selection."; continue; } username="$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p")" if [[ -z "${username:-}" ]]; then echo "Invalid selection."; else current="${SUDO_USER:-$USER}" @@ -134,12 +134,12 @@ case $choice in 6) echo "------------------Normal Users List------------------" awk -F: '$3 >= 1000 {print $1}' /etc/passwd | nl -w1 -s". " #Filtering only normal users - read -p "Select the existing user: " choice - username=$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p") - if getent passwd "$username" > /dev/null; then - echo "Changing $username shell to /bin/bash" - chsh -s /bin/bash "$username" - echo "$username's shell is successfully activated to bash" + read -rp "Select the existing user: " choice + [[ "$choice" =~ ^[0-9]+$ ]] || { echo "Invalid selection."; continue; } + username="$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p")" + if [[ -n "${username}" ]] && getent passwd "$username" > /dev/null; then + echo "Changing $username shell to /bin/bash" + usermod -s /bin/bash "$username" echo "---------------------------------------------------" else echo "User $username not found!" From 1c2c1e992e6914ace8d848ec520ea6b570b0078e Mon Sep 17 00:00:00 2001 From: Bhikesh Khute <35907619+norfluxX@users.noreply.github.com> Date: Thu, 11 Sep 2025 15:08:07 +0530 Subject: [PATCH 05/10] Update pam-main.sh --- day02/pam-main.sh | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/day02/pam-main.sh b/day02/pam-main.sh index 22bec97..eaf300b 100755 --- a/day02/pam-main.sh +++ b/day02/pam-main.sh @@ -97,18 +97,22 @@ case $choice in 4) echo "------------------Normal Users List------------------" awk -F: '$3 >= 1000 {print $1}' /etc/passwd | nl -w1 -s". " #Filtering only normal users - read -p "Select the existing username: " choice - username=$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p") - curr_shell=$(getent passwd "$username" | cut -d: -f7) - if [ "$curr_shell" = "/bin/sh" ]; then - chsh -s /bin/bash "$username" - echo "$username's shell is successfully changed to bash" - echo "-------------------------------------------------" - elif [ "$curr_shell" = "/bin/bash" ]; then - chsh -s /bin/sh "$username" - echo "$username's shell is successfully changed to sh" - echo "-----------------------------------------------" - fi + read -r -p "Select the existing user NUMBER: " choice + [[ "$choice" =~ ^[0-9]+$ ]] || { echo "Invalid selection."; continue; } + username="$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p")" + if [[ -n "${username}" ]]; then + curr_shell="$(getent passwd "$username" | cut -d: -f7)" + if [[ "$curr_shell" == "/bin/sh" ]]; then + usermod -s /bin/bash "$username" && echo "$username's shell changed to bash" + elif [[ "$curr_shell" == "/bin/bash" ]]; then + usermod -s /bin/sh "$username" && echo "$username's shell changed to sh" + else + echo "Unsupported current shell: $curr_shell" + fi + echo "-------------------------------------------------" + else + echo "Invalid selection." + fi unset username unset curr_shell unset choice From 373a869b9d3f5c08ebe0e4127ff48627fd53acca Mon Sep 17 00:00:00 2001 From: Bhikesh Khute <35907619+norfluxX@users.noreply.github.com> Date: Thu, 11 Sep 2025 15:16:09 +0530 Subject: [PATCH 06/10] Update pam-main.sh --- day02/pam-main.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/day02/pam-main.sh b/day02/pam-main.sh index eaf300b..a8e749d 100755 --- a/day02/pam-main.sh +++ b/day02/pam-main.sh @@ -80,10 +80,12 @@ case $choice in echo "------------------Group List-------------------------" getent group | awk -F: '{print $1}' | nl -w1 -s". " read -p "Select the existing user NUMBER: " choice + [[ "$choice" =~ ^[0-9]+$ ]] || { echo "Invalid selection."; continue; } read -p "Select the group NUMBER to be added: " gchoice + [[ "$choice" =~ ^[0-9]+$ ]] || { echo "Invalid selection."; continue; } username=$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p") grpname=$(awk -F: '$3 >= 1000 {print $1}' /etc/group | sed -n "${gchoice}p") - if [[ -n "${username:-}" && -n "${grpname:-}" ]]; then + if [[ -n "${username}" && -n "${grpname}" ]]; then usermod -aG "$grpname" "$username" && echo "$username added to group $grpname" else echo "Invalid selection." From c6aaf8c9a331d91f6a8dc63a61361fdd1e2f5ad6 Mon Sep 17 00:00:00 2001 From: Bhikesh Khute Date: Thu, 11 Sep 2025 18:56:16 +0530 Subject: [PATCH 07/10] modified pam-main.sh with working enchancements --- .../simonjames_2025-09-11_13-24-21.tar.gz | Bin 0 -> 4680 bytes day02/pam-main.sh | 9 ++++----- 2 files changed, 4 insertions(+), 5 deletions(-) create mode 100644 day02/dir_backups/simonjames_2025-09-11_13-24-21.tar.gz diff --git a/day02/dir_backups/simonjames_2025-09-11_13-24-21.tar.gz b/day02/dir_backups/simonjames_2025-09-11_13-24-21.tar.gz new file mode 100644 index 0000000000000000000000000000000000000000..a2533748f2da60eb5b0755405113e2f48b37f763 GIT binary patch literal 4680 zcmV-O61VLiiwFP!000001MFORciT9U&%dKj!FBxFaTJ?3DcR4nqx(Lo%j#tFcz_}$ zillhxn)H75Es&Be+e!CyzeLW?s9s``K%uHoz^_n%NL`a}emGG;B2VE@TIA`&&po}N zc}k#oPT)o0qZyXw-VpqcH|@=v&<|vfAl~@q+_9(f9Qj}8`=k2*r*utfruUfq;P7tqyb@2Uk-~ko{?wjM!(9E;(7by53sPAXO zx9LB8{QK3_LUFQwIN~O)N`Iy_+OvEGS>vv+t>^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|6L)}dWJe+L$)v>d%8KK?Z26E-Xc|miTid{`3AXOf z4-S9;-H1dy9o_)>0gjE3N_$LkGuy!Yh zfpZZb_{8=2hZ!!SA7S_w$1-ABzT`R3GVzdmcY*(YMNZ73$2|J)GNG6WeT6BG`uq4= z%H@ca?BOIY9tGri(EG$&SjX}3K>cS(5L#Ucq~WSNJn`w9U7P5RzkvvdqD$dMg-{Sg z*UXKDLn~b0EW()KB3f`9h(Xa}jR$@=$~z}pv6E%1c(E^%&HIfL)qKzoAlEd)NIDF}~P#!gFgeRlyJgT?&H z#{ul>D)c4@mdQImc5j?AKLF6`=Wx&2m5x)eoX~8l; zf5Dl+6(TM^UR?O(|9te{|4Ln@KYdKX6-nOu_+eo<6%egJIBBKQ_*MopMd=OdF^gC?;;CC#)&7x zP{&~gYsCE}bR;RG5h-yiH^ zxEvl!M{_%bK<&Tay}ZW#`!8ab!a57Wb$oOE(6xQ%5BwLo=&=*g{pW*DJ@w1K-Y|g| z)CYZgJAX&=S6CAJ#rL(h&i@v)`~5ebqJFO5e_^T9^Pk6{!|{(F0O45XJ3j#SSDyc5 zPv?I}p-z>uTA4~&Er%1`8V*~+C|NJb+r}iVRz{mpQ~<~|JOd<;~)$IU;y2{3WHXysjGqz zA(jA5NVi&>5b9qfO1-_OaQogJURyEy6-2i>^_SITX@68yf0lLTC-df>n<>gU8)vUW z*zh_Q= 1000 {print $1}' /etc/passwd | nl -w1 -s". " #Filtering only normal users echo "------------------Group List-------------------------" - getent group | awk -F: '{print $1}' | nl -w1 -s". " + getent group | awk -F: '$3 >= 1000 {print $1}' | nl -w1 -s". " read -p "Select the existing user NUMBER: " choice read -p "Select the group NUMBER to be added: " gchoice username=$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p") From 70d4b4cb287871c3627a7e9290ae15d8e9ccdfcb Mon Sep 17 00:00:00 2001 From: Bhikesh Khute Date: Thu, 11 Sep 2025 18:57:36 +0530 Subject: [PATCH 08/10] modified pam-main.sh with working enchancements --- .../simonjames_2025-09-11_13-24-21.tar.gz | Bin 4680 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 day02/dir_backups/simonjames_2025-09-11_13-24-21.tar.gz diff --git a/day02/dir_backups/simonjames_2025-09-11_13-24-21.tar.gz b/day02/dir_backups/simonjames_2025-09-11_13-24-21.tar.gz deleted file mode 100644 index a2533748f2da60eb5b0755405113e2f48b37f763..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4680 zcmV-O61VLiiwFP!000001MFORciT9U&%dKj!FBxFaTJ?3DcR4nqx(Lo%j#tFcz_}$ zillhxn)H75Es&Be+e!CyzeLW?s9s``K%uHoz^_n%NL`a}emGG;B2VE@TIA`&&po}N zc}k#oPT)o0qZyXw-VpqcH|@=v&<|vfAl~@q+_9(f9Qj}8`=k2*r*utfruUfq;P7tqyb@2Uk-~ko{?wjM!(9E;(7by53sPAXO zx9LB8{QK3_LUFQwIN~O)N`Iy_+OvEGS>vv+t>^W0r&1?t*^X|=WMCH>sbsd4D!bzb zZD^TAhF8+V`sAL*bPAJmuye!$OjfPF8Q#Ti>GP$CRj4b*qep;uv>?RDWg<;DtCt6dUtBC@g zQLKA!Y6{{E1=b`jP3nW1Lw_B^a#EYXjzo_+V znwKnhGegNm!Ms=!Nh9A+6l;Pn_GM2_$(xqzv81#h7gV92?y$9q)!aC_B-fDoZm2fx zbj4m&DrAA?d$Y+2|F1>YY-?jq)R$=Qj+i_We!CIiVDaitP8MG>TimSpJ3w&u)< zF9gh3ByhyvK_eEhYos|gx{dxuU9ke_%4@s#r>A)Rr)i3#f2#8zMm(MW9D@$WKR*9~ z{EvS9{Es{B|BgbltxazGg;8ayTb!_Qbas`!pZndUP;d6w1(!8QzLxEa4Y9z?y^Wsk zW%4<3;!mrJ*g#o%$~4@~eTOznqh-P>(@kxm&ReN~ez}`Xe_j+$`@f?Q8Zr0EvdGO?ZxHC(gw{J+#~Bvdt7>Z84~Oz{$a_6+-K;xG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ZgJAX&=S6CAJ#rL(h&i@v)`~5ebqJFO5e_^T9^Pk6{!|{(F0O45XJ3j#SSDyc5 zPv?I}p-z>uTA4~&Er%1`8V*~+C|NJb+r}iVRz{mpQ~<~|JOd<;~)$IU;y2{3WHXysjGqz zA(jA5NVi&>5b9qfO1-_OaQogJURyEy6-2i>^_SITX@68yf0lLTC-df>n<>gU8)vUW z*zh_Q Date: Thu, 11 Sep 2025 19:12:27 +0530 Subject: [PATCH 09/10] modified pam-main.sh with backup validation --- day02/pam-main.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/day02/pam-main.sh b/day02/pam-main.sh index 547cf85..fe42598 100755 --- a/day02/pam-main.sh +++ b/day02/pam-main.sh @@ -179,14 +179,16 @@ case $choice in mkdir -p /opt/dir_backups awk -F: '$3 >= 1000 {print $1}' /etc/passwd | nl -w1 -s". " read -r -p "Select the user NUMBER to associate with the backup: " choice + [[ "$choice" =~ ^[0-9]+$ ]] || { echo "Invalid selection."; continue; } username="$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p")" + [[ -n "${username:-}" ]] || { echo "Invalid selection."; continue; } read -r -p "Specify the absolute path of the directory to be backed up: " dirback - if [[ -d "$dirback" ]]; then + if [[ "$dirback" = /* && -d "$dirback" && -r "$dirback" ]]; then ts="$(date +%F_%H-%M-%S)" tar -czf "/opt/dir_backups/${username}_$ts.tar.gz" -C "$dirback" . >/dev/null 2>&1 && \ echo "Backup completed successfully" || echo "Backup failed" else - echo "Directory '$dirback' not found." + echo "Invalid directory: '$dirback'. Provide an existing absolute path." fi echo "-----------------------------" unset username From 0edfd1764ce524882e63eaaab6346d88a91d5e50 Mon Sep 17 00:00:00 2001 From: Bhikesh Khute Date: Thu, 11 Sep 2025 19:47:08 +0530 Subject: [PATCH 10/10] modified pam-main.sh to make nologin more stealth --- day02/pam-main.sh | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/day02/pam-main.sh b/day02/pam-main.sh index fe42598..74ab126 100755 --- a/day02/pam-main.sh +++ b/day02/pam-main.sh @@ -40,6 +40,7 @@ case $choice in randpass="$(openssl rand -base64 14 2>/dev/null || tr -dc 'A-Za-z0-9!@#%+=' /dev/null + chage -M 0 "$username" >/dev/null 2>&1 echo "$username created. Temporary password: $randpass (will be forced to change on first login)" else echo "Failed to create user '$username'." @@ -99,21 +100,21 @@ case $choice in echo "------------------Normal Users List------------------" awk -F: '$3 >= 1000 {print $1}' /etc/passwd | nl -w1 -s". " #Filtering only normal users read -r -p "Select the existing user NUMBER: " choice - [[ "$choice" =~ ^[0-9]+$ ]] || { echo "Invalid selection."; continue; } - username="$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p")" - if [[ -n "${username}" ]]; then - curr_shell="$(getent passwd "$username" | cut -d: -f7)" - if [[ "$curr_shell" == "/bin/sh" ]]; then - usermod -s /bin/bash "$username" && echo "$username's shell changed to bash" - elif [[ "$curr_shell" == "/bin/bash" ]]; then - usermod -s /bin/sh "$username" && echo "$username's shell changed to sh" - else - echo "Unsupported current shell: $curr_shell" - fi - echo "-------------------------------------------------" - else - echo "Invalid selection." - fi + [[ "$choice" =~ ^[0-9]+$ ]] || { echo "Invalid selection."; continue; } + username="$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p")" + if [[ -n "${username}" ]]; then + curr_shell="$(getent passwd "$username" | cut -d: -f7)" + if [[ "$curr_shell" == "/bin/sh" ]]; then + usermod -s /bin/bash "$username" && echo "$username's shell changed to bash" + elif [[ "$curr_shell" == "/bin/bash" ]]; then + usermod -s /bin/sh "$username" && echo "$username's shell changed to sh" + else + echo "Unsupported current shell: $curr_shell" + fi + echo "-------------------------------------------------" + else + echo "Invalid selection." + fi unset username unset curr_shell unset choice @@ -121,9 +122,10 @@ case $choice in 5) echo "------------------Normal Users List------------------" awk -F: '$3 >= 1000 {print $1}' /etc/passwd | nl -w1 -s". " #Filtering only normal users - read -p "Select the existing username: " choice + read -r -p "Select the existing user NUMBER: " choice + [[ "$choice" =~ ^[0-9]+$ ]] || { echo "Invalid selection."; continue; } username=$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p") - if getent passwd "$username" > /dev/null; then + if [[ -n "${username:-}" ]] && getent passwd "$username" > /dev/null; then nologin_path="$(command -v nologin || echo /sbin/nologin)" echo "Changing $username shell to $nologin_path" usermod -s "$nologin_path" "$username"