Skip to content

Instantly share code, notes, and snippets.

@redherringbone
Created November 11, 2016 16:08
Show Gist options
  • Save redherringbone/5122751351b2992e715361498090780b to your computer and use it in GitHub Desktop.
Save redherringbone/5122751351b2992e715361498090780b to your computer and use it in GitHub Desktop.
# https://github.com/maaaaz/nmaptocsv/blob/master/nmaptocsv.py
# http://www.idiotinside.com/2015/09/18/csv-json-pretty-print-python/
# only find open ports
# https://github.com/d1b/python-nmap-xml-output-parser/blob/master/shows_hosts_with_open_port_and_service_desc.py
import time
import subprocess
import sys
import xml.etree.ElementTree as ET
import base64
import os
import shutil
import csv
import logging
import logging.handlers
import json
import pprint
from optparse import OptionParser
import requests
VERSION = "2016-10-30"
options = None
REPORT_DIR = "/nmap/reports/"
DATA_DIR = "/nmap/"
HOSTS_SCAN_FILE = DATA_DIR + "hoststoscan.csv"
OUTPUT_SCRIPT_FILE = DATA_DIR + "nmapprocessscript.sh"
if not os.path.exists("/var/log/log.log"):
print " Log file needed. Create with sudo."
print "sudo touch /var/log/log.log && sudo chmod a+w+r /var/log/log.log"
quit()
theLogger = logging.getLogger('theLogger')
theLogger.setLevel(logging.DEBUG)
logging.basicConfig(filename='/var/log/log.log',level=logging.DEBUG)
handler = logging.handlers.SysLogHandler(address = '/dev/log')
theLogger.addHandler(handler)
theLogger.info("Starting nmap runner %s",os.uname()[1] )
def logOut(theMessage):
theMessage = "%s %s" % (time.strftime('%Y-%m-%d_%H:%M:%S'), theMessage)
theLogger.info(theMessage)
print theMessage
def parse_opts():
global NMAPFILE, OPT_DIRECTORY, options
parser = OptionParser(usage="""Process CSV or TSV file of host/IP from input [default=%s].
Create bash script file [default=%s] of NMAP commands to execute.
Then run bash script
Then run nmapgtrim.py
This version =%s""" %(HOSTS_SCAN_FILE, OUTPUT_SCRIPT_FILE, VERSION) )
parser.add_option("-t", "", action="store", dest="target_host", default = HOSTS_SCAN_FILE, help = "the target hosts filename default %s" % (HOSTS_SCAN_FILE))
parser.add_option("-o", "", action="store", dest="outputScriptFilePath", default = OUTPUT_SCRIPT_FILE, help = "Output script file path. Default = %s." % (OUTPUT_SCRIPT_FILE))
parser.add_option("-x", "", action="store", dest="outputXMLDirectoryPath", default = REPORT_DIR, help = "Output XML file path. Default = %s." % (REPORT_DIR))
parser.add_option("-s", "", action="store", dest="speed", default = "FAST", help = "Speed 'FAST' or 'NORMAL' or 'TEST'. Default = FAST")
(options, args) = parser.parse_args()
return
def createScriptFile(theFilePath):
logOut("Creating script file %s" % (theFilePath))
with open(theFilePath, 'w+') as theScriptFile:
theScriptFile.write("#!/bin/bash\n")
theScriptFile.write("# Generated by Script on %s \n" % (time.strftime('%Y-%m-%d_%H:%M:%S')))
theScriptFile.write("# Called: %s \n" % ("nmaprunner.py") )
theScriptFile.write("sudo rm %s*.xml -f \n" % (options.outputXMLDirectoryPath) )
theScriptFile.write("sudo rm %s*.nmap -f \n" % (options.outputXMLDirectoryPath) )
theScriptFile.write("sudo rm %s*.gnmap -f \n" % (options.outputXMLDirectoryPath) )
def appendScriptFile(theFilePath, theText):
with open(theFilePath, 'a') as theScriptFile:
theScriptFile.write(theText)
def generateScriptCommandsFromDict(theDict, scriptType = "FAST"):
if scriptType == "FAST":
commanda = "sudo nmap -T4 -sS -F -vv --max-rtt-timeout 200ms --max-retries 3 --max-scan-delay 1 --scan-delay 1 "
elif scriptType == "NORMAL" :
commanda = "sudo nmap -T4 -sS -p- -vv --max-rtt-timeout 200ms --max-retries 3 --max-scan-delay 1 --scan-delay 1 -n --min-hostgroup 100 --min-rate 500 "
elif scriptType == "TEST":
print "TEST DEBUG conditions "
appendScriptFile(options.outputScriptFilePath, "echo 'TEST DEBUG' " + "\n")
commanda = "sudo nmap -T4 -p80 -vv --max-rtt-timeout 200ms --max-retries 3 --max-scan-delay 1 --scan-delay 1 "
else:
print "ERROR: invalid scriptType"
quit()
nCount = 0
for host, ip_addr in theDict.iteritems():
nCount += 1
if nCount > 10 and scriptType == "TEST":
break
commandb = " -oA " + options.outputXMLDirectoryPath + host
commandb += " " + ip_addr
appendScriptFile(options.outputScriptFilePath, "echo '%s %s'" % (nCount, host) + "\n")
appendScriptFile(options.outputScriptFilePath, "logger '%s nmap %s'" % (nCount, host) + "\n")
appendScriptFile(options.outputScriptFilePath, commanda + commandb + "\n")
logOut("Script updated %s" % (options.outputScriptFilePath) )
def parseHostsFile():
theFilePath = options.target_host
theFileName = os.path.splitext(os.path.basename(theFilePath))[0]
## theHosts => scanname : hosts spec
print "processing %s " % (theFilePath)
theHosts = {}
filedelim = ','
if "try" == "try":
## can use row.find(string)
with open(theFilePath, 'rb') as theCSV:
dialect = csv.Sniffer().sniff(theCSV.read(1024), delimiters=",\t")
theCSV.seek(0)
csvReader = csv.reader(theCSV, dialect)
for row in csvReader:
if (len(row) != 0) and ("." in row[1]) :
if row[0] in theHosts:
theHosts[row[0]] += " " + row[1]
else:
theHosts[row[0]] = row[1]
logOut("Hosts file parsed %s" % (theFilePath))
generateScriptCommandsFromDict(theHosts, options.speed)
if "except Exception, err" == "":
logOut ("ERROR %s parseFile (%s)" %(err, theFilePath))
parse_opts()
createScriptFile(options.outputScriptFilePath)
parseHostsFile()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment