Skip to main content

Anti-abuse security middleware for Telegram bots

Project description

🛡️ Shieldgram

PyPI version Python Tests License

Anti-abuse security middleware for Telegram bots.

Protect your bot from:

  • ✅ Flood attacks
  • ✅ Spam
  • ✅ Command abuse
  • ✅ Automated users
pip install shieldgram

Quick Start

from aiogram import Bot, Dispatcher, Router
from aiogram.filters import Command
from aiogram.types import Message
from shieldgram import Shield

router = Router()
shield = Shield(redis="redis://localhost:6379/0")

@router.message(Command("start"))
async def cmd_start(message: Message) -> None:
    await message.answer("Hello!")

async def main():
    bot = Bot(token="YOUR_BOT_TOKEN")
    dp = Dispatcher()
    dp.message.middleware(shield)
    dp.include_router(router)
    await dp.start_polling(bot)

Threat Score Engine

Shieldgram assigns a threat score to every user action:

Signal Weight
Flood (burst) +0.40
Spam (repeat) +0.30
Too many links +0.20
Suspicious behavior +0.10
Score Verdict
0.0–0.4 ALLOW
0.4–0.7 WARN
0.7–1.0 BLOCK

Architecture

Telegram Update → Shield Middleware → Detection Engine
                                          │
                              ┌───────────┼───────────┐
                         RateLimiter  FloodDetector  ...
                              │           │
                              └───────────┘
                                    │
                            Threat Score Engine
                                    │
                            Decision Engine
                               │        │
                           Redis    PostgreSQL

Configuration

shield = Shield(
    redis="redis://localhost:6379/0",
    rate_limiter={
        "sliding_window_seconds": 60,
        "max_requests_per_window": 30,
        "token_bucket_rate": 5,
        "token_bucket_burst": 10,
    },
    flood_detector={
        "burst_window_seconds": 5,
        "burst_threshold": 10,
        "repeat_window_seconds": 30,
        "repeat_threshold": 5,
    },
    block_threshold=0.7,
    warn_threshold=0.4,
    ignore_users=[123456789],
)

Run the demo

docker compose up -d

Or locally:

pip install shieldgram
export BOT_TOKEN="your_token"
python examples/protected_bot.py

Then attack it:

python examples/attack_simulator.py

Roadmap

  • v0.1 — Middleware, Rate Limiter, Flood Detector, Threat Scoring
  • v0.2 — Spam detection, Reputation system, PostgreSQL logs
  • v0.3 — Dashboard, API, Docker deployment
  • v1.0 — Plugin system, production hardening

License

MIT — see LICENSE

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

shieldgram-0.2.0.tar.gz (20.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

shieldgram-0.2.0-py3-none-any.whl (23.0 kB view details)

Uploaded Python 3

File details

Details for the file shieldgram-0.2.0.tar.gz.

File metadata

  • Download URL: shieldgram-0.2.0.tar.gz
  • Upload date:
  • Size: 20.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for shieldgram-0.2.0.tar.gz
Algorithm Hash digest
SHA256 21415e2247db90240cf2c4eef765f6183a29bc552b9389d67ec058f29bfb229b
MD5 67a749e55e8fa28d3c2014f068a6ffda
BLAKE2b-256 96a14b36e30ce5835a80237b31f58f347d007dffd55d0df2607920d3afe2061f

See more details on using hashes here.

File details

Details for the file shieldgram-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: shieldgram-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 23.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for shieldgram-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 38f13592c02ab844f480c2ef905fc84123137971f55375f9952c9964cb7da824
MD5 1912fe731750d7123f052f44c5bc4f01
BLAKE2b-256 5458bf4600591f8c85e975330493f20eb1ed5ac0cc74ec13150f12e1a507e383

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page