🤖
Security

Protecting your Telegram bot token: how it leaks and how to recover

24.11.2026
← All articles

When you create a Telegram bot, BotFather gives you a token made of digits, a colon and a long string of characters. Many people treat it as an ordinary setting and paste it straight into the code. In reality the token is the only key to the bot: whoever holds it can send messages as the bot through the Bot API, receive messages addressed to it and change its webhook URL. No password or extra confirmation is involved.

What happens when a token is stolen

Imagine a private school in Samarkand running a bot that tells parents about grades and attendance. Hundreds of names and phone numbers live in that bot, and parents also send it personal questions. With the token, an attacker points the webhook at their own server and starts reading every new message. The school's bot seems to "break" because updates no longer reach the school. Worse, the attacker can send every subscriber a "payment link", and parents will trust it because it came from a familiar bot. The token does not expose the full past chat history, but from the moment of takeover the attacker sees everything new.

Where tokens usually leak

The most common case is pushing code to a public repository such as GitHub. Automated scanners constantly crawl public repositories looking for strings that match token patterns, and a token removed in a later commit still lives in git history. The second source is exposed files on hosting: if .env or config.json sits in the web root and the server serves it as plain text, anyone can read it in a browser. The third is logs and screenshots, where full request URLs containing the token end up in error logs or in a terminal photo shared in a group chat.

Keep the token out of the code

Store the token in an environment variable or in a file outside the web root, one level above public_html:

# /home/user/bot.env  (outside public_html)
BOT_TOKEN=1234567890:AAExampleTokenForIllustrationOnly
WEBHOOK_SECRET=long_random_string_123
<?php
$env = parse_ini_file('/home/user/bot.env');
$token = $env['BOT_TOKEN'];

If the file must stay inside the site folder, deny access to it in .htaccess, and add secret files to .gitignore before your first commit. You can also search history for anything that looks like a token:

<Files ".env">
    Require all denied
</Files>
git log -p --all | grep -nE "[0-9]{8,10}:[A-Za-z0-9_-]{35}"

Protect the webhook with secret_token

With a webhook, Telegram POSTs every update to your URL, and anyone who finds that URL can send fake updates. Pass a secret_token when setting the webhook; Telegram then includes it in the X-Telegram-Bot-Api-Secret-Token header of each request, and your code rejects anything without it.

curl -F "url=https://example.uz/bot/hook.php"      -F "secret_token=long_random_string_123"      https://api.telegram.org/bot<TOKEN>/setWebhook
<?php
$got = $_SERVER['HTTP_X_TELEGRAM_BOT_API_SECRET_TOKEN'] ?? '';
if (!hash_equals($env['WEBHOOK_SECRET'], $got)) {
    http_response_code(403);
    exit;
}

Avoid putting the token in the webhook path itself, as some old tutorials suggest, because the full path is written to web server logs. Use getWebhookInfo to see which URL is set and the last delivery error.

After a leak: recovery steps

  1. Send /revoke to BotFather and pick the bot. The old token stops working immediately and you get a new one.
  2. Put the new token in the secret file and restart the bot process.
  3. Set the webhook again with the new token and a new secret_token, then confirm it with getWebhookInfo.
  4. Review the bot's admin rights in groups and channels and remove what it does not need.
  5. If messages were sent in the bot's name, warn users through your official channel.

Then close the hole that caused the leak, or the new token will escape the same way. Never paste tokens into chats, give contractors a separate test bot, and do not reuse one token across projects. On sayt.uz hosting you can keep bot files outside the site folder and manage permissions in the ISPmanager panel.

Related articles

🏴‍☠️ Nulled themes and plugins: why free premium software costs more 🛡️ Securing a Joomla site: updates, extensions and server settings 🔑 How to Disable SSH Password Login and Switch to Keys 🗂️ Exposed .git directory: how your code and passwords leak
🌐 Language
🇺🇿 O⁠'zbek 🇺🇿 Özbek (yangi) 🇺🇿 Ўзбек 🇷🇺 Русский 🇬🇧 English ✓