A python 3 library which helps in using nmap port scanner.

Overview

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")

Using the nmap vulners script to identify vulnerabilities (CVE's)

You scan the the target IP using version detection ('-sV') to get the service and, the script performs a lookup in the CVE database. The nmap vulners script is part of the default Nmap installation, so you shouldn't need to install any other packages.

   import nmap3
   nmap = nmap3.Nmap()
   ressults = nmap_version_detection("host", args="--script vulners --script-args mincvss+5.0")

Cross-Selling

Comments
  • parse_noportscan does not parse mac address

    parse_noportscan does not parse mac address

    I'm trying to use this package to find the IP, knowing the MAC address, but... This data:

    <host><status state="up" reason="arp-response" reason_ttl="0"/>
    <address addr="10.11.12.13" addrtype="ipv4"/>
    <address addr="00:01:02:03:04:05" addrtype="mac" vendor="Vendor Inc."/>
    <hostnames>
    <hostname name="00-01-02-03-04-05-dns-inc.com" type="PTR"/>
    </hostnames>
    <times srtt="1000" rttvar="5000" to="100000"/>
    </host>
    

    Is only parsed as:

    {'state': 'up', 'reason': 'arp-response', 'reason_ttl': '0', 'addr': '10.11.12.13', 'addrtype': 'ipv4'}

    opened by egabrum 16
  • udp scan under Windows 10 issue

    udp scan under Windows 10 issue

    We identified an issue when we did with Administrator rights an udp scan: results = nmapscan.nmap_udp_scan(f'{ip}')

    under Windows 10 ver. 21H1

    File "C:\Users\mrene\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\nmap3\nmap3.py", line 78, in default_command return self.default_command_privileged() File "C:\Users\mrene\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\nmap3\nmap3.py", line 89, in default_command_privileged return self.default_command() File "C:\Users\mrene\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\nmap3\nmap3.py", line 78, in default_command return self.default_command_privileged() File "C:\Users\mrene\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\nmap3\nmap3.py", line 89, in default_command_privileged return self.default_command() File "C:\Users\mrene\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\nmap3\nmap3.py", line 78, in default_command return self.default_command_privileged() File "C:\Users\mrene\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\nmap3\nmap3.py", line 89, in default_command_privileged return self.default_command() File "C:\Users\mrene\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\nmap3\nmap3.py", line 78, in default_command return self.default_command_privileged() File "C:\Users\mrene\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\nmap3\nmap3.py", line 89, in default_command_privileged return self.default_command() File "C:\Users\mrene\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\nmap3\nmap3.py", line 78, in default_command return self.default_command_privileged() RecursionError: maximum recursion depth exceeded

    it works fine with root and normal rights under Linux.

    Also tcp scan is running well: results = nmapscan.nmap_tcp_scan(f'{ip}')

    on Windows 10 and Linux

    created a new ncurses front-end for your python3-nmap:

    https://github.com/MrEnergy64/ncurses-IPventur

    python-nmap3 version is: 1.4.9

    opened by MrEnergy64 11
  • Docker Compose Issue

    Docker Compose Issue

    I can't seem to get this to work in a docker container. For some reason, Nmap seems to think every IP address is on the network (when it's not). No issues when running outside of the docker container.

    This is what I've got so far. https://github.com/charlesomer/network-devices-scanner

    I've set network_mode: host in the compose file so I'm not sure. I may have missed something obvious?

    opened by charlesomer 8
  • Ping scan doesn't return mac address

    Ping scan doesn't return mac address

    Hello,

    When i execute the function nmap.nmap_ping_scan('192.168.1.0/24'), all hosts (50) have a "None" value as their mac address. If i run the command /usr/bin/nmap -oX - -sP 192.168.1.0/24, i get the same results, no mac address. But if i run this same command in sudo, i get the mac address.

    Is there a parameter to run this command in sudo ?

    Thanks you

    opened by 0xN0x 7
  • wrong handling of xml.etreeElementTree objects

    wrong handling of xml.etreeElementTree objects

    This library has code like this in multiple places:

    if (some_xml_element.find('something_else')):
        do_some_stuff
    

    Under some Python / xml.etree versions this will not work as intended, for example I am getting this output from Nmap().nmap_version_detection() (notice missing information about port state and service exposed on the port):

    {'port': '443',
     'protocol': 'tcp'}
    

    In the case of Nmap().nmap_version_detection() this happens because <state> and <service> do not have any further sub-elements and as the docs say:

    Elements with no subelements will test as False. This behavior will change in future versions. Use specific len(elem) or elem is None test instead.

    Changing if(port.find("service")): with if port.find("service") is not None: will restore correct behaviour and give complete results like:

     {'port': '443',
      'protocol': 'tcp',
      'reason': 'syn-ack',
      'reason_ttl': '0',
      'service': {'conf': '10',
                  'devicetype': 'security-misc',
                  'method': 'probed',
                  'name': 'http',
                  'product': 'Fortinet security device httpd',
                  'tunnel': 'ssl'},
      'state': 'open'}]
    
    opened by lesinigo 7
  • Implement optional argument for more command line options to be used in nmap?

    Implement optional argument for more command line options to be used in nmap?

    Can you add an argument which allows you to pass selected arguments to the process? For example like this:

    import nmap3
    nmap = nmap3.NmapScanTechniques()
    nmap.nmap_tcp_scan(target, args="-Pn")
    # or
    nmap.nmap_tcp_scan(target, args=["-Pn"])
    
    opened by ioncodes 7
  • the function

    the function "nmap3.Nmap().nmap_version_detection()" seems that it has a problem

    in nmap3\nmapparser.py the function "version_detection()" has not defined the variable "service_version_dict". when I used the "nmap3.Nmap().nmap_version_detection()" to call the "version_detection()", it raised "NameError: name 'service_version_dict' is not defined".

    ps: I programmed my code at Win10 OS

    opened by bobolike123 6
  • still recognice issue with os.geteuid() in Windows 10 21H1

    still recognice issue with os.geteuid() in Windows 10 21H1

    We still identify the issue that your python3-nmap is using "os.geteuid()" which is not exist in Windows 10 (21H1) anymore:

    File "C:\Users\mrene\Documents\PYTHON\ncurses-IPventur.py", line 389, in scannen results = nmap.nmap_os_detection(f'{ip}') File "C:\Users\mrene\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\nmap3\utils.py", line 74, in wrapper if(os.geteuid() == 0): AttributeError: module 'os' has no attribute 'geteuid'

    Therefore actual you cannot do these scans under Windows 10:

    nmap_detect_firewall nmap_os_detection nmap_stealth_scan nmap.nmap_os_detection

    In our python3-nmap front ncurses-IPventur we use this workaround with win32api.GetConsoleTitle(). Or do you have another solution instead of os.getuid? python-nmap3 version is: 1.4.9

    os_version = platform.system() if os_version == "Windows": ScreenDel = "cls" import win32api check = win32api.GetConsoleTitle() cmd = "where" if platform.system() == "Windows" else "which" if "Administrator" in check: User = "Administrator" return os_version, User, ScreenDel, cmd else: User = "Normal User - limited scan" return os_version, User, ScreenDel, cmd else: ScreenDel = "clear" cmd = "where" if platform.system() == "Windows" else "which" if os.geteuid()==0: User = "Root User" return os_version, User, ScreenDel, cmd else: User = "Normal User - limited scan" return os_version, User, ScreenDel, cmd

    opened by MrEnergy64 5
  • nmap_dns_brute_script doesn't work properly

    nmap_dns_brute_script doesn't work properly

    When I run nmap.nmap_dns_brute_script("google.com") a few times it will eventually get the hostname and address wrong. Expected:

    [{'address': '172.217.168.46', 'hostname': 'admin.google.com'},...
    

    What I get sometimes:

    [{'address': 'admin.google.com', 'hostname': '172.217.168.46'},...
    
    opened by ioncodes 5
  • Version detection not running with Python3.10

    Version detection not running with Python3.10

    Hi,

    I am trying to run version detection and vulners script on Python 3.10 but its failing for some reason

    import nmap3
    
    nmap = nmap3.Nmap()
    result = nmap.nmap_version_detection('8.8.8.8',args="--script vulners --script-args mincvss+5.0" )
    print(result)
    

    and the error looks something like

    Traceback (most recent call last):
      File "/home/cybersoldier/Documents/nmap.py", line 4, in <module>
        result = nmap.nmap_version_detection('8.8.8.8',args="--script vulners --script-args mincvss+5.0" ) #Add the -p- flag here, removed for testing purposes
      File "/usr/local/lib/python3.10/dist-packages/nmap3/nmap3.py", line 195, in nmap_version_detection
        xml_root = self.scan_command(target=target, arg=arg, args=args, timeout=timeout)
      File "/usr/local/lib/python3.10/dist-packages/nmap3/nmap3.py", line 125, in scan_command
        output = self.run_command(scan_shlex, timeout=timeout)
      File "/usr/local/lib/python3.10/dist-packages/nmap3/nmap3.py", line 264, in run_command
        raise NmapExecutionError('Error during command: "' + ' '.join(cmd) + '"\n\n' + errs.decode('utf8'))
    nmap3.exceptions.NmapExecutionError: Error during command: "/usr/bin/nmap -oX - 8.8.8.8 -sV --script vulners --script-args mincvss+5.0"
    

    I have tried running the script both with and without sudo but the error stays the same.

    opened by njmulsqb 4
  • Removed possibility to elevate privileges in code (commit: 3c2246b7dfdd6164dd5b7a0a74cfdc6585ea5a32)

    Removed possibility to elevate privileges in code (commit: 3c2246b7dfdd6164dd5b7a0a74cfdc6585ea5a32)

    In commit: 3c2246b7dfdd6164dd5b7a0a74cfdc6585ea5a32 the possibility to use this tool as a normal user and only allow the usage of sudo for scanning was removed as part of an IPv6 fix. Any particular reason why this change was included there?

    opened by tnyblom 4
  • License classifier doesn't match LICENSE file

    License classifier doesn't match LICENSE file

    Hello,

    I noticed that the license classifier https://github.com/nmmapper/python3-nmap/blob/master/setup.py#L27 doesn't match the license defined in the license file https://github.com/nmmapper/python3-nmap/blob/master/LICENSE

    opened by mimi89999 1
Releases(1.5.2)
Owner
Nmmapper
Nmmapper offers Pentest tools from nmap online to subdomain finder, theHarvester, wappalyzer. Discover dns records of domains, detect cms using cmseek & whatweb
Nmmapper
TLD records archive. Revisiting the original TLDR project by mandatoryprogrammer, on the hunt for more root nameserver changes.

tldr A(nother) continuously updated historical TLD records archive. This repository is updated approximately every three hours with the results from D

Chris Partridge 11 Dec 14, 2022
EchoDNS - Analyze your DNS traffic super easy, shows all requested DNS traffic

EchoDNS - Analyze your DNS traffic super easy, shows all requested DNS traffic

Oli Zimmermann 1 Jan 11, 2022
Takes a file of hosts or domains and outputs the IP address of each host/domain in the file.

Takes a file of hosts or domains and outputs the IP address of each host/domain in the file. Installation $ git clone https://github.com/whoamisec75/i

whoami security 2 May 10, 2022
A Simplest TCP client and echo server

Простейшие TCP-клиент и эхо-сервер Цель работы Познакомиться с приемами работы с сетевыми сокетами в языке программирования Python. Задания для самост

Юля Нагубнева 1 Oct 25, 2021
A simple tcpdump sidecar injector to demonstrate Kubernetes's Mutating Webhook

k8s-tcpdump-webhook A simple tcpdump sidecar injector to demonstrate Kubernetes's Mutating Webhook Build and Deploy Build docker image; docker build -

Bilal Ünal 2 Sep 01, 2022
DataShare - Simple library for data sharing between scripts and public functions calling

DataShare - Simple library for data sharing between scripts and public functions calling. Installation. Install code, Delete LICENSE, README, readme.t

Ivan Perzhinsky. 1 Dec 17, 2021
pyngrok is a Python wrapper for ngrok

pyngrok is a Python wrapper for ngrok that manages its own binary, making ngrok available via a convenient Python API.

Alex Laird 329 Dec 31, 2022
Serves some data over HTTP, once. Based on the built-in Python module http.server

serve-me-once Serves some data over HTTP, once. Based on the built-in Python module http.server.

Peder Bergebakken Sundt 2 Jan 06, 2022
Mass querying whois records using whois tool

Mass querying whois records using whois tool

Mohamed Elbadry 24 Nov 10, 2022
A collection of domains, wildcards and substrings designed for dnscrypt-proxy filter method.

A collection of domains, wildcards and substrings designed for dnscrypt-proxy filter method.

3 Oct 25, 2022
The best way to send tokens into a specific server, which can be used for discord bots, and some tools..

XTRA420 The simplified version of sending tokens into a server, the basic and fastest way.. When using this, you have the option to use proxies (http)

07v 1 Nov 30, 2021
PoC code for stealing the WiFi password of a network with a Lovebox IOT device connected

LoveBoxer PoC code for stealing the WiFi password of a network with a Lovebox IOT device connected. This PoC was is what I used in this blogpost Usage

Graham Helton 10 May 24, 2022
DNSStager is an open-source project based on Python used to hide and transfer your payload using DNS.

What is DNSStager? DNSStager is an open-source project based on Python used to hide and transfer your payload using DNS. DNSStager will create a malic

Askar 547 Dec 20, 2022
the objective of this project is to create a Node Js server with a Python client

Socket.io-Server-client Objective The objective of this project is to send data real time ,we use socket.io(server, client) on this project Server Nod

Reda Ennakouri 5 Mar 07, 2022
A Python library to ease the integration with the Beem Africa (SMS, AIRTIME, OTP, 2WAY-SMS, BPAY, USSD)

python-client A Python library to easy the integration with the Beem Africa SMS Gateway Features to be Implemented Airtime OTP SMS Two way SMS USSD Bp

Beem Africa 24 Oct 29, 2022
This python script can change the mac address after some attack

MAC-changer Hello people, this python script was written for people who want to change the mac address after some attack, I know there are many ways t

5 Oct 10, 2022
Transfer files to and from a Windows host via ICMP in restricted network environments.

ICMP-TransferTools ICMP-TransferTools is a set of scripts designed to move files to and from Windows hosts in restricted network environments. This is

icyguider 269 Dec 20, 2022
A simple framwork to streamline the Domain Adaptation training process.

FastDA Introduction This is a simple framework for domain adaptation training. You can use it to build your own training process. It heavily relies on

Vincent Zhang 7 Nov 22, 2022
test whether http(s) proxies actually hide your ip

Proxy anonymity I made this for other projects, to find working proxies. If it gets enough support and if i have time i might make it into a gui Repos

gxzs1337 1 Nov 09, 2021
A simple hosts picker for Microsoft Services

A simple Python scrip for you to select the fastest IP for Microsoft services.

Konnyaku 394 Dec 17, 2022