Advertisement
Guest User

anonym

a guest
Mar 4th, 2015
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.14 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # BackBox Script for Anonymous Internet Navigation
  4. #
  5. # This script is intended to set up your BackBox machine to guarantee
  6. # anonymity through Tor. Additionally, the script takes further steps to
  7. # guarantee prevantion of data leakage by killing dangerous processes,
  8. # changing MAC address and IP information and so on.
  9. #
  10. # Author: Raffaele Forte <[email protected]>
  11. # Version: 1.2
  12.  
  13. # List, separated by spaces, of destinations that you do not want to be
  14. # routed through Tor
  15. NON_TOR="192.168.0.0/16 172.16.0.0/12"
  16.  
  17. # The UID as which Tor runs
  18. TOR_UID="debian-tor"
  19.  
  20. # Tor TransPort
  21. TRANS_PORT="9040"
  22.  
  23. # List, separated by spaces, of process names that should be killed
  24. TO_KILL="chrome dropbox firefox pidgin skype thunderbird xchat"
  25.  
  26. # List, separated by spaces, of BleachBit cleaners
  27. BLEACHBIT_CLEANERS="bash.history system.cache system.clipboard system.custom system.recent_documents system.rotated_logs system.tmp system.trash"
  28.  
  29. # Overwrite files to hide contents
  30. OVERWRITE="true"
  31.  
  32. # The default local hostname
  33. REAL_HOSTNAME="backbox"
  34.  
  35. # Include default options, if any
  36. if [ -f /etc/default/backbox-anonymous ] ; then
  37.     . /etc/default/backbox-anonymous
  38. fi
  39.  
  40. warning() {
  41.     echo "\n[!] WARNING! It's a simple script that avoid the most common system data"
  42.     echo "    leaks. Your coumputer behaviour is the key to guarantee you a strong"
  43.     echo "    privacy protection and a good anonimate."
  44.    
  45.     echo "\n[i] Please edit /etc/default/backbox-anonymous with your custom values."
  46. }
  47.  
  48. # General-purpose Yes/No prompt function
  49. ask() {
  50.     while true; do
  51.         if [ "${2:-}" = "Y" ]; then
  52.             prompt="Y/n"
  53.             default=Y
  54.         elif [ "${2:-}" = "N" ]; then
  55.             prompt="y/N"
  56.             default=N
  57.         else
  58.             prompt="y/n"
  59.             default=
  60.         fi
  61.  
  62.         # Ask the question
  63.         echo
  64.         read -p "$1 [$prompt] > " REPLY
  65.  
  66.         # Default?
  67.         if [ -z "$REPLY" ]; then
  68.             REPLY=$default
  69.         fi
  70.  
  71.         # Check if the reply is valid
  72.         case "$REPLY" in
  73.             Y*|y*) return 0 ;;
  74.             N*|n*) return 1 ;;
  75.         esac
  76.     done
  77. }
  78.  
  79. # Make sure that only root can run this script
  80. check_root() {
  81.     if [ $(id -u) -ne 0 ]; then
  82.         echo "\n[!] This script must run as root\n" >&2
  83.         exit 1
  84.     fi
  85. }
  86.  
  87. # Kill processes at startup
  88. kill_process() {
  89.     if [ "$TO_KILL" != "" ]; then
  90.         killall -q $TO_KILL
  91.         echo " * Killed processes to prevent leaks"
  92.     fi
  93. }
  94.  
  95. # Release DHCP address
  96. clean_dhcp() {
  97.     dhclient -r
  98.     rm -f /var/lib/dhcp/dhclient*
  99.     echo " * DHCP address released"
  100. }
  101.  
  102. # Change the local hostname
  103. change_hostname() {
  104.    
  105.     echo
  106.  
  107.     CURRENT_HOSTNAME=$(hostname)
  108.  
  109.     clean_dhcp
  110.  
  111.     RANDOM_HOSTNAME=$(shuf -n 1 /etc/dictionaries-common/words | sed -r 's/[^a-zA-Z]//g' | awk '{print tolower($0)}')
  112.  
  113.     NEW_HOSTNAME=${1:-$RANDOM_HOSTNAME}
  114.  
  115.     echo $NEW_HOSTNAME > /etc/hostname
  116.     sed -i 's/127.0.1.1.*/127.0.1.1\t'$NEW_HOSTNAME'/g' /etc/hosts
  117.  
  118.     echo -n " * Service "
  119.     service hostname start 2>/dev/null || echo "hostname already started"
  120.  
  121.     if [ -f "$HOME/.Xauthority" ] ; then
  122.         su $SUDO_USER -c "xauth -n list | grep -v $CURRENT_HOSTNAME | cut -f1 -d\ | xargs -i xauth remove {}"
  123.         su $SUDO_USER -c "xauth add $(xauth -n list | tail -1 | sed 's/^.*\//'$NEW_HOSTNAME'\//g')"
  124.         echo " * X authority file updated"
  125.     fi
  126.    
  127.     avahi-daemon --kill
  128.  
  129.     echo " * Hostname changed to $NEW_HOSTNAME"
  130. }
  131.  
  132. # Change the MAC address for network interfaces
  133. change_mac() {
  134.    
  135.     VAR=0
  136.    
  137.     while [ $VAR -eq 0 ]; do
  138.         echo -n "Select network interfaces ["
  139.         echo -n $(ifconfig -a | grep Ethernet | awk '{print $1}')
  140.         read -p "] > " IFACE
  141.        
  142.         ifconfig -a | grep Ethernet | awk '{print $1}' | grep -q -x "$IFACE"
  143.        
  144.         if [ $? -ne 1 ]; then
  145.             VAR=1
  146.         fi
  147.     done
  148.  
  149.     if [ "$1" = "permanent" ]; then
  150.         NEW_MAC=$(macchanger -p $IFACE | tail -n 1 | sed 's/  //g')
  151.         echo "\n * $NEW_MAC"
  152.     else
  153.         NEW_MAC=$(macchanger -A $IFACE | tail -n 1 | sed 's/  //g')
  154.         echo "\n * $NEW_MAC"
  155.     fi
  156. }
  157.  
  158. # Check Tor configs
  159. check_configs() {
  160.  
  161.     grep -q -x 'RUN_DAEMON="yes"' /etc/default/tor
  162.     if [ $? -ne 0 ]; then
  163.         echo "\n[!] Please add the following to your '/etc/default/tor' and restart the service:\n"
  164.         echo ' RUN_DAEMON="yes"\n'
  165.         exit 1
  166.     fi
  167.  
  168.     grep -q -x 'VirtualAddrNetwork 10.192.0.0/10' /etc/tor/torrc
  169.     VAR1=$?
  170.  
  171.     grep -q -x 'TransPort 9040' /etc/tor/torrc
  172.     VAR2=$?
  173.  
  174.     grep -q -x 'DNSPort 53' /etc/tor/torrc
  175.     VAR3=$?
  176.  
  177.     grep -q -x 'AutomapHostsOnResolve 1' /etc/tor/torrc
  178.     VAR4=$?
  179.  
  180.     if [ $VAR1 -ne 0 ] || [ $VAR2 -ne 0 ] || [ $VAR3 -ne 0 ] || [ $VAR4 -ne 0 ]; then
  181.         echo "\n[!] Please add the following to your '/etc/tor/torrc' and restart service:\n"
  182.         echo ' VirtualAddrNetwork 10.192.0.0/10'
  183.         echo ' TransPort 9040'
  184.         echo ' DNSPort 53'
  185.         echo ' AutomapHostsOnResolve 1\n'
  186.         exit 1
  187.     fi
  188. }
  189.  
  190. iptables_flush() {
  191.     iptables -F
  192.     iptables -t nat -F
  193.     echo " * Deleted all iptables rules"
  194. }
  195.  
  196. # BackBox implementation of Transparently Routing Traffic Through Tor
  197. # https://trac.torproject.org/projects/tor/wiki/doc/TransparentProxy
  198. redirect_to_tor() {
  199.    
  200.     echo
  201.  
  202.     if [ ! -e /var/run/tor/tor.pid ]; then
  203.         echo "\n[!] Tor is not running! Quitting...\n"
  204.         exit 1
  205.     fi
  206.  
  207.     if ! [ -f /etc/network/iptables.rules ]; then
  208.         iptables-save > /etc/network/iptables.rules
  209.         echo " * Saved iptables rules"
  210.     fi
  211.  
  212.     iptables_flush
  213.  
  214.     echo -n " * Service "
  215.     service resolvconf stop 2>/dev/null || echo "resolvconf already stopped"
  216.  
  217.     echo 'nameserver 127.0.0.1' > /etc/resolv.conf
  218.     echo " * Modified resolv.conf to use Tor"
  219.  
  220.     iptables -t nat -A OUTPUT -m owner --uid-owner $TOR_UID -j RETURN
  221.     iptables -t nat -A OUTPUT -p udp --dport 53 -j REDIRECT --to-ports 53
  222.  
  223.     for NET in $NON_TOR 127.0.0.0/9 127.128.0.0/10; do
  224.         iptables -t nat -A OUTPUT -d $NET -j RETURN
  225.     done
  226.  
  227.     iptables -t nat -A OUTPUT -p tcp --syn -j REDIRECT --to-ports $TRANS_PORT
  228.     iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  229.  
  230.     for NET in $NON_TOR 127.0.0.0/8; do
  231.         iptables -A OUTPUT -d $NET -j ACCEPT
  232.     done
  233.  
  234.     iptables -A OUTPUT -m owner --uid-owner $TOR_UID -j ACCEPT
  235.     iptables -A OUTPUT -j REJECT
  236. }
  237.  
  238. # BleachBit cleaners deletes unnecessary files to preserve privacy
  239. do_bleachbit() {
  240.     if [ "$OVERWRITE" = "true" ] ; then
  241.         echo -n "\n * Deleting and overwriting unnecessary files... "
  242.         bleachbit -o -c $BLEACHBIT_CLEANERS >/dev/null
  243.     else
  244.         echo -n "\n * Deleting unnecessary files... "
  245.         bleachbit -c $BLEACHBIT_CLEANERS >/dev/null
  246.     fi
  247.  
  248.     bleachbit -o -c $BLEACHBIT_CLEANERS >/dev/null
  249.     echo "Done!"
  250. }
  251.  
  252. do_start() {
  253.     check_configs
  254.     check_root
  255.    
  256.     warning
  257.  
  258.     echo "\n[i] Starting anonymous mode\n"
  259.  
  260.     echo -n " * Service "
  261.     service network-manager stop 2>/dev/null || echo " network-manager already stopped"
  262.  
  263.     kill_process
  264.    
  265.     if [ "$(virt-what)" != "" ]; then
  266.         echo " * Unable to change MAC address in a Virtual Machine"
  267.     else
  268.         if ask "Do you want to change the MAC address?" Y; then
  269.             change_mac
  270.         fi
  271.     fi
  272.    
  273.     if ask "Do you want to change the local hostname?" Y; then
  274.         read -p "Type it or press Enter for a random one > " CHOICE
  275.  
  276.         if [ "$CHOICE" = "" ]; then
  277.             change_hostname
  278.         else
  279.             change_hostname $CHOICE
  280.         fi
  281.     fi
  282.  
  283.     if ask "Do you want to transparently routing traffic through Tor?" Y; then
  284.         redirect_to_tor
  285.     else
  286.         echo
  287.     fi
  288.  
  289.     echo -n " * Service "
  290.     service network-manager start 2>/dev/null || echo "network-manager already started"
  291.     service tor restart
  292.     echo
  293. }
  294.  
  295. do_stop() {
  296.  
  297.     check_root
  298.  
  299.     echo "\n[i] Stopping anonymous mode\n"
  300.  
  301.     echo -n " * Service "
  302.     service network-manager stop 2>/dev/null || echo " network-manager already stopped"
  303.  
  304.     iptables_flush
  305.  
  306.     if [ -f /etc/network/iptables.rules ]; then
  307.         iptables-restore < /etc/network/iptables.rules
  308.         rm /etc/network/iptables.rules
  309.         echo " * Restored iptables rules"
  310.     fi
  311.  
  312.     echo -n " * Service "
  313.     service resolvconf start 2>/dev/null || echo "resolvconf already started"
  314.  
  315.     kill_process
  316.    
  317.     if [ "$(virt-what)" != "" ]; then
  318.         echo " * Unable to change MAC address in a Virtual Machine"
  319.     else
  320.         if ask "Do you want to change the MAC address?" Y; then
  321.             change_mac permanent
  322.         fi
  323.     fi
  324.    
  325.     if ask "Do you want to change the local hostname?" Y; then
  326.         read -p "Type it or press Enter to restore default [$REAL_HOSTNAME] > " CHOICE
  327.  
  328.         if [ "$CHOICE" = "" ]; then
  329.             change_hostname $REAL_HOSTNAME
  330.         else
  331.             change_hostname $CHOICE
  332.         fi
  333.     else
  334.         echo
  335.     fi
  336.    
  337.     echo -n " * Service "
  338.     service network-manager start 2>/dev/null || echo "network-manager already started"
  339.     service tor restart
  340.  
  341.     if [ "$DISPLAY" ]; then
  342.         if ask "Delete unnecessary files to preserve your privacy?" Y; then
  343.             do_bleachbit
  344.         fi
  345.     fi
  346.  
  347.     echo
  348. }
  349.  
  350. do_status() {
  351.  
  352.     echo "\n[i] Showing anonymous status\n"
  353.  
  354.     ifconfig -a | grep "encap:Ethernet" | awk '{print " * " $1, $5}'
  355.  
  356.     CURRENT_HOSTNAME=$(hostname)
  357.     echo " * Hostname $CURRENT_HOSTNAME"
  358.    
  359.     HTML=$(curl -s https://check.torproject.org/?lang=en_US)
  360.     IP=$(echo $HTML | egrep -m1 -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
  361.  
  362.     echo $HTML | grep -q "Congratulations. This browser is configured to use Tor."
  363.  
  364.     if [ $? -ne 0 ]; then
  365.         echo " * IP $IP"
  366.         echo " * Tor OFF\n"
  367.         exit 3
  368.     else
  369.         echo " * IP $IP"
  370.         echo " * Tor ON\n"
  371.     fi
  372. }
  373.  
  374. case "$1" in
  375.     start)
  376.         do_start
  377.     ;;
  378.     stop)
  379.         do_stop
  380.     ;;
  381.     status)
  382.         do_status
  383.     ;;
  384.     *)
  385.         echo "Usage: $0 {start|stop|status}" >&2
  386.         exit 3
  387.     ;;
  388. esac
  389.  
  390. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement