~~~~~~

~~~~~~

我想運行一個開放的托爾路由器。

我的退出政策將類似於減少退出政策

但我也想讓 Tor 網路很難濫用我的資源。

我想阻止客戶透過 Tor 進行的情況:

  • 用大量資料包攻擊一個網站。
  • 對整個 IP 區塊進行積極的網路掃描

我不想阻止客戶透過 Tor 進行的情況:

  • 將數百個圖像檔案上傳到雲端
  • 播種種子

我的問題是,這是否可以做到,以及如何做到?

我的第一個想法是某種防火牆(Linux/iptables 或 *BSD/ipfw/pf) - 但由於 Onion 路由器的固有屬性,這可能毫無用處。

是否有關於此主題的正在進行的 torproject 團隊開發?

我還要求獲得有關保護 Tor 出口節點的一般提示。

更新(2012 年 9 月)

從有用的答案和其他一些研究來看,我認為這是不可能的。

要阻止人們濫用出口節點在 DDOS 中做出貢獻,您能做的最好的事情就是檢測定向到一個 IP 的非常頻繁的資料包。

「非常頻繁」的閾值取決於總節點頻寬…如果錯誤,就會出現誤報,從而阻止即時 TCP 應用程式的合法流量以及從大量客戶端發送到一個目的地的流量。

更新(2014 年 12 月)

我的預測顯然是正確的 - 我收到了幾起來自我的互聯網提供商的網絡濫用投訴。

