#!/usr/bin/python
import os,sys,string
import httplib, urllib
import json,requests
# Variables globales estaticas
SERVER_INCAPSULA = "my.incapsula.com"
PAGE_SIZE = 125
PAGE_NUM = 0
def getBandwidthSite(site_id):
c = httplib.HTTPSConnection(SERVER)
if (time_range == "custom"):
params = urllib.urlencode({'api_id':api_id,'api_key':api_key,'site_id':site_id,'stats':'bandwidth_timeseries','time_range':time_range,'start':start_time,'end':end_time})
else :
params = urllib.urlencode({'api_id':api_id,'api_key':api_key,'site_id':site_id,'stats':'bandwidth_timeseries','time_range':time_range})
headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
c.request("POST", "/api/stats/v1",params,headers)
response = c.getresponse()
data = response.read()
c.close()
json_data = json.loads(data)
total = 0
for i in json_data["bandwidth_timeseries"][0]["data"]:
cont = 0
for j in i:
cont+=1
if (cont%2 == 0):
total=total+j
#print "total bytes:%s" %total
return total
def getSitesList():
# Devuelve una lista con los sites que hay en esa cuenta
# con los valores de api_id y api_key ya sabe el account_id
URI = "/api/prov/v1/sites/list"
c = httplib.HTTPSConnection(SERVER)
params = urllib.urlencode({'api_id':api_id,'api_key':api_key,'page_size':PAGE_SIZE,'page_num':PAGE_NUM})
headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
c.request("POST", URI,params,headers)
response = c.getresponse()
data = response.read()
c.close()
json_data = json.loads(data)
# print "Site_id = %s" %json_data["sites"][1]["site_id"]
# print "Domain = %s" %json_data["sites"][1]["domain"]
site_id_list= []
domain_list = []
for i in json_data["sites"]:
#print "Site_id => %s" %i["site_id"]
#print "Domain => %s" %i["domain"]
site_id_list.append(i["site_id"])
domain_list.append(i["domain"])
return site_id_list,domain_list
def getVisits(site_id):
c = httplib.HTTPSConnection("my.incapsula.com")
if (time_range == "custom"):
params = urllib.urlencode({'api_id':api_id,'api_key':api_key,'site_id':site_id,'stats':'visits_timeseries','time_range':time_range,'start':start_time,'end':end_time})
else :
params = urllib.urlencode({'api_id':api_id,'api_key':api_key,'site_id':site_id,'stats':'visits_timeseries','time_range':time_range})
headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
c.request("POST", "/api/stats/v1",params,headers)
response = c.getresponse()
data = response.read()
c.close()
json_data = json.loads(data)
visitas_humanas = 0
for i in json_data["visits_timeseries"][0]["data"]: # visitas humanas
cont = 0
for j in i:
cont+=1
if (cont%2 == 0):
visitas_humanas=visitas_humanas+j
#print "total visitas humanas :%s" %visitas_humanas
visitas_bots = 0
for i in json_data["visits_timeseries"][1]["data"]: # visitas bots
cont = 0
for j in i:
cont+=1
if (cont%2 == 0):
visitas_bots=visitas_bots+j
#print "total visitas bots :%s" %visitas_bots
return visitas_humanas,visitas_bots,visitas_humanas+visitas_bots
def getHits(site_id):
c = httplib.HTTPSConnection("my.site.com")
if (time_range == "custom"):
params = urllib.urlencode({'api_id':api_id,'api_key':api_key,'site_id':site_id,'stats':'hits_timeseries','time_range':time_range,'start':start_time,'end':end_time})
else :
params = urllib.urlencode({'api_id':api_id,'api_key':api_key,'site_id':site_id,'stats':'hits_timeseries','time_range':time_range})
headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
c.request("POST", "/api/stats/v1",params,headers)
response = c.getresponse()
data = response.read()
c.close()
json_data = json.loads(data)
hits_humanos = 0
for i in json_data["hits_timeseries"][0]["data"]: # hits humanos
cont = 0
for j in i:
cont+=1
if (cont%2 == 0):
hits_humanos=hits_humanos+j
#print "total hits humanos :%s" %hits_humanos
hits_bots = 0
for i in json_data["hits_timeseries"][1]["data"]: # hits bots
cont = 0
for j in i:
cont+=1
if (cont%2 == 0):
hits_bots=hits_bots+j
# print "total hits bots :%s" %hits_bots
return hits_humanos,hits_bots,hits_humanos+hits_bots
def generateCSVReport():
file = open("./report.csv","w")
file.write("Site,Human Visits,Bot Visits,Total Visits,Hits,Bandwidth (MB),SQL Injection,Illegal Resource Access,Cross Site Scripting,Bad Bots,DDoS\n")
j = 0
for site in lista_sites[0]:
#print "Site: %s (%s) => Ancho de banda (%s)" %(lista_sites[1][j],site,getBandwidthSite(site))
visitas_list = getVisits(site)
hits_list = getHits(site)
threat_list = getThreats(site)
bandwidth = round(getBandwidthSite(site)/float(1024*1024),2)
domain = lista_sites[1][j]
file.write(domain+" (" + str(site) + ")," + str(visitas_list[0]) + "," + str(visitas_list[1]) + "," + str(visitas_list[2]) + "," + str(hits_list[2]) + "," + str(bandwidth) + "," + str(threat_list[0]) + "," + str(threat_list[1]) + "," + str(threat_list[2]) + "," + str(threat_list[3]) + "," + str(threat_list[4]) + "\n")
j+=1
print "%s/%s Sites Completados: %s " %(j,len(lista_sites[0]),lista_sites[1][j-1])
file.close()
return
def getThreats(site_id):
c = httplib.HTTPSConnection("my.site.com")
if (time_range == "custom"):
params = urllib.urlencode({'api_id':api_id,'api_key':api_key,'site_id':site_id,'stats':'threats','time_range':time_range,'start':start_time,'end':end_time})
else :
params = urllib.urlencode({'api_id':api_id,'api_key':api_key,'site_id':site_id,'stats':'threats','time_range':time_range})
headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
c.request("POST", "/api/stats/v1",params,headers)
response = c.getresponse()
data = response.read()
c.close()
json_data = json.loads(data)
sql_injection = 0
illegal_resource = 0
xss = 0
bad_bots = 0
ddos = 0
# print "XSS -> %s" %xss
return sql_injection,illegal_resource,xss,bad_bots,ddos
if __name__ == "__main__" :
api_id = "xxxxxx"
api_key = "xxxxxxxxxxxxx"
# 1 de Enero 2015 a las 00:00:00h en milisegundos
#start_time = "1420070400000"
# 31 de Enero 2015 a las 23.59.59h en milisegundos
#end_time = "1422748799000"
start_time = "1428883200000"
end_time = "1429487999000"
time_range = "custom"
lista_sites = getSitesList()
generateCSVReport()
# Probar un site especifico
##visitas = getVisits("1872262")
##print "Visitas humanas => %s " %visitas[0]
## print "Visitas bots => %s " %visitas[1]
##print "Total Visitas => %s " %visitas[2]
# print "lista de dominios => %s" %lista_sites[1]i
# print "site id => %s " %lista_sites[0]i
#j = 0
#for site in lista_sites[0]:
# print "Site: %s (%s) => Ancho de banda (%s)" %(lista_sites[1][j],site,getBandwidthSite(site))
# j+=1
#for site in lista_sites[0]:
# visitas_list = getVisits(site)
# print "Visitas humanas => %s " %visitas_list[0]
# print "Visitas bots => %s " %visitas_list[1]
# print "Total Visitas => %s " %visitas_list[2]
#hits_list = getHits(site)
#for site in lista_sites[0]:
# print "Hits humanos => %s " %hits_list[0]
# print "Hits bots => %s " %hits_list[1]
# print "Hits Totales => %s " %hits_list[2]
#threats_list = getThreats(site)
#for site in lista_sites[0]:
# print "SQLi => %s " %threats_list[0]
# print "Illegal Resource => %s " %threats_list[1]
# print "XSS => %s " %threats_list[2]
# print "Bad Bots => %s " %threats_list[3]