Outils d'analyse de la centralisation du Web à partir de données recueillies par Firefox LightBeam.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

165 lines
4.8 KiB

#!/usr/bin/python3
# coding: utf-8
###
# Analyseur des données produites par LightBeam
# Version : 0.1
# Créé par Eliott Lavier le 08/12/2020
###
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (C) 2020 Eliott Lavier
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import json.decoder
import socket
import ipwhois as whois
from asn_db import db
colors = {
"red" : "\033[0;31m",
"green" : "\033[0;32m",
"rst" : "\033[0m"
}
jsonFile = 0
domains = []
# Récupère et trie les noms de domaines stockés dans le fichier JSON.
def getDomains(data):
jsonData = json.load(data)
firstPartyDomain = []
thirdPartyDomain = []
for i in jsonData.items():
if i[1]["firstParty"] == True:
firstPartyDomain.append(i[1]["hostname"])
else:
thirdPartyDomain.append(i[1]["hostname"])
return {
"FirstPartyDomains" : firstPartyDomain,
"ThirdPartyDomains" : thirdPartyDomain
}
# Recherche l'IPv4 ou de l'IPv6 du nom de domaine.
def getIpFromDomain(domains):
for party in domains.items():
for index, domain in enumerate(party[1]):
ip = 0
try:
ip = socket.gethostbyname(domain)
except Exception:
pass
domains[party[0]][index] = [domain, ip]
return domains
# Recherche l'ASN associé aux addresses IP.
def getAsnFromIp(domains):
for party in domains.items():
for index, domainInfo in enumerate(party[1]):
asn = 0
try:
lookup = whois.IPWhois(domainInfo[1])
asn = lookup.lookup_rdap()
asn = "AS" + str(asn["asn"])
except Exception:
pass
domains[party[0]][index].append(asn)
return domains
# Compare les ASN recus à ceux de la base de donnée.
def compareAsn(domains):
global db
for party in domains.items():
for index, domainInfo in enumerate(party[1]):
for group in db:
for dbAsn in group:
if domainInfo[2] == dbAsn:
domains[party[0]][index].append(True)
return domains
# Fait des statistiques à partir des données générées.
def makeStats(domains):
print(colors["green"] + "\n======================================================")
print(colors["red"] + " RESULTATS")
print(colors["green"] + "======================================================\n" + colors["rst"])
totalCentralisation = 0
domainCount = []
for party in domains.items():
centralisedCount = 0
for domainInfo in party[1]:
if len(domainInfo) > 3:
if domainInfo[3] == True:
centralisedCount+=1
domainCount.append(len(party[1]))
if party[0] == "FirstPartyDomains":
print("Taux de centralisation des domaines 1st party : " +
colors["red"] + "{:.1f}%".format((centralisedCount / len(party[1]))*100) + colors["rst"])
else:
print("Taux de centralisation des domaines 3rd party : " +
colors["red"] + "{:.1f}%".format((centralisedCount / len(party[1]))*100) + colors["rst"])
totalCentralisation += centralisedCount
print("Taux de centralisation global : " + colors["red"] +
"{:.1f}%\n".format((totalCentralisation / (domainCount[0] + domainCount[1]))*100) + colors["rst"])
print(colors["green"] + "======================================================" + colors["rst"])
# Enregistre les correspondances Nom de Domaine <-> IP <-> ASN.
def saveInfos(domains):
pass
if __name__ == "__main__" :
if len(sys.argv) != 2:
print(colors["red"] + "ERREUR : Nombre d'arguments incorrect !" + colors["rst"])
exit(1)
else:
jsonFile = open(sys.argv[1])
print("Récupération des noms de domaine...")
domains = getDomains(jsonFile)
print("Récupération des addresses IP...")
domains = getIpFromDomain(domains)
print("Récupération des ASN...")
domains = getAsnFromIp(domains)
print("Comparaison des ASN avec la base de données...")
domains = compareAsn(domains)
print("Calcul des résultats...")
makeStats(domains)
exit(0)