import dns.resolver
import sys
import colorama
import platform
from colorama import init, Fore, Back, Style
import re

# pip install -r requirements.txt (colorama)

os = platform.platform()
if os.find('Windows')!= (-1):
	init(convert=True)


print("""



███████╗░░░░░░██╗░░░██╗░█████╗░██╗░░░░░██╗░░░██╗░█████╗░████████╗░█████╗░██████╗░
██╔════╝░░░░░░██║░░░██║██╔══██╗██║░░░░░██║░░░██║██╔══██╗╚══██╔══╝██╔══██╗██╔══██╗
█████╗░░█████╗╚██╗░██╔╝███████║██║░░░░░██║░░░██║███████║░░░██║░░░██║░░██║██████╔╝
██╔══╝░░╚════╝░╚████╔╝░██╔══██║██║░░░░░██║░░░██║██╔══██║░░░██║░░░██║░░██║██╔══██╗
███████╗░░░░░░░░╚██╔╝░░██║░░██║███████╗╚██████╔╝██║░░██║░░░██║░░░╚█████╔╝██║░░██║
╚══════╝░░░░░░░░░╚═╝░░░╚═╝░░╚═╝╚══════╝░╚═════╝░╚═╝░░╚═╝░░░╚═╝░░░░╚════╝░╚═╝░░╚═╝


\x1B[3mSimple Python3 Script for Checking SPF & DMARC Records.\x1B[0m
""" + '\n')
Domain = input('Domain: ')


# Checking SPF
print ('\n[+] Checking SPF Record...')
try:
 	obj_answer = dns.resolver.resolve(Domain, 'TXT')
except:
	sys.exit(Fore.RED + "\n[+] Domain can't be resolved! Check the domain name and try again..")

answer = str(obj_answer.response)
cond = answer.find("v=spf")
if cond != -1:
	print ('[+] SPF Record Found!')
	spf_pos= answer.find("v=spf")
	spf_end_tmp= (answer[spf_pos:].find("\n"))-1
	spf_end= answer[spf_pos:spf_pos+spf_end_tmp]
	print (Fore.GREEN + '[+] Domain: ' + Domain)
	print (Fore.GREEN + '[+] SPF Record: ' +spf_end)

	neutral_check = answer.find('?all')
	fail_check = answer.find('-all')
	soft_check = answer.find('~all')
	pass_check = answer.find('+all')


	if neutral_check != -1:
		print (Fore.RED +'[+] Result: ?all IS FOUND!! Domain emails can be spoofed!')

	elif fail_check != -1:
		print (Fore.GREEN +'[+] Result: -all is found. SPF is correctly configured.')

	elif soft_check != -1:
		print (Fore.GREEN +'[+] Result: ~all is found. SPF is correctly configured.')
	
	elif pass_check != -1:
		print (Fore.RED +'[+] Result: +all DOMAIN IS VERY BADLY CONFIGURED! Domain emails can be spoofed!')

	else:
		print (Fore.RED +'[+] Result: No condition is set for "all"! Domain emails can be spoofed!')	

else:
	print (Fore.RED +'[+] No SPF Record Found!!')



# Checking DMARC
print (Fore.WHITE + '\n\n[+] Checking DMARC Policy..')
try:
 	obj2_answer = dns.resolver.resolve('_dmarc.'+ Domain, 'TXT')
except:
	sys.exit(Fore.RED + "[+] The domain doesn't have DMARC policy configured!")

answer2 = str(obj2_answer.response)	
print (Fore.WHITE + '[+] DMARC Policy Found!')


none_check = re.search("[\;\s]p\=none\;", answer2)
reject_check = re.search("[\;\s]p\=reject\;", answer2)
quarantine_check = re.search("[\;\s]p\=quarantine\;", answer2)


if none_check:
	print (Fore.RED + '[+] Result: DMARC Policy is set as none! Domain emails can be spoofed!')
if reject_check:	
	print (Fore.GREEN + '[+] Result: DMARC Policy is set as reject! Domain emails are safe from spoofing.')
if quarantine_check:	
	print (Fore.GREEN + '[+] Result: DMARC Policy is set as quarantine! Domain emails are safe from spoofing.')
[((10, 5, 10, 24), 'platform.platform', 'platform.platform', ({}, {}), '()', False, 'import platform\n'), ((86, 13, 86, 50), 're.search', 're.search', ({(86, 23, 86, 40): '"""[\\\\;\\\\s]p\\\\=none\\\\;"""', (86, 42, 86, 49): 'answer2'}, {}), "('[\\\\;\\\\s]p\\\\=none\\\\;', answer2)", False, 'import re\n'), ((87, 15, 87, 54), 're.search', 're.search', ({(87, 25, 87, 44): '"""[\\\\;\\\\s]p\\\\=reject\\\\;"""', (87, 46, 87, 53): 'answer2'}, {}), "('[\\\\;\\\\s]p\\\\=reject\\\\;', answer2)", False, 'import re\n'), ((88, 19, 88, 62), 're.search', 're.search', ({(88, 29, 88, 52): '"""[\\\\;\\\\s]p\\\\=quarantine\\\\;"""', (88, 54, 88, 61): 'answer2'}, {}), "('[\\\\;\\\\s]p\\\\=quarantine\\\\;', answer2)", False, 'import re\n'), ((12, 1, 12, 19), 'colorama.init', 'init', (), '', False, 'from colorama import init, Fore, Back, Style\n'), ((37, 1, 37, 93), 'sys.exit', 'sys.exit', ({(37, 10, 37, 92): '(Fore.RED +\n    """\n[+] Domain can\'t be resolved! Check the domain name and try again..""")'}, {}), '(Fore.RED +\n    """\n[+] Domain can\'t be resolved! Check the domain name and try again..""")', False, 'import sys\n'), ((80, 1, 80, 76), 'sys.exit', 'sys.exit', ({(80, 10, 80, 75): '(Fore.RED + "[+] The domain doesn\'t have DMARC policy configured!")'}, {}), '(Fore.RED + "[+] The domain doesn\'t have DMARC policy configured!")', False, 'import sys\n')]