다음과 같이 Nginx(작성 당시 1.14.1)의 간단한 HTTP 기본 인증 설정을 가정해 보겠습니다.
server {
...
location / {
auth basic "HTTP Auth Required";
auth basic user file "/path/to/htpasswd";
}
}
... 실패한 로그인 시도에 속도 제한을 어떻게 적용합니까? 예를 들어 30초 동안 10번의 로그인 시도 실패가 있는 경우 해당 소스 IP가 한 시간 동안 사이트에 액세스하지 못하도록 차단하고 싶습니다. 관련 지시문을 사용하려고 했지만 limit_req_zone
요청의 인증 상태에 연결하는 방법을 찾을 수 없었습니다.
이는 다음 작업 예제와 같은 것을 사용하여 스틱 테이블과 ACL을 사용하는 HAproxy에서 상당히 간단합니다.
userlist users
user me password s3cr3t
frontend https.local
...
# Set up the stick table to track our source IPs, both IPv4 & IPv6
stick-table type ipv6 size 100k expire 1h store http_req_rate(30s)
# Check if the user has authenticated
acl auth_ok http_auth(users)
# Track the client IP
http-request track-sc0 src
# Deny the connection if it exceeds 10 requests within the defined
# stick-table period AND if the client isn't authenticated already
http-request deny deny_status 429 if { sc_http_req_rate(0) gt 10 } !auth_ok
# Define the auth realm if the users isn't authenticated and made it this far
http-request auth realm Authorization\ Required unless auth_ok
접근 방식을 사용하지 않고 외부 인증 메커니즘을 auth_request
location
차단 하도록 요청을 제한하지 않고도 Nginx에서 이것이 가능합니까?
답변1
Fail2ban을 사용한 다음 nginx에 감옥을 사용할 수도 있습니다.
답변2
limit_req_status 429;
auth_basic "-";
auth_basic_user_file "...";
error_page 401 = @nein;
proxy_intercept_errors on; # not needed if not proxying
location / {
# note you cannot use a short circuiting `return` here.
try_files /dev/null =204;
}
location @nein {
internal ;
limit_req zone=<zone>;
# this is the magic trick, `try_files` takes place AFTER `limit_req`.
try_files /dev/null =401;
}
참고: 이는 인증 실패 후에만 적용되므로 다음 요청을 지연시키기 위해 버스트 제한을 가지고 놀아야 합니다. 무차별 대입 공격으로부터 이런 방식으로 보안을 유지하는 것이 가능한지조차 확신할 수 없습니다.