<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Xabi</title>
    <description>The latest articles on Forem by Xabi (@xabikip).</description>
    <link>https://forem.com/xabikip</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F127824%2F9872c87f-9368-4279-adbc-16a3030b85e8.jpg</url>
      <title>Forem: Xabi</title>
      <link>https://forem.com/xabikip</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/xabikip"/>
    <language>en</language>
    <item>
      <title>Get a Telegram message when someone connects via ssh on your linux server</title>
      <dc:creator>Xabi</dc:creator>
      <pubDate>Thu, 30 Mar 2023 10:29:10 +0000</pubDate>
      <link>https://forem.com/xabikip/telegram-message-when-ssh-login-happens-on-your-linux-server-33bn</link>
      <guid>https://forem.com/xabikip/telegram-message-when-ssh-login-happens-on-your-linux-server-33bn</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;We create a &lt;strong&gt;Telegram Bot&lt;/strong&gt; and a &lt;strong&gt;Bash script&lt;/strong&gt; that sends a message to a Telegram chat when a user logs in via SSH on your linux server. The script is designed to help administrators &lt;strong&gt;monitor SSH login activity&lt;/strong&gt; on their server and receive notifications in real-time.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  CREATE A TELEGRAM BOT
&lt;/h3&gt;

&lt;p&gt;We look for the user &lt;code&gt;@BotFather&lt;/code&gt; in our Telegram application, and we start to "talk to him".&lt;/p&gt;

&lt;p&gt;We ask to launch the creation of a bot: &lt;code&gt;/newbot&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, follow the steps and if everything went well, it will return the &lt;strong&gt;Token&lt;/strong&gt; that we will use to configure our alert sistem. Save this &lt;strong&gt;Token&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu1pbjgbr38n94gzqvc4q.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu1pbjgbr38n94gzqvc4q.jpg" alt="Telegram bot created estpes" width="379" height="618"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we need to create a new Group in Telegram which is where the alert messages will arrive. And we add the bot that we have created before as if it were a normal user. We need the &lt;strong&gt;ChatID&lt;/strong&gt; of this group. To know this &lt;strong&gt;ChatID&lt;/strong&gt; there are many options, I used this one that is well explained in stackoverflow:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://stackoverflow.com/a/38388851/3115093" rel="noopener noreferrer"&gt;https://stackoverflow.com/questions/32423837/telegram-bot-how-to-get-a-group-chat-id&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In conclusion, we need the &lt;strong&gt;token&lt;/strong&gt; that has been generated when creating our bot and the &lt;strong&gt;ChatID&lt;/strong&gt; of the group that we have created. This is very important so that we can later use it in our bash script.&lt;/p&gt;

&lt;h3&gt;
  
  
  CREATE A BASH SCRIPT
&lt;/h3&gt;

&lt;p&gt;It's time to have fun. We will create a bash script that will send a message to our Telegram bot.&lt;/p&gt;

&lt;p&gt;We simply generate a file in the path &lt;code&gt;/etc/ssh&lt;/code&gt; as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;xabikip@debian11:~$ sudo nano /etc/ssh/xabikip-login-ssh.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And we put this content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/usr/bin/env bash

TELEGRAM_TOKEN="90273023019:tHeTokENyougetwhENcreatETeleGRamBoT"
CHAT_ID="-xxxxxxxxxxx"

if [ ${PAM_TYPE} = "open_session" ]; then
  MESSAGE="$PAM_USER@$PAM_RHOST: New login $PAM_SERVICE"
  curl -s -X POST "https://api.telegram.org/bot$TELEGRAM_TOKEN/sendMessage" -d chat_id="$CHAT_ID" -d text="$MESSAGE" &amp;gt; /dev/null 2&amp;gt;&amp;amp;1
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We give the right execution permissions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;xabikip@debian11:~$ sudo chmod +x /etc/ssh/xabikip-login-ssh.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We set the following variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;xabikip@debian11:~$ PAM_TYPE="open_session" /etc/ssh/xabikip-login-ssh.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we modify the PAM configuration. The script then uses the PAM (Pluggable Authentication Modules) system to trigger when a user successfully logs in. PAM is a system used by Linux and other Unix-like operating systems to handle user authentication. By using PAM, the script can be sure that it will only trigger when a user has successfully authenticated and logged in.&lt;/p&gt;

&lt;p&gt;First, we make a backup:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;xabikip@debian11:~$ sudo cp /etc/pam.d/sshd /etc/pam.d/sshd.old
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And we add at the end of the file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;xabikip@debian11:~$ sudo nano /etc/pam.d/sshd
-----------
# Login Telegram Notifications
session optional pam_exec.so /etc/ssh/xabikip-login-ssh.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s all, we can try to see if we done well by login on our server and seen if we got an alert message.&lt;/p&gt;

&lt;p&gt;Now, I will to explain the script.&lt;/p&gt;

&lt;p&gt;The script begins by setting the Telegram bot token and chat ID as environment variables. The Telegram bot token is a unique identifier that allows the script to interact with the Telegram API, while the chat ID specifies the target chat where the message will be sent.&lt;/p&gt;

&lt;p&gt;When a user logs in, the script uses the PAM_TYPE variable to determine whether the action was an "open_session" (i.e., a successful login). If the login was successful, the script constructs a message that includes the user's name, the remote host they logged in from, and the service they are logging into (e.g., SSH)&lt;/p&gt;

&lt;p&gt;The curl command is then used to send the message to the Telegram API. The command includes the Telegram bot token, chat ID, and message text as parameters. Any output from the curl command is redirected to /dev/null, which means that the script will not produce any visible output when it runs.&lt;/p&gt;

&lt;p&gt;Overall, this script provides a simple and effective way for administrators to monitor SSH login activity on their server and receive notifications in real-time. By using the Telegram API, the notifications can be sent to a group chat or individual user, making it easy to keep track of login activity across multiple servers and users.&lt;/p&gt;

&lt;p&gt;Any improvement or any doubt you have will be welcome in the comments ;)&lt;/p&gt;

</description>
      <category>linux</category>
      <category>telegram</category>
      <category>tutorial</category>
      <category>bash</category>
    </item>
  </channel>
</rss>
