################################################################################
# Example 1                                                              pf.conf
#
# Two interfaces -- redirecting local and remote traffic into Tor
#
# Locally and remotely generated DNS queries are redirected into the DNS proxy
# listening on 127.0.0.1:5353. Locally and remotely initiated TCP connections
# are redirected into the transparent proxy listening on 127.0.0.1:1112.
#
# SSH connections are allowed from the internal network. All other packets
# destined for the host, except DNS, are dropped.
#
# All outgoing traffic, except traffic generated by the _tor user, is blocked.
# A line for allowing outgoing traffic into the internal network has been
# commented out.
#
# VirtualAddrNetwork should be set to 10.192.0.0/10 in torrc.
#
# Suggested procedure:
#
# ifconfig lo1 create up 127.0.0.2
# trans-proxy-tor -b lo:1112 -s 127.0.0.1:9050 -c /var/empty \
#   -u nobody:nogroup -p /var/run/trans-proxy-tor.pid
# dns-proxy-tor -b lo:5353 -t 127.0.0.1:9051 -c /var/empty \
#   -u nobody:nogroup -p /var/run/dns-proxy-tor.pid
################################################################################

int_if = "fxp0"
ext_if = "fxp1"

trans_proxy = 1112
dns_proxy   = 5353

scrub in

# redirect locally generated traffic into tor
rdr pass on lo1 inet proto tcp all -> 127.0.0.1 port $trans_proxy

# redirect traffic from the internal network into tor
rdr pass on $int_if inet proto tcp to !($int_if:network) \
  -> 127.0.0.1 port $trans_proxy

# redirect dns traffic from both into the dns proxy
rdr pass on { lo1 $int_if } inet proto udp to port domain \
  -> 127.0.0.1 port $dns_proxy

block in
block return out

### loopback

# reroute loopback traffic destined for tor's mapaddress range
# this is unnecessary when VirtualAddrNetwork has been modified
pass out quick on lo0 route-to lo1 inet proto tcp to 127.192.0.0/10 \
  flags S/SA modulate state

# reroute loopback dns queries
pass out quick on lo0 route-to lo1 inet proto udp to port domain keep state

# skip all other loopback traffic
pass quick on lo0 keep state

### internal

# allow ssh access from the internal network
pass in on $int_if inet proto tcp to port ssh flags S/SA keep state

## allow all outgoing traffic to the internal network
#pass out on $int_if inet to ($int_if:network) keep state

### external

# allow tor to connect to the internet
pass out quick on $ext_if inet proto tcp user _tor flags S/SA modulate state

# reroute locally generated traffic trying to leave on the external interface
pass out on $ext_if route-to lo1 inet proto tcp all flags S/SA modulate state

# reroute locally generated dns trying to leave on the external interface
pass out on $ext_if route-to lo1 inet proto udp to port domain keep state

################################################################################
# Example 2                                                              pf.conf
#
# Two interfaces -- redirecting remote traffic into Tor
#
# Remotely generated DNS queries are redirected into the DNS proxy listening on
# 127.0.0.1:5353. Remotely initiated TCP connections are redirected into the
# transparent proxy listening on 127.0.0.1:1112.
#
# SSH connections are allowed from the internal network. All other packets
# destined for the host, except DNS, are dropped.
#
# All outgoing traffic, except traffic generated by the _tor user, is blocked.
# A line for allowing outgoing traffic into the internal network has been
# commented out.
#
# VirtualAddrNetwork should be set to 10.192.0.0/10 in torrc.
#
# Suggested procedure:
#
# trans-proxy-tor -b lo:1112 -s 127.0.0.1:9050 -c /var/empty \
#   -u nobody:nogroup -p /var/run/trans-proxy-tor.pid
# dns-proxy-tor -b lo:5353 -t 127.0.0.1:9051 -c /var/empty \
#   -u nobody:nogroup -p /var/run/dns-proxy-tor.pid
################################################################################

