Automating and optimizating Nmap with Python

An optimized way to use Masscan and Nmap with Python

Download as .zip Download as .tar.gz View on GitHub

Home Page

Information

I end up wasting a lot of time doing long and detailed scans with nmap, but in the end they were needed.

To get rid of this problem, I needed to be very accurate with what I was supposed to scan. A good way to speed up this process would be to extract a list of open ports and then scan these ports with the full scan to get all the details that I needed.

To do this, run masscan, get the list of open ports, and run a full scan with nmap. If masscan has a problem, run nmap’s fast scan, get the list of open ports, and run a full scan later. See Details and Example to see how it works with this code.

Configuration

Install some Python packages:

pip install netifaces
pip install python-nmap
pip install python-masscan

Details

The python_masscan.py file contains 1 usable function:

The python_nmap.py file contains 3 usable functions:

Example

I usually use these functions like that:

import python_masscan
import python_nmap

# Setting the target
host = '192.168.0.120'

# Try, Except to execute masscan first, if it fails for some reason, execute nmap fast scan instead
try:
    # Extracting the dictionary and the list of ports from the masscan module
    scan_ms, port_list_ms = python_masscan.masscanProcess(host)
    # Extracting the dictionary from the nmap module, passing the port list from the masscan module
    complete_scan = python_nmap.nmapCompleteScanProcess(host, port_list_ms)
except:
    # Extracting the dicionary and the list os ports from the nmap module
    scan_nm, port_list_nm = python_nmap.nmapFastScanProcess(host)
    # Extracting the dictionary from the nmap module, passing the port list from the nmap module
    complete_scan = python_nmap.nmapCompleteScanProcess(host, port_list_nm)

# Printing the dictionary
print(complete_scan)

This example will generate the following output after the scan that I did on the metasploitable machine:

