nmap web ui
Drop files here
or click to upload
import streamlit as st
import nmap
import re
st.title("Nmap Web Scanner")
# Initialize nmap scanner
nm = nmap.PortScanner()
# Input for target IP/hostname
target = st.text_input("Enter target IP address or hostname:")
# Scan options
scan_type = st.selectbox(
"Select scan type",
[
"Quick Scan (-T4 -F)",
"Full Scan (-T4 -A -v)",
"Ping Scan (-sn)",
"Custom"
]
)
if scan_type == "Custom":
custom_args = st.text_input(
"Enter custom nmap arguments:",
help="Example: -sS -sV -p 80,443"
)
# Validate IP address/hostname
def is_valid_host(host):
# Simple IP validation regex
ip_pattern = r'^(\d{1,3}\.){3}\d{1,3}$'
# Simple hostname validation
hostname_pattern = r'^[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z]{2,})+$'
return bool(re.match(ip_pattern, host) or re.match(hostname_pattern, host))
# Scan button
if st.button("Start Scan"):
if not target:
st.error("Please enter a target IP address or hostname")
elif not is_valid_host(target):
st.error("Please enter a valid IP address or hostname")
else:
try:
with st.spinner("Scanning..."):
# Set scan arguments based on selection
if scan_type == "Quick Scan (-T4 -F)":
args = "-T4 -F"
elif scan_type == "Full Scan (-T4 -A -v)":
args = "-T4 -A -v"
elif scan_type == "Ping Scan (-sn)":
args = "-sn"
else:
args = custom_args
# Run the scan
nm.scan(hosts=target, arguments=args)
# Display results
st.subheader("Scan Results")
for host in nm.all_hosts():
st.markdown(f"### Host: {host}")
# Host state
st.write("State:", nm[host].state())
# Display protocols
for proto in nm[host].all_protocols():
st.markdown(f"#### Protocol: {proto}")
# Get ports
ports = nm[host][proto].keys()
for port in ports:
state = nm[host][proto][port]['state']
service = nm[host][proto][port].get('name', '')
product = nm[host][proto][port].get('product', '')
version = nm[host][proto][port].get('version', '')
info = f"Port {port}: {state}"
if service:
info += f", Service: {service}"
if product:
info += f", Product: {product}"
if version:
info += f", Version: {version}"
st.write(info)
# OS detection if available
if 'osmatch' in nm[host]:
st.markdown("#### OS Detection")
for os in nm[host]['osmatch']:
st.write(f"OS: {os['name']}, Accuracy: {os['accuracy']}%")
except Exception as e:
st.error(f"Scan failed: {str(e)}")
st.warning("Note: Some scans may require root/administrator privileges")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?