Skip to content

Commit

Permalink
calibration of the filtered detection, detect if host up before port …
Browse files Browse the repository at this point in the history
…scan and black code format applied
  • Loading branch information
ugomeguerditchian committed Mar 27, 2023
1 parent 8c82105 commit fa1b850
Show file tree
Hide file tree
Showing 9 changed files with 795 additions and 458 deletions.
2 changes: 1 addition & 1 deletion __init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
import libs
import libs
46 changes: 34 additions & 12 deletions libs/custom_logger.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,59 @@
import logging


class CustomFormatter(logging.Formatter):
"""Logging colored formatter, adapted from https://stackoverflow.com/a/56944256/3638629"""

grey = '\x1b[38;21m'
blue = '\x1b[38;5;39m'
yellow = '\x1b[38;5;226m'
red = '\x1b[38;5;196m'
bold_red = '\x1b[31;1m'
reset = '\x1b[0m'
grey = "\x1b[38;21m"
blue = "\x1b[38;5;39m"
yellow = "\x1b[38;5;226m"
red = "\x1b[38;5;196m"
bold_red = "\x1b[31;1m"
reset = "\x1b[0m"

def __init__(self, fmt):
super().__init__()
self.fmt = fmt.split("|")
self.FORMATS = {
logging.DEBUG: self.grey+ self.fmt[0] + self.fmt[1] + self.reset+ self.fmt[2],
logging.INFO: self.blue+ self.fmt[0] + self.fmt[1] + self.reset+ self.fmt[2],
logging.WARNING: self.yellow+ self.fmt[0] + self.fmt[1] + self.reset+ self.fmt[2],
logging.ERROR: self.red+ self.fmt[0] + self.fmt[1] + self.reset+ self.fmt[2],
logging.CRITICAL: self.bold_red+ self.fmt[0] + self.fmt[1] + self.reset+ self.fmt[2]
logging.DEBUG: self.grey
+ self.fmt[0]
+ self.fmt[1]
+ self.reset
+ self.fmt[2],
logging.INFO: self.blue
+ self.fmt[0]
+ self.fmt[1]
+ self.reset
+ self.fmt[2],
logging.WARNING: self.yellow
+ self.fmt[0]
+ self.fmt[1]
+ self.reset
+ self.fmt[2],
logging.ERROR: self.red
+ self.fmt[0]
+ self.fmt[1]
+ self.reset
+ self.fmt[2],
logging.CRITICAL: self.bold_red
+ self.fmt[0]
+ self.fmt[1]
+ self.reset
+ self.fmt[2],
}

def format(self, record):
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt)
return formatter.format(record)


# Create custom logger logging all five levels
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

# Define format for logs
fmt = '%(asctime)s | %(levelname)8s | %(message)s'
fmt = "%(asctime)s | %(levelname)8s | %(message)s"

# Create stdout handler for logging to the console (logs all five levels)
stdout_handler = logging.StreamHandler()
Expand Down
116 changes: 61 additions & 55 deletions libs/dns_request.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#from domain name get dns information and detect all subdomain associated
# from domain name get dns information and detect all subdomain associated
import dns.resolver
import requests
import socket
Expand All @@ -8,136 +8,142 @@


def get_dns_information(domain):
#get the dns information of the domain with dnspyton
#return a list of dns information
dns_informations= []
#set timeout to 0.2 seconds
# get the dns information of the domain with dnspyton
# return a list of dns information
dns_informations = []
# set timeout to 0.2 seconds
socket.setdefaulttimeout(0.2)
# try:
# answers = dns.resolver.resolve(domain, 'NS')
# for rdata in answers:
# dns_informations.append(rdata)
# except:
# pass
try :
answers = dns.resolver.resolve(domain, 'MX')
try:
answers = dns.resolver.resolve(domain, "MX")
for rdata in answers:
dns_informations.append(rdata)
except:
pass
try :
answers = dns.resolver.resolve(domain, 'A')
try:
answers = dns.resolver.resolve(domain, "A")
for rdata in answers:
dns_informations.append(rdata)
except:
pass
try :
answers = dns.resolver.resolve(domain, 'AAAA')
try:
answers = dns.resolver.resolve(domain, "AAAA")
for rdata in answers:
dns_informations.append(rdata)
except:
pass
try :
answers = dns.resolver.resolve(domain, 'CNAME')
try:
answers = dns.resolver.resolve(domain, "CNAME")
for rdata in answers:
dns_informations.append(rdata)
except:
pass
try :
#get the dns information
answers = dns.resolver.resolve(domain, 'TXT')
try:
# get the dns information
answers = dns.resolver.resolve(domain, "TXT")
for rdata in answers:
dns_informations.append(rdata)
except:
pass
return dns_informations

def get_dns_informations_thread(domain :str, threads_number:int) :
dns_informations=[]

def get_dns_informations_thread(domain: str, threads_number: int):
dns_informations = []

with ThreadPoolExecutor(max_workers=threads_number) as executor:
results = executor.map(get_dns_information, domain)
for result in results:
dns_informations+= result
dns_informations += result
return dns_informations

def detect_subdomain(dns_information :list) -> list:
#detect all the subdomain from the dns information
#return a list of subdomain

def detect_subdomain(dns_information: list) -> list:
# detect all the subdomain from the dns information
# return a list of subdomain
subdomains = []
for dns in dns_information:
#get the dns information
# get the dns information
dns = str(dns)
#split the dns information
# split the dns information
dns = dns.split(" ")
#get the subdomain
# get the subdomain
subdomain = dns[0]
#add the subdomain to the list
# add the subdomain to the list
subdomains.append(subdomain)
return subdomains

def detect_real_subdomain(subdomains :list) -> list:
#detect all the real subdomain from the list of subdomain
#return a list of real subdomain

def detect_real_subdomain(subdomains: list) -> list:
# detect all the real subdomain from the list of subdomain
# return a list of real subdomain
real_subdomains = []
for subdomain in subdomains:
#test if the subdomain is real
# test if the subdomain is real
try:
#try to connect to the subdomain
# try to connect to the subdomain
socket.gethostbyname(subdomain)
#if the connection is successful, add the subdomain to the list
# if the connection is successful, add the subdomain to the list
real_subdomains.append(subdomain)
except:
pass
return real_subdomains

def delete_ip_from_list(subdomains :list) -> list:
#delete all the ip address from the list of subdomain
#return a list of subdomain

def delete_ip_from_list(subdomains: list) -> list:
# delete all the ip address from the list of subdomain
# return a list of subdomain
subdomains_without_ip = []
for subdomain in subdomains:
#test if the subdomain is an ip address
# test if the subdomain is an ip address
try:
#try to convert the subdomain to an ip address
# try to convert the subdomain to an ip address
socket.inet_aton(subdomain)
except:
#if the subdomain is not an ip address, add it to the list
# if the subdomain is not an ip address, add it to the list
subdomains_without_ip.append(subdomain)
return subdomains_without_ip

def test_dns_zone_transfer(domain :str) -> list:
#test if the dns zone transfer is enable
#return a list of subdomain

def test_dns_zone_transfer(domain: str) -> list:
# test if the dns zone transfer is enable
# return a list of subdomain
subdomains = []
#get the dns server
dns_server = dns.resolver.resolve(domain, 'NS')
#test if the dns zone transfer is enable
# get the dns server
dns_server = dns.resolver.resolve(domain, "NS")
# test if the dns zone transfer is enable
for server in dns_server:
#get the dns server
# get the dns server
server = str(server)
#split the dns server
# split the dns server
server = server.split(" ")
#get the dns server
# get the dns server
server = server[0]
#test if the dns zone transfer is enable
# test if the dns zone transfer is enable
try:
#try to get the dns zone transfer
# try to get the dns zone transfer
answers = dns.query.xfr(server, domain)
#if the dns zone transfer is enable, add the subdomain to the list
# if the dns zone transfer is enable, add the subdomain to the list
for answer in answers:
subdomains.append(answer)
except:
pass
return subdomains


def main(domain, threads_number):
#get the dns information
# get the dns information
dns_information = get_dns_informations_thread(domain, threads_number)
#detect all the subdomain
# detect all the subdomain
subdomains = detect_subdomain(dns_information)
#detect all the real subdomain
# detect all the real subdomain
real_subdomains = detect_real_subdomain(subdomains)
#delete all the ip address from the list of subdomain
# delete all the ip address from the list of subdomain
subdomains_without_ip = delete_ip_from_list(real_subdomains)
#return the list of subdomain
# return the list of subdomain
return subdomains_without_ip
Loading

0 comments on commit fa1b850

Please sign in to comment.