Difference between revisions of "Spf checker"

From MyWiki
Jump to: navigation, search
Line 1: Line 1:
 +
Reference - https://modwsgi.readthedocs.io/en/master/user-guides/quick-configuration-guide.html <br>
 +
 +
 
<source lang="python">
 
<source lang="python">
  

Revision as of 10:02, 2 February 2023

Reference - https://modwsgi.readthedocs.io/en/master/user-guides/quick-configuration-guide.html


import argparse
import dns.resolver
import smtplib
 
 
# Specify the DNS servers to be queried
dns.resolver.default_resolver = dns.resolver.Resolver(configure=False)
dns.resolver.default_resolver.nameservers = ['8.8.8.8',  '8.8.4.4' ]
 
substring="include"       # If we find this we need to call the function again
good_string="ip4"         # If we find this we append the result to "the_result_list"
another_good_string="ip6"         # If we find this we append the result to "the_result_list"
 
 
the_result_list = []     # The list to which the results will be appended
the_recursive_result_list = []     # The list to which the results will be appended
count = 0
 
def lookup(domain):
   # print("in lookup")
    try:
      test_spf = dns.resolver.resolve(domain , 'TXT')
      for dns_data in test_spf:
            if 'spf1' in str(dns_data):
              the_string  = str(dns_data)
              the_string = the_string.replace('"', '')   # Removing quotes as they mess up queries
              for i in the_string.split():
                  if substring in i:
                    the_result_list.append(i)
                    #new_search=i.split(":")
                    #new_domain=new_search[1]
                    ##print("looking up" + new_domain)
                    #lookup(new_domain)                    # Recursive call
 
                  if good_string in i:
                    the_result_list.append(i)
 
                  if another_good_string in i:
                    the_result_list.append(i)
 
    except:
       print ("Some sort of failure has occurred")
       pass
 
    return(' '.join(the_result_list))
 
 
def recursive_lookup(domain):
   # print("in recursive_lookup")
    try:
      test_spf = dns.resolver.resolve(domain , 'TXT')
      for dns_data in test_spf:
            if 'spf1' in str(dns_data):
              the_string  = str(dns_data)
              the_string = the_string.replace('"', '')   # Removing quotes as they mess up queries
              for i in the_string.split():
                  if substring in i:
                    global count
                    count = count + 1
                    #the_resursive_result_list.append(i)
                    new_search=i.split(":")
                    new_domain=new_search[1]
                    #print("looking up" + new_domain)
                    recursive_lookup(new_domain)                    # Recursive call
 
                  if good_string in i:
                    the_recursive_result_list.append(i)
 
                  if another_good_string in i:
                    the_recursive_result_list.append(i)
 
    except:
       print ("Some sort of failure has occurred")
       pass
 
    return(' '.join(the_recursive_result_list))
 
 
def get_count():
    global count
    return str(count)
 
 
 
def application(environment, start_response):
  from webob import Request, Response
 
  request = Request(environment)
  params = request.params
  post = request.POST
 
  page = """
<html>
<body>
<h1>Sender Policy Framework (SPF) Check</h2>
<p style="color:orange;">This is being served using mod_wsgi and WebOb</p>
<p>
 <form action="" method="POST">
 Enter Domain: <input name="a" size="30" value="%s" />
 <input type="submit" value="Lookup" />
 </form
</p>
<p><h3> Normal lookup:</h3> %s</p>
<p><h3> Recursive lookup, expanding the "includes":</h3> %s</p>
<p><h3> Number of includes = %s </h3></p>
</body>
</html>
""" % (post.get('a'), "".join(lookup(post.get('a'))), "".join(recursive_lookup(post.get('a'))), get_count())
 
  response = Response(body = page,
                      content_type = "text/html",
                      charset = "utf8",
                      status = "200 OK")
  global count
  #print("count is " + str(count))
  count = 0
  the_result_list.clear()
  the_recursive_result_list.clear()
  return response(environment, start_response)