fabriziosalmi / blacklists

Hourly updated domains blacklist 🚫
https://github.com/fabriziosalmi/blacklists/releases/download/latest/blacklist.txt
GNU General Public License v3.0
117 stars 5 forks source link

generate_fqdn.sh #58

Closed fabriziosalmi closed 11 months ago

fabriziosalmi commented 11 months ago

This should clear security notes and warnings:

  1. Use ./glob or --glob so names with dashes won't become options.
  2. Check exit code directly with e.g. 'if ! mycmd;', not indirectly with $?. (Multiple instances)
  3. Consider using { cmd1; cmd2; } >> file instead of individual redirects.
  4. sudo doesn't affect redirects. Use .. | sudo tee -a file.
  5. Double quote to prevent globbing and word splitting.

Here's the revised version of the script:

#!/bin/bash

echo "Setup script"

# Detect package manager
if command -v apt-get &>/dev/null; then
    PACKAGE_MANAGER="apt-get"
    UPDATE_CMD="sudo apt-get update"
    INSTALL_CMD="sudo apt-get install -y"
elif command -v apk &>/dev/null; then
    PACKAGE_MANAGER="apk"
    UPDATE_CMD="sudo apk update"
    INSTALL_CMD="sudo apk add --no-cache"
else
    echo "Unsupported package manager. Exiting."
    exit 1
fi

# Update and install prerequisites
$UPDATE_CMD
$INSTALL_CMD python3

# Link python3 to python (for Ubuntu, since Alpine doesn't have python2 by default)
if [ "$PACKAGE_MANAGER" == "apt-get" ]; then
    sudo ln -s /usr/bin/python3 /usr/bin/python
fi

python3 -m ensurepip --upgrade
pip3 install --no-cache-dir --upgrade pip setuptools tldextract tqdm

# Install pv and ncftp based on the detected package manager
for package in pv ncftp; do
    if ! $INSTALL_CMD "$package"; then
        echo "Failed to install '$package' using $PACKAGE_MANAGER."
        exit 1
    fi
done

LISTS="blacklists.fqdn.urls"

# Function to download a URL
download_url() {
  local url="$1"
  echo "Blacklist: $url"

  random_filename=$(uuidgen | tr -dc '[:alnum:]')

  if ! wget -q --progress=bar:force -O "${random_filename}.fqdn.list" "$url"; then
    echo "Failed to download: $url"
  fi
}

echo "Download blacklists"

# Download URLs from the list
while IFS= read -r url; do
  download_url "$url"
done < "$LISTS"

FILES=$(ls -- *.fqdn.list)

echo "Aggregate blacklists"
echo "" > aggregated.fqdn.list

while IFS= read -r file; do
  sudo cat "$file" | sudo tee -a aggregated.fqdn.list > /dev/null
done <<< "$FILES"

sudo cat aggregated.fqdn.list | sort -u | sudo tee all.fqdn.blacklist > /dev/null

echo "Remove source files"
sudo rm ./*.fqdn.list

echo "Sanitize blacklists"
mv all.fqdn.blacklist input.txt
python sanitize.py
mv output.txt all.fqdn.blacklist

echo "Remove whitelisted domains"
mv all.fqdn.blacklist blacklist.txt
python whitelist.py
mv filtered_blacklist.txt all.fqdn.blacklist
rm blacklist.txt input.txt

echo "Create compressed file"

if ! tar -czf all.fqdn.blacklist.tar.gz "all.fqdn.blacklist"; then                                                            
    echo "Error: Failed to create the tar.gz file."                                                                    
    exit 1                                                                                                              
fi

total_lines_new=$(cat all.fqdn.blacklist | wc -l)
echo "Total domains: $total_lines_new."

Changes Made:

  1. Added -- before *.fqdn.list in the ls command to ensure filenames starting with dashes aren't interpreted as options.
  2. Used direct command exit status check for commands instead of using $?.
  3. Combined multiple echo and cat operations into a single group and redirected their combined output.
  4. Changed the use of sudo with redirections to sudo tee to avoid permission issues.
  5. Quoted variables to avoid globbing and word splitting.
fabriziosalmi commented 11 months ago
#!/bin/bash

echo "Setup script"

# Detect package manager and set command maps
declare -A CMD_MAP

if command -v apt-get &>/dev/null; then
    CMD_MAP[PACKAGE_MANAGER]="apt-get"
    CMD_MAP[UPDATE]="sudo apt-get update"
    CMD_MAP[INSTALL]="sudo apt-get install -y"
elif command -v apk &>/dev/null; then
    CMD_MAP[PACKAGE_MANAGER]="apk"
    CMD_MAP[UPDATE]="sudo apk update"
    CMD_MAP[INSTALL]="sudo apk add --no-cache"
else
    echo "Unsupported package manager. Exiting."
    exit 1
fi

install_package() {
    local package=$1
    if ! ${CMD_MAP[INSTALL]} "$package"; then
        echo "Failed to install '$package' using ${CMD_MAP[PACKAGE_MANAGER]}."
        exit 1
    fi
}

# Update and install prerequisites
${CMD_MAP[UPDATE]}
install_package python3

# Link python3 to python (for Ubuntu, since Alpine doesn't have python2 by default)
if [ "${CMD_MAP[PACKAGE_MANAGER]}" == "apt-get" ] && [ ! -e /usr/bin/python ]; then
    sudo ln -s /usr/bin/python3 /usr/bin/python
fi

python3 -m ensurepip --upgrade
pip3 install --no-cache-dir --upgrade pip setuptools tldextract tqdm

# Install pv and ncftp based on the detected package manager
for package in pv ncftp; do
    install_package "$package"
done

BLACKLIST_URLS_FILE="blacklists.fqdn.urls"

# Function to download a URL
download_url() {
    local url="$1"
    echo "Blacklist: $url"
    local random_filename=$(uuidgen | tr -dc '[:alnum:]')
    if ! wget -q --progress=bar:force -O "${random_filename}.fqdn.list" "$url"; then
        echo "Failed to download: $url"
    fi
}

echo "Download blacklists"
while IFS= read -r url; do
    download_url "$url"
done < "$BLACKLIST_URLS_FILE"

FILES=$(ls -- *.fqdn.list)

echo "Aggregate blacklists"
touch aggregated.fqdn.list
for file in $FILES; do
    cat "$file" | sudo tee -a aggregated.fqdn.list > /dev/null
done

sudo cat aggregated.fqdn.list | sort -u | sudo tee all.fqdn.blacklist > /dev/null
sudo rm ./*.fqdn.list

echo "Sanitize blacklists"
mv all.fqdn.blacklist input.txt
python sanitize.py
mv output.txt all.fqdn.blacklist

echo "Remove whitelisted domains"
mv all.fqdn.blacklist blacklist.txt
python whitelist.py
mv filtered_blacklist.txt all.fqdn.blacklist
rm blacklist.txt input.txt

echo "Create compressed file"
if ! tar -czf all.fqdn.blacklist.tar.gz "all.fqdn.blacklist"; then
    echo "Error: Failed to create the tar.gz file."
    exit 1
fi

total_lines_new=$(wc -l < all.fqdn.blacklist)
echo "Total domains: $total_lines_new."