Skip to content

Instantly share code, notes, and snippets.

@zouppen
Last active April 17, 2024 21:52
Show Gist options
  • Select an option

  • Save zouppen/bc005e0038860164714f0cdf376369b4 to your computer and use it in GitHub Desktop.

Select an option

Save zouppen/bc005e0038860164714f0cdf376369b4 to your computer and use it in GitHub Desktop.

Revisions

  1. zouppen revised this gist Nov 10, 2022. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -50,4 +50,8 @@ addition to public Internet.

    ## Author

    Joel Lehtonen. [Feel free to support me on Github](https://github.com/sponsors/zouppen).
    Joel Lehtonen. [Feel free to support me on
    Github](https://github.com/sponsors/zouppen).

    Some ideas are borrowed from [jkullick's
    gist](https://gist.github.com/jkullick/62695266273608a968d0d7d03a2c4185), thanks!
  2. zouppen revised this gist Nov 10, 2022. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    # Block Tor exit nodes with Linux and ipset

    This gist blocks incoming traffic from Tor exit nodes. You can also
    use it to `MARK` or redirect the incoming traffic, depending of your
    needs.
    This gist blocks incoming traffic from
    [Tor](https://www.torproject.org/) exit nodes. You can also use it to
    `MARK` or redirect the incoming traffic, depending of your needs.

    This guide is for IPv4 only, feel free to contribute IPv6 support if
    you have it.
  3. zouppen revised this gist Nov 10, 2022. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -39,6 +39,15 @@ iptables -I INPUT 1 -j TORDROP

    To keep your rules after reboot, use *iptables-persistent* or similar tool.

    ## A word about Tor

    The objective of this script is not to prevent Tor users from
    accessing information. My goal is minimize the attack surface to my
    services and/or marking Tor traffic to detect scamming attempts and
    so. Please don't use it to prevent information sharing and consider
    serving your content using a hidden service (onion address) in
    addition to public Internet.

    ## Author

    Joel Lehtonen. [Feel free to support me on Github](https://github.com/sponsors/zouppen).
  4. zouppen revised this gist Nov 10, 2022. 4 changed files with 69 additions and 1 deletion.
    45 changes: 44 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1 +1,44 @@
    # TODO
    # Block Tor exit nodes with Linux and ipset

    This gist blocks incoming traffic from Tor exit nodes. You can also
    use it to `MARK` or redirect the incoming traffic, depending of your
    needs.

    This guide is for IPv4 only, feel free to contribute IPv6 support if
    you have it.

    ## Installing

    Deploy `generate_tor_exit_ipset` to `/opt` and `tor_block.service` and
    `tor_block.timer` to `/etc/systemd/system`.

    Create empty ipset called `tor_exit`. If you use
    *netfilter-persistent* you can add the following line to
    `/etc/iptables/ipsets`:

    ```
    create tor_exit hash:ip family inet hashsize 1024 maxelem 65536
    ```

    Reload systemd and then enable and start the timer:

    ```sh
    systemctl daemon-reload
    systemctl enable tor_block.timer
    systemctl start tor_block.timer
    ```

    Add the filter rule to your firewall, e.g. in iptables:

    ```
    iptables -N TORDROP
    iptables -A TORDROP -m state --state RELATED,ESTABLISHED -j RETURN
    iptables -A TORDROP -m set --match-set tor_exit src -j REJECT --reject-with icmp-host-prohibited
    iptables -I INPUT 1 -j TORDROP
    ```

    To keep your rules after reboot, use *iptables-persistent* or similar tool.

    ## Author

    Joel Lehtonen. [Feel free to support me on Github](https://github.com/sponsors/zouppen).
    7 changes: 7 additions & 0 deletions generate_tor_exit_ipset
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    #!/bin/sh -eu
    #
    # See /etc/systemd/system/tor_block.service

    echo flush tor_exit
    curl -sSL "https://check.torproject.org/cgi-bin/TorBulkExitList.py?ip=`curl -4 ifconfig.co`" | \
    sed -nre 's/^[0-9\.]*$/add tor_exit \0/p'
    9 changes: 9 additions & 0 deletions tor_block.service
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    [Unit]
    Description=Tor exit node ipset population
    After=network-online.target

    [Service]
    Type=oneshot
    User=root
    Group=root
    ExecStart=/bin/sh -c "sudo -u nobody /opt/generate_tor_exit_ipset | ipset restore"
    9 changes: 9 additions & 0 deletions tor_block.timer
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    [Unit]
    Description=Update Tor exit node list daily

    [Timer]
    OnUnitActiveSec=1day
    OnBootSec=0s

    [Install]
    WantedBy=timers.target
  5. zouppen created this gist Nov 10, 2022.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    # TODO