Skip to main content

Get Telegram notifications when requests are being made to your web server

Project description

pipeline status

Telegram HTTP notification script

This script will notify you of any or selected HTTP requests made to your server, using the Telegram API. It basically performs a tail -F on a specified web server access log file (only Nginx is supported for now). Then for each new request it sends a Telegram message to your specified channel.

Rationale

During a red teaming operation I was tasked with writing a script similar to this one, as part of a phishing campaign we launched for our intial entry. The goal of this script was to keep track of people that clicked on our link, downloaded our malicious payload, and submitted a form with their personal details. We wanted to instantly know when such events happened, so that we could move on to the next flag using their compromised box. I decided to make this script more general-purpose and further maintain it here. :-)

Features

  • Continuously read the access log, even when the log file is rotated;
  • Can only match specified requests (e.g. only /robots.txt, /wp-login.php);
  • Can add the user agent to the message;
  • Can ignore specified IPs (e.g. 127.0.0.1, ::1);
  • Can ignore specified HTTP status codes (e.g. 301, 302);
  • Can look up geo location of the requesting IP;
  • Can post the location on the map;
  • Can silently push Telegram messages.

How to set it up

TL;DR - basically 4 steps:

  1. Create your own Telegram bot;
  2. Create a new (private) channel and add your bot to that channel;
  3. Either hardcode your bot key, chat ID, and web server log file in this script, or use the script arguments;
  4. Consider either editing and installing the systemd / OpenRC service script, or running the script in a screen session to run it in the background.

Detailed explanation:

First, you will need to create your own Telegram bot, which is basically just an API key without any logic (unless you really want to create a full fledged bot). Once you've created your bot using the @BotFather it will also tell your the bot key (API key). You can either hardcode the bot key in this script, or provide it as an argument (-k) when running this script.

Second, create a Telegram (private) channel and write down the chat ID. If you have trouble finding the chat ID, you can use @username_to_id_bot and paste the invite link in there. It will tell you the channel's chat ID, which you can also hardcode into the script if you want, or provide it as an argument (-c) when running this script. Don't forget to also explicitely add the bot to the channel.

Third, either hardcode the web server's access log file location as well, or provide it as an argument (-l) when running this script.

Finally, run the script. You could use the thns.service systemd script if you use systemd. Edit the script and install it like described in the Install section below. Or, you could also quickly run the script in a screen session to run it in the background so that you can leave your shell. See the Example section for an example on how to do this. Now you can safely logout and the script will remain in the background. To attach again, login to your server again and run screen -r thns.

Install

Using Pip:

# python3 -m pip install thns

Or, using setup.py:

# python3 setup.py install

If you use systemd, you can also add the service script. Edit the script with your own arguments and install it like so:

# cp thns.service /etc/systemd/system/
# systemctl daemon-reload
# systemctl enable --now thns.service

For OpenRC, edit the init script and then install it like so:

# cp thns.init /etc/init.d/
# rc-update add thns
# rc-service thns start

Installing the service script remains a manual step for now. I don't know if there's any way to make this part of the pip package (please reach out to me if you know how to). You could also reach out to your local OS package manager and ask them to make an OS package for this. ;-)

Usage

You can use thns in two ways:

  • When installed as a package, call the installed script: thns --help;
  • When Git cloned, call the package directly from the root of the Git repository: python -m thns --help.
usage: thns [-h] [-c TELEGRAM_CHAT_ID] [-g] [-iI IGNORE_IPS [IGNORE_IPS ...]]
            [-iH IGNORE_HTTP_STATUS_CODES [IGNORE_HTTP_STATUS_CODES ...]]
            [-k TELEGRAM_BOT_KEY] [-l LOG_FILE] [-m]
            [-r MATCH_REQUESTS [MATCH_REQUESTS ...]] [-s] [-u]

Get Telegram notifications when requests are being made to your web server

optional arguments:
  -h, --help            show this help message and exit
  -c TELEGRAM_CHAT_ID, --chat-id TELEGRAM_CHAT_ID
                        Specify the Telegram chat ID to post to.
  -g, --geo             Include geographical information.
  -iI IGNORE_IPS [IGNORE_IPS ...], --ignore-ips IGNORE_IPS [IGNORE_IPS ...]
                        Ignore (multiple) IP addresses (e.g. 127.0.0.1 ::1).
  -iH IGNORE_HTTP_STATUS_CODES [IGNORE_HTTP_STATUS_CODES ...], --ignore-http-status-codes IGNORE_HTTP_STATUS_CODES [IGNORE_HTTP_STATUS_CODES ...]
                        Ignore (multiple) HTTP status codes (e.g. 301 302).
  -k TELEGRAM_BOT_KEY, --bot-key TELEGRAM_BOT_KEY
                        Specify the Telegram bot key.
  -l LOG_FILE, --log-file LOG_FILE
                        Specify the (Nginx) log file.
  -m, --map             Post a map pointer.
  -r MATCH_REQUESTS [MATCH_REQUESTS ...], --match-requests MATCH_REQUESTS [MATCH_REQUESTS ...]
                        Match (multiple) specific HTTP requests (e.g. /
                        /robots.txt).
  -s, --silent          Send Telegram messages silently.
  -u, --user-agent      Include the user agent string.

Example

If you've edited and installed the systemd / OpenRC service script (see the Install section), you can start / stop it like any other service:

# systemcl start thnd.service  # systemd
# rc-service thns start        # OpenRC

You could also use screen to quickly run it in the background. For example:

$ screen -S thns -d -m python3 -m thns -m -s -l /var/log/nginx/access.log -c "-1234567890123" -k "123456789:1234567890abcdefghijklmnopqrstuvwxyz" -iI 127.0.0.1 ::1

Then watch your Telegram channel fill:

Example

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

thns-0.0.5.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

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

thns-0.0.5-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file thns-0.0.5.tar.gz.

File metadata

  • Download URL: thns-0.0.5.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.24.1

File hashes

Hashes for thns-0.0.5.tar.gz
Algorithm Hash digest
SHA256 75a768032b93bc257ec2368899e22cf1fd4ec5bde0306a59695d2ccf78554eb1
MD5 49956da3f2c24695aa003c793cde6503
BLAKE2b-256 73aee172f085ec6583f6ca2cdbcffb65fdce5436ea6bd47a4d1e6ba0dc616acf

See more details on using hashes here.

File details

Details for the file thns-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: thns-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.24.1

File hashes

Hashes for thns-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 8dba57f142a810a1565dd31a9c64c89aefb405ff998233699368570ee4eb8525
MD5 1253fee4e43b1782904af6913d75c9d4
BLAKE2b-256 12af831d4b68ece86b313ebe81fae40d81d8ad014f86d9c231a2881db8432a3f

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