Skip to content

Instantly share code, notes, and snippets.

@MohamedElashri
Last active August 23, 2024 19:22
Show Gist options
  • Save MohamedElashri/14f14a688d310d3a7575bfc6262ea1f2 to your computer and use it in GitHub Desktop.
Save MohamedElashri/14f14a688d310d3a7575bfc6262ea1f2 to your computer and use it in GitHub Desktop.
Clean the system from any crowdsec precence.
#!/bin/bash
# Color definitions
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_color() {
printf "${1}%s${NC}\n" "${2}"
}
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to check if running as root
check_root() {
if [ "$(id -u)" -ne 0 ]; then
print_color "$RED" "Please run as root"
exit 1
fi
}
# Function to check for CrowdSec services
check_services() {
print_color "$BLUE" "Checking for CrowdSec services..."
services=$(systemctl list-units --type=service | grep -i crowdsec | awk '{print $1}')
if [ -z "$services" ]; then
print_color "$GREEN" "No CrowdSec services found."
else
print_color "$YELLOW" "Found CrowdSec services:"
echo "$services"
fi
}
# Function to check for CrowdSec packages
check_packages() {
print_color "$BLUE" "Checking for CrowdSec packages..."
if command_exists dpkg; then
packages=$(dpkg -l | grep -i crowdsec | awk '{print $2}')
elif command_exists rpm; then
packages=$(rpm -qa | grep -i crowdsec)
else
print_color "$RED" "Unable to determine package manager."
return
fi
if [ -z "$packages" ]; then
print_color "$GREEN" "No CrowdSec packages found."
else
print_color "$YELLOW" "Found CrowdSec packages:"
echo "$packages"
fi
}
# Function to disable services
disable_services() {
print_color "$BLUE" "Disabling CrowdSec services..."
if [ -z "$services" ]; then
print_color "$YELLOW" "No CrowdSec services to disable."
else
echo "$services" | while read -r service; do
systemctl stop "$service" 2>/dev/null
systemctl disable "$service" 2>/dev/null
print_color "$GREEN" "Disabled $service"
done
fi
}
# Function to remove packages
remove_packages() {
print_color "$BLUE" "Removing CrowdSec packages..."
if [ -z "$packages" ]; then
print_color "$YELLOW" "No CrowdSec packages to remove."
else
if command_exists apt; then
apt remove --purge -y $packages
apt autoremove -y
elif command_exists yum; then
yum remove -y $packages
else
print_color "$RED" "Unable to determine package manager."
return
fi
print_color "$GREEN" "Removed CrowdSec packages."
fi
}
# Function to remove APT repository entries
remove_apt_repos() {
print_color "$BLUE" "Removing CrowdSec entries from APT repositories..."
if command_exists apt; then
grep -irl "crowdsec" /etc/apt/sources.list /etc/apt/sources.list.d/ | while read -r repo_file; do
sed -i '/crowdsec/d' "$repo_file"
print_color "$GREEN" "Removed CrowdSec entry from $repo_file"
done
apt update
print_color "$GREEN" "APT repositories updated."
else
print_color "$RED" "APT not found on this system."
fi
}
# Function to remove configuration files
remove_config() {
print_color "$BLUE" "Removing CrowdSec configuration files..."
rm -rf /etc/crowdsec 2>/dev/null
rm -rf /var/lib/crowdsec 2>/dev/null
print_color "$GREEN" "Removed CrowdSec configuration files."
}
# Main function
main() {
check_root
if [ "$1" = "-n" ]; then
print_color "$RED" "Nuking all CrowdSec components..."
check_services
check_packages
disable_services
remove_packages
remove_apt_repos
remove_config
print_color "$GREEN" "CrowdSec has been completely removed from your system."
exit 0
fi
check_services
check_packages
if [ -z "$services" ] && [ -z "$packages" ]; then
print_color "$GREEN" "No CrowdSec components found on your system."
exit 0
fi
print_color "$YELLOW" "What would you like to do?"
print_color "$BLUE" "1) Disable CrowdSec services"
print_color "$BLUE" "2) Remove CrowdSec packages"
print_color "$BLUE" "3) Remove everything (services, packages, configuration, and APT repos)"
print_color "$BLUE" "4) Keep everything and exit"
read -r -p "Enter your choice (1-4): " choice
case $choice in
1) disable_services ;;
2) remove_packages ;;
3)
disable_services
remove_packages
remove_apt_repos
remove_config
;;
4) print_color "$GREEN" "Exiting without changes." ; exit 0 ;;
*) print_color "$RED" "Invalid choice. Exiting." ; exit 1 ;;
esac
print_color "$GREEN" "Operation completed successfully."
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment