toriptables2/toriptables2.py

105 lines
3.4 KiB
Python
Raw Normal View History

2015-06-17 21:42:03 +00:00
#! /usr/bin/env python
2015-12-22 02:14:35 +00:00
# Written by Rupe version 2
2015-06-17 21:42:03 +00:00
"""
Tor Iptables script is an anonymizer
that sets up iptables and tor to route all services
and traffic including DNS through the tor network.
"""
from __future__ import print_function
from commands import getoutput
2015-12-28 00:28:18 +00:00
from subprocess import call, check_call
2015-06-17 21:42:03 +00:00
from os.path import isfile
from os import devnull
2015-12-22 02:14:35 +00:00
from sys import stdout, stderr
2015-12-19 04:27:05 +00:00
from atexit import register
2015-06-17 21:42:03 +00:00
from argparse import ArgumentParser
class TorIptables(object):
2015-12-19 04:27:05 +00:00
def __init__(self):
self.tor_config_file = '/etc/tor/torrc'
self.torrc = '''
2015-06-17 21:42:03 +00:00
VirtualAddrNetwork 10.0.0.0/10
AutomapHostsOnResolve 1
TransPort 9040
DNSPort 53
'''
2015-12-19 04:27:05 +00:00
self.non_tor_net = ["192.168.0.0/16", "172.16.0.0/12"]
self.non_tor = ["127.0.0.0/9", "127.128.0.0/10", "127.0.0.0/8"]
self.tor_uid = getoutput("id -ur debian-tor") # Tor user uid
self.trans_port = "9040" # Tor port
2015-06-17 21:42:03 +00:00
2015-12-19 04:27:05 +00:00
def flush_iptables_rules(self):
2015-12-19 05:18:22 +00:00
call(["iptables", "-F"])
2015-12-19 04:27:05 +00:00
call(["iptables", "-t", "nat", "-F"])
2015-06-17 21:42:03 +00:00
2015-12-19 04:27:05 +00:00
def load_iptables_rules(self):
self.flush_iptables_rules()
self.non_tor.extend(self.non_tor_net)
2015-06-17 21:42:03 +00:00
2015-12-19 04:27:05 +00:00
@register
def restart_tor():
2016-01-04 02:36:53 +00:00
import sys
sys.tracebacklimit = 0
2015-12-19 04:27:05 +00:00
fnull = open(devnull, 'w')
2015-12-28 00:28:18 +00:00
tor_restart = check_call(["service", "tor", "restart"],
2015-12-28 00:28:52 +00:00
stdout=fnull, stderr=fnull)
2015-12-28 00:28:18 +00:00
if tor_restart is 0:
print(" {0}".format("[\033[92m+\033[0m] Anonymizer \033[92mON\033[0m"))
2015-06-17 21:42:03 +00:00
2015-12-19 04:27:05 +00:00
call(["iptables", "-t", "nat", "-A", "OUTPUT", "-m", "owner", "--uid-owner",
"%s" % self.tor_uid, "-j", "RETURN"])
call(["iptables", "-t", "nat", "-A", "OUTPUT", "-p", "udp", "--dport", "53",
"-j", "REDIRECT", "--to-ports", "53"])
2015-06-17 21:42:03 +00:00
2015-12-21 23:18:40 +00:00
for net in self.non_tor:
2015-12-28 00:28:18 +00:00
call(["iptables", "-t", "nat", "-A", "OUTPUT", "-d", "%s" % net, "-j",
"RETURN"])
2015-06-17 21:42:03 +00:00
2015-12-19 04:27:05 +00:00
call(["iptables", "-t", "nat", "-A", "OUTPUT", "-p", "tcp", "--syn", "-j",
"REDIRECT", "--to-ports", "%s" % self.trans_port])
2015-06-17 21:42:03 +00:00
2015-12-19 04:27:05 +00:00
call(["iptables", "-A", "OUTPUT", "-m", "state", "--state",
"ESTABLISHED,RELATED", "-j", "ACCEPT"])
2015-06-17 21:42:03 +00:00
2015-12-21 23:31:00 +00:00
for net in self.non_tor:
2015-12-21 23:18:40 +00:00
call(["iptables", "-A", "OUTPUT", "-d", "%s" % net, "-j", "ACCEPT"])
2015-06-17 21:42:03 +00:00
2015-12-19 04:27:05 +00:00
call(["iptables", "-A", "OUTPUT", "-m", "owner", "--uid-owner", "%s" %
self.tor_uid, "-j", "ACCEPT"])
call(["iptables", "-A", "OUTPUT", "-j", "REJECT"])
2015-06-17 21:42:03 +00:00
if __name__ == '__main__':
2015-12-19 04:27:05 +00:00
parser = ArgumentParser(
2016-01-04 02:36:53 +00:00
description=
'Tor Iptables script for loading and unloading iptables rules')
parser.add_argument('-l',
'--load',
2015-12-22 05:18:04 +00:00
action='store_true',
2015-12-19 04:27:05 +00:00
help='This option will load tor iptables rules')
2016-01-04 02:36:53 +00:00
parser.add_argument('-f',
'--flush',
2015-12-19 04:27:05 +00:00
action='store_true',
help='This option flushes the iptables rules to default')
args = parser.parse_args()
try:
load_tables = TorIptables()
if isfile(load_tables.tor_config_file):
if not 'VirtualAddrNetwork' in open(load_tables.tor_config_file).read():
with open(load_tables.tor_config_file, 'a+') as torrconf:
torrconf.write(load_tables.torrc)
if args.load:
load_tables.load_iptables_rules()
elif args.flush:
load_tables.flush_iptables_rules()
2015-12-22 02:58:24 +00:00
print(" {0}".format("[\033[93m!\033[0m] Anonymizer \033[91mOFF\033[0m"))
2015-12-19 04:27:05 +00:00
else:
parser.print_help()
except Exception as err:
2016-01-04 03:05:46 +00:00
print(" [!] Run as super user: %s" % err[1])