Skip to main content

python3-nmap

A python 3 library which helps in using nmap port scanner. The way this tools works is by defining each nmap command into a python function making it very easy to use sophisticated nmap commands in other python scripts. Nmap is a complicated piece of software used for reconnaissance on target networks, over the years new features have been added making it more sophisticated.

With this python3-nmap we make using nmap in python very easy and painless

For example in nmap if you want to scan for common ports you would to something like this

$ nmap your-host.com --top-ports 10

But in this python3-nmap script you would do something like this

import nmap3
nmap = nmap3.Nmap()
results = nmap.scan_top_ports("your-host.com")
# And you would get your results in json

You will notice each nmap command is defined as a python function/method. this make it easy to remember this in python and easily use them.

Again in nmap if you want to use the famous dns-brute script you would do something like this

$ nmap your-host.com  --script dns-brute.nse

But in this python3 script again it's very easy you just do something like this

import nmap3
nmap = nmap3.Nmap()
results = nmap.nmap_dns_brute_script("your-host.com")

# And you would get your results in json
[
    {
        "address": "mail.your-host.com",
        "hostname": "68.65.122.10"
    },
    {
        "address": "www.your-host.com",
        "hostname": "5.189.129.43"
    }
]

How to use python3-nmap

Using this scripts is very easy, though it assumes you have nmap already installed, as it is the primary dependence required. Also this tools supports both windows and linux, it's cross platform so to say.

Installation

$ git clone https://github.com/wangoloj/python3-nmap.git

$ pip3 install -r requirements.txt

# Install nmap online

$ apt-get install nmap

# That's all is needed to get started

In nmap some commands require root privileges for example the command to identify OS requires root privileges;

$ nmap -O your-host.com

TCP/IP fingerprinting (for OS scan) requires root privileges.
QUITTING!
# Until you sudo

$ sudo nmap -O your-host.com

The same applies to the script to be able to run the os identifier you have to be a super user.

How to use the script to identify OS

import nmap3
nmap = nmap3.Nmap()
os_results = nmap.nmap_os_detection("192.168.178.2") # MOST BE ROOT
[
    {
        "accuracy": "100",
        "cpe": "cpe:/o:linux:linux_kernel:2.6",
        "line": "45249",
        "name": "Linux 2.6.14 - 2.6.34",
        "osclass": {
            "accuracy": "100",
            "osfamily": "Linux",
            "osgen": "2.6.X",
            "type": "general purpose",
            "vendor": "Linux"
        }
    },
    {
        "accuracy": "100",
        "cpe": "cpe:/o:linux:linux_kernel:2.6.17",
        "line": "45775",
        "name": "Linux 2.6.17",
        "osclass": {
            "accuracy": "100",
            "osfamily": "Linux",
            "osgen": "2.6.X",
            "type": "general purpose",
            "vendor": "Linux"
        }
    },
    {
        "accuracy": "100",
        "cpe": "cpe:/o:linux:linux_kernel:2.6.17",
        "line": "45811",
        "name": "Linux 2.6.17 (Mandriva)",
        "osclass": {
            "accuracy": "100",
            "osfamily": "Linux",
            "osgen": "2.6.X",
            "type": "general purpose",
            "vendor": "Linux"
        }
    },
    {
        "accuracy": "100",
        "cpe": "cpe:/o:linux:linux_kernel:3.13",
        "line": "60884",
        "name": "Linux 3.13",
        "osclass": {
            "accuracy": "100",
            "osfamily": "Linux",
            "osgen": "3.X",
            "type": "general purpose",
            "vendor": "Linux"
        }
    }
]

Class components of python3-nmap

The script is made of up the following classes, each holding different nmap abilities and scan types.

  • Nmap
  • NmapHostDiscovery
  • NmapScanTechniques

Identifying service version

In nmap if you want to identify versions you would run this kind of command

$ nmap 192.168.178.1  -sV

In this python script you would do something like this

import nmap3
nmap = nmap3.Nmap()
version_result = nmap.nmap_version_detection("your-host.com")
[
    {
        "cpe": [
            {
                "cpe": "cpe:/o:linux:linux_kernel"
            }
        ],
        "port": "80",
        "protocol": "tcp",
        "service": {
            "conf": "10",
            "extrainfo": "Ubuntu",
            "method": "probed",
            "name": "http",
            "ostype": "Linux",
            "product": "nginx",
            "version": "1.14.0"
        }
    },
    {
        "cpe": [
            {
                "cpe": "cpe:/o:linux:linux_kernel"
            }
        ],
        "port": "443",
        "protocol": "tcp",
        "service": {
            "conf": "10",
            "extrainfo": "Ubuntu",
            "method": "probed",
            "name": "http",
            "ostype": "Linux",
            "product": "nginx",
            "tunnel": "ssl",
            "version": "1.14.0"
        }
    },
    {
        "cpe": [
            {
                "cpe": "cpe:/o:linux:linux_kernel"
            }
        ],
        "port": "2000",
        "protocol": "tcp",
        "service": {
            "conf": "10",
            "extrainfo": "Ubuntu Linux; protocol 2.0",
            "method": "probed",
            "name": "ssh",
            "ostype": "Linux",
            "product": "OpenSSH",
            "version": "7.6p1 Ubuntu 4ubuntu0.3"
        }
    }
]