int_if = "fxp0"
ext_if = "fxp1"

trans_proxy = 1112
dns_proxy   = 5353

set skip on lo

scrub in

# redirect traffic from the internal network into tor
rdr pass on $int_if inet proto tcp to !($int_if:network) \
  -> 127.0.0.1 port $trans_proxy

# redirect dns traffic from the internal network into the dns proxy
rdr pass on $int_if inet proto udp to port domain -> 127.0.0.1 port $dns_proxy

block in
block return out

### internal

# allow ssh access from the internal network
pass in on $int_if inet proto tcp to port ssh flags S/SA keep state

## allow all outgoing traffic to the internal network
#pass out on $int_if inet to ($int_if:network) keep state

### external

# allow tor to connect to the internet
pass out on $ext_if inet proto tcp user _tor flags S/SA modulate state

################################################################################
# Example 3                                                              pf.conf
#
# One interface -- redirecting local traffic into Tor
#
# Locally generated DNS queries are redirected into the DNS proxy listening on
# 127.0.0.1:5353. Locally initiated TCP connections are redirected into the
# transparent proxy listening on 127.0.0.1:1112.
#
# SSH connections are allowed. All other packets destined for the host are
# dropped.
#
# All outgoing traffic, except traffic generated by the _tor user, is blocked.
# A line for allowing outgoing traffic into the local subnet has been commented
# out.
#
# Suggested procedure:
#
# ifconfig lo1 create up 127.0.0.2
# trans-proxy-tor -b lo:1112 -s 127.0.0.1:9050 -c /var/empty \
#   -u nobody:nogroup -p /var/run/trans-proxy-tor.pid
# dns-proxy-tor -b lo:5353 -t 127.0.0.1:9051 -c /var/empty \
#   -u nobody:nogroup -p /var/run/dns-proxy-tor.pid
################################################################################

if = "fxp0"

trans_proxy = 1112
dns_proxy   = 5353

scrub in

# redirect locally generated traffic into tor
rdr pass on lo1 inet proto tcp all -> 127.0.0.1 port $trans_proxy

# redirect dns traffic into the dns proxy
rdr pass on lo1 inet proto udp to port domain -> 127.0.0.1 port $dns_proxy

block in
block return out

### loopback

# reroute loopback traffic destined for tor's mapaddress range
pass out quick on lo0 route-to lo1 inet proto tcp to 127.192.0.0/10 \
  flags S/SA modulate state

# reroute loopback dns queries
pass out quick on lo0 route-to lo1 inet proto udp to port domain keep state

# skip all other loopback traffic
pass quick on lo0 keep state

### external

# allow ssh access
pass in on $if inet proto tcp to port ssh flags S/SA keep state

# allow tor to connect to the internet
pass out quick on $if inet proto tcp user _tor flags S/SA modulate state

## allow all outgoing traffic to the internal network
#pass out quick on $if inet to ($if:network) keep state

# reroute locally generated traffic trying to leave on the actual interface
pass out on $if route-to lo1 inet proto tcp all flags S/SA modulate state

# reroute locally generated dns queries trying to leave on the actual interface
pass out on $if route-to lo1 inet proto udp to port domain keep state

################################################################################
# Example 4                                                     iptables-init.sh
#
# Redirecting local and remote traffic into Tor
#
# Locally and remotely generated DNS queries are redirected into the DNS proxy
# listening on 0.0.0.0:5353. Locally and remotely initiated TCP connections
# are redirected into the transparent proxy listening on 0.0.0.0:1112.
#
# SSH connections are allowed from the internal network. All other packets
# destined for the host, except DNS, are rejected.
#
# Outgoing traffic destined for the internal network or generated by the Tor
# user is allowed. All other outgoing traffic is blocked.
#
# VirtualAddrNetwork should be set to 10.192.0.0/10 in torrc.
#
# Suggested procedure:
#
# echo 'nameserver 127.0.0.1' >/etc/resolv.conf
# mkdir /var/empty
# trans-proxy-tor -b any:1112 -s 127.0.0.1:9050 -c /var/empty \
#   -u nobody:nogroup -p /var/run/trans-proxy-tor.pid
# dns-proxy-tor -b any:5353 -t 127.0.0.1:9051 -c /var/empty \
#   -u nobody:nogroup -p /var/run/dns-proxy-tor.pid
################################################################################
#!/bin/sh

