Divide full port scan results and use it for targeted Nmap runs
Project description
Divide Et Impera And Scan (and also merge the scan results)
DivideAndScan is used to efficiently automate port scanning routine by splitting it into 3 phases:
- Discover open ports for a bunch of targets.
- Run Nmap individually for each target with version grabbing and NSE actions.
- Merge the results into a single Nmap report (different formats available).
For the 1st phase a fast port scanner* is intended to be used, whose output is parsed and stored in a single file database (TinyDB). Next, during the 2nd phase individual Nmap scans are launched for each target with its set of open ports (multiprocessing is supported) according to the database data. Finally, in the 3rd phase separate Nmap outputs are merged into a single report in different formats (XML / HTML / simple text / grepable) with nMap_Merger.
Potential use cases:
- Pentest engagements / red teaming with a large scope to enumerate.
- Cybersecurity wargames / training CTF labs.
- OSCP certification exam.
* Available port scanners:
How It Works
How to Install
Prerequisites
To successfully divide and scan we need to get some good port scanning tools.
📑 Note: if you don't feel like messing with dependecies on your host OS, skip to the Docker part.
Nmap
sudo apt install nmap sudo xsltproc -y
sudo nmap --script-updatedb
Masscan
cd /tmp
git clone https://github.com/robertdavidgraham/masscan.git
cd masscan
make
sudo make install
cd && rm -rf /tmp/masscan
RustScan
cd /tmp
wget -qO- https://api.github.com/repos/RustScan/RustScan/releases/latest \
| grep "browser_download_url.*amd64.deb" \
| cut -d: -f2,3 \
| tr -d \" \
| wget -qO rustscan.deb -i-
sudo dpkg -i rustscan.deb
cd && rm /tmp/rustscan.deb
sudo wget https://gist.github.com/snovvcrash/8b85b900bd928493cd1ae33b2df318d8/raw/fe8628396616c4bf7a3e25f2c9d1acc2f36af0c0/rustscan-ports-top1000.toml -O /root/.rustscan.toml
Naabu
sudo mkdir /opt/projectdiscovery
cd /opt/projectdiscovery
wget -qO- https://api.github.com/repos/projectdiscovery/naabu/releases/latest \
| grep "browser_download_url.*linux-amd64.tar.gz" \
| cut -d: -f2,3 \
| tr -d \" \
| sudo wget -qO naabu.tar.gz -i-
sudo tar -xvzf naabu.tar.gz
sudo mv naabu-linux-amd64 naabu
sudo rm naabu.tar.gz README.md LICENSE.md
sudo ln -vs /opt/projectdiscovery/naabu /usr/local/bin/naabu
NimScan
sudo mkdir /opt/nimscan
cd /opt/nimscan
wget -qO- https://api.github.com/repos/elddy/NimScan/releases/latest \
| grep 'browser_download_url.*NimScan"' \
| cut -d: -f2,3 \
| tr -d \" \
| sudo wget -qO nimscan -i-
sudo chmod +x nimscan
sudo ln -s /opt/nimscan/nimscan /usr/local/bin/nimscan
Installation
DivideAndScan is available on PyPI as divideandscan
, though I recommend installing it from GitHub with pipx in order to always have the bleeding-edge version:
~$ pipx install -f "git+https://github.com/snovvcrash/DivideAndScan.git"
~$ das
For debbugging purposes you can set up a dev environment with poetry:
~$ git clone https://github.com/snovvcrash/DivideAndScan
~$ cd DivideAndScan
~$ poetry install
~$ poetry run das
📑 Note: DivideAndScan uses sudo to run all the port scanners, so it will ask for the password when scanning commands are invoked.
Using from Docker
You can run DivideAndScan in a Docker container as follows:
~$ docker run -it --rm --name das -v `pwd`:/app snovvcrash/divideandscan
Since the tool requires some input data and produces some output data, you should specify your current working directory as the mount point at /app
within the container. You may want to set an alias to make the base command shorter:
~$ alias das='docker run -it --rm --name das -v `pwd`:/app snovvcrash/divideandscan'
~$ das
How to Use
0. Preparations
Make a new directory to start DivideAndScan from. The tool will create subdirectories in CWD to store the output, so I recommend launching it from a clean directory to stay organized: ~$ mkdir divideandscan
~$ cd divideandscan
|
1. Filling the DB
Provide the ⚠️ Warning: please, make sure that you understand what you're doing, because nearly all port scanning tools can damage the system being tested if used improperly. # Masscan
~$ das add masscan '--rate 1000 -iL hosts.txt -p1-65535 --open'
# RustScan
~$ das add rustscan '-b 1000 -t 2000 -u 5000 -a hosts.txt -r 1-65535 -g --no-config'
# Naabu
~$ das add naabu '-rate 1000 -iL hosts.txt -p - -silent -s s'
# NimScan
~$ das add nimscan '192.168.1.0/24 -vi -p:1-65535 -f:500'
# Nmap, -v flag is always required for correct parsing!
~$ das add nmap '-v -n -Pn --min-rate 1000 -T4 -iL hosts.txt -p1-65535 --open'
When the module completes its work, a hidden directory |
2. Targeted Scanning
Launch targeted Nmap scans with the # Scan by hosts
~$ das scan -hosts all -oA report1
~$ das scan -hosts 192.168.1.0/24,10.10.13.37 -oA report1
~$ das scan -hosts hosts.txt -oA report1
# Scan by ports
~$ das scan -ports all -oA report2
~$ das scan -ports 22,80,443,445 -oA report2
~$ das scan -ports ports.txt -oA report2
To start Nmap simultaneously in multiple processes, specify the ~$ das scan -hosts all -oA report -parallel [-proc 4]
The output format is selected with Also, you can inspect the contents of the database with ~$ das scan -hosts all -show
|
3 (Optional). Merging the Reports
In order to generate a report independently of the # Merge outputs by hosts
~$ das report -hosts all -oA report1
~$ das report -hosts 192.168.1.0/24,10.10.13.37 -oA report1
~$ das report -hosts hosts.txt -oA report1
# Merge outputs by ports
~$ das report -ports all -oA report2
~$ das report -ports 22,80,443,445 -oA report2
~$ das report -ports ports.txt -oA report2
📑 Note: keep in mind that the |
🔥 Example 🔥
Let's enumerate open ports for all live machines on Hack The Box.
- Add mappings "host ⇄ open ports" to the database with Masscan. For demonstration purposes I will exclude dynamic port range to avoid unnecessary stuff by using
-p1-49151
. On the second screenshot I'm reviewing scan results by hosts and by ports:
~$ das add -db htb -rm masscan '-e tun0 --rate 1000 -iL hosts.txt -p1-49151 --open'
~$ das scan -db htb -hosts all -show
~$ das scan -db htb -ports all -show
- Launch Nmap processes for each target to enumerate only ports that we're interested in (the open ports). On the second screenshot I'm doing the same but starting Nmap processes simultaneously:
~$ das scan -db htb -hosts all -oA report
~$ das scan -db htb -hosts all -oA report -nmap '-Pn -sVC -O' -parallel
- As a result we now have a single report in all familiar Nmap formats (simple text, grepable, XML) as well as a pretty HTML report.
Bring Your Own Scanner!
You can pair your favourite port scanner with DivideAndScan by implementing a single parse method in das/parsers/<DUMMY_SCANNER>.py
:
from das.parsers import IAddPortscanOutput
class AddPortscanOutput(IAddPortscanOutput):
"""Child class for processing <DUMMY_SCANNER> output."""
def parse(self):
"""
<DUMMY_SCANNER> raw output parser.
:return: a pair of values (portscan raw output filename, number of hosts added to DB)
:rtype: tuple
"""
hosts = set()
for line in self.portscan_raw:
# <DUMMY_SCANNER> parser implementation
pass
return (self.portscan_out, len(hosts))
Help
usage: das [-h] {add,scan,report,help} ...
-----------------------------------------------------------------------------------------------
| ________ .__ .__ .___ _____ .____________ |
| \______ \ |__|__ _|__| __| _/____ / _ \ ____ __| _/ _____/ ____ _____ ____ |
| | | \| \ \/ / |/ __ |/ __ \ / /_\ \ / \ / __ |\_____ \_/ ___\\__ \ / \ |
| | ` \ |\ /| / /_/ \ ___// | \ | \/ /_/ |/ \ \___ / __ \| | \ |
| /_______ /__| \_/ |__\____ |\___ >____|__ /___| /\____ /_______ /\___ >____ /___| / |
| \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ |
| {@snovvcrash} {https://github.com/snovvcrash/DivideAndScan} {vX.Y.Z} |
-----------------------------------------------------------------------------------------------
positional arguments:
{add,scan,report,help}
add run a full port scan and add the output to DB
scan run targeted Nmap scans against hosts and ports from DB
report merge separate Nmap outputs into a single report in different formats
help show builtin --help dialog of a selected port scanner
optional arguments:
-h, --help show this help message and exit
Psst, hey buddy... Wanna do some organized p0r7 5c4nn1n6?
ToDo
- Add projectdiscovery/naabu parser
- Add elddy/NimScan parser
- Add ZMap parser
- Add armada (?) parser
Support
If this tool has been useful for you, feel free to buy me a beer coffee!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for divideandscan-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 393558db2f274c74cfa9aa8c9156daa975658b28c1a33641d18b32185294f3a2 |
|
MD5 | cc1a8cebdd9e7571e562483e51a02bbc |
|
BLAKE2b-256 | 702a8b53e7ec0b655d2ae543d78371b0e82c4f620bc4f8eb687ea05666d8ffed |