Sunday, 5 August 2012

Network Snoop - NMAP scan with python

Upon deployment within a network the first task will be to undertake a scan of the network, as the tool will be targeted towards small networks it will only look at the Class-C range.


First task for the tool will be to find out what the Hosts internal IP address is;
    ipAddr = ([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")][:1])

The next step is to feed that into an nmap scan, I found the following nmap scan to be good at doing a quick scout around the network:

NMAP -v --open -T4 -oX $NMAPOUT.xml 10.0.0.0/24

The IP address we have needs to be cut down to a class C address and have '/24' appended:
    CuripAddr = ipAddr[0]
    NMAPOutputFile = 'scan-%T-%D.xml'
    
    #Nmap scan of class-C subnet: NMAP -v --open -T4 -oX $NMAPOUT.xml 10.0.0.0/24
    ClassCAddr = CuripAddr.rsplit('.',1)[0] + '.0'
    NmapCommand = 'nmap -v --open -T4 -oX \'scan-%T-%D.xml ' + ClassCAddr + '/24'

Once the command is formed it can be run as a subprocess:
    from subprocess import Popen, PIPE    
    runningNmap = subprocess.Popen(NmapCommand, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True, bufsize=1)

This creates an XML file which can then be parsed into a DB or used for further scans.

1 comment:

  1. Thanks you very much for sharing these links. Will definitely check this out..
    best 10 vpn in the world

    ReplyDelete