Skip to content

Instantly share code, notes, and snippets.

@bulletinmybeard
Last active January 25, 2024 07:43
Show Gist options
  • Save bulletinmybeard/c3c6eac64cd550a8d54a8fe5b4f45cdc to your computer and use it in GitHub Desktop.
Save bulletinmybeard/c3c6eac64cd550a8d54a8fe5b4f45cdc to your computer and use it in GitHub Desktop.
Use the Nmap network scanner command to perform a TCP SYN scan on open ports specified (macOS/Linux)

Use, on macOS/Linux, the nmap command to scan open ports (e.g., github.com) and retrieve the results in a valid JSON format using xml2json and jq.

Prerequisites

  • Install xml2json with npm install -g xml2json-command (NodeJS required!)
  • Install jq with brew install jq (Homebrew required)

CLI Arguments

Arg Function
sudo This command needs to run as root
nmap Launch the Nmap network scanning tool
-sS Use TCP SYN scan to discover open ports
-Pn Treat all hosts as online (skip host discovery)
-p Specify the ports to scan (comma-separated list)
22, 80, 443, 21, 25, 110, 143, 53, 3389, 3306, 5432, 23, 161, 162, 5900 List of ports to scan
-oX - Output the results in XML format to standard output (stdout)
github.com The target hostname or IP address to scan
sudo nmap \
  -sS \
  -Pn \
  -p 22, 80, 443, 21, 25, 110, 143, 53, 3389, 3306, 5432, 23, 161, 162, 5900 \
  -oX - github.com | \
  env NODE_OPTIONS="--no-deprecation" \
  xml2json \
  -t xml2json | \
  jq .

Verbose and Debug level set

sudo nmap \
    -sS \
    -Pn \
    -p 22,80,443,21,25,110,143,53,3389,3306,5432,23,161,162,5900 \
    -oX - \
    github.com \
    -d 2 \              # Debug level 2
    -v 1 \              # Verbosity level 1
    | env NODE_OPTIONS="--no-deprecation" xml2json -t xml2json \
    | jq .
{
  "$c": "Nmap 7.94 scan initiated Thu Jan 25 08:38:53 2024 as: nmap -sS -Pn -p 22,80,443,21,25,110,143,53,3389,3306,5432,23,161,162,5900 -oX - -d -v github.com 2 1",
  "nmaprun": {
    "scanner": "nmap",
    "args": "nmap -sS -Pn -p 22,80,443,21,25,110,143,53,3389,3306,5432,23,161,162,5900 -oX - -d -v github.com 2 1",
    "start": "1706168333",
    "startstr": "Thu Jan 25 08:38:53 2024",
    "version": "7.94",
    "xmloutputversion": "1.05",
    "scaninfo": {
      "type": "syn",
      "protocol": "tcp",
      "numservices": "15",
      "services": "21-23,25,53,80,110,143,161-162,443,3306,3389,5432,5900"
    },
    "verbose": {
      "level": "2"
    },
    "debugging": {
      "level": "1"
    },
    "taskbegin": [
      {
        "task": "Parallel DNS resolution of 3 hosts.",
        "time": "1706168333"
      },
      {
        "task": "SYN Stealth Scan",
        "time": "1706168333"
      }
    ],
    ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment