Преглед изворни кода

Beginning of tor outgoing proxy

Bob Mottram пре 10 година
родитељ
комит
b698c3d629
1 измењених фајлова са 113 додато и 0 уклоњено
  1. 113
    0
      install-freedombone.sh

+ 113
- 0
install-freedombone.sh Прегледај датотеку

@@ -105,6 +105,12 @@ CONFIGURATION_FILE="freedombone.cfg"
105 105
 
106 106
 SSH_PORT=2222
107 107
 
108
+# The static IP address of the system within the local network
109
+FIXED_IP_ADDRESS="192.168.1.60"
110
+
111
+# whether to route outgoing traffic through Tor
112
+ROUTE_THROUGH_TOR="no"
113
+
108 114
 # Why use Google as a time source?
109 115
 # The thinking here is that it's likely to be reliable and fast.
110 116
 # The ping doesn't reveal any information other than that the server
@@ -321,6 +327,12 @@ function argument_checks {
321 327
 
322 328
 function read_configuration {
323 329
   if [ -f $CONFIGURATION_FILE ]; then
330
+      if grep -q "FIXED_IP_ADDRESS" $CONFIGURATION_FILE; then
331
+          FIXED_IP_ADDRESS=$(grep "FIXED_IP_ADDRESS" $CONFIGURATION_FILE | awk -F '=' '{print $2}')
332
+      fi
333
+      if grep -q "ROUTE_THROUGH_TOR" $CONFIGURATION_FILE; then
334
+          ROUTE_THROUGH_TOR=$(grep "ROUTE_THROUGH_TOR" $CONFIGURATION_FILE | awk -F '=' '{print $2}')
335
+      fi
324 336
       if grep -q "WIKI_TITLE" $CONFIGURATION_FILE; then
325 337
           WIKI_TITLE=$(grep "WIKI_TITLE" $CONFIGURATION_FILE | awk -F '=' '{print $2}')
326 338
       fi
@@ -1680,6 +1692,8 @@ function configure_firewall {
1680 1692
   ip6tables -P INPUT ACCEPT
1681 1693
   iptables -F
1682 1694
   ip6tables -F
1695
+  iptables -t nat -F
1696
+  ip6tables -t nat -F
1683 1697
   iptables -X
1684 1698
   ip6tables -X
1685 1699
   iptables -P INPUT DROP
@@ -4944,6 +4958,104 @@ function intrusion_detection {
4944 4958
   echo 'intrusion_detection' >> $COMPLETION_FILE
4945 4959
 }
4946 4960
 
4961
+# see https://trac.torproject.org/projects/tor/wiki/doc/TransparentProxy
4962
+# Local Redirection and Anonymizing Middlebox
4963
+function route_outgoing_traffic_through_tor {
4964
+  if grep -Fxq "route_outgoing_traffic_through_tor" $COMPLETION_FILE; then
4965
+      return
4966
+  fi
4967
+  if [[ $ROUTE_THROUGH_TOR != "yes" ]]; then
4968
+      return
4969
+  fi
4970
+  apt-get -y --force-yes install tor
4971
+
4972
+  ### set variables
4973
+  # Destinations you don't want routed through Tor
4974
+  _non_tor="192.168.1.0/24 192.168.0.0/24"
4975
+
4976
+  # The UID that Tor runs as (varies from system to system)
4977
+  # TODO this changes every time tor is started, so won't work
4978
+  _tor_uid=$(ps -ef | grep /usr/bin/tor | grep -v grep | awk -F ' ' '{print $2}')
4979
+
4980
+  # Tor's TransPort
4981
+  _trans_port="9040"
4982
+
4983
+  # Your internal interface
4984
+  _int_if="eth0"
4985
+
4986
+  ### Set iptables *nat
4987
+  iptables -t nat -A OUTPUT -o lo -j RETURN
4988
+  iptables -t nat -A OUTPUT -m owner --uid-owner $_tor_uid -j RETURN
4989
+  iptables -t nat -A OUTPUT -p udp --dport 53 -j REDIRECT --to-ports 53
4990
+
4991
+  # Allow clearnet access for hosts in $_non_tor
4992
+  for _clearnet in $_non_tor; do
4993
+      iptables -t nat -A OUTPUT -d $_clearnet -j RETURN
4994
+      iptables -t nat -A PREROUTING -i $_int_if -d $_clearnet -j RETURN
4995
+  done
4996
+
4997
+  #redirect all other pre-routing and output to Tor
4998
+  iptables -t nat -A OUTPUT -p tcp --syn -j REDIRECT --to-ports $_trans_port
4999
+  iptables -t nat -A PREROUTING -i $_int_if -p udp --dport 53 -j REDIRECT --to-ports 53
5000
+  iptables -t nat -A PREROUTING -i $_int_if -p tcp --syn -j REDIRECT --to-ports $_trans_port
5001
+
5002
+  ### set iptables *filter
5003
+  iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
5004
+
5005
+  # Allow clearnet access for hosts in $_non_tor
5006
+  for _clearnet in $_non_tor 127.0.0.0/8; do
5007
+      iptables -A OUTPUT -d $_clearnet -j ACCEPT
5008
+  done
5009
+
5010
+  # Allow only Tor output
5011
+  iptables -A OUTPUT -m owner --uid-owner $_tor_uid -j ACCEPT
5012
+  iptables -A OUTPUT -j REJECT
5013
+
5014
+  save_firewall_settings
5015
+
5016
+  echo 'domain localdomain' > /etc/resolv.conf
5017
+  echo 'search localdomain' >> /etc/resolv.conf
5018
+  echo 'nameserver 127.0.0.1' >> /etc/resolv.conf
5019
+
5020
+  sed -i 's|VirtualAddrNetworkIPv4*|VirtualAddrNetworkIPv4 10.192.0.0/10|g' /etc/tor/torrc
5021
+  if ! grep -q "VirtualAddrNetworkIPv4" /etc/tor/torrc; then
5022
+      echo 'VirtualAddrNetworkIPv4 10.192.0.0/10' >> /etc/tor/torrc
5023
+  fi
5024
+
5025
+  sed -i 's|AutomapHostsOnResolve*|AutomapHostsOnResolve 1|g' /etc/tor/torrc
5026
+  if ! grep -q "AutomapHostsOnResolve" /etc/tor/torrc; then
5027
+      echo 'AutomapHostsOnResolve 1' >> /etc/tor/torrc
5028
+  fi
5029
+
5030
+  sed -i 's|TransPort*|TransPort 9040|g' /etc/tor/torrc
5031
+  if ! grep -q "TransPort" /etc/tor/torrc; then
5032
+      echo 'TransPort 9040' >> /etc/tor/torrc
5033
+  fi
5034
+
5035
+  if ! grep -q "TransListenAddress 127.0.0.1" /etc/tor/torrc; then
5036
+      echo 'TransListenAddress 127.0.0.1' >> /etc/tor/torrc
5037
+  fi
5038
+
5039
+  if ! grep -q "TransListenAddress $FIXED_IP_ADDRESS" /etc/tor/torrc; then
5040
+      echo "TransListenAddress $FIXED_IP_ADDRESS" >> /etc/tor/torrc
5041
+  fi
5042
+
5043
+  sed -i 's|DNSPort*|DNSPort 53|g' /etc/tor/torrc
5044
+  if ! grep -q "DNSPort" /etc/tor/torrc; then
5045
+      echo 'DNSPort 53' >> /etc/tor/torrc
5046
+  fi
5047
+
5048
+  if ! grep -q "DNSListenAddress 127.0.0.1" /etc/tor/torrc; then
5049
+      echo 'DNSListenAddress 127.0.0.1' >> /etc/tor/torrc
5050
+  fi
5051
+
5052
+  if ! grep -q "DNSListenAddress $FIXED_IP_ADDRESS" /etc/tor/torrc; then
5053
+      echo "DNSListenAddress $FIXED_IP_ADDRESS" >> /etc/tor/torrc
5054
+  fi
5055
+
5056
+  echo 'route_outgoing_traffic_through_tor' >> $COMPLETION_FILE
5057
+}
5058
+
4947 5059
 function install_final {
4948 5060
   if grep -Fxq "install_final" $COMPLETION_FILE; then
4949 5061
       return
@@ -4992,6 +5104,7 @@ search_for_attached_usb_drive
4992 5104
 regenerate_ssh_keys
4993 5105
 script_to_make_self_signed_certificates
4994 5106
 create_upgrade_script
5107
+route_outgoing_traffic_through_tor
4995 5108
 configure_email
4996 5109
 create_procmail
4997 5110
 #spam_filtering