intruders

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 67708e1d9d64b051487d25c1887ea750a0945a19
Author: tin <ichtinnotl@gmail.com>
Date:   Sat, 22 Dec 2018 16:13:46 +0100

adds intruders, plot and alive scripts

Diffstat:
.gitignore | 2++
alive.py | 27+++++++++++++++++++++++++++
intruders.py | 153+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
plot.r | 21+++++++++++++++++++++
4 files changed, 203 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,2 @@ +venv + diff --git a/alive.py b/alive.py @@ -0,0 +1,27 @@ +import requests, sqlite3 +from requests.exceptions import ConnectionError, TooManyRedirects + +live = {} + +def alive(): + conn = sqlite3.connect('two_letter_mk_domains_log.db') + c = conn.cursor() + + for row in c.execute('SELECT domainName FROM full_log WHERE address="CN"'): + domainName = row[0] + print("Checking ..." + row[0]) + try: + request = requests.get("http://" + row[0]) + statusCode = request.status_code + + live[domainName] = statusCode + print(live) + + print("Domain name {} returned status code of {}".format(domainName, statusCode)) + except ConnectionError: + print("Domain {} doesn't respond".format(domainName)) + + print(live) + return None + +alive() diff --git a/intruders.py b/intruders.py @@ -0,0 +1,152 @@ +import subprocess, pprint, itertools, time, sqlite3 + +addresses = {'MK': 0, 'RS': 0, 'BG': 0, 'GR': 0, 'AL': 0, + 'US': 0, 'CA': 0, 'DE': 0, 'FR': 0, 'ES': 0, + 'SE': 0, 'RU': 0, 'IN': 0, 'CN': 0, 'JP': 0, + 'MY': 0, 'GB': 0, 'OTHER_COUNTRY_CODES': 0, 'UNREGISTERED_DOMAINS': 0, + 'TOTAL_FOREIGN_DOMAINS': 0} + +alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', + 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', + 't', 'u', 'v', 'w', 'x', 'y', 'z'] + +def create_database(): + conn = sqlite3.connect('two_letter_mk_domains_log.db') + c = conn.cursor() + + try: + c.execute('''CREATE TABLE full_log + (domainName text, address text, registeredDate text, expireDate text)''') + + c.execute('''CREATE TABLE clean_log + (address text, totalRegistered int)''') + + c.execute('''CREATE TABLE unregistered_domains_log + (unregistered text)''') + + conn.commit() + conn.close() + except Exception as e: + print(e) + + return None + +def main(): + create_database() + + countSavesInDatabase = 0 + + for (x, y) in itertools.product(alphabet, repeat = 2): + domain = x + y + ".mk" + command = f"whois {domain} | grep 'domain:\|registered:\|expire:\|address:'" + + try: + output = str((subprocess.check_output(command, shell=True)), 'UTF-8') + except Exception: + addresses["UNREGISTERED_DOMAINS"] += 1 + logUnregisteredDomain(domain) + continue + + domainName, addressName, registeredDate, expireDate = beautifyOutput(output) + saveAsFullLog = writeFullLog(domainName, addressName, registeredDate, expireDate) + + if saveAsFullLog: + countSavesInDatabase += 1 + print(f"Query number {countSavesInDatabase} was successfully written in database.") + else: + print("Cannot write full log to database for some reason.") # 'for some reason' :D + + # chill or hammer? + # time.sleep(3) + + saveAsCleanLog = writeCleanLog() + + if saveAsCleanLog: + print("Clean log was successfully written in database.") + else: + print("Cannot write clean log to database.") + + print("done.") + return True + +def beautifyOutput(output): + output = output.splitlines() + + for line in output: + """ Get info from the domain line """ + if line.startswith("domain:"): + domainName = line.partition(':')[2].strip() + + """ Get info from the address line """ + if line.startswith("address:"): + addressName = line.partition(':')[2].strip() + + if addressName in addresses: + addresses[addressName] += 1 + break + elif len(addressName) == 2 and addressName.isupper(): + addresses["OTHER_COUNTRY_CODES"] += 1 + break + else: + pass + + """ Get info from the registered line """ + if line.startswith("registered:"): + registeredDate = line.partition(':')[2].strip() + + """ Get info from the expire line """ + if line.startswith("expire:"): + expireDate = line.partition(':')[2].strip() + + return domainName, addressName, \ + registeredDate, expireDate + +def writeFullLog(domainName, addressName, registeredDate, expireDate): + conn = sqlite3.connect('two_letter_mk_domains_log.db') + c = conn.cursor() + + query = f"INSERT INTO full_log VALUES ('{domainName}', '{addressName}', '{registeredDate}', '{expireDate}')" + c.execute(query) + conn.commit() + conn.close() + + return True + +def writeCleanLog(): + total_foreign_domains = 0 + skipAddress = ["MK", "UNREGISTERED_DOMAINS"] + + for address in addresses: + if address in skipAddress: + pass + else: + total_foreign_domains += addresses[address] + + addresses['TOTAL_FOREIGN_DOMAINS'] = total_foreign_domains + + conn = sqlite3.connect('two_letter_mk_domains_log.db') + c = conn.cursor() + + for address in addresses: + query = f"INSERT INTO clean_log VALUES ('{address}', '{addresses[address]}')" + c.execute(query) + conn.commit() + + conn.close() + # pprint.pprint(addresses) + + return True + + +def logUnregisteredDomain(unregistered): + conn = sqlite3.connect('two_letter_mk_domains_log.db') + c = conn.cursor() + + query = f"INSERT INTO unregistered_domains_log VALUES ('{unregistered}')" + c.execute(query) + conn.commit() + conn.close() + + return None + +main()+ \ No newline at end of file diff --git a/plot.r b/plot.r @@ -0,0 +1,21 @@ +# set global chunk options +knitr::opts_chunk$set(echo = FALSE, warning=FALSE, message=FALSE, cache=FALSE) + +suppressPackageStartupMessages(library(tidyverse)) + +#get the data +dnames <- read_csv("full_log.csv", col_types = "ccDD", locale = locale(date_format = "%d.%m.%Y %H:%M:%S")) + +#entries with total count < 5 are grouped as other. +addressSummary <- dnames %>% group_by(address) %>% mutate(count=n()) %>% arrange(count) %>% ungroup %>% mutate(new_address=case_when(count <= 5 ~ "Other", count > 5 ~ address)) + +#split dates +addressSummary <- addressSummary %>% separate(registeredDate, c("Year", "Month", "Day", "Hour", "Minute", "Second"), sep = "-", remove = FALSE) + +#Plot domain names by country +ggplot(addressSummary, aes(new_address)) + geom_bar(aes(x = reorder(new_address, count), fill=new_address), width = 0.3, colour="white") + scale_fill_brewer(palette = "Paired") + coord_flip() + labs(title="Земји од каде лица регистирале 5 или повеќе МК домени\n (во Other се групирани тие со помалку од 5)", x = "Земји", y = "Број на регистрирани домени", fill = "Земја") + +#Plot domain registration by country over time +addressSummary %>% group_by(new_address, Year) %>% mutate(N=n()) %>% ggplot(aes(x=Year, y=N, group=new_address, colour=new_address)) + geom_line() + geom_point() + scale_color_brewer(palette="Paired") + labs(title="Регистрирани домени по земја на потекло на регистрант по години (во Other се групирани тие со помалку од 5)", x = "Години", y = "Број на регистрирани домени", color = "Земја") + theme(axis.text.x = element_text(angle=45, vjust=1, hjust=1)) + +