2018-07-30 16:36:34 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
|
|
|
'''
|
2018-09-12 11:21:11 +02:00
|
|
|
Create statistics pie charts by tld
|
|
|
|
|
|
|
|
Default tld: lu
|
2018-07-30 16:36:34 +02:00
|
|
|
'''
|
|
|
|
|
2018-09-12 19:30:00 +02:00
|
|
|
import matplotlib
|
|
|
|
matplotlib.use('Agg')
|
2018-07-30 16:36:34 +02:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import redis
|
2018-07-31 10:36:29 +02:00
|
|
|
import argparse
|
2018-07-30 16:36:34 +02:00
|
|
|
import datetime
|
|
|
|
import heapq
|
|
|
|
import operator
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
|
|
|
|
|
|
|
from Helper import Process
|
|
|
|
|
2018-07-31 10:36:29 +02:00
|
|
|
def create_pie_chart(country ,db_key, date, pie_title, path, save_name):
|
2018-07-30 16:36:34 +02:00
|
|
|
|
|
|
|
monthly_credential_by_tld = server_statistics.hkeys(db_key + date)
|
|
|
|
|
|
|
|
l_tld = []
|
|
|
|
for tld in monthly_credential_by_tld:
|
|
|
|
nb_tld = server_statistics.hget(db_key + date, tld)
|
|
|
|
if nb_tld is not None:
|
|
|
|
nb_tld = int(nb_tld)
|
|
|
|
else:
|
|
|
|
nb_tld = 0
|
|
|
|
l_tld.append( (tld, nb_tld) )
|
|
|
|
|
|
|
|
mail_tld_top5 = heapq.nlargest(5, l_tld, key=operator.itemgetter(1))
|
|
|
|
|
|
|
|
# Pie chart, where the slices will be ordered and plotted counter-clockwise:
|
|
|
|
labels = []
|
|
|
|
sizes = []
|
|
|
|
explode = [] # only "explode" the 2nd slice (i.e. 'Hogs')
|
2018-07-31 10:36:29 +02:00
|
|
|
explode_value = 0
|
2018-07-30 16:36:34 +02:00
|
|
|
for tld in mail_tld_top5:
|
2018-07-31 10:36:29 +02:00
|
|
|
labels.append(tld[0] +' ('+str(tld[1])+')')
|
2018-07-30 16:36:34 +02:00
|
|
|
sizes.append(tld[1])
|
2018-07-31 10:36:29 +02:00
|
|
|
explode.append(explode_value)
|
|
|
|
explode_value = explode_value +0.1
|
2018-07-30 16:36:34 +02:00
|
|
|
|
2018-07-31 10:36:29 +02:00
|
|
|
nb_tld = server_statistics.hget(db_key + date, country)
|
2018-07-30 16:36:34 +02:00
|
|
|
if nb_tld is not None:
|
|
|
|
nb_tld = int(nb_tld)
|
|
|
|
else:
|
|
|
|
nb_tld = 0
|
2018-07-31 10:36:29 +02:00
|
|
|
country_label = country + ' ('+str(nb_tld)+')'
|
|
|
|
if country_label not in labels:
|
|
|
|
labels.append(country_label)
|
|
|
|
sizes.append(nb_tld)
|
|
|
|
explode.append(explode_value)
|
2018-07-30 16:36:34 +02:00
|
|
|
explode = tuple(explode)
|
|
|
|
|
|
|
|
fig1, ax1 = plt.subplots()
|
|
|
|
ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
|
|
|
|
shadow=True, startangle=90)
|
|
|
|
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
|
|
|
|
|
|
|
|
ax1.set_title(pie_title)
|
|
|
|
#plt.show()
|
2018-09-12 11:21:11 +02:00
|
|
|
plt.savefig(os.path.join(path,save_name))
|
2018-07-30 16:36:34 +02:00
|
|
|
plt.close(fig1)
|
|
|
|
|
|
|
|
def create_donut_chart(db_key, date, pie_title, path, save_name):
|
|
|
|
|
|
|
|
monthly_credential_by_tld = server_statistics.hkeys(db_key + date)
|
|
|
|
print()
|
|
|
|
|
|
|
|
l_tld = []
|
|
|
|
for tld in monthly_credential_by_tld:
|
|
|
|
nb_tld = server_statistics.hget(db_key + date, tld)
|
|
|
|
if nb_tld is not None:
|
|
|
|
nb_tld = int(nb_tld)
|
|
|
|
else:
|
|
|
|
nb_tld = 0
|
|
|
|
l_tld.append( (tld, nb_tld) )
|
|
|
|
|
|
|
|
mail_tld_top5 = heapq.nlargest(5, l_tld, key=operator.itemgetter(1))
|
|
|
|
|
|
|
|
# Pie chart, where the slices will be ordered and plotted counter-clockwise:
|
|
|
|
recipe = []
|
|
|
|
data = []
|
|
|
|
for tld in mail_tld_top5:
|
|
|
|
recipe.append(tld[0])
|
|
|
|
data.append(tld[1])
|
|
|
|
|
2018-07-31 10:36:29 +02:00
|
|
|
nb_tld = server_statistics.hget(db_key + date, country)
|
2018-07-30 16:36:34 +02:00
|
|
|
if nb_tld is not None:
|
|
|
|
nb_tld = int(nb_tld)
|
|
|
|
else:
|
|
|
|
nb_tld = 0
|
2018-07-31 10:36:29 +02:00
|
|
|
if country not in recipe:
|
|
|
|
recipe.append(country)
|
|
|
|
data.append(nb_tld)
|
2018-07-30 16:36:34 +02:00
|
|
|
|
|
|
|
fig1, ax1 = plt.subplots(figsize=(6, 3), subplot_kw=dict(aspect="equal"))
|
|
|
|
|
|
|
|
wedges, texts = ax1.pie(data, wedgeprops=dict(width=0.5), startangle=-40)
|
|
|
|
|
|
|
|
bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="k", lw=0.72)
|
|
|
|
kw = dict(xycoords='data', textcoords='data', arrowprops=dict(arrowstyle="-"),
|
|
|
|
bbox=bbox_props, zorder=0, va="center")
|
|
|
|
|
|
|
|
for i, p in enumerate(wedges):
|
|
|
|
ang = (p.theta2 - p.theta1)/2. + p.theta1
|
|
|
|
y = np.sin(np.deg2rad(ang))
|
|
|
|
x = np.cos(np.deg2rad(ang))
|
|
|
|
horizontalalignment = {-1: "right", 1: "left"}[int(np.sign(x))]
|
|
|
|
connectionstyle = "angle,angleA=0,angleB={}".format(ang)
|
|
|
|
kw["arrowprops"].update({"connectionstyle": connectionstyle})
|
|
|
|
ax1.annotate(recipe[i], xy=(x, y), xytext=(1.35*np.sign(x), 1.4*y),
|
|
|
|
horizontalalignment=horizontalalignment, **kw)
|
|
|
|
|
|
|
|
ax1.set_title(pie_title)
|
|
|
|
#plt.show()
|
|
|
|
plt.savefig(os.path.join(path, save_name))
|
|
|
|
plt.close(fig1)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
2018-07-31 10:36:29 +02:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description='''This script is a part of the Analysis Information Leak
|
2018-09-12 11:21:11 +02:00
|
|
|
framework. Create statistics pie charts".''',
|
2018-07-31 10:36:29 +02:00
|
|
|
epilog='Example: ./create_lu_graph.py 0 lu now, create_lu_graph.py 0 lu 201807')
|
|
|
|
|
|
|
|
parser.add_argument('type', type=int, default=0,
|
|
|
|
help='''The graph type (default 0),
|
|
|
|
0: all,
|
|
|
|
1: credential_pie,
|
|
|
|
2: mail_pie
|
|
|
|
3: sqlinjection_pie,
|
2018-09-12 11:21:11 +02:00
|
|
|
4: iban_pie,''',
|
|
|
|
choices=[0, 1, 2, 3, 4], action='store')
|
2018-07-31 10:36:29 +02:00
|
|
|
|
2018-09-12 11:21:11 +02:00
|
|
|
parser.add_argument('country', type=str, default="lu",
|
|
|
|
help='''The country code, lu:default''',
|
2018-07-31 10:36:29 +02:00
|
|
|
action='store')
|
|
|
|
|
|
|
|
parser.add_argument('date', type=str, default="now",
|
|
|
|
help='''month %Y%m, example: 201810''', action='store')
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2018-09-12 11:21:11 +02:00
|
|
|
path = os.path.join(os.environ['AIL_HOME'], 'doc', 'statistics') # save path
|
2018-07-30 16:36:34 +02:00
|
|
|
|
|
|
|
config_section = 'ARDB_Statistics'
|
|
|
|
|
|
|
|
p = Process(config_section, False)
|
|
|
|
|
|
|
|
# ARDB #
|
|
|
|
server_statistics = redis.StrictRedis(
|
|
|
|
host=p.config.get("ARDB_Statistics", "host"),
|
|
|
|
port=p.config.getint("ARDB_Statistics", "port"),
|
|
|
|
db=p.config.getint("ARDB_Statistics", "db"),
|
|
|
|
decode_responses=True)
|
|
|
|
|
2018-07-31 10:36:29 +02:00
|
|
|
if args.date == 'now' or len(args.date) != 6:
|
|
|
|
date = datetime.datetime.now().strftime("%Y%m")
|
|
|
|
else:
|
|
|
|
date = args.date
|
|
|
|
|
|
|
|
if args.type == 0:
|
|
|
|
create_pie_chart(args.country, 'credential_by_tld:', date, "AIL: Credential leak by tld", path, 'AIL_credential_by_tld.png')
|
|
|
|
create_pie_chart(args.country, 'mail_by_tld:', date, "AIL: mail leak by tld", path, 'AIL_mail_by_tld.png')
|
|
|
|
create_pie_chart(args.country, 'SQLInjection_by_tld:', date, "AIL: SQLInjection by tld", path, 'AIL_SQLInjection_by_tld.png')
|
2018-09-12 11:21:11 +02:00
|
|
|
create_pie_chart(args.country.upper(), 'iban_by_country:', date, "AIL: Iban by country", path, 'AIL_iban_by_country.png')
|
2018-07-31 10:36:29 +02:00
|
|
|
elif args.type == 1:
|
|
|
|
create_pie_chart(args.country, 'credential_by_tld:', date, "AIL: Credential leak by tld", path, 'AIL_credential_by_tld.png')
|
|
|
|
elif args.type == 2:
|
|
|
|
create_pie_chart(args.country, 'mail_by_tld:', date, "AIL: mail leak by tld", path, 'AIL_mail_by_tld.png')
|
|
|
|
elif args.type == 3:
|
|
|
|
create_pie_chart(args.country, 'SQLInjection_by_tld:', date, "AIL: sqlInjection by tld", path, 'AIL_sqlInjectionl_by_tld.png')
|
|
|
|
elif args.type == 4:
|
2018-09-12 11:21:11 +02:00
|
|
|
create_pie_chart(args.country.upper(), 'iban_by_country:', date, "AIL: Iban by country", path, 'AIL_iban_by_country.png')
|