Created
May 10, 2019 17:17
-
-
Save milo2012/82e15d7c75859d191c16f81e0e2a8d67 to your computer and use it in GitHub Desktop.
for segmentation tests - compare nmap xml files with scope and check which scopes were accessible and which weren't
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python -tt | |
# -*- coding: utf-8 -*- | |
import sys, glob, optparse | |
reload(sys); | |
from netaddr import IPNetwork | |
from libnmap.parser import NmapParser | |
ipDict={} | |
scopeDict={} | |
accessibleList=[] | |
notAccessibleList=[] | |
fileSearch="" | |
scopeFile="" | |
def convertSubnetToIPAddrList(tmpStr): | |
tmpList=[] | |
for ip in IPNetwork(tmpStr): | |
tmpList.append(str(ip)) | |
return tmpList | |
def parseFile(scopeFile,fileSearch): | |
scopeList = [line.rstrip('\n') for line in open(scopeFile)] | |
for x in scopeList: | |
tmpIPList=convertSubnetToIPAddrList(x) | |
scopeDict[x]=tmpIPList | |
tmpFileList=glob.glob(fileSearch+"*.xml") | |
for filename in tmpFileList: | |
try: | |
rep = NmapParser.parse_fromfile(filename) | |
for scanned_hosts in rep.hosts: | |
tmpPortList=[] | |
svcList = scanned_hosts.services | |
for x in svcList: | |
portStr=str(x.port)+"/"+x.protocol | |
if portStr not in tmpPortList: | |
tmpPortList.append(portStr) | |
if scanned_hosts.address not in ipDict: | |
ipDict[scanned_hosts.address]=tmpPortList | |
else: | |
tmpPortList1=[] | |
tmpPortList1=ipDict[scanned_hosts.address] | |
for y in tmpPortList1: | |
if y not in tmpPortList: | |
tmpPortList.append(y) | |
ipDict[scanned_hosts.address]=tmpPortList | |
except Exception as e: | |
continue | |
#print e | |
#print "\n" | |
for key, value in ipDict.iteritems(): | |
for key1, value1 in scopeDict.iteritems(): | |
if key in value1: | |
if key1 not in accessibleList: | |
accessibleList.append(key1) | |
print "\n[+] Accessible subnets" | |
accessibleList.sort() | |
for x in accessibleList: | |
print x | |
print "\n[+] Inaccessible subnets" | |
for key,value in scopeDict.iteritems(): | |
if key not in accessibleList: | |
notAccessibleList.append(key) | |
notAccessibleList.sort() | |
for x in notAccessibleList: | |
print x | |
if __name__ == "__main__": | |
parser = optparse.OptionParser() | |
parser.add_option('-f','--file', action="store", dest="scopeFilename",help="file containing list of scopes") | |
parser.add_option('-k', '--keyword', action="store", dest="keywordSearch",help="nmap xml file partial match") | |
options, remainder = parser.parse_args() | |
if len(sys.argv)==1: | |
parser.print_help() | |
sys.exit(1) | |
else: | |
if not options.scopeFilename: | |
print "[-] Please provide the file containing list of scopes using -f or --file option" | |
sys.exit() | |
if not options.keywordSearch: | |
print "[-] Please provide the prefix for the nmap files using -k or --keyword option" | |
sys.exit() | |
parseFile(options.scopeFilename,options.keywordSearch) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment