Skip to content

Improve nmap #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ After installation, use the `netbox-scanner.conf` file as an example to create y
## Quick Start

0. Clone the repo and install the dependencies as shown above.
1. Move the `netbox-scanner.conf` file to your Netbox directory (`/opt/netbox`) and fill out the variables according to your setup. Don't forget to change the path to match where you put this repo under `[NMAP].path`.
2. Go to the `samples` subdirectory of this repo and execute `./nmap-scan.sh` to get a first look at the behavior of this project.
1. Use the `netbox-scanner.conf` file that you find in the project directory or create your own under (`/opt/netbox/netbox-scanner.conf` or `~/.netbox-scanner.conf`) and fill out the variables according to your setup.
2. Fill the networks you want to scan (one per line) under `networks.txt` and then run `python netbox-scanner.py nmap` to get a first look at the behavior of this project.

## Basics
netbox-scanner reads a user-defined source to discover IP addresses and descriptions, and insert them into NetBox. To control what was previously inserted, netbox-scanner adds tags to each record, so it will know that that item can be handled. In order to guarantee the integrity of manual inputs, records without such tags will not be updated or removed.

It is important to note that if netbox-scanner cannot define the description for a given host, then it will insert the string defined in the `unknown` parameter. Users can change those names at their own will.

For NetBox access, this script uses [pynetbox](https://github.com/digitalocean/pynetbox) --worth saying that was tested under NetBox v2.6.7.
For NetBox access, this script uses [pynetbox](https://github.com/digitalocean/pynetbox) --worth saying that was tested under NetBox v2.9.11.

### Garbage Collection
If the user marked the `cleanup` option to `yes`, then netbox-scanner will run a garbage collector after the synchronization finishes. Basically, it will get all IP addresses recorded in NetBox under the same tag. Then, both lists will be compared: the one just retrieved from NetBox and the list that was synced. Hosts in the first list that don't appear in the second list will be deleted.
Expand All @@ -40,9 +40,9 @@ If the user marked the `cleanup` option to `yes`, then netbox-scanner will run a
## Configuration
Users can interact with netbox-scanner by command line and configuration file. The latter is pretty simple and straight forward: the only parameter accepted is the module you want to use.

The configuration file (`netbox-scanner.conf`) is where netbox-scanner looks for details such as authentication data and path to files. This file can be stored on the user's home directory or on `/opt/netbox`, but if you choose the first option, it must be a hidden file --`.netbox-scanner.conf`.
The configuration file (`netbox-scanner.conf`) is where netbox-scanner looks for details such as authentication data and path to files. This file can be stored on the user's home directory, on `/opt/netbox` or under the project root directory, but if you choose the first option, it must be a hidden file --`.netbox-scanner.conf`.

> Remember that netbox-scanner will always look for this file at home directory, then at `/opt/netbox`, in this order. The first occurrence will be considered.
> Remember that netbox-scanner will always look for this file at home directory, then at `/opt/netbox`, and finally in the project root dir, in this order. The first occurrence will be considered.


## Modules
Expand All @@ -54,10 +54,8 @@ Since version 2.0, netbox-scanner is based on modules. This way, this program i


## Nmap Module
Performing the scans is beyond netbox-scanner features, so you must run [Nmap](https://nmap.org/) and save the output as an XML file using the `-oX` parameter. Since this file can grow really fast, you can scan each network and save it as a single XML file. You just have to assure that all files are under the same directory before running the script --see `samples/nmap.sh` for an example.

To properly setup this module, you must inform the path to the directory where the XML files reside, define a tag to insert to discovered hosts, and decide if clean up will take place. Tested on Nmap v7.80.

Performing the scans is incorporated now under netbox-scanner features, so you don't need to run [Nmap](https://nmap.org/) yourself.
In this version the scan doesn't need root privileges since it only checks for hosts up.

## Prime Module
This script accesses [Prime](https://www.cisco.com/c/en/us/products/cloud-systems-management/prime-infrastructure/index.html) through RESTful API and all routines are implemented here. Users only have to point to Prime's API, which looks like `https://prime.domain/webacs/api/v4/`, inform valid credentials allowed to use the API, and fill the other variables, just like in Nmap.
Expand Down
11 changes: 8 additions & 3 deletions nbs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import requests

import requests
from pynetbox import api


Expand Down Expand Up @@ -47,7 +47,12 @@ def sync_host(self, host):
return False

if nbhost:
if (self.tag in nbhost.tags):
tag_update = False
for tag in nbhost.tags:
if (self.tag == tag.name):
tag_update = True
break
if tag_update:
if (host[1] != nbhost.description):
aux = nbhost.description
nbhost.description = host[1]
Expand All @@ -59,7 +64,7 @@ def sync_host(self, host):
logging.info(f'unchanged: {host[0]}/32 "{host[1]}"')
self.stats['unchanged'] += 1
else:
logging.info(f'unchanged: {host[0]}/32 "{host[1]}"')
logging.info(f'no-tag(unchanged): {host[0]}/32 "{host[1]}"')
self.stats['unchanged'] += 1
else:
self.netbox.ipam.ip_addresses.create(
Expand Down
48 changes: 25 additions & 23 deletions nbs/nmap.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
import os
import xml.etree.ElementTree as ET

import nmap3

class Nmap(object):

def __init__(self, path, unknown):
def __init__(self, unknown, networks):
self.unknown = unknown
self.path = path
self.networks = networks
self.hosts = list()
self.scan_results = {}

def run(self):
for f in os.listdir(self.path):
if not f.endswith('.xml'):
continue
abspath = os.path.join(self.path, f)
tree = ET.parse(abspath)
root = tree.getroot()
def scan(self):
nmap = nmap3.NmapHostDiscovery() # instantiate nmap object
for item in self.networks:
temp_scan_result = nmap.nmap_no_portscan(item.replace('\n', ''), args="-R --system-dns")
self.scan_results = {**self.scan_results, **temp_scan_result}
self.scan_results.pop("stats")
self.scan_results.pop("runtime")
return self.scan_results

for host in root.findall('host'):
try:
self.hosts.append((
host.find('address').attrib['addr'],
host.find('hostnames').find('hostname').attrib['name']
))
except AttributeError:
self.hosts.append((
host.find('address').attrib['addr'],
self.unknown
))
def run(self):
self.scan()
for k,v in self.scan().items():
try:
self.hosts.append((
k,
v['hostname'][0]['name']
))
except (IndexError, KeyError):
self.hosts.append((
k,
self.unknown
))
1 change: 1 addition & 0 deletions netbox-scanner.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ tls_verify = no

[NMAP]
path = ./
networks = networks.txt
unknown = autodiscovered:netbox-scanner
tag = nmap
cleanup = no
Expand Down
7 changes: 6 additions & 1 deletion netbox-scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@

local_config = expanduser('~/.netbox-scanner.conf')
global_config = '/opt/netbox/netbox-scanner.conf'
dir_config = './netbox-scanner.conf'
config = ConfigParser()

if isfile(local_config):
config.read(local_config)
elif isfile(global_config):
config.read(global_config)
elif isfile(dir_config):
config.read(dir_config)
else:
raise FileNotFoundError('Configuration file was not found.')

Expand Down Expand Up @@ -66,9 +69,11 @@
# useful if you have tls_verify set to no
disable_warnings(InsecureRequestWarning)

with open(nmap['networks'], 'r') as file:
networks = file.readlines()

def cmd_nmap(s): # nmap handler
h = Nmap(nmap['path'], nmap['unknown'])
h = Nmap(nmap['unknown'], networks)
h.run()
s.sync(h.hosts)

Expand Down
5 changes: 5 additions & 0 deletions networks.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
192.168.2.0/24
192.168.3.0/24
192.168.4.0/24
192.168.5.0/24
192.168.15.0/24
11 changes: 6 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
certifi==2020.4.5.1
certifi==2020.12.5
chardet==3.0.4
idna==2.9
pynetbox==4.3.1
requests==2.23.0
idna==2.10
pynetbox==5.1.0
requests==2.25.0
six==1.15.0
urllib3==1.25.9
urllib3==1.26.2
python3-nmap==1.4.9
7 changes: 4 additions & 3 deletions tests/test_nmap.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import unittest
from os import environ

from nbs.nmap import Nmap


class TestRequest(unittest.TestCase):
def test_api(self):
path = environ.get('NMAP_PATH')

nmap = Nmap(path, 'test')
nmap = Nmap("test", ["127.0.0.1/32"])
self.assertIsInstance(nmap, Nmap)
nmap.run()
self.assertIsInstance(nmap.hosts, list)
self.assertEqual(nmap.hosts[0][0], "127.0.0.1")
self.assertEqual(nmap.hosts[0][1], "localhost")


if __name__ == '__main__':
Expand Down