implement accurate multi-threadding

This commit is contained in:
Matt Keeley
2024-08-11 13:09:44 -07:00
parent 030412f7bb
commit 4094b7d60b

View File

@@ -1,6 +1,7 @@
#! /usr/bin/env python3 #! /usr/bin/env python3
import argparse import argparse
import threading import threading
from queue import Queue
from modules.dns import DNS from modules.dns import DNS
from modules.spf import SPF from modules.spf import SPF
from modules.dmarc import DMARC from modules.dmarc import DMARC
@@ -8,6 +9,8 @@ from modules.bimi import BIMI
from modules.spoofing import Spoofing from modules.spoofing import Spoofing
from modules import report from modules import report
print_lock = threading.Lock()
def process_domain(domain): def process_domain(domain):
"""Process a domain to gather DNS, SPF, DMARC, and BIMI records, and evaluate spoofing potential.""" """Process a domain to gather DNS, SPF, DMARC, and BIMI records, and evaluate spoofing potential."""
@@ -74,26 +77,21 @@ def process_domain(domain):
return result return result
def worker(domain_queue, result_queue): def worker(domain_queue, print_lock, output, results):
"""Worker function to process domains and put results into the result queue.""" """Worker function to process domains and output results."""
while not domain_queue.empty(): while True:
domain = domain_queue.get() domain = domain_queue.get()
if domain is None: if domain is None:
break break
result = process_domain(domain) result = process_domain(domain)
result_queue.put(result) with print_lock:
if output == "stdout":
report.printer(**result)
else:
results.append(result)
domain_queue.task_done() domain_queue.task_done()
def process_domain_and_output(domain, output, results):
"""Process a domain and handle output based on the specified format."""
result = process_domain(domain)
if output == "stdout":
report.printer(**result)
else:
results.append(result)
def main(): def main():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description="Process domains to gather DNS, SPF, DMARC, and BIMI records." description="Process domains to gather DNS, SPF, DMARC, and BIMI records."
@@ -110,37 +108,43 @@ def main():
default="stdout", default="stdout",
help="Output format: stdout or xls (default: stdout).", help="Output format: stdout or xls (default: stdout).",
) )
parser.add_argument(
"-t", type=int, default=4, help="Number of threads to use (default: 4)"
)
args = parser.parse_args() args = parser.parse_args()
# Load domains
if args.d: if args.d:
domains = [args.d] domains = [args.d]
elif args.iL: elif args.iL:
with open(args.iL, "r") as file: with open(args.iL, "r") as file:
domains = [line.strip() for line in file] domains = [line.strip() for line in file]
# Prepare for processing domain_queue = Queue()
results = [] results = []
threads = []
# Start threads to process each domain
for domain in domains: for domain in domains:
domain_queue.put(domain)
threads = []
for _ in range(min(args.t, len(domains))):
thread = threading.Thread( thread = threading.Thread(
target=process_domain_and_output, args=(domain, args.o, results) target=worker, args=(domain_queue, print_lock, args.o, results)
) )
thread.start() thread.start()
threads.append(thread) threads.append(thread)
# Wait for all threads to complete domain_queue.join()
for thread in threads:
thread.join()
# Handle output for xls format
if args.o == "xls" and results: if args.o == "xls" and results:
report.write_to_excel(results) report.write_to_excel(results)
print("Results written to output.xlsx") print("Results written to output.xlsx")
for _ in range(len(threads)):
domain_queue.put(None)
for thread in threads:
thread.join()
if __name__ == "__main__": if __name__ == "__main__":
main() main()