|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- ## Editez le fichier config.py pour y mettre les bonnes variables.
- ## Utilisez la commande 'python3 -m pip install cowsay' pour ajouter ce MOTD.
- ## Sinon, supprimez cette partie du programme jusqu'au prochain commentaire
- import socket
-
-
-
- #import cowsay
- #cowsay.ghostbusters("SSH-Notifier \n The owner has been notified of this login")
-
- ## importation des dependances
- import re
- import sys
- from config import config
- sys.path.insert(1, config["plateform_directory"])
- from telegram import telegram_send_message
- from discord import discord_send_message
- import os
- import subprocess
- import json
- import requests
- #Pattern pour reconnaitre une IP
- ip_pattern = "(?:^|\b(?<!\.))(?:1?\d\d?|2[0-4]\d|25[0-5])(?:\.(?:1?\d\d?|2[0-4]\d|25[0-5])){3}(?=$|[^\w.])"
-
- def main():
- out = subprocess.Popen(['tail', '-6', '/var/log/auth.log'],
- stdout=subprocess.PIPE,
- stderr=subprocess.STDOUT)
- stdout,stderr = out.communicate()
- resultat = stdout.decode('ascii')
-
- auth = resultat.split(' ')
-
- for i in range(len(auth)):
- line = auth[i]
-
- if find_ip(line) != None :
- IP = line
-
- where = geoip(IP)
- message = "Nouvelle connexion sur " + socket.gethostname() + " Provenant de " + IP + '\n' + 'Pays : ' + where['countryName'] + '\n' + 'Ville : ' + where['cityName'] + '\n' + 'Code Postal : ' + where['zipCode']
-
- if config["plateform"] == 'telegram' :
- telegram_send_message(message)
- if config["plateform"] == 'discord' :
- discord_send_message(message)
-
-
-
- def find_ip(str) :
- pattern = re.compile(ip_pattern)
- ip = re.match(pattern, str)
- return ip
-
- def geoip(ip):
- key = config['ipinfodb_key']
- url_api = 'http://api.ipinfodb.com/v3/ip-city/?key=' + key + '&ip=' + ip + "&format=json"
- localisation = requests.get(url_api)
- if str(localisation) != '<Response [200]>' :
- print("Mauvaise réponse de l'api")
- return "Echec de la recuperation du lieu"
- data = localisation
- return data.json()
-
- if __name__ == '__main__':
- try :
- main()
- except :
- main()
|