# set the internal network
INT_NET=192.168.1.0/24

# set this to the uid tor runs as
TOR_UID=`grep ^debian-tor /etc/passwd | cut -d: -f3`

DNS_PROXY=5353
TRANS_PROXY=1112

# flush tables
iptables -F
iptables -t nat -F

# set policies
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP
iptables -t nat -P OUTPUT ACCEPT
iptables -t nat -P PREROUTING ACCEPT
iptables -t nat -P POSTROUTING ACCEPT

########################################
# nat table rules

# output #############

# redirect dns traffic into the dns proxy
# set resolv.conf to 'nameserver 127.0.0.1' for this to work
iptables -t nat -A OUTPUT -p udp --dport 53 -j DNAT \
  --to-destination 127.0.0.1:$DNS_PROXY

# skip loopback
# 127.192.0.0/10 is reserved for Tor
# note: when VirtualAddrNetwork has been modified, this can be changed to:
#       iptables -t nat -A OUTPUT -d 127.0.0.0/8 -j RETURN
iptables -t nat -A OUTPUT -d 127.0.0.0/9 -j RETURN
iptables -t nat -A OUTPUT -d 127.128.0.0/10 -j RETURN

# skip tor
iptables -t nat -A OUTPUT -m owner --uid-owner $TOR_UID -j RETURN

# redirect locally generated tcp connections into the transparent proxy
iptables -t nat -A OUTPUT -d ! $INT_NET -p tcp --syn -j DNAT \
  --to-destination 127.0.0.1:$TRANS_PROXY

# prerouting #########

# redirect dns requests from the internal network into the dns proxy
iptables -t nat -A PREROUTING -s $INT_NET -p udp --dport 53 \
  -j REDIRECT --to-ports $DNS_PROXY

# redirect tcp connections from the internal network into the transparent proxy
iptables -t nat -A PREROUTING -s $INT_NET -d ! $INT_NET -p tcp --syn \
  -j REDIRECT --to-ports $TRANS_PROXY

########################################
# filter table rules

# input ##############

# keep state
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# pass loopback
# 127.192.0.0/10 is reserved for tor
# note: when VirtualAddrNetwork has been modified, this can be changed to:
#       iptables -A INPUT -d 127.0.0.0/8 -j RETURN
iptables -A INPUT -d 127.0.0.0/9 -j ACCEPT
iptables -A INPUT -d 127.128.0.0/10 -j ACCEPT

# pass traffic destined for ssh and transparent proxy from the internal network
for PORT in 22 $TRANS_PROXY; do
  iptables -A INPUT -s $INT_NET -p tcp --syn --dport $PORT -j ACCEPT
done

# pass traffic destined for the dns proxy from the internal network
iptables -A INPUT -s $INT_NET -p udp --dport $DNS_PROXY -j ACCEPT

# reject and log everything else
iptables -A INPUT -j LOG --log-level info
iptables -A INPUT -j REJECT

# output #############

# keep state
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# pass loopback
# 127.192.0.0/10 is reserved for Tor
# note: when VirtualAddrNetwork has been modified, this can be changed to:
#       iptables -A OUTPUT -d 127.0.0.0/8 -j RETURN
iptables -A OUTPUT -d 127.0.0.0/9 -j ACCEPT
iptables -A OUTPUT -d 127.128.0.0/10 -j ACCEPT

# pass traffic destined for the internal network
iptables -A OUTPUT -d $INT_NET -j ACCEPT

