-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnmap-email.py
More file actions
111 lines (98 loc) · 4.68 KB
/
nmap-email.py
File metadata and controls
111 lines (98 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#target: Stores a single IP address for scanning.
#targets_input: Stores the raw input string provided by the user, containing one or more IP addresses separated by commas or spaces.
#targets: Represents a list of individual IP addresses extracted from the targets_input, used for scanning each IP sequentially.
#MAKE SURE TO ADD YOUR EMAIL ADDRESS AND SMTP CREDENTIALS ON LINES 86-88!!!!!
#Also set your SMTP server on line 103
#importing and start stuff
import nmap
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from tabulate import tabulate
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def run_nmap_scan(targets):
scanner = nmap.PortScanner()
for target in targets:
scanner.scan(hosts=target.strip(), arguments='-sS -T3 -F -n')
# Create PDF
pdf_output = "nmap_scan_results.pdf"
c = canvas.Canvas(pdf_output, pagesize=letter)
c.setFont("Helvetica-Bold", 14)
add_scan_results_to_pdf(targets, scanner, c) # Add scan results to PDF
scan_table = generate_table(scanner)
print_scan_results(scan_table) # Print scan results as a table
send_email(scan_table)
# Add scan results to PDF
def add_scan_results_to_pdf(targets, scanner, c):
y = 750
for target in targets:
c.setFont("Helvetica-Bold", 14)
c.drawString(100, y, "Nmap scan report for " + target.strip())
c.setFont("Helvetica", 12)
y -= 20
for host in scanner.all_hosts():
c.setFont("Helvetica-Bold", 12) # Make host IP bold
c.drawString(100, y, "Host: " + host)
y -= 15
c.setFont("Helvetica", 12) # Revert to normal font
c.drawString(100, y, "State: " + scanner[host].state())
y -= 15
for proto in scanner[host].all_protocols():
c.drawString(100, y, "Protocol: " + proto)
y -= 15
ports = scanner[host][proto].keys()
for port in ports:
c.drawString(100, y, "Port: " + str(port) + " State: " + scanner[host][proto][port]['state'])
y -= 15
if y < 50:
c.showPage() # Move to the next page if there isn't enough space.
c.setFont("Helvetica", 12)
y = 750 # Reset y-coordinate for the next page
c.drawString(100, y, "") # Keep items seperated by IP address in the PDF
y -= 15
c.save()
def generate_table(scanner):
ip_addresses = [] # Initialize a list to store IP addresses
data = []
previous_ip = None # Initialize previous_ip
for target in sorted(scanner.all_hosts(), key=lambda x: tuple(map(int, x.split('.')))):
target_info = [] # Store port info for each target IP
ip_addresses.append(target) # Collect IP addresses
for proto in scanner[target].all_protocols():
for port in scanner[target][proto]:
port_info = scanner[target][proto][port]
target_info.append([target, port, proto, port_info['state']])
# Split each IP address with a space but keep associated ports together
if target_info: # Ensure target_info is not empty
if target != previous_ip:
data.append(["", "", "", ""]) # Insert blank row
previous_ip = target
data.extend(target_info) # Add port info for current target IP
print("Nmap scan report for " + ", ".join(ip_addresses))
return data
def print_scan_results(scan_table): # Print scan results as a table
print("Scan Results:")
print(tabulate(scan_table, headers=["Target", "Port", "Protocol", "State"]))
def send_email(scan_table):
# Email configuration
sender_email = "<SENDER_EMAIL_HERE>"
receiver_email = "<RECEIVER_EMAIL_HERE>"
password = "<PASSWORD_HERE>"
# Compose email
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = "Nmap Scan Results"
html_table = tabulate(scan_table, headers=["Target", "Port", "Protocol", "State"], tablefmt="html")
email_body = f"<html><body><pre style='font-family: Arial, sans-serif; font-size: 18px;'>Scan Results:{html_table.replace(' ', ' ')}</p></body></html>"
msg.attach(MIMEText(email_body, 'html'))
# Connect to SMTP server and send email
with smtplib.SMTP('smtp.example.com', 587) as server:
server.starttls()
server.login(sender_email, password)
server.send_message(msg)
if __name__ == "__main__":
targets_input = input("Enter the target IP addresses separated by commas or spaces: ")
targets = targets_input.split(",")
run_nmap_scan(targets)