{
   "nmap":{
      "command_line":"nmap -oX - -p 514,1524,1099,21,6000,6667,445,512,111,139,53,22,25,23,2121,5900,5432,3306,513,8009,80,2049,8180 -O -sT -sV -sC -A -T5 192.168.0.120",
      "scaninfo":{
         "tcp":{
            "method":"connect",
            "services":"21-23,25,53,80,111,139,445,512-514,1099,1524,2049,2121,3306,5432,5900,6000,6667,8009,8180"
         }
      },
      "scanstats":{
         "timestr":"Tue Dec 21 19:13:38 2021",
         "elapsed":"36.47",
         "uphosts":"1",
         "downhosts":"0",
         "totalhosts":"1"
      }
   },
   "scan":{
      "192.168.0.120":{
         "hostnames":[
            {
               "name":"",
               "type":""
            }
         ],
         "addresses":{
            "ipv4":"192.168.0.120",
            "mac":"08:00:27:62:D8:33"
         },
         "vendor":{
            "08:00:27:62:D8:33":"Oracle VirtualBox virtual NIC"
         },
         "status":{
            "state":"up",
            "reason":"arp-response"
         },
         "uptime":{
            "seconds":"450",
            "lastboot":"Tue Dec 21 19:06:08 2021"
         },
         "tcp":{
            "21":{
               "state":"open",
               "reason":"syn-ack",
               "name":"ftp",
               "product":"vsftpd",
               "version":"2.3.4",
               "extrainfo":"",
               "conf":"10",
               "cpe":"cpe:/a:vsftpd:vsftpd:2.3.4",
               "script":{
                  "ftp-anon":"Anonymous FTP login allowed (FTP code 230)",
                  "ftp-syst":"\n  STAT: \nFTP server status:\n     Connected to 192.168.0.113\n     Logged in as ftp\n     TYPE: ASCII\n     No session bandwidth limit\n     Session timeout in seconds is 300\n     Control connection is plain text\n     Data connections will be plain text\n     vsFTPd 2.3.4 - secure, fast, stable\nEnd of status"
               }
            },
            "22":{
               "state":"open",
               "reason":"syn-ack",
               "name":"ssh",
               "product":"OpenSSH",
               "version":"4.7p1 Debian 8ubuntu1",
               "extrainfo":"protocol 2.0",
               "conf":"10",
               "cpe":"cpe:/o:linux:linux_kernel",
               "script":{
                  "ssh-hostkey":"\n  1024 60:0f:cf:e1:c0:5f:6a:74:d6:90:24:fa:c4:d5:6c:cd (DSA)\n  2048 56:56:24:0f:21:1d:de:a7:2b:ae:61:b1:24:3d:e8:f3 (RSA)"
               }
            },
            "23":{
               "state":"open",
               "reason":"syn-ack",
               "name":"telnet",
               "product":"Linux telnetd",
               "version":"",
               "extrainfo":"",
               "conf":"10",
               "cpe":"cpe:/o:linux:linux_kernel"
            },
            "25":{
               "state":"open",
               "reason":"syn-ack",
               "name":"smtp",
               "product":"Postfix smtpd",
               "version":"",
               "extrainfo":"",
               "conf":"10",
               "cpe":"cpe:/a:postfix:postfix",
               "script":{
                  "smtp-commands":"metasploitable.localdomain, PIPELINING, SIZE 10240000, VRFY, ETRN, STARTTLS, ENHANCEDSTATUSCODES, 8BITMIME, DSN, ",
                  "ssl-date":"2021-12-21T22:13:22+00:00; -2s from scanner time.",
                  "sslv2":"\n  SSLv2 supported\n  ciphers: \n    SSL2_RC2_128_CBC_EXPORT40_WITH_MD5\n    SSL2_RC4_128_EXPORT40_WITH_MD5\n    SSL2_RC2_128_CBC_WITH_MD5\n    SSL2_DES_192_EDE3_CBC_WITH_MD5\n    SSL2_RC4_128_WITH_MD5\n    SSL2_DES_64_CBC_WITH_MD5"
               }
            },
            "53":{
               "state":"open",
               "reason":"syn-ack",
               "name":"domain",
               "product":"ISC BIND",
               "version":"9.4.2",
               "extrainfo":"",
               "conf":"10",
               "cpe":"cpe:/a:isc:bind:9.4.2",
               "script":{
                  "dns-nsid":"\n  bind.version: 9.4.2"
               }
            },
            "80":{
               "state":"open",
               "reason":"syn-ack",
               "name":"http",
               "product":"Apache httpd",
               "version":"2.2.8",
               "extrainfo":"(Ubuntu) DAV/2",
               "conf":"10",
               "cpe":"cpe:/a:apache:http_server:2.2.8",
               "script":{
                  "http-server-header":"Apache/2.2.8 (Ubuntu) DAV/2",
                  "http-title":"Metasploitable2 - Linux"
               }
            },
            "111":{
               "state":"open",
               "reason":"syn-ack",
               "name":"rpcbind",
               "product":"",
               "version":"2",
               "extrainfo":"RPC #100000",
               "conf":"10",
               "cpe":""
            },
            "139":{
               "state":"open",
               "reason":"syn-ack",
               "name":"netbios-ssn",
               "product":"Samba smbd",
               "version":"3.X - 4.X",
               "extrainfo":"workgroup: WORKGROUP",
               "conf":"10",
               "cpe":"cpe:/a:samba:samba"
            },
            "445":{
               "state":"open",
               "reason":"syn-ack",
               "name":"netbios-ssn",
               "product":"Samba smbd",
               "version":"3.X - 4.X",
               "extrainfo":"workgroup: WORKGROUP",
               "conf":"10",
               "cpe":"cpe:/a:samba:samba"
            },
            "512":{
               "state":"open",
               "reason":"syn-ack",
               "name":"exec",
               "product":"netkit-rsh rexecd",
               "version":"",
               "extrainfo":"",
               "conf":"10",
               "cpe":"cpe:/o:linux:linux_kernel"
            },
            "513":{
               "state":"open",
               "reason":"syn-ack",
               "name":"login",
               "product":"",
               "version":"",
               "extrainfo":"",
               "conf":"10",
               "cpe":""
            },
            "514":{
               "state":"open",
               "reason":"syn-ack",
               "name":"tcpwrapped",
               "product":"",
               "version":"",
               "extrainfo":"",
               "conf":"8",
               "cpe":""
            },
            "1099":{
               "state":"open",
               "reason":"syn-ack",
               "name":"java-rmi",
               "product":"GNU Classpath grmiregistry",
               "version":"",
               "extrainfo":"",
               "conf":"10",
               "cpe":""
            },
            "1524":{
               "state":"open",
               "reason":"syn-ack",
               "name":"bindshell",
               "product":"Metasploitable root shell",
               "version":"",
               "extrainfo":"",
               "conf":"10",
               "cpe":""
            },
            "2049":{
               "state":"open",
               "reason":"syn-ack",
               "name":"nfs",
               "product":"",
               "version":"2-4",
               "extrainfo":"RPC #100003",
               "conf":"10",
               "cpe":""
            },
            "2121":{
               "state":"open",
               "reason":"syn-ack",
               "name":"ftp",
               "product":"ProFTPD",
               "version":"1.3.1",
               "extrainfo":"",
               "conf":"10",
               "cpe":"cpe:/a:proftpd:proftpd:1.3.1"
            },
            "3306":{
               "state":"open",
               "reason":"syn-ack",
               "name":"mysql",
               "product":"MySQL",
               "version":"5.0.51a-3ubuntu5",
               "extrainfo":"",
               "conf":"10",
               "cpe":"cpe:/a:mysql:mysql:5.0.51a-3ubuntu5",
               "script":{
                  "mysql-info":"\n  Protocol: 10\n  Version: 5.0.51a-3ubuntu5\n  Thread ID: 11\n  Capabilities flags: 43564\n  Some Capabilities: SupportsCompression, Support41Auth, SupportsTransactions, SwitchToSSLAfterHandshake, Speaks41ProtocolNew, ConnectWithDatabase, LongColumnFlag\n  Status: Autocommit\n  Salt: Tg(-Jmf:,kZbZ!0lQ1B6"
               }
            },
            "5432":{
               "state":"open",
               "reason":"syn-ack",
               "name":"postgresql",
               "product":"PostgreSQL DB",
               "version":"8.3.0 - 8.3.7",
               "extrainfo":"",
               "conf":"10",
               "cpe":"cpe:/a:postgresql:postgresql:8.3",
               "script":{
                  "ssl-date":"2021-12-21T22:13:22+00:00; -2s from scanner time."
               }
            },
            "5900":{
               "state":"open",
               "reason":"syn-ack",
               "name":"vnc",
               "product":"VNC",
               "version":"",
               "extrainfo":"protocol 3.3",
               "conf":"10",
               "cpe":"",
               "script":{
                  "vnc-info":"\n  Protocol version: 3.3\n  Security types: \n    VNC Authentication (2)"
               }
            },
            "6000":{
               "state":"open",
               "reason":"syn-ack",
               "name":"X11",
               "product":"",
               "version":"",
               "extrainfo":"access denied",
               "conf":"10",
               "cpe":""
            },
            "6667":{
               "state":"open",
               "reason":"syn-ack",
               "name":"irc",
               "product":"UnrealIRCd",
               "version":"",
               "extrainfo":"",
               "conf":"10",
               "cpe":"cpe:/a:unrealircd:unrealircd",
               "script":{
                  "irc-info":"\n  users: 1\n  servers: 1\n  lusers: 1\n  lservers: 0\n  server: irc.Metasploitable.LAN\n  version: Unreal3.2.8.1. irc.Metasploitable.LAN \n  uptime: 0 days, 0:11:54\n  source ident: nmap\n  source host: 37B90734.F0D9233E.FFFA6D49.IP\n  error: Closing Link: jfcsgrgdq[192.168.0.113] (Quit: jfcsgrgdq)"
               }
            },
            "8009":{
               "state":"open",
               "reason":"syn-ack",
               "name":"ajp13",
               "product":"Apache Jserv",
               "version":"",
               "extrainfo":"Protocol v1.3",
               "conf":"10",
               "cpe":"",
               "script":{
                  "ajp-methods":"Failed to get a valid response for the OPTION request"
               }
            },
            "8180":{
               "state":"open",
               "reason":"syn-ack",
               "name":"http",
               "product":"Apache Tomcat/Coyote JSP engine",
               "version":"1.1",
               "extrainfo":"",
               "conf":"10",
               "cpe":"cpe:/a:apache:coyote_http_connector:1.1",
               "script":{
                  "http-favicon":"Apache Tomcat",
                  "http-server-header":"Apache-Coyote/1.1",
                  "http-title":"Apache Tomcat/5.5"
               }
            }
         },
         "hostscript":[
            {
               "id":"clock-skew",
               "output":"mean: -2s, deviation: 0s, median: -2s"
            },
            {
               "id":"ms-sql-info",
               "output":"ERROR: Script execution failed (use -d to debug)"
            },
            {
               "id":"nbstat",
               "output":"NetBIOS name: METASPLOITABLE, NetBIOS user: <unknown>, NetBIOS MAC: <unknown> (unknown)"
            },
            {
               "id":"smb-os-discovery",
               "output":"ERROR: Script execution failed (use -d to debug)"
            },
            {
               "id":"smb-security-mode",
               "output":"ERROR: Script execution failed (use -d to debug)"
            },
            {
               "id":"smb2-time",
               "output":"Protocol negotiation failed (SMB2)"
            }
         ],
         "portused":[
            {
               "state":"open",
               "proto":"tcp",
               "portid":"21"
            },
            {
               "state":"closed",
               "proto":"udp",
               "portid":"41096"
            }
         ],
         "osmatch":[
            {
               "name":"Linux 2.6.9 - 2.6.33",
               "accuracy":"100",
               "line":"59153",
               "osclass":[
                  {
                     "type":"general purpose",
                     "vendor":"Linux",
                     "osfamily":"Linux",
                     "osgen":"2.6.X",
                     "accuracy":"100",
                     "cpe":[
                        "cpe:/o:linux:linux_kernel:2.6"
                     ]
                  }
               ]
            }
         ]
      }
   }
}