The script combines the capabilities of Shodan and Nmap to perform a red team exercise. It starts by searching Shodan for devices matching a specific query, saves the results to a JSON file, and then scans each target IP using Nmap with a comprehensive set of options. The results of the Nmap scan are saved to a text file, including details about open ports, services, OS detection, and vulnerability scripts.
To use this script, you need to install the required libraries (shodan and python-nmap), replace the placeholder with your actual Shodan API key, and run the script with optional arguments to customize the query, limit, and output file.
python
#!/usr/bin/env python3
import shodan
import nmap
import json
import time
from datetime import datetime
import argparse
# Replace with your Shodan API key
SHODAN_API_KEY = "your_api_key_here"
def search_shodan(query, limit=10):
api = shodan.Shodan(SHODAN_API_KEY)
try:
results = api.search(query, limit=limit)
return results['matches']
except shodan.APIError as e:
print(f"Error: {e}")
return []
def save_shodan_results(results, filename="shodan_results.json"):
with open(filename, 'w') as f:
json.dump(results, f, indent=4)
print(f"Shodan results saved to {filename}")
def scan_with_nmap(targets, output_file="scan_results.txt"):
nm = nmap.PortScanner()
with open(output_file, 'w') as f:
f.write(f"Scan results as of {datetime.now()}\n\n")
for target in targets:
print(f"Scanning {target}...")
f.write(f"Scanning {target}...\n")
try:
# Perform a comprehensive scan
nm.scan(hosts=target, arguments='-sS -sV -O -A --script=vuln')
for host in nm.all_hosts():
f.write(f"Host: {host}\n")
f.write(f"State: {nm[host].state()}\n")
if 'osmatch' in nm[host]:
for osmatch in nm[host]['osmatch']:
f.write(f"OS Match: {osmatch['name']} (Accuracy: {osmatch['accuracy']}%)\n")
for proto in nm[host].all_protocols():
f.write(f"Protocol: {proto}\n")
ports = nm[host][proto].keys()
for port in ports:
f.write(f"Port: {port}\tState: {nm[host][proto][port]['state']}\tService: {nm[host][proto][port]['name']}\n")
if 'script' in nm[host][proto][port]:
for script in nm[host][proto][port]['script']:
f.write(f"Script Output: {script}\n")
f.write("---")
except Exception as e:
print(f"Error scanning {target}: {e}")
f.write(f"Error scanning {target}: {e}\n")
time.sleep(1) # To avoid rate limiting
def main():
parser = argparse.ArgumentParser(description="Red Team Script using Shodan and Nmap")
parser.add_argument('--query', type=str, default="default password", help="Shodan search query")
parser.add_argument('--limit', type=int, default=10, help="Limit the number of Shodan results")
parser.add_argument('--output', type=str, default="scan_results.txt", help="Output file for scan results")
args = parser.parse_args()
print(f"Searching Shodan for: {args.query}")
results = search_shodan(args.query, args.limit)
if results:
save_shodan_results(results)
targets = [result['ip_str'] for result in results]
print(f"Found {len(targets)} targets.")
scan_with_nmap(targets, args.output)
else:
print("No targets found.")
if __name__ == "__main__":
main()
This script is designed to be used in a red team exercise to gather information about potential targets and scan them for vulnerabilities. Always ensure you have permission to scan the targets to avoid legal issues.