作為 load balancer, Happroxy 常常作為服務器的前端,向外界用戶提供服務的入口,如果能在入口處處理安全相關問題,將極大簡化后端的設計。事實上,Haproxy 不僅僅是一款開源出色的 load balancer(四層和七層),而且在安全上也相當出色。它配合內核 IP/TCP 協議棧,能夠較好的抵抗 DOS, DDOS 攻擊,還能通過限制單個 IP 的連接數和請求速率等,防止用戶的惡意行為。
TCP syn flood attacks
# Protection SYN flood net.ipv4.tcp_syncookies = 1 net.ipv4.conf.all.rp_filter = 1 net.ipv4.tcp_max_syn_backlog = 1024
Slowloris like attacks
defaults option http-server-close mode http timeout http-request 5s # 防止 Slowloris like attacks timeout connect 5s timeout server 10s timeout client 30s listen stats bind 0.0.0.0:8880 stats enable stats hide-version stats uri / stats realm HAProxy\ Statistics stats auth admin:admin frontend ft_web bind 0.0.0.0:8080 default_backend bk_web backend bk_web balance roundrobin cookie MYSRV insert indirect nocache server srv1 192.168.1.2:80 check cookie srv1 maxconn 100 server srv2 192.168.1.3:80 check cookie srv2 maxconn 100
通過 telnet 登錄驗證結果,登陸連接后超過5秒就會自動被拒絕斷掉。
Limiting the number of connections per users
以網站為例,普通用戶訪問網站,或者從網站下載東西時,瀏覽器一般會建立 5-7 個 TCP 鏈接。當一個惡意打開了大量 TCP 鏈接時,耗費服務器大量資源,影響其它用戶的訪問,因此我們需要根據實際情況,限制同一個用戶的鏈接數。
defaults option http-server-close mode http timeout http-request 5s timeout connect 5s timeout server 10s timeout client 30s listen stats bind 0.0.0.0:8880 stats enable stats hide-version stats uri / stats realm HAProxy\ Statistics stats auth admin:admin frontend ft_web bind 0.0.0.0:8080
# Table definition stick-table type ip size 100k expire 30s store conn_cur # Allow clean known IPs to bypass the filter tcp-request connection accept if { src -f /etc/haproxy/whitelist.lst } # Shut the new connection as long as the client has already 10 opened tcp-request connection reject if { src_conn_cur ge 10 } tcp-request connection track-sc1 src default_backend bk_web backend bk_web balance roundrobin cookie MYSRV insert indirect nocache server srv1 192.168.1.2:80 check cookie srv1 maxconn 100 server srv2 192.168.1.3:80 check cookie srv2 maxconn 100
size 定義了這個table可以存儲的entry最大數量
expire 定義entry在stick-table中的過期時間,默認大概24天.如果不指定expire這個參數,千萬不要指定nopurge!
store 用於存儲其他額外信息,這些信息可以用於ACL.用於控制各種與客戶端活動相關的標准。多種data type可以用逗號分隔,寫在store后邊
注:若某些用戶在同一個私有網段通過 NAT 訪問網站,這樣的配置存在不合理之處,最好把 NAT 處的公網地址添加到 whitelist.lst 文件中。
利用 apache ab測試工具做驗證,和服務器一直保持建立 10 個鏈接。
# ab -n 50000000 -c 10 http://127.0.0.1:8080/
用 telnet 打開第 11 個鏈接,服務器拒絕該鏈接。
Limiting the connection rate per user
僅僅限制單個用戶的並發鏈接數並意味着萬事大吉,如果用戶在短時間內向服務器不斷的發送建立和關閉鏈接請求,也會耗費服務器資源,影響服務器端的性能,因此需要控制單個用戶建立連接的訪問速率/頻率。
通常情況下,考慮到用戶通過瀏覽器一般會建立 5-7 條 TCP 鏈接,我們可以認為普通用戶在 3 秒內不應該建立超過 20 條鏈接。
defaults option http-server-close mode http timeout http-request 5s timeout connect 5s timeout server 10s timeout client 30s listen stats bind 0.0.0.0:8880 stats enable stats hide-version stats uri / stats realm HAProxy\ Statistics stats auth admin:admin frontend ft_web bind 0.0.0.0:8080 # Table definition stick-table type ip size 100k expire 30s store conn_cur,conn_rate(3s) # 3秒內的連接次數限制到20次 # Allow clean known IPs to bypass the filter tcp-request connection accept if { src -f /etc/haproxy/whitelist.lst } # Shut the new connection as long as the client has already 10 opened or rate more than 20 tcp-request connection reject if { src_conn_cur ge 10 } || { src_conn_rate ge 20} tcp-request connection track-sc1 src default_backend bk_web backend bk_web balance roundrobin cookie MYSRV insert indirect nocache server srv1 192.168.1.2:80 check cookie srv1 maxconn 100 server srv2 192.168.1.3:80 check cookie srv2 maxconn 100
注:若某些用戶在同一個私有網段通過 NAT 訪問網站,這樣的配置存在不合理之處,最好把 NAT 處的公網地址添加到 whitelist.lst 文件中。
測試,采用 ab 打開 20 個鏈接。(測試時記得把 “限制單個用戶並發數功能” 去掉)
ab -n 20 -c 1 -r http://127.0.0.1:8080/
再用 telnet 打開第 21 個鏈接,服務器拒絕該請求。
-------------------------------------------------------------------------------------------------------------
如何直觀的查看stick-table里的內容
1、安裝socat
# yum install socat -y
2、在haproxy.cfg的global項中開啟stats
stats socket /var/run/haproxy.stats level admin
3、執行查看命令
echo "show table ft-web" | socat unix:/var/run/haproxy.stats - 其中table名字同frontend的名稱
可以進行實時查看:
# watch -n 1 -d 'echo "show table ft-web" | socat unix:/var/run/haproxy.stats -'
參考資料:http://blog.haproxy.com/2012/02/27/use-a-load-balancer-as-a-first-row-of-defense-against-ddos/
http://blog.sina.com.cn/s/blog_704836f40102w243.html