Skip to content

Bulk asynchronous downloader for multiple file hosts

fix: false positive DDosGuard detections (#1933)

NTFSvolume0a86f5f44552
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 658b6e5c0..770ce51b0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -22,6 +22,22 @@ All notable changes to this project will be documented here. For more details, v
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
+## UNRELEASED
+
+### Added
+
+### Changed
+
+### Deprecated
+
+### Removed
+
+### Fixed
+
+- Some false positive DDoS-Guard detections
+
+### Security
+
## [9.13.0] - 2026-05-29
### Changed
diff --git a/cyberdrop_dl/ddos_guard.py b/cyberdrop_dl/ddos_guard.py
index 42157ac25..b31818fc6 100644
--- a/cyberdrop_dl/ddos_guard.py
+++ b/cyberdrop_dl/ddos_guard.py
@@ -6,33 +6,58 @@ import hashlib
import json
import os
import sys
-from typing import Protocol
+from typing import TYPE_CHECKING, Protocol, final
import yarl
from bs4 import BeautifulSoup
+from typing_extensions import override
from cyberdrop_dl.exceptions import DDOSGuardError
+if TYPE_CHECKING:
+ from collections.abc import Iterable
+
+ from multidict import MultiDict
+
class _Response(Protocol):
@property
def content_type(self) -> str: ...
+ @property
+ def headers(self) -> MultiDict[str]: ...
+ @property
+ def status_code(self) -> int: ...
async def text(self) -> str: ...
+def _soup(html: str) -> BeautifulSoup:
+ return BeautifulSoup(html, "html.parser")
+
+
async def check_resp(resp: _Response, /) -> None:
if "html" not in resp.content_type:
- return None
+ return
+
+ posibilities = [cls for cls in (DDosGuard, CloudFlareTurnstile, Anubis) if cls.may_be_challenge(resp)]
+ if not posibilities:
+ return
- return check_html(await resp.text())
+ for protection in posibilities:
+ if protection._is_challenge(resp): # pyright: ignore[reportPrivateUsage]
+ raise DDOSGuardError(f"{protection.__name__} anti-bot protection detected")
+
+ soup = _soup(await resp.text())
+ check_soup(soup, posibilities)
def check_html(html: str) -> None:
- check_soup(BeautifulSoup(html, "html.parser"))
+ check_soup(_soup(html))
-def check_soup(soup: BeautifulSoup) -> None:
- for protection in (DDosGuard, CloudFlareTurnstile, Anubis):
+def check_soup(soup: BeautifulSoup, /, posibilities: Iterable[type[DDosGuard]] | None = None) -> None:
+ if posibilities is None:
+ posibilities = (DDosGuard, CloudFlareTurnstile, Anubis)
+ for protection in posibilities:
if protection.check(soup):
raise DDOSGuardError(f"{protection.__name__} anti-bot protection detected")
@@ -41,17 +66,28 @@ class DDosGuard:
TITLES = "Just a moment...", "DDoS-Guard"
SELECTOR = ", ".join(
(
- "#cf-challenge-running",
- ".ray_id",
- ".attack-box",
- "#cf-please-wait",
+ "#ddg-captcha",
"#challenge-spinner",
"#trk_jschal_js",
"#turnstile-wrapper",
- ".lds-ring",
)
)
+ @classmethod
+ def may_be_challenge(cls, resp: _Response) -> bool:
+ server = resp.headers.get("server")
+ return bool(server and server.casefold().startswith("ddos-guard"))
+
+ @final
+ @classmethod
+ def is_challenge(cls, resp: _Response) -> bool:
+ return cls.may_be_challenge(resp) and cls._is_challenge(resp)
+
+ @classmethod
+ def _is_challenge(cls, resp: _Response) -> bool:
+ assert resp is not None
+ return False
+
@classmethod
def check(cls, soup: BeautifulSoup) -> bool:
if (
@@ -70,11 +106,31 @@ class CloudFlareTurnstile(DDosGuard):
(
"captchawrapper",
"cf-turnstile",
- "script[src*='challenges.cloudflare.com/turnstile']",
+ "#cf-challenge-running",
+ "#cf-please-wait",
+ "iframe[id^='cf-chl-']",
+ "iframe script:-soup-contains('_cf_chl_opt')",
"script:-soup-contains('Dont open Developer Tools')",
)
)
+ @classmethod
+ @override
+ def may_be_challenge(cls, resp: _Response) -> bool:
+ server = resp.headers.get("server")
+ return bool(server and server.casefold().startswith("cloudflare"))
+
+ @classmethod
+ @override
+ def _is_challenge(cls, resp: _Response) -> bool:
+ mitigated = resp.headers.get("cf-mitigated")
+ return bool(mitigated and mitigated.casefold() == "challenge")
+
+ @classmethod
+ @override
+ def check(cls, soup: BeautifulSoup) -> bool:
+ return super().check(soup) and bool(soup.select_one("script[src*='challenges.cloudflare.com/turnstile/v']"))
+
class Anubis(DDosGuard):
TITLES = ("Making sure you're not a bot!",)
@@ -86,6 +142,14 @@ class Anubis(DDosGuard):
),
)
+ @classmethod
+ @override
+ def may_be_challenge(cls, resp: _Response) -> bool:
+ for cookie in resp.headers.getall("Set-Cookie", ()):
+ if "-anubis-cookie-verification" in cookie or "-anubis-auth" in cookie:
+ return True
+ return False
+
@classmethod
def parse_challenge(cls, soup: BeautifulSoup) -> _AnubisChallenge | None:
if script := soup.select_one(cls.CHALLENGE):
diff --git a/tests/test_ddos_guard.py b/tests/test_ddos_guard.py
index 1d752cbff..4583d7e7a 100644
--- a/tests/test_ddos_guard.py
+++ b/tests/test_ddos_guard.py
@@ -1,7 +1,11 @@
+import dataclasses
+
import pytest
from bs4 import BeautifulSoup
+from multidict import CIMultiDict
from cyberdrop_dl import ddos_guard
+from cyberdrop_dl.clients.request import prepare_headers
from cyberdrop_dl.exceptions import DDOSGuardError
anubis_html = """
@@ -63,3 +67,64 @@ async def test_solve_anubis_challenge() -> None:
async def test_ddos_response_should_raise_ddos_guard_error() -> None:
with pytest.raises(DDOSGuardError):
ddos_guard.check_html(anubis_html)
+
+
[email protected](slots=True)
+class DummyResponse:
+ headers: CIMultiDict[str]
+ status_code: int = 403
+ _text: str = ""
+ content_type: str = "html"
+
+ async def text(self) -> str:
+ return self._text
+
+
+ ("headers", "status_code", "cls", "expected"),
+ [
+ ({}, 403, ddos_guard.DDosGuard, False),
+ ({"server": "ddos-guard"}, 403, ddos_guard.DDosGuard, True),
+ ({"server": "ddos-guard"}, 200, ddos_guard.DDosGuard, True),
+ ({"server": "ddos-guard"}, 403, ddos_guard.Anubis, False),
+ (
+ {
+ "set-cookie": "techaro.lol-anubis-auth=; Path=/; Expires=Sat, 30 May 2026 11:53:24 GMT; Max-Age=0; Secure; SameSite=None"
+ },
+ 200,
+ ddos_guard.Anubis,
+ True,
+ ),
+ ({"server": "ddos-guard"}, 403, ddos_guard.CloudFlareTurnstile, False),
+ ({"server": "cloudflare", "cf-mitigated": "challenge"}, 403, ddos_guard.CloudFlareTurnstile, True),
+ ({}, 403, ddos_guard.CloudFlareTurnstile, False),
+ ({"server": "cloudflare", "cf-mitigated": "challenge"}, 200, ddos_guard.CloudFlareTurnstile, True),
+ ],
+)
+def test_may_by_challenge(
+ headers: dict[str, str], status_code: int, cls: type[ddos_guard.DDosGuard], *, expected: bool
+) -> None:
+
+ resp = DummyResponse(prepare_headers(headers), status_code)
+ assert cls.may_be_challenge(resp) is expected
+
+
+ ("headers", "status_code", "cls", "expected"),
+ [
+ ({}, 403, ddos_guard.DDosGuard, False),
+ ({"server": "ddos-guard"}, 403, ddos_guard.DDosGuard, False),
+ ({"server": "ddos-guard"}, 200, ddos_guard.DDosGuard, False),
+ ({"server": "ddos-guard"}, 403, ddos_guard.Anubis, False),
+ ({"server": "ddos-guard"}, 403, ddos_guard.CloudFlareTurnstile, False),
+ ({"server": "cloudflare", "cf-mitigated": "challenge"}, 403, ddos_guard.CloudFlareTurnstile, True),
+ ({"server": "cloudflare", "cf-mitigated": "challenge"}, 200, ddos_guard.CloudFlareTurnstile, True),
+ ({"server": "ddos-guard", "cf-mitigated": "challenge"}, 200, ddos_guard.CloudFlareTurnstile, False),
+ ],
+)
+def test_is_challenge(
+ headers: dict[str, str], status_code: int, cls: type[ddos_guard.DDosGuard], *, expected: bool
+) -> None:
+
+ resp = DummyResponse(prepare_headers(headers), status_code)
+ assert cls.is_challenge(resp) is expected