119 lines
3.6 KiB
Python
Executable file
119 lines
3.6 KiB
Python
Executable file
#!/usr/bin/env python
|
|
# -*- coding: utf8 -*-
|
|
|
|
import psycopg2
|
|
import psycopg2.extras
|
|
import sys
|
|
import smtplib
|
|
|
|
sys.path.append('/usr/scripts/gestion')
|
|
from config import mac_prise
|
|
from affich_tools import tableau
|
|
sys.path.append('/usr/scripts/lc_ldap')
|
|
import lc_ldap
|
|
import collections
|
|
|
|
ldap = lc_ldap.lc_ldap_local()
|
|
|
|
membres_actifs = ldap.search('(|(droits=Cableur)(droits=Nounou)(droits=Apprenti)(droits=Bureau))')
|
|
chambres_ma = []
|
|
for membre_actif in membres_actifs:
|
|
try:
|
|
chambres_ma.append(str(membre_actif['chbre'][0]).lower())
|
|
except:
|
|
pass
|
|
|
|
clubs = ldap.search('cid=*')
|
|
chambres_clubs = []
|
|
for club in clubs:
|
|
try:
|
|
chambres_clubs.append(str(club['chbre'][0]).lower())
|
|
except:
|
|
pass
|
|
|
|
conn = psycopg2.connect(user='crans', database='mac_prises')
|
|
cur = conn.cursor(cursor_factory = psycopg2.extras.DictCursor)
|
|
|
|
def reperage_mac_inconnue():
|
|
"""
|
|
Fonction de repérage d'une mac qui ne devrait pas être
|
|
dans telle chambre, sur une plage de 24h, suivant un
|
|
paramètre de config pour le nombre d'occurrences.
|
|
|
|
Sans doute le truc le plus important, sera en tête du mail
|
|
"""
|
|
|
|
output = u""
|
|
probleme = {}
|
|
requete = "SELECT chambre, mac, COUNT(mac) as nb_min FROM correspondance WHERE date >= timestamp 'now' - interval '24 hours' GROUP BY chambre, mac ORDER BY chambre ASC;"
|
|
cur.execute(requete)
|
|
fetched = cur.fetchall()
|
|
liste_parsee = collections.defaultdict(dict)
|
|
|
|
for entry in fetched:
|
|
liste_parsee[entry['chambre']][entry['mac']] = int(entry['nb_min'])
|
|
|
|
for chambre in liste_parsee.keys():
|
|
if chambre in chambres_ma + chambres_clubs:
|
|
continue
|
|
|
|
for mac in liste_parsee[chambre].keys():
|
|
try:
|
|
proprio_associe = ldap.search('macAddress=%s' % mac)[0].proprio()
|
|
if str(proprio_associe['chbre'][0]).lower() == chambre.lower():
|
|
garbage = liste_parsee[chambre].pop(mac)
|
|
except:
|
|
pass
|
|
number = sum(liste_parsee[chambre].values())
|
|
|
|
if number >= mac_prise.max_inconnues_par_jour:
|
|
probleme[chambre] = (liste_parsee[chambre].keys(), number)
|
|
|
|
if len(probleme) > 0:
|
|
output += mac_prise.titre_mac_inconnue+"\n"
|
|
|
|
longueur_max = max([len(", ".join(a[0])) for a in probleme.values()] + [len("macs")]) + 2
|
|
largeurs = (len('chambre') + 2, longueur_max, len('compteur') + 2, len('seuil') + 2)
|
|
|
|
data = []
|
|
clefs = probleme.keys()
|
|
clefs.sort()
|
|
for clef in clefs:
|
|
data.append([clef, ", ".join(probleme[clef][0]), probleme[clef][1], mac_prise.max_inconnues_par_jour])
|
|
|
|
output += tableau(data, ('chambre', 'macs', 'compteur', 'seuil'), largeurs, ('c', 'c', 'c', 'c'))
|
|
output += u"\n\n\n"
|
|
|
|
return output
|
|
|
|
if __name__ == '__main__':
|
|
output = u'Repérage de spoof potentiel par comptage'
|
|
coupure = len(output)
|
|
|
|
output += reperage_mac_inconnue()
|
|
|
|
if len(output) == coupure and not mac_prise.hargneux:
|
|
sys.exit(0)
|
|
|
|
message = """From: %(from)s
|
|
To: %(to)s
|
|
Subject: %(subject)s
|
|
Content-Type: text/plain, charset="UTF-8"
|
|
|
|
%(contenu)s
|
|
|
|
--
|
|
Script d'analyse mac_prise (en test)
|
|
|
|
"""
|
|
|
|
corps = message % { 'from': 'Spoofing watcher <spoof-watcher@crans.org>',
|
|
'to': 'test@lists.crans.org',
|
|
'subject': 'Analyse horaire du spoofing',
|
|
'contenu': output,
|
|
}
|
|
|
|
mail = smtplib.SMTP('localhost')
|
|
mailfrom = 'spoof-watcher@crans.org'
|
|
mailto = 'test@lists.crans.org'
|
|
mail.sendmail(mailfrom, mailto, corps.encode('utf-8'))
|