為了避免服務關閉,我必須採用以下一組iptables規則(ONEW是傳出 TCP SYN(又稱新)封包的鏈:

我不確定這是否足夠,但它是:

-A ONEW -o lo -j ACCEPT
-A ONEW -p udp --dport 53 -m limit --limit 2/sec --limit-burst 5 -j ACCEPT
-A ONEW -m hashlimit --hashlimit-upto 1/second --hashlimit-mode dstip --hashlimit-dstmask 24 --hashlimit-name ONEW -j ACCEPT
-A ONEW -m limit --limit 1/sec -j LOG --log-prefix "REJECTED: "
-A ONEW -j REJECT --reject-with icmp-admin-prohibited

答案1

請記住:

  • 根據我目前的理解,Tor 客戶端每 10 分鐘左右就會切換一次虛擬電路。這意味著來源 IP 在該時間範圍內發生變化。您不可能阻止任何您認為惡意的行為超過該持續時間。

  • 請注意,Tor 僅代理 TCP 流量而不代理任何其他協定這一事實在很大程度上限制了濫用的可能性。

iptables可以讓您以不同於現有連線的方式對待新的傳出 TCP 連線。任何內容都ESTABLISHED,RELATED應該ACCEPTED通過「現有 TCP 連線」鏈,且不被該鏈捕獲的傳出 TCP 可能會受到速率限制。任何傳出的 Tor 流量都應遵守此規定。

我相信在上述內容和使用「減少退出政策」之間將是您能做的最好的事情。

理想情況下,不要在 Tor 盒子上運行任何其他東西,除了:

  • 您可能至少會啟動 SSH,並將其放在與 22 不同的連接埠上。
  • 您可能需要執行一個簡單的網頁伺服器來顯示這一頁。 chrootmini-httpd實例應該可以。不要使用inetd.

不要在正在用於其他用途的機器上運行 Tor。確保您已閱讀《退出繼電器》部分Tor 法律常見問題解答並充分理解其意義。也閱讀並完成所有這些

答案2

由於來源 IP 不是恆定的,因此阻止這些攻擊將比平常更困難。然而,據我所知,tor 的路線每隔幾分鐘左右就會改變一次。

因此,您仍然可以部署一些標準限制/過濾規則,但具有更高的閾值,因為您必須假設來源 IP 背後有一個完整的網路。

您可以過濾:

  • 錯誤或典型的指紋/掃描資料包(錯誤的 TCP/IP 標誌、XMAS、大多數 ICMP 類型等)
  • 不適合正在進行的或新的連接的無效資料包(-m 狀態)
  • 新連接以相當高的閾值開始

但是,請注意,此類操作通常是在入站流量上完成的。您不知道您的“客戶”將運行哪種協議,並且您可能會以令人煩惱/不清楚的方式限制它們。

另外,對於速率限制的新(或無狀態)資料包,您可能需要考慮一些更複雜的方案,其中被拒絕的(永遠不會丟棄,除非它明顯是攻擊!)資料包是隨機的。這樣,即使總體速率目前處於限制,普通用戶也可以嘗試重新加載並獲得幸運,而並發連接埠掃描器將無法繞過您的速率限制。

另外在 Tor 郵件列表上詢問,您可能不是第一個有這樣想法的人:https://lists.torproject.org/cgi-bin/mailman/listinfo

答案3

首先,我不建議iptables 來解決所有這些問題,實際上,理想的退出Tor 節點會透過一些VPN 隧道載入平衡流量,以使ISP 的視線遠離封包和真正的目的地和/或利用快取代理來保留出站重複請求流行靜止的內容最少......在研究這些選項時,這裡有一個OK繃對於濫用投訴問題;

使用的資訊來源

http://www.ossramblings.com/using_iptables_rate_limiting_to_prevent_portscans

http://blog.nintechnet.com/how-to-block-w00tw00t-at-isc-sans-dfind-and-other-web-vulnerability-scanners/

將兩個來源連結組合成規則,可用於挫敗嘗試使用 Tor 出口節點進行連接埠掃描的機器人。請注意,這可能會讓使用您的退出節點的駭客非常不高興,因為這些規則會導致 nmap 掛起時間。

#!/bin/bash
## Network interface used by Tor exit daemon
_tor_iface="eth1"
## Ports that Tor exit daemon binds to, maybe comma or space sepperated.
_tor_ports="9050,9051"
## Time to ban connections out in secconds, default equates to 10 minutes, same as default Tor cercut.
_ban_time="600"
## How long to monitor conections in seconds, default equates to 10 minutes.
_outgoing_tcp_update_seconds="600"
## How many new connections can be placed to a server in aloted update time limits. May nead to increes this depending on exit node usage and remote servers usages.
_outgoing_tcp_hitcount="8"
## How long to monitor connections for in minuets, default is 15 minutes but could be lessoned.
_outgoing_tcp_burst_minute="15"
## Hom many connections to accept untill un-matched
_outgoing_tcp_burst_limit="1000"

iptables -N out_temp_ban -m comment --comment "Make custom chain for tracking ban time limits" || exit 1
iptables -A out_temp_ban -m recent --set --name temp_tcp_ban -p TCP -j DROP -m comment --comment "Ban any TCP packet coming to this chain" || exit 1

iptables -N out_vuln_scan -m comment --comment "Make custom chain for mitigating port scans originating from ${_tor_iface}" || exit 1
for _tor_port in ${_tor_ports//,/ }; do
    iptables -A out_vuln_scan -p TCP -o ${_tor_iface} --sport ${_tor_port} -m recent --name temp_tcp_ban --update --seconds ${_ban_time} -j DROP -m comment --comment "Update ban time if IP address is found in temp_tcp_ban list" || exit 1
    iptables -A out_vuln_scan -p TCP -o ${_tor_iface} --sport ${_tor_port} -m state --state NEW -m recent --set -m comment --comment "Monitor number of new conncetions to ${_server_iface}" || exit 1
    iptables -A out_vuln_scan -p TCP -o ${_tor_iface} --sport ${_tor_port} -m state --state NEW -m recent --update --seconds 30 --hitcout 10 -j out_temp_ban -m comment --comment "Ban address when to many new connections are attempted on ${_tor_iface}" || exit 1
done
iptables -A out_vuln_scan -j RETURN -m comment --comment "Return un-matched packets for further processing" || exit 1

## Add rules to accept/allow outbound packets
iptables -N tor_out -m comment --comment "Make custom chain for allowing Tor exit node services" || exit 1
for _tor_port in ${_tor_ports//,/ }; do
    iptables -A tor_out -p TCP -o ${_tor_iface} --sport ${_tor_port} -m state --state NEW -m recent --set --name limit_${_tor_port} -m comment --comment "Track out-going tcp connections from port ${_tor_port}" || exit 1
    iptables -A tor_out -p TCP -o ${_tor_iface} --sport ${_tor_port} -m state --state NEW -m recent --update --seconds ${_outgoing_tcp_update_seconds:-60} --hitcount ${_outgoing_tcp_hitcount:-8} --rttl --name limit_${_tor_port} -j LOG --log-prefix "TCP flooding port ${_tor_port}" -m comment --comment "Log atempts to flood port ${_tor_port} from your server" || exit 1
    iptables -A tor_out -p TCP -o ${_tor_iface} --sport ${_tor_port} -m state --state NEW -m recent --update --seconds ${_outgoing_tcp_update_seconds:-60} --hitcount ${_outgoing_tcp_hitcount:-8} --rttl --name limit_${_tor_port} -j DROP -m comment --comment "Drop attempts to flood port ${_tor_port} from your server" || exit 1
    iptables -A tor_out -p TCP -o ${_tor_iface} --sport ${_tor_port} -m limit --limit ${_outgoing_tcp_burst_minute:-15}/minute --limit-burst ${_outgoing_tcp_burst_limit:-1000} -j ACCEPT -m comment --comment "Accept with conditions new connections from port ${_tor_port} from your server" || exit 1
done
iptables -A tor_out -j RETURN -m comment ---comment "Reurn un-matched packets for further filtering or default polices to take effect." || exit 1
## Activate jumps from default output chain to new custom filtering chains
iptables -A OUTPUT -p TCP -o ${_tor_iface} -j out_vuln_scan -m comment --comment "Jump outbound packets through vulnerability scaning mitigation" || exit 1
iptables -A OUTPUT -p TCP -o ${_tor_iface} -j tor_out -m comment --comment "Jump outbound packets through conditional acceptance" || exit 1

運行上面的 withbash來對帶有 cammas 的變數執行魔法,,即;

user@host~# bash iptables_limit_tor.sh

這又是變數列表

_tor_iface="eth1"
_tor_ports="9050,9051"
_ban_time="600"
_outgoing_tcp_update_seconds="600"
_outgoing_tcp_hitcount="8"
_outgoing_tcp_burst_minute="15"
_outgoing_tcp_burst_limit="1000"

請注意,您可能還希望過濾新的出站連接以查找-m state NEW ! --syn各種類型的連接有趣的一些機器人使用業務來尋找可利用的伺服器,這是一個範例鏈,您可以使用上面的兩個鏈來進一步過濾此類格式錯誤的聊天內容

iptables -N out_bad_packets -m comment --comment "Make new chain for filtering malformed packets" || exit 1
iptables -A out_bad_packets -p TCP --fragment -j out_temp_ban -m comment --comment "Drop all fragmented packets" || exit 1
iptables -A out_bad_packets -p TCP -m state --state INVALID -j out_temp_ban -m comment --comment "Drop all invalid packets" || exit 1
iptables -A out_bad_packets -p TCP ! --syn -m state --state NEW -j out_temp_ban -m comment --comment "Drop new non-syn packets" || exit 1
iptables -A out_bad_packets -p TCP --tcp-flags ALL NONE -j out_temp_ban -m comment --comment "Drop NULL scan" || exit 1
iptables -A out_bad_packets -p TCP --tcp-flags ALL ALL -j out_temp_ban -m comment --comment "Drop XMAS scan"|| exit 1
iptables -A out_bad_packets -p TCP --tcp-flags ALL FIN,URG,PSH -j out_temp_ban -m comment --comment "Drop stealth scan 1" || exit 1
iptables -A out_bad_packets -p TCP --tcp-flags ALL SYN,RST,ACK,FIN,URG -j out_temp_ban -m comment --comment "Drop pscan 1"|| exit 1
iptables -A out_bad_packets -p TCP --tcp-flags SYN,FIN SYN,FIN -j out_temp_ban -m comment --comment "Drop pscan 2" || exit 1
iptables -A out_bad_packets -p TCP --tcp-flags FIN,RST FIN,RST -j out_temp_ban -m comment --comment "Drop pscan 3" || exit 1
iptables -A out_bad_packets -p TCP --tcp-flags SYN,RST SYN,RST -j out_temp_ban -m comment --comment "Drop SYN-RST scan" || exit 1
iptables -A out_bad_packets -p TCP --tcp-flags ACK,URG URG -j out_temp_ban -m comment --comment "Drop URG scans" || exit 1
iptables -A out_bad_packets -p TCP --tcp-flags ALL SYN,FIN -j out_temp_ban -m comment --comment "Drop SYNFIN scan" || exit 1
iptables -A out_bad_packets -p TCP --tcp-flags ALL URG,PSH,FIN -j out_temp_ban -m comment --comment "Drop nmap Xmas scan" || exit 1
iptables -A out_bad_packets -p TCP --tcp-flags ALL FIN -j out_temp_ban -m comment --comment "Drop FIN scan" || exit 1
iptables -A out_bad_packets -p TCP --tcp-flags ALL URG,PSH,SYN,FIN -j out_temp_ban -m comment --comment "Drop nmap-id scan" || exit 1
iptables -A out_bad_packets -p TCP --tcp-flags RST RST -o ${_tor_iface} --sport ${_tor_port} -m limit --limit 2/second --limit-burst 3 -j out_temp_ban -m comment --comment "Mitigate Smurf attacks from excesive RST packets"
iptables -A out_bad_packets -p TCP --tcp-flags RST RST -o ${_tor_iface} --sport ${_tor_port} -m limit --limit 2/second --limit-burst 2 -j RETURN -m comment --comment "Ban Smurf attacks using excesive RST packets"
iptables -A out_bad_packets -j RETURN -m comment --comment "Return un-matched packets for further processing." || exit 1

然而,上述鏈將受到非常嚴格的限制,因為任何匹配的資料包都將被禁止 IP(可能更改-j out_temp_ban-j DROP-j REJECT用於測試),無論該鏈的規則中選擇多少秒。當用戶端上編碼錯誤的應用程式透過新的 Tor cercut 重新連接時,這組規則也可能導致誤報。

~~~~~~

用於進一步調整流量的軟體 請查看firejailLinux 版本,原始碼位於 Github 和 Source forge 上,手冊頁可以在舊主頁(wordpress 子網域)上找到,DigitalOcean 提供了使用 PHP 和 Firejail 的 Nginx 指南稍加修改就可以讓您更了解應該在哪裡限製網路。還有其他工具,例如KVM太,可用於將特殊服務保持在操作邊界內,以便店鋪找到最適合您的系統的一個。

還有一種選擇是以這樣的方式運作fail2ban:當瘋狂的系統管理員嘗試透過 http 或 ssl 連接到您的 IP 時,會新增一條規則來刪除-m state --state NEW與那些要求退出通知頁面的連線。如果與合理的解除禁令時間限制相結合,可能會允許遠端伺服器休息,而他們的系統管理員會抱怨日誌污染;-)但是,這超出了當前答案的範圍,並且取決於您使用的服務軟體退出通知頁面;提示如果現在請求 URL,nginx 和 apache 都會為您的配置中的第一個虛擬主機或伺服器區塊提供服務。如果使用apache 或nginx 以外的其他東西,您需要查閱手冊頁,但對我來說,這就像將第一個虛擬主機設定為登入不同的檔案一樣簡單,並讓fail2ban將該日誌中的任何IP 新增到臨時禁止清單中;這對於禁止公共伺服器上的機器人也非常有效,因為它們通常使用 IP 位址,並且不提供網域請求會導致伺服器提供機器人陷阱,或在本例中為退出通知。

我傾向於運行受限制的 Tor 退出策略(看起來您已經處理了該策略),然後透過 VPN 隧道推送流量,為多個隧道之間的負載平衡提供額外的信用點。因為這會減少對 Tor 網路流量的干擾,並使您的 ISP 無法察覺到您正在運行出口節點……除非他們願意承認嗅探和破解您的 VPN 流量。這是因為執行臨時禁止或允許遠端主機自我禁止的規則可能會導致節點客戶端的隱私受到侵犯,而將流量推送到 VPN(或少數)將有助於保護客戶端的隱私並保留您的資訊。遭追捕要求任何有能力運作的政府都會記錄您的網路流量日誌whois www.some.domain

~~~

編輯/更新

~~~

我查閱了詳細的筆記並提取了我使用的公共伺服器的配置

這是fail2banjail.local斯坦薩

[apache-ipscan]
enabled  = true
port = http,https
filter = apache-ipscan
logpath = /var/log/apache*/*error_ip*
action = iptables-repeater[name=ipscan]
maxretry = 1

這是過濾器apache-ipscan.conf文件

[DEFAULT]
_apache_error_msg = \[[^]]*\] \[\S*:error\] \[pid \d+\] \[client <HOST>(:\d{1,5})?\]
[Definition]
failregex = \[client <HOST>\] client denied by server .*(?i)/.*
#^<HOST>.*GET*.*(?!)/.*
#   ^%(_apache_error_msg)s (AH0\d+: )?client denied by server configuration: (uri )?.*$
#            ^%(_apache_error_msg)s script '\S+' not found or unable to stat(, referer: \S+)?\s*$
ignoreregex = 
# DEV Notes: 
# the web server only responds to clients with a valid Host: 
# header. anyone who tries using IP only will get shunted into 
# the dummy-error.log and get a client-denied message
#
# the second regex catches folks with otherwise valid CGI paths but no good Host: header
#
# Author: Paul Heinlein

這是操作iptables-repeater.conf文件

# Fail2Ban configuration file
#
# Author: Phil Hagen <[email protected]>
# Author: Cyril Jaquier
# Modified by Yaroslav Halchenko for multiport banning and Lukas Camenzind for persistent banning
# Modified by S0AndS0 to combine features of previous Authors and Modders
#
[Definition]
# Option:  actionstart
# Notes.:  command executed once at the start of Fail2Ban.
# Values:  CMD
#
actionstart = iptables -N fail2ban-BADIPS-<name>
              iptables -A fail2ban-BADIPS-<name> -j RETURN
          iptables -I INPUT -j fail2ban-BADIPS-<name>
          ## Comment above line and uncomment bello line to use multiport and protocol in addition to named jails
          #iptables -I INPUT -p <protocol> -m multiport --dports <port> -j fail2ban-BADIPS-<name>
          # set up from the static file
          #cat /etc/fail2ban/ip.blocklist.<name> |grep -v ^\s*#|awk '{print $1}' | while read IP; do iptables -I fail2ban-BADIPS-<name> 1 -s $IP -j DROP; done
          cat /etc/fail2ban/ip.blocklist.<name> |grep -v ^\s*#|awk '{print $1}' | while read IP; do iptables -I fail2ban-BADIPS-<name> 1 -d $IP -j DROP; done
          ## Comment above line and uncomment bellow line to check if there are blacklist files to load before attempting to load them
          # if [ -f /etc/fail2ban/ip.blacklist.<name> ]; then cat /etc/fail2ban/ip.blacklist.<name> | grep -e <name>$ | cut -d "," -s -f 1 | while read IP; do iptables -I fail2ban-BADIPS-<name> 1 -s $IP -j DROP; done; fi
# Option:  actionstop
# Notes.:  command executed once at the end of Fail2Ban
# Values:  CMD
#
actionstop = iptables -D INPUT -p <protocol> -m multiport --dports <port> -j fail2ban-BADIPS-<name>
         iptables -F fail2ban-BADIPS-<name> 
         iptables -X fail2ban-BADIPS-<name>
# Option:  actioncheck
# Notes.:  command executed once before each actionban command
# Values:  CMD
#
#actioncheck = iptables -n -L INPUT | grep -q fail2ban-BADIPS-<name>
actioncheck = iptables -n -L OUTPUT | grep -q fail2ban-BADIPS-<name>
# Option:  actionban
# Notes.:  command executed when banning an IP. Take care that the
#          command is executed with Fail2Ban user rights.
# Tags:    <ip>  IP address
#          <failures>  number of failures
#          <time>  unix timestamp of the ban time
# Values:  CMD
#
#actionban = if ! iptables -C fail2ban-BADIPS-<name> -s <ip> -j DROP; then iptables -I fail2ban-BADIPS-<name> 1 -s <ip> -j DROP; fi
actionban = if ! iptables -C fail2ban-BADIPS-<name> -d <ip> -j DROP; then iptables -I fail2ban-BADIPS-<name> 1 -d <ip> -j DROP; fi
# Add offenders to local blacklist, if not already there
        if ! grep -Fxq '<ip>,<name>' /etc/fail2ban/ip.blocklist.<name>; then echo "<ip>,<name> # fail2ban/$( date '+%%Y-%%m-%%d %%T' ): auto-add for BadIP offender" >> /etc/fail2ban/ip.blocklist.<name>; fi
# Report offenders to badips.com
#        wget -q -O /dev/null www.badips.com/add/<name>/<ip>
# Option:  actionunban
# Notes.:  command executed when unbanning an IP. Take care that the
#          command is executed with Fail2Ban user rights.
# Tags:    <ip>  IP address
#          <failures>  number of failures
#          <time>  unix timestamp of the ban time
# Values:  CMD
#
#actionunban = iptables -D fail2ban-REPEAT-<name> -s <ip> -j DROP
actionunban = iptables -D fail2ban-REPEAT-<name> -d <ip> -j DROP
# Disabled clearing out entry from ip.blacklist (somehow happens after each stop of fail2ban)
#sed --in-place '/<ip>,<name>/d' /etc/fail2ban/ip.blacklist.<name>
[Init]
# Defaut name of the chain
# 
# Defaut name of the chain
name = BADIPS
# Option:  port
# Notes.:  specifies port to monitor
# Values:  [ NUM | STRING ]  Default:
# 
#port = ssh
# Option:  protocol
# Notes.:  internally used by config reader for interpolations.
# Values:  [ tcp | udp | icmp | all ] Default: tcp

請注意,上面的過濾器已被編輯為阻止OUTPUT啟動/停止操作,但您仍然需要將-p TCP -m state --state NEW配置添加到每一行,以便僅禁止來自記錄的 IP 位址的新出站連接。

最後是設定一個Apache vHost 配置,將那些不請求網域的內容路由到特定的訪問和錯誤經驗,並設定允許與拒絕的訪問,使其始終出錯,甚至環回也不應該能夠在不彈出錯誤的情況下拉出頁面。最後但並非最不重要的是將 Apache 的錯誤頁面設為 Tor 的預設退出通知,以便提供該通知而不是503平淡404的訊息。或者,如果您已將狀態列新增至fail2ban 的 iptables 操作中,您可以輕鬆指向退出通知所使用的相同日誌檔案。結果將是您的伺服器將無法與檢查您的 IP 位址的伺服器的 IP 建立新連接,但已建立的相關連接仍將被允許,即它們仍然可以瀏覽您的其他頁面,但您無法瀏覽這些頁面。

答案4

我有一個更好的解決方案:squid 快取伺服器。 Squid 快取伺服器可用於設定定義acl和您denyaccept每個acl.非常有趣的是,魷魚團隊在您的問題發現的維基中定義了一組規則那裡 iptables,PF否則其他人無法完成您的工作,因為您只是在另一層工作。

相關內容