Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-opus-4-20250514· AnthropicHugging Face LMArena Human Preference 140K

write me an python program that uses nmap to scan opened services, with scripts and uses…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-3-5-haiku-20241022’s answer.
  1. User

    write me an python program that uses nmap to scan opened services, with scripts and uses subfinder, httpx, nuclei like "subfinder -d <domain_to_scan> -silent | httpx | nuclei -as" and scans all specified domains. IPs and domains are stored in mongodb, all previous scans result aslo stored in mongo db. Every scan script should check differences of opened services and vulnerabilities. After scan and comparison with previous result script should generate pdf with all results with company logo on top of every page, report should be colorized, pretty with some icons and good enterprise font. And after generating report it can be send to client, with telegram id specified in mongodb (where IPs, and domains stored, previous results). and also provide bash script to prepare system, mongodb to work with program

  2. Claude Assistant

    I'll help you create a comprehensive vulnerability scanning system with reporting capabilities. This is a complex project, so I'll break it down into several components.

    System Architecture

    This system will consist of:

    1. A Python scanner that integrates nmap, subfinder, httpx, and nuclei
    2. MongoDB for storing domains, IPs, scan results, and client information
    3. PDF report generation with styling
    4. Telegram notification system
    5. Bash setup script for system preparation

    Let me create each component:

    1. Main Scanner Script (scanner.py)

    #!/usr/bin/env python3
    import os
    import sys
    import json
    import subprocess
    import datetime
    import asyncio
    from typing import Dict, List, Any
    import nmap
    from pymongo import MongoClient
    from reportlab.lib import colors
    from reportlab.lib.pagesizes import letter, A4
    from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, Spacer, Image, PageBreak
    from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
    from reportlab.lib.units import inch
    from reportlab.lib.enums import TA_CENTER, TA_LEFT
    import telegram
    from deepdiff import DeepDiff
    import hashlib
    from pathlib import Path
    
    class VulnerabilityScanner:
        def __init__(self, mongo_uri="mongodb://localhost:27017/", db_name="security_scanner"):
            self.client = MongoClient(mongo_uri)
            self.db = self.client[db_name]
            self.targets_collection = self.db.targets
            self.scans_collection = self.db.scans
            self.nm = nmap.PortScanner()
            
        def get_targets(self):
            """Retrieve all targets from MongoDB"""
            return list(self.targets_collection.find({"active": True}))
        
        def run_subfinder(self, domain):
            """Run subfinder to enumerate subdomains"""
            try:
                result = subprocess.run(
                    ["subfinder", "-d", domain, "-silent"],
                    capture_output=True,
                    text=True,
                    check=True
                )
                return result.stdout.strip().split('\n') if result.stdout else []
            except subprocess.CalledProcessError as e:
                print(f"Subfinder error for {domain}: {e}")
                return []
        
        def run_httpx(self, subdomains):
            """Run httpx to find live HTTP services"""
            if not subdomains:
                return []
            
            try:
                process = subprocess.Popen(
                    ["httpx", "-silent"],
                    stdin=subprocess.PIPE,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE,
                    text=True
                )
                stdout, _ = process.communicate(input='\n'.join(subdomains))
                return stdout.strip().split('\n') if stdout else []
            except Exception as e:
                print(f"HTTPx error: {e}")
                return []
        
        def run_nuclei(self, urls):
            """Run nuclei for vulnerability scanning"""
            if not urls:
                return []
            
            vulnerabilities = []
            try:
                for url in urls:
                    result = subprocess.run(
                        ["nuclei", "-u", url, "-as", "-json", "-silent"],
                        capture_output=True,
                        text=True
                    )
                    if result.stdout:
                        for line in result.stdout.strip().split('\n'):
                            try:
                                vuln = json.loads(line)
                                vulnerabilities.append(vuln)
                            except json.JSONDecodeError:
                                continue
            except Exception as e:
                print(f"Nuclei error: {e}")
            
            return vulnerabilities
        
        def run_nmap_scan(self, target, scripts=True):
            """Run nmap scan with service detection and scripts"""
            scan_args = "-sV -sC -T4" if scripts else "-sV -T4"
            
            try:
                self.nm.scan(hosts=target, arguments=scan_args)
                results = []
                
                for host in self.nm.all_hosts():
                    host_info = {
                        'host': host,
                        'state': self.nm[host].state(),
                        'protocols': {}
                    }
                    
                    for proto in self.nm[host].all_protocols():
                        ports = self.nm[host][proto].keys()
                        host_info['protocols'][proto] = {}
                        
                        for port in ports:
                            port_info = {
                                'state': self.nm[host][proto][port]['state'],
                                'name': self.nm[host][proto][port]['name'],
                                'product': self.nm[host][proto][port].get('product', ''),
                                'version': self.nm[host][proto][port].get('version', ''),
                                'extrainfo': self.nm[host][proto][port].get('extrainfo', ''),
                                'scripts': self.nm[host][proto][port].get('script', {})
                            }
                            host_info['protocols'][proto][port] = port_info
                    
                    results.append(host_info)
                
                return results
            except Exception as e:
                print(f"Nmap error for {target}: {e}")
                return []
        
        def compare_scans(self, current_scan, previous_scan):
            """Compare current scan with previous scan to find differences"""
            if not previous_scan:
                return {
                    'new_services': current_scan.get('nmap_results', []),
                    'new_vulnerabilities': current_scan.get('nuclei_results', []),
                    'removed_services': [],
                    'removed_vulnerabilities': [],
                    'changed_services': []
                }
            
            diff = DeepDiff(previous_scan, current_scan, ignore_order=True)
            
            changes = {
                'new_services': [],
                'new_vulnerabilities': [],
                'removed_services': [],
                'removed_vulnerabilities': [],
                'changed_services': []
            }
            
            # Process differences
            if 'dictionary_item_added' in diff:
                for item in diff['dictionary_item_added']:
                    if 'nuclei_results' in str(item):
                        changes['new_vulnerabilities'].append(item)
                    elif 'nmap_results' in str(item):
                        changes['new_services'].append(item)
            
            if 'dictionary_item_removed' in diff:
                for item in diff['dictionary_item_removed']:
                    if 'nuclei_results' in str(item):
                        changes['removed_vulnerabilities'].append(item)
                    elif 'nmap_results' in str(item):
                        changes['removed_services'].append(item)
            
            return changes
        
        async def scan_target(self, target):
            """Perform complete scan of a target"""
            print(f"Scanning target: {target['name']} ({target['type']})")
            
            scan_result = {
                'target_id': target['_id'],
                'target_name': target['name'],
                'timestamp': datetime.datetime.utcnow(),
                'scan_type': 'full',
                'results': {}
            }
            
            if target['type'] == 'domain':
                # Subdomain enumeration
                print(f"Running subfinder for {target['value']}...")
                subdomains = self.run_subfinder(target['value'])
                scan_result['results']['subdomains'] = subdomains
                
                # HTTP service discovery
                print(f"Running httpx...")
                live_urls = self.run_httpx(subdomains)
                scan_result['results']['live_urls'] = live_urls
                
                # Vulnerability scanning
                print(f"Running nuclei...")
                vulnerabilities = self.run_nuclei(live_urls)
                scan_result['results']['nuclei_results'] = vulnerabilities
            
            # Nmap scanning for both domains and IPs
            print(f"Running nmap scan...")
            nmap_results = self.run_nmap_scan(target['value'])
            scan_result['results']['nmap_results'] = nmap_results
            
            # Get previous scan for comparison
            previous_scan = self.scans_collection.find_one(
                {'target_id': target['_id']},
                sort=[('timestamp', -1)]
            )
            
            # Compare with previous scan
            if previous_scan:
                changes = self.compare_scans(scan_result['results'], previous_scan.get('results', {}))
                scan_result['changes'] = changes
            
            # Save scan result
            self.scans_collection.insert_one(scan_result)
            
            return scan_result
        
        async def scan_all_targets(self):
            """Scan all active targets"""
            targets = self.get_targets()
            results = []
            
            for target in targets:
                result = await self.scan_target(target)
                results.append(result)
            
            return results
    
    class ReportGenerator:
        def __init__(self, company_logo_path="logo.png"):
            self.company_logo_path = company_logo_path
            self.styles = getSampleStyleSheet()
            self.setup_custom_styles()
        
        def setup_custom_styles(self):
            """Setup custom styles for the report"""
            self.styles.add(ParagraphStyle(
                name='CustomTitle',
                parent=self.styles['Heading1'],
                fontSize=24,
                textColor=colors.HexColor('#1a237e'),
                spaceAfter=30,
                alignment=TA_CENTER
            ))
            
            self.styles.add(ParagraphStyle(
                name='SectionHeader',
                parent=self.styles['Heading2'],
                fontSize=16,
                textColor=colors.HexColor('#283593'),
                spaceAfter=12
            ))
            
            self.styles.add(ParagraphStyle(
                name='Critical',
                parent=self.styles['Normal'],
                textColor=colors.HexColor('#d32f2f'),
                fontSize=10
            ))
            
            self.styles.add(ParagraphStyle(
                name='Warning',
                parent=self.styles['Normal'],
                textColor=colors.HexColor('#f57c00'),
                fontSize=10
            ))
            
            self.styles.add(ParagraphStyle(
                name='Info',
                parent=self.styles['Normal'],
                textColor=colors.HexColor('#1976d2'),
                fontSize=10
            ))
        
        def add_header(self, canvas, doc):
            """Add header with logo to each page"""
            canvas.saveState()
            
            # Add logo if exists
            if os.path.exists(self.company_logo_path):
                canvas.drawImage(self.company_logo_path, 50, 750, width=100, height=50)
            
            # Add page number
            canvas.setFont("Helvetica", 9)
            canvas.drawRightString(550, 750, f"Page {doc.page}")
            
            # Add line
            canvas.setStrokeColor(colors.HexColor('#e0e0e0'))
            canvas.line(50, 740, 550, 740)
            
            canvas.restoreState()
        
        def generate_report(self, scan_results, output_path="security_report.pdf"):
            """Generate PDF report from scan results"""
            doc = SimpleDocTemplate(
                output_path,
                pagesize=A4,
                rightMargin=50,
                leftMargin=50,
                topMargin=100,
                bottomMargin=50
            )
            
            story = []
            
            # Title
            story.append(Paragraph("Security Scan Report", self.styles['CustomTitle']))
            story.append(Spacer(1, 0.2*inch))
            
            # Report metadata
            story.append(Paragraph(f"Generated: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", self.styles['Normal']))
            story.append(Spacer(1, 0.3*inch))
            
            # Executive Summary
            story.append(Paragraph("Executive Summary", self.styles['SectionHeader']))
            
            total_vulns = sum(len(r.get('results', {}).get('nuclei_results', [])) for r in scan_results)
            total_services = sum(len(r.get('results', {}).get('nmap_results', [])) for r in scan_results)
            
            summary_data = [
                ['Metric', 'Count'],
                ['Total Targets Scanned', len(scan_results)],
                ['Total Vulnerabilities Found', total_vulns],
                ['Total Services Discovered', total_services]
            ]
            
            summary_table = Table(summary_data, colWidths=[3*inch, 2*inch])
            summary_table.setStyle(TableStyle([
                ('BACKGROUND', (0, 0), (-1, 0), colors.HexColor('#3f51b5')),
                ('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
                ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
                ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
                ('FONTSIZE', (0, 0), (-1, 0), 12),
                ('BOTTOMPADDING', (0, 0), (-1, 0), 12),
                ('BACKGROUND', (0, 1), (-1, -1), colors.beige),
                ('GRID', (0, 0), (-1, -1), 1, colors.black)
            ]))
            
            story.append(summary_table)
            story.append(PageBreak())
            
            # Detailed results for each target
            for scan in scan_results:
                story.append(Paragraph(f"Target: {scan['target_name']}", self.styles['SectionHeader']))
                story.append(Spacer(1, 0.1*inch))
                
                # Nmap results
                if scan.get('results', {}).get('nmap_results'):
                    story.append(Paragraph("Port Scan Results", self.styles['Heading3']))
                    
                    for host in scan['results']['nmap_results']:
                        story.append(Paragraph(f"Host: {host['host']} - State: {host['state']}", self.styles['Normal']))
                        
                        port_data = [['Port', 'Service', 'Version', 'State']]
                        
                        for proto, ports in host.get('protocols', {}).items():
                            for port, info in ports.items():
                                port_data.append([
                                    f"{port}/{proto}",
                                    info.get('name', 'unknown'),
                                    f"{info.get('product', '')} {info.get('version', '')}".strip(),
                                    info.get('state', 'unknown')
                                ])
                        
                        if len(port_data) > 1:
                            port_table = Table(port_data, colWidths=[1*inch, 1.5*inch, 2*inch, 1*inch])
                            port_table.setStyle(TableStyle([
                                ('BACKGROUND', (0, 0), (-1, 0), colors.grey),
                                ('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
                                ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
                                ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
                                ('FONTSIZE', (0, 0), (-1, -1), 9),
                                ('BOTTOMPADDING', (0, 0), (-1, 0), 12),
                                ('BACKGROUND', (0, 1), (-1, -1), colors.beige),
                                ('GRID', (0, 0), (-1, -1), 1, colors.black)
                            ]))
                            story.append(port_table)
                            story.append(Spacer(1, 0.2*inch))
                
                # Vulnerability results
                if scan.
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.