• Subscribe to the low volume list for updates.

Parse Nmap XML to get SSL Certificate details

Extract SSL certificate details from a range of IP addresses using Nmap XML and a simple python script. The python script parses the Nmap XML output from the ssl-cert.nse script and produces csv output with the target SSL certificate details.

When compiling Nmap you will need to have the libssl-dev package installed as Nmap nse scripts such as ssl-cert will not work without it being installed. Once this is installed ./configure, make, make install to install the latest version of Nmap.

apt-get install libssl-dev

Once the package is installed go ahead and install Nmap from source. Extract the source into a folder, configure and install.

For a quick test of the SSL cert parse script I grabbed the top 25 computing sites from Alexa. Start Nmap with the ssl-cert nse script. The -iL option loads the list 25 target host names with the -oX producing the Nmap XML results.

nmap -iL top25-tech.txt -sV -p 443 -oX nmap-results-top25 --script=ssl-cert

Once the scan has completed the python script below can be used parse the Nmap XML and produce the csv output. The results can then be loaded into a spreadsheet, or parsed further to depending on your needs.

testuser@ubuntu:~$ python nmap-ssl-certs.py nmap-results-top25.xml,www.google.com,Google Inc,US,2014-05-07,2014-08-05,*.facebook.com,Facebook, Inc.,US,2014-02-28,2015-04-13,*.google.com,Google Inc,US,2014-05-07,2014-08-05,mail.google.com,Google Inc,US,2014-05-07,2014-08-05,www.yahoo.com,Yahoo Inc.,US,2014-04-09,2015-04-09,*.wikipedia.org,Wikimedia Foundation, Inc.,US,2012-10-21,2016-01-20,twitter.com,Twitter, Inc.,US,2014-04-08,2016-05-09,www.linkedin.com,LinkedIn Corporation,US,2013-12-19,2016-12-30,*.login.yahoo.com,Yahoo Inc.,US,2014-04-08,2015-04-09,mail.live.com,Microsoft Corporation,US,2013-05-21,2015-05-22,*.google.com,Google Inc,US,2014-05-07,2014-08-05,*.google.com,Google Inc,US,2014-05-07,2014-08-05,search.yahoo.com,Yahoo Inc.,US,2014-04-08,2015-04-09,wordpress.com,Automattic, Inc.,US,2014-04-16,2016-04-16,*.bing.com,Microsoft Corporation,US,2014-05-20,2016-05-19,*.pinterest.com,Pinterest Inc,US,2014-04-09,2017-04-13,,,,,,*.google.com,Google Inc,US,2014-05-07,2014-08-05,,,,,,paypal.com,PayPal, Inc.,US,2013-01-09,2015-01-11,microsoft.com,,,2013-06-20,2015-06-20,apple.com,Apple Inc.,US,2012-11-13,2014-11-03,*.imgur.com,Imgur, Inc.,US,2013-06-25,2016-08-31,*.stackexchange.com,Stack Exchange, Inc.,US,2013-07-02,2016-07-06,,,,,

Here is the python script. The script is simple but it works, it should be pretty easy to read allowing modification to parse other NSE scripts and results from the Nmap XML output. There are many ways to parse XML data, the xml.dom method used here seems to be one of the more straightforward for parsing the Nmap XML. Another option could include using ElementTree, or even using xmlstarlet in bash as seen on this stack.exchange post.

#!/usr/bin/env python
import xml.dom.minidom
import sys
import getopt
    scandata = sys.argv[1]
    print "*** You need to supply an Nmap XML file ***"
if scandata:
    doc = xml.dom.minidom.parse(scandata)
    output = []
    for host in doc.getElementsByTagName("host"):
        ip = ''
        commonName = ''
        organizationName = ''
        countryName = ''
        notBefore = ''
        notAfter = ''
        addresses = host.getElementsByTagName("address")
        ip = addresses[0].getAttribute("addr")                         # Get IP address from addr element 
        scripts = host.getElementsByTagName("script")
        for script in scripts:
              for elem in script.getElementsByTagName("elem"):         # Get cert details for each target 
                    if elem.getAttribute("key") == 'commonName':
                       if commonName == '':                            # Only get the first commonName 
                           commonName =  elem.childNodes[0].nodeValue
                    if elem.getAttribute("key") == 'organizationName':
                       if organizationName == '': 
                           organizationName =  elem.childNodes[0].nodeValue
                    if elem.getAttribute("key") == 'countryName':
                       countryName =  elem.childNodes[0].nodeValue
                    if elem.getAttribute("key") == 'notBefore':
                       notBefore =  elem.childNodes[0].nodeValue
                       notBefore = notBefore.split('T')[0]
                    if elem.getAttribute("key") == 'notAfter':
                       notAfter =  elem.childNodes[0].nodeValue
                       notAfter = notAfter.split('T')[0]
        output.append(ip + ',' + commonName + ',' + organizationName + ',' + countryName + ',' + notBefore + ',' + notAfter)
    for i in output:
        print i

Get the most from this powerful tool.
Practical example commands for running Nmap

Next level testing with advanced Security Vulnerability Scanners.
Trusted tools. Hosted for easy access.