Nmap commands available

The following nmaps commands have been added to the following scripts

  • get Nmap version details
    import nmap3
    nmap = nmap3.Nmap()
    results = nmap.nmap_version()
    
  • Nmap top port scan
    import nmap3
    nmap = nmap3.Nmap()
    results = nmap.scan_top_ports("your-host")
    
  • Nmap Dns-brute-script( to get subdomains )
   import nmap3
   nmap = nmap3.Nmap()
   results = nmap.nmap_dns_brute_script("domain")
  • Nmap list scan
   import nmap3
   nmap = nmap3.Nmap()
   results = nmap.nmap_list_scan("your-host")
  • Nmap Os detection
  import nmap3
  nmap = nmap3.Nmap()
  results = nmap.nmap_os_detection("your-host");
  • Nmap subnet scan
   import nmap3
   nmap = nmap3.Nmap()
   results = nmap.nmap_subnet_scan("your-host") #Must be root
  • Nmap version detection
   import nmap3
   nmap = nmap3.Nmap()
   results = nmap.nmap_version_detection("your-host") # Must be root

Nmap Scanning Techniques

The script offers nmap scan techniques also as python function/methods

  • nmap_fin_scan

    import nmap3
    nmap = nmap3.NmapScanTechniques()
    result = nmap.nmap_fin_scan("192.168.178.1")
    
  • nmap_idle_scan

   import nmap3
   nmap = nmap3.NmapScanTechniques()
   result = nmap.nmap_idle_scan("192.168.178.1")
  • nmap_ping_scan
   import nmap3
   nmap = nmap3.NmapScanTechniques()
   result = nmap.nmap_ping_scan("192.168.178.1")
  • nmap_syn_scan
   import nmap3
   nmap = nmap3.NmapScanTechniques()
   result = nmap.nmap_syn_scan("192.168.178.1")
  • nmap_tcp_scan
   import nmap3
   nmap = nmap3.NmapScanTechniques()
   result = nmap.nmap_tcp_scan("192.168.178.1")
  • nmap_udp_scan
   import nmap3
   nmap = nmap3.NmapScanTechniques()
   result = nmap.nmap_udp_scan("192.168.178.1")

Supporting the nmap host discovery

The script also offers support for map Added Nmap Host discovery techniques still as python function/methods

  • Only port scan (-Pn)
  • Only host discover (-sn)
  • Arp discovery on a local network (-PR)
  • Disable DNS resolution (-n)

NmapHostDiscovery

  • def nmap_portscan_only(self, host, args=None)
   import nmap3
   nmap = nmap3.NmapHostDiscovery()
   results = nmap.nmap_portscan_only("your-host")
  • def nmap_no_portscan(self, host, args=None):
   import nmap3
   nmap = nmap3.NmapHostDiscovery()
   results = nmap.nmap_no_portscan("your-host")
  • def nmap_arp_discovery(self, host, args=None):
  import nmap3
  nmap = nmap3.NmapHostDiscovery()
  results = nmap.nmap_arp_discovery("your-host")
  • def nmap_disable_dns(self, host, args=None):
  import nmap3
  nmap = nmap3.NmapHostDiscovery()
  results = nmap.nmap_disable_dns("your-host")

Nmap is a large tool, as you can see python3-nmap provides only things what you could say commonly used nmap features.

Using custom nmap command line arguments.

As we said, the script defines each set of nmap command as python function/methods. You can also pass arguments to those methods/function thus extending your capabilities for example. Let's say we want to scan top ports but also perform version detection .

   import nmap3
   nmap = nmap3.Nmap()
   results = nmap.scan_top_ports("host", args="-sV")

Cross-Selling

Metadata

Release files for python3-nmap 1.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python3-nmap 1.5.0
File Size Uploaded
python3-nmap-1.5.0.tar.gz 13.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python3-nmap 1.5.0
File Interpreter ABI Platform
python3_nmap-1.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 39.3 kB

Release files / python3-nmap-1.5.0.tar.gz

Download URL python3-nmap-1.5.0.tar.gz
Size 13.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b52744e0c9944c567733b8deb60d6363e17233ee40466edfb1b09a5780576f9a
BLAKE2b-256 checksum
How to use checksums
d7c85a2f7f05f68d42b9a40bad514f41a7cb32b4043c1d622efc2f70eb2eaa3e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.5

Release files / python3_nmap-1.5.0-py3-none-any.whl

Download URL python3_nmap-1.5.0-py3-none-any.whl
Size 25.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
27555349c519cb0c4a22c0afd2c05dfa051c1d33cc1ff21d32b5bc89ff9464b4
BLAKE2b-256 checksum
How to use checksums
3c5f3e768c99298b841d67f3df637bce8402d46ea0576ec26ab7bd15864831eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.5

Release history Release notifications | RSS feed

1.9.1

2 release files

1.6.0

2 release files

1.5.4

2 release files

1.5.3

2 release files

1.5.2

3 release files

1.5.1

2 release files

This release

1.5.0 This release

2 release files

1.4.9

2 release files

1.4.8

2 release files

1.4.7

2 release files

1.4.6

2 release files

1.4.5

2 release files

1.4.4

2 release files

1.4.3

2 release files

1.4.2

2 release files

1.4.1

2 release files

1.3.1

2 release files

1.2.1

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page