SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2013-04-13 19:37:51

Ragnarok
#! Member
Registered: 2011-11-29
Posts: 69

[Script] Generate an IP blocklist for P2P

I created a simple script today that generates an IP blocklist using the blocklists provided from iblocklist.com
This is useful when using BitTorrent because it prevents you from connecting to some known "bad" IPs if your client supports it.
I believe you can also use this with iptables, though it probably requires some modification.

Features:
Checks if the commands used in the script exist
Backups up the last blocklist you generated before generating a new blocklist
Merges all blocklists into one large blocklist
Easy to add/remove blocklists
Generated blocklist is in the .p2p format
Beautifies the blocklist after it's generated (this option can be disabled through a variable that acts as a pseudo boolean)

Notes:
I suggest changing the blocklist and tmpfldr paths to whatever you prefer before running the script.
Everything is stored inside functions because it's easier for me/you/everyone to debug and play with the script.
I attempted to look into implementing error handling, then realized that bash and error handling simply don't go together. It wouldn't be worth the effort for such a simple script.

FAQ:
Q: What is an IP blocklist?
A: An IP blocklist is a list of IP addresses that you don't want to connect to that can be used with P2P software, though not limited to P2P software

Q: How do I use the blocklist for P2P?
A: Check your BitTorrent client's documentation. The blocklist generated using this script is in the .p2p format

blocklistgen.sh: [Dropbox]blocklistgen.sh
Feedback is welcome. I don't have any intention/motivation to add new features, but I don't mind small, quick changes or fixes.
Feedback is more than welcome on the syntax. This is my first bash script that's more than a few lines. (likely my last)

#!/bin/bash
#======================================================================
# Title: blocklistgen.sh
# Description: Script for downloading and generating an IP blocklist
# Author: Ragnarok (http://crunchbang.org/forums/profile.php?id=13069)
# Date: 2013-04-13
# Version: 0.1
# Usage: $ bash blocklistgen.sh
# Dependencies: bash, coreutils, gzip, wget
#======================================================================


# Save path for the blocklist
blocklist="./blocklist.p2p"
# Directory for storing temporary lists
tmpfldr="/tmp"

# Beautify the blocklist after it is generated
beautify=true

# Disabled. This doesn't work as expected for some reason
#check_for_paths() {
#  if [[ ! -d "$tmpfldr" ]] && [[ ! -r "$tmpfldr" ]] && [[ ! -w "$tmpfldr" ]]; then
#    echo -e "Please make sure " "$tmpfldr" " exists and you have read/write access\nExiting"
#    exit 1
#  fi
#}

# http://wiki.bash-hackers.org/scripting/style
check_for_depends() {
  my_needed_commands="cat gunzip mv rm sed sort wget"

  missing_counter=0
  for needed_command in $my_needed_commands; do
    if ! hash "$needed_command" >/dev/null 2>&1; then
      printf "Command not found in PATH: %s\n" "$needed_command" >&2
      ((missing_counter++))
    fi
  done

  if ((missing_counter > 0)); then
    printf "Minimum %d commands are missing in PATH, aborting\n" "$missing_counter" >&2
    exit 1
  fi
}

backup_blocklist() {
  # Check for and backup blocklist
  echo "Checking for blocklist..."
  if [[ -f "$blocklist" ]]; then
    echo "Backing up blocklist and overwriting old backup if exists..."
    mv -f $blocklist ${blocklist}.old
  fi
}

downloads_lists() {
  # Download blocklists
  # This is where you add/remove blocklists
  # Downloaded lists must follow the following naming format: bl-[zero or more characters].gz
  echo "Downloading lists..."
  wget -q http://list.iblocklist.com/?list=bcoepfyewziejvcqyhqo -O $tmpfldr/bl-iana-reserved.gz
  wget -q http://list.iblocklist.com/?list=bt_ads -O $tmpfldr/bl-ads.gz
  wget -q http://list.iblocklist.com/?list=bt_bogon -O $tmpfldr/bl-bogon.gz
  wget -q http://list.iblocklist.com/?list=bt_dshield -O $tmpfldr/bl-dshield.gz
  wget -q http://list.iblocklist.com/?list=bt_hijacked -O $tmpfldr/bl-hijacked.gz
  wget -q http://list.iblocklist.com/?list=bt_level1 -O $tmpfldr/bl-level1.gz
  wget -q http://list.iblocklist.com/?list=bt_level2 -O $tmpfldr/bl-level2.gz
  wget -q http://list.iblocklist.com/?list=bt_microsoft -O $tmpfldr/bl-microsoft.gz
  wget -q http://list.iblocklist.com/?list=bt_spyware -O $tmpfldr/bl-spyware.gz
  wget -q http://list.iblocklist.com/?list=bt_templist -O $tmpfldr/bl-badpeers.gz
  wget -q http://list.iblocklist.com/?list=ijfqtofzixtwayqovmxn -O $tmpfldr/bl-primary-threats.gz
  wget -q http://list.iblocklist.com/?list=pwqnlynprfgtjbgqoizj -O $tmpfldr/bl-iana-multicast.gz
  echo "Download complete"
}

merge_lists() {
  # Merge blocklists
  echo "Merging lists..."
  cat ${tmpfldr}/bl-*.gz > ${blocklist}.gz
}

decompress_blocklist() {
  # Decompress the gzip archive
  if [[ -f "${blocklist}.gz" ]]; then
    echo "Decompressing..."
    gunzip ${blocklist}.gz
    echo "Blocklist successfully generated"
  else
    echo -e "Unable to find ${blocklist}.gz\nExiting"
    remove_temp
    exit 1
  fi
}

beautify_blocklist () {
  # Cleanup the blocklist
  # This will remove comments, empty lines and sort the list alphabetically
  if $beautify; then
    echo -e "Beautification started\nRemoving comments and blank lines..."
    sed -i -e '/^\#/d' -e '/^$/d' $blocklist
    echo "Sorting alphabetically..."
    sort $blocklist > ${tmpfldr}/blocklist.p2p.tmp && mv -f ${tmpfldr}/blocklist.p2p.tmp $blocklist
    echo "Beautification complete"
  fi
}

remove_temp() {
  # Remove temporary blocklists
  echo "Removing temporary files..."
  rm -f ${tmpfldr}/bl-*.gz
}

check_for_depends
#check_for_paths
backup_blocklist
downloads_lists
merge_lists
decompress_blocklist
remove_temp
beautify_blocklist

echo "Done!"

exit 0

Last edited by Ragnarok (2013-04-13 19:39:44)

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.
Server: acrobat

Debian Logo