# pass tor traffic to the internet
iptables -A OUTPUT -m owner --uid-owner $TOR_UID -j ACCEPT

# reject and log everything else
iptables -A OUTPUT -j LOG --log-level info
iptables -A OUTPUT -j REJECT

################################################################################
# Example 5                                                     iptables-init.sh
#
# Redirecting local traffic into Tor
#
# Locally generated DNS queries are redirected into the DNS proxy listening on
# 127.0.0.1:5353. Locally initiated TCP connections are redirected into the
# transparent proxy listening on 127.0.0.1:1112.
#
# SSH connections are allowed from the internal network. All other packets
# destined for the host are rejected.
#
# Outgoing traffic destined for the internal network or generated by the Tor
# user is allowed. All other outgoing traffic is blocked.
#
# Suggested procedure:
#
# echo 'nameserver 127.0.0.1' >/etc/resolv.conf
# mkdir /var/empty
# trans-proxy-tor -b lo:1112 -s 127.0.0.1:9050 -c /var/empty \
#   -u nobody:nogroup -p /var/run/trans-proxy-tor.pid
# dns-proxy-tor -b lo:5353 -t 127.0.0.1:9051 -c /var/empty \
#   -u nobody:nogroup -p /var/run/dns-proxy-tor.pid
################################################################################
#!/bin/sh

# set the internal network
INT_NET=192.168.1.0/24

# set this to the uid tor runs as
TOR_UID=`grep ^debian-tor /etc/passwd | cut -d: -f3`

DNS_PROXY=5353
TRANS_PROXY=1112

# flush tables
iptables -F
iptables -t nat -F

# set policies
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP
iptables -t nat -P OUTPUT ACCEPT
iptables -t nat -P PREROUTING ACCEPT
iptables -t nat -P POSTROUTING ACCEPT

########################################
# nat table rules

# output #############

# redirect dns traffic into the dns proxy
# set resolv.conf to 'nameserver 127.0.0.1' for this to work
iptables -t nat -A OUTPUT -p udp --dport 53 -j DNAT \
  --to-destination 127.0.0.1:$DNS_PROXY

# skip loopback
# 127.192.0.0/10 is reserved for Tor
iptables -t nat -A OUTPUT -d 127.0.0.0/9 -j RETURN
iptables -t nat -A OUTPUT -d 127.128.0.0/10 -j RETURN

# skip tor
iptables -t nat -A OUTPUT -m owner --uid-owner $TOR_UID -j RETURN

# redirect tcp connections to the transparent proxy
iptables -t nat -A OUTPUT -d ! $INT_NET -p tcp --syn -j DNAT \
  --to-destination 127.0.0.1:$TRANS_PROXY

########################################
# filter table rules

# input ##############

# keep state
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# pass loopback
# 127.192.0.0/10 is reserved for tor
iptables -A INPUT -d 127.0.0.0/9 -j ACCEPT
iptables -A INPUT -d 127.128.0.0/10 -j ACCEPT

# pass traffic destined for ssh from the internal network
iptables -A INPUT -s $INT_NET -p tcp --syn --dport 22 -j ACCEPT

# reject and log everything else
iptables -A INPUT -j LOG --log-level info
iptables -A INPUT -j REJECT

# output #############

# keep state
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# pass loopback
# 127.192.0.0/10 is reserved for tor
iptables -A OUTPUT -d 127.0.0.0/9 -j ACCEPT
iptables -A OUTPUT -d 127.128.0.0/10 -j ACCEPT

# pass traffic destined for the internal network
iptables -A OUTPUT -d $INT_NET -j ACCEPT

# pass tor traffic to the internet
iptables -A OUTPUT -m owner --uid-owner $TOR_UID -j ACCEPT

# reject and log everything else
iptables -A OUTPUT -j LOG --log-level info
iptables -A OUTPUT -j REJECT
