# Additional materials:
# https://andreyex.ru/linux/komandy-linux-i-komandy-shell/bash-heredoc/ - usage of heredoc
# Telegram - checking of curl command syntax (can be changed)


#!/bin/bash

PATHTOFAILCONF="/etc/fail2ban/jail.conf"
PATHTOBOT="/etc/profile.d/spytrick.sh


apt-get install fail2ban

configfile ()
{
tee -a $PATHTOFAILCONF << "EOF"
[ssh]
enabled = true
port = ssh
filter = sshd
action = iptables[name=sshd, port=ssh, protocol=tcp]
logpath = /var/log/auth.log
maxretry = 2
bantime = 1800

[ssh-ddos]
enabled = true
port = ssh
filter = sshd-ddos
logpath = /var/log/auth.log
maxretry = 2
EOF
}

enableservice ()
{
	systemctl enable fail2ban
	systemctl restart fail2ban
}

botfile ()
{
tee -a $PATHTOBOT << "EOF"
#!/bin/bash
TOKEN='1322235264:AAE7QI-f1GtAF_huVz8E5IBdb5JbWIIiGKI'
MSG_URL='https://api.telegram.org/bot'$TOKEN'/sendMessage'
ID_MSG='1121093080'
gg=$(who) #получаем список сессий
hostname=$(hostname)
lastlog=$(lastlog | grep -E "1")

trap '' INT TSTP

send_message ()
{
  res=$(curl -s -X POST $MSG_URL -d chat_id=$ID_MSG -d text="HOST:$hostname KEYWORD:$item DATA:$lastlog" &)
}

echo -n "Enter your ID "

read item

case "$item" in
    *) echo "Done"
       send_message
esac
EOF
}



main ()
{
    configfile
    enableservice
    botfile
}

main
