Difference between revisions of "Spf checker"

From MyWiki
Jump to: navigation, search
(Created page with "<source lang="python"> <head> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.0.3/styles/default.min.css"> <script src="https:...")
 
Line 1: Line 1:
<source lang="python">
+
import argparse
 
+
<head>
+
  <link rel="stylesheet"
+
        href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.0.3/styles/default.min.css">
+
  <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.0.3/highlight.min.js"></script>
+
  <script>hljs.initHighlightingOnLoad();</script>
+
</head>
+
<body>
+
  <pre><code class="python">
+
 
+
  import argparse
+
 
import dns.resolver
 
import dns.resolver
 
import smtplib
 
import smtplib

Revision as of 09:52, 2 February 2023

import argparse import dns.resolver import smtplib


  1. 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_l ist" 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 append ed 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>

Sender Policy Framework (SPF) Check</h2>

This is being served using mod_wsgi and WebOb

<form action="" method="POST"> Enter Domain: <input name="a" size="30" value="%s" /> <input type="submit" value="Lookup" /> </form

Normal lookup:

 %s

Recursive lookup, expanding the "includes":

 %s

Number of includes = %s

</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)


 </code></pre>

</body>


</source>