Skip to content

Latest commit

 

History

History
314 lines (217 loc) · 31.4 KB

cli.md

File metadata and controls

314 lines (217 loc) · 31.4 KB

Language / Langue: English · Français

Command-line reference

PortAtlas is a set of commands. Each command reads only its own options: an option given to a command that does not read it, an unknown option or a value outside the accepted list stops the program with an error and exit code 1.

PortAtlas <command> [options]
Command Purpose
audit Scan sockets continuously and record deduplicated flows
map Render an HTML topology map and/or a Draw.io diagram from audit logs
export rules Consolidate audit logs into a firewall rule matrix
export ndjson Convert CSV and NDJSON audit logs into one NDJSON file
resolve Merge DNS caches and write host names into audit logs
lookup Diagnose the name resolution of one IP address
config init Write a configuration file with the default values

Without a command, PortAtlas prints the help and exits with code 0. -h or --help after any command prints the help of that command.

Sources of values

A value comes from, in order of precedence:

  1. the command line;
  2. the configuration file given by --config, or portatlas.json in the working directory, or portatlas.json next to the executable;
  3. the default built into the program.

The key of the configuration file matching each option is listed in the tables below and described in the configuration reference. Each command reads only some keys of the file; the section of each command lists them.

An option combines with the file as follows:

  • An option that takes a value replaces the value of the file. A list option replaces the whole list of the file: --exclude-ports 53 drops the ports listed in MapExcludedPorts. --alias is the exception: its pairs are added to CustomHostAliases and take precedence over the file for the same address or subnet.
  • An option without value that matches a key of the file switches it one way only. --listeners-only turns MapListenersOnly on but cannot turn it off; --no-tui, --no-dns, --no-rdap and --include-ephemeral-ports turn off what the file turns on, never the reverse. To undo such a setting of the file, edit the file or give another file with --config. export rules does not read MapExcludeEphemeralPorts, so there --include-ephemeral-ports alone decides. --obfuscate replaces MaskServerName and MaskLocalIp, unless --mask-server or --mask-ip is given.

Global option

Option Value Default Effect
--config <path> portatlas.json Configuration file read by every command
--version none Prints the version and exits (root command only)

File name tokens

These tokens are replaced in the path keys of the configuration file (OutputCsvPath, OutputNdjsonPath, OutputHtmlMapPath, OutputDrawioPath, DnsCacheFilePath) and in the output options: -o, --ndjson, --html and --drawio of audit; --html and --drawio of map; -o, --pairs-output, --flows-output, --host-rules-output, --listeners-output and --group-rules-output of export rules; -o of export ndjson; --output-cache of resolve. They are not replaced in --input, --cache, --config or the path given to config init. An --input left to its default reads OutputCsvPath with {ServerName} and {HostName} replaced. With {Date} or {Timestamp} in OutputCsvPath, each audit start writes a new log, so map, export rules, export ndjson without -i, and resolve without -i or --cache, read none: they stop with OutputCsvPath '<value>' names a new audit log at each audit start, so it names no log to read: give the logs with --input, for example --input "<pattern>". and exit code 1, where <pattern> is the path with * in place of these tokens. With {Timestamp} in DnsCacheFilePath, each command starts from an empty cache and writes a new one, instead of reusing the previous cache.

Token Replaced by
{ServerName} Name of the machine
{HostName} Name of the machine (same value as {ServerName})
{Date} UTC date, yyyy-MM-dd
{Timestamp} UTC date and time, yyyyMMdd_HHmmss

Tokens are matched without case ({date} works). A character that a file name cannot hold is replaced by - in the machine name.

Shared option families

Inputs

Read by map, export rules, export ndjson and resolve.

Option Value Default Effect
-i, --input <path>, repeatable OutputCsvPath of the configuration A file, a directory or a wildcard pattern on the file name (audits/nas-*.csv)

A directory or a wildcard pattern contributes its files with the extensions read by the command (.csv for map; .csv, .ndjson, .jsonl for export rules, export ndjson and resolve). Subdirectories are not read. When no file matches, the command prints No audit log found for: … and exits with code 1.

Each CSV log is read with the separator found on its own first line (,, ;, tab or |), so logs written with different CsvDelimiter values can be read together; CsvDelimiter is kept only when the first line does not tell (empty file, or a tie).

Among the files of a directory or of a wildcard pattern, the command reads the audit logs only: a CSV whose first line names the ServerName or LocalPort column, an NDJSON whose first record carries one of these two properties. A matrix written by export rules, a log without header line, an empty file or a file that cannot be opened (held by another program, access denied) is left out. When several files of a directory hold the same audit, the command keeps one: map, export rules and export ndjson read the copy written by resolve (<name>_enriched.<ext>) rather than the original, and export rules and export ndjson read the CSV rather than the NDJSON of the same name; resolve enriches each original, CSV and NDJSON alike, and leaves out an enriched copy whose original is present. Each file left out is named on the console with its reason. A file whose own path is given to --input is always read, whatever it contains; if it cannot be opened, the command stops with The audit log '<path>' cannot be read: … and exit code 1. The enriched copy does not receive the flows observed after resolve ran: run resolve again, or name the original, to read them.

Flow filters

Read by audit (for its map), map and export rules. List values are separated by commas; a list option can be repeated and the values add up.

Option Value Configuration key Effect
--include-ports ports or ranges MapIncludedPorts Keep only these service ports (80,443,8000-8080)
--exclude-ports ports or ranges MapExcludedPorts Remove these service ports
--include-hosts wildcard patterns MapIncludedHosts Keep only servers whose name matches (nas*,*.home.arpa)
--exclude-hosts wildcard patterns MapExcludedHosts Remove servers whose name matches
--include-ips addresses or CIDR MapIncludedIps Keep only these addresses (10.0.0.0/8)
--exclude-ips addresses or CIDR MapExcludedIps Remove these addresses
--include-processes names MapIncludedProcesses Keep only these process names or application targets
--exclude-processes names MapExcludedProcesses Remove these process names or application targets
--direction all, in, out MapDirection Flow direction to keep; default all
--listeners-only none MapListenersOnly Keep only listening sockets
--include-ephemeral-ports none MapExcludeEphemeralPorts Keep ports 49152-65535, removed by default

A port is a number between 0 and 65535, a range is written in increasing order (8000-8080). An address filter is an address (192.168.1.50, an IPv4 address being written with its four decimal bytes) or a CIDR subnet; a subnet written with the address of one of its hosts (192.168.1.10/24) is read as the subnet of that host (192.168.1.0/24). Any other value given to --include-ports, --exclude-ports, --include-ips or --exclude-ips, or to their configuration keys, stops the command before it starts: the message names the option, the key and the value, and the exit code is 1.

Map rendering

Read by audit and map. Accepted values are compared without case.

Option Value Configuration key Default
--html <path> OutputHtmlMapPath none
--drawio <path> OutputDrawioPath none
--layout layered, tree, horizontal-tree, organic, circular, grid MapLayoutMode layered
--sort barycentric, tiers, subnet, degree, alpha, ip MapSortMethod barycentric
--routing bezier, orthogonal, straight, bus MapRoutingMode bezier
--spacing compact, balanced, spacious MapSpacingProfile balanced
--edge-labels ports, none MapEdgeLabels ports
--drawio-edges adaptive, direction, pair, none DrawioEdgeMode adaptive
--drawio-subnets on, off DrawioSubnets on
--drawio-theme dark, light DrawioTheme dark

--layout, --sort, --routing, --spacing and --edge-labels set the initial state of the HTML map; the map page can change them. --sort also orders the target servers, and the subnet zones, of the Draw.io diagram.

  • bus routing: the arrows of a server share one trunk per direction, then split into one branch per target. The branches that reach a server through the same side and in the same direction end under one arrow head. Designed for the tree layout.
  • --edge-labels none: unchecks Show Ports. The arrows lose their port labels and the flows of a server pair are merged into one arrow, with a head at each end that receives a flow. The server inspector still lists the ports.
  • --drawio-edges pair: one edge per server pair in the Draw.io diagram, with two heads when flows run both ways.

audit

Scans the TCP and UDP sockets of the machine at a fixed interval, attributes each socket to its process and writes one line per deduplicated flow in the CSV audit log. Runs until Q, Ctrl+C or SIGTERM.

PortAtlas audit [options]
Option Value Configuration key Default Effect
-o, --output <path> OutputCsvPath portatlas_audit.csv CSV audit log
--ndjson <path> OutputNdjsonPath none NDJSON log written alongside the CSV log
--interval seconds, at least 1 ScanIntervalSeconds 5 Time between two scans
--columns column names CsvColumns all 20 Ordered CSV columns (list)
--rewrite-interval minutes, at least 1 LogRewriteIntervalMinutes 5 Time between two rewrites of the logs, which are also written at start and stop (details)
--no-tui none EnableDashboard dashboard on Run without the terminal dashboard
--no-dns none ResolveRemoteHostNames DNS on No name resolution at all: no reverse DNS, no RDAP, no alias
--no-rdap none ResolveIpOwners RDAP on No RDAP query to Internet registries; reverse DNS stays active
--dns-timeout-ms milliseconds, at least 1 DnsResolutionTimeoutMs 3000 Time to wait for a reverse DNS answer
--rdap-timeout-ms milliseconds, at least 1 RdapTimeoutMs 4000 Time to wait for an RDAP answer, redirect to the registry included
--exclude-remote-ips addresses or CIDR ExcludedRemoteIps none Remote addresses that are not recorded
--alias range=host, where the range is an address or a CIDR subnet (IPv4 or IPv6), or range:host for IPv4, repeatable CustomHostAliases none Host name used instead of reverse DNS for the address, or for every address of the subnet (192.168.10.0/24=lab.home.arpa); the most precise range wins; a pair without a readable range or a name is refused
--mask-server <name> MaskServerName none Server name shown by the dashboard
--mask-ip <address> MaskLocalIp none Local address shown by the dashboard
--obfuscate none none off Shows HOMELAB-01 and 192.168.1.10 unless --mask-server or --mask-ip is given

Families read: flow filters and map rendering. When --html (or OutputHtmlMapPath) is set, the HTML map is rewritten after every scan; the M key writes it on demand, with the Draw.io diagram when --drawio (or OutputDrawioPath) is set. One map is written at a time: scans that end while it is written lead to a single later rewrite, and a map that cannot be written (file locked or read-only) shows a warning while the audit goes on.

The dashboard is not shown when the input or the output of the process is redirected, even without --no-tui.

A scan that fails (socket table that cannot be read, file held for a moment) does not stop the audit: the dashboard shows Scan failed (n of 10 in a row): … in the title of the flow table, or the line is printed with its time without the dashboard, and the next scan runs at the usual interval. After 10 failed scans in a row the audit stops with The audit stopped after 10 failed scans in a row: … and exit code 1, after writing the logs.

An invalid column name in --columns or CsvColumns, or a list without FirstSeenUtc, Protocol, Direction, LocalPort, RemoteIp, RemotePort, ProcessName, ExecutablePath and TargetExecutable (the log is read back at the next start, see output formats), stops the command with Column configuration error: … and exit code 1.

With name resolution on, the DNS cache named by DnsCacheFilePath is read at start and saved during the audit. If it exists but cannot be read (truncated or invalid JSON, file held by another program or not readable by the account), the audit stops before its first scan with The DNS cache '<path>' cannot be read: … and exit code 1, and writes neither the logs nor the cache, so that the cache is not replaced by the names of this run alone.

The audit command resumes from the CSV log only. If the NDJSON log (--ndjson, OutputNdjsonPath) exists and holds at least one record while the CSV log is missing or holds no flow, the audit stops before its first scan with The NDJSON log '<path>' holds flows but the CSV log '<path>' is missing or holds none: … and exit code 1, and writes nothing: starting would replace the NDJSON log with the flows of this run alone. Restore the CSV log or move the NDJSON log. An empty or absent NDJSON log does not stop the audit.

Configuration keys read: every key of the configuration reference.

map

Builds the topology graph from CSV audit logs and writes the requested outputs.

PortAtlas map [-i <path>]... [--html <path>] [--drawio <path>] [options]

Outputs:

  • --html writes the HTML map, --drawio the Draw.io diagram; both can be given.
  • Without --html and --drawio, the command writes the HTML map to OutputHtmlMapPath (or portatlas_topology.html when the key is empty), plus the Draw.io diagram when OutputDrawioPath is set.

A log line without a valid local and remote port is left out of the map; the console names the log and the numbers of these lines (the first ten).

Families read: inputs (.csv), flow filters, map rendering.

Configuration keys read: OutputCsvPath (default input), CsvDelimiter, the flow filter and map rendering keys, ExcludedLocalPorts, ExcludedRemotePorts and ExcludedRemoteIps. The other recording filters and the name resolution keys are not read: the names come from the logs.

export rules

Consolidates audit logs into a matrix with one row per source, destination, port, protocol and process.

PortAtlas export rules [-i <path>]... [-o <path>] [options]
Option Value Default Effect
-o, --output <path> portatlas_rules.csv Matrix file (columns)
--pairs-output <path> not written Also writes the pair matrix, one row per source and destination (columns); same tokens as --output
--flows-output <path> not written Also writes the flow matrix, one row per flow with the two ends of a connection merged (columns); same tokens as --output
--host-rules-output <path> not written Also writes the host rule matrix, an outbound rule on the source and an inbound rule on the destination of each flow, for network security groups (columns); same tokens as --output
--listeners-output <path> not written Also writes the listening sockets that --listeners-only would export, in the same pass (columns); same tokens as --output
--group-rules-output <path> not written Also writes the group rule matrix, one row per source group, destination group, protocol and port (columns); same tokens as --output
--address-group name=range, repeatable none Puts an IP address or a CIDR subnet in a group of the group rule matrix (lab=192.168.10.0/24), in addition to AddressGroups; a value without a name or a readable range is refused
--min-observations number, at least 1 1 Keep flows observed at least this number of times
--active-within-days number, at least 1 no limit Keep flows last seen during the last N days
--zone all, internal, public all Destination zone to keep

Families read: inputs (.csv, .ndjson, .jsonl) and flow filters, with two differences:

  • ephemeral destination ports (49152 and above) are removed unless --include-ephemeral-ports is given; the MapExcludeEphemeralPorts key is not read;
  • listening sockets are exported only with --listeners-only (or MapListenersOnly), as inbound rules from any source (0.0.0.0, host Any). A socket is listening when its Direction is Listening, whatever its state: TCP listeners and bound UDP sockets.

A destination is Internal when it is a loopback address, an address of a private range, or 0.0.0.0 / ::; otherwise it is Public.

A loopback or 0.0.0.0 / :: destination names no machine: the same flow in the logs of two servers gives one rule per server. A routable destination gives one rule whatever the log that saw it.

A rule carries the server name, the remote name, the executable path and the account found on any of its log rows, the most recently seen one when several rows disagree, whatever the order of the inputs. --include-hosts and --exclude-hosts are tested on the consolidated rule, against both of its ends, by name and by the address the rule shows: one match is enough, whether to keep the rule or to remove it. The Any source of a listener is not tested.

--include-ips and --exclude-ips follow the same rule on the two addresses the rule shows, whatever the direction of the flow: one match is enough, whether to keep the rule or to remove it. Excluding a remote address removes the flows it receives and the flows it opens; including the subnet of the server keeps all of its flows, excluding it removes them all. A listening socket is tested on the address it is bound to only: a listener bound to 0.0.0.0 or :: is not in the subnet of its server.

A CSV log is read with the separator found on its own first line (,, ;, tab or |), so logs written with different CsvDelimiter values can be consolidated together: the separator between the column names of the header or, in a log without header, the one that occurs most often outside the quoted fields. The matrix is written with CsvDelimiter.

An address group is a set of addresses and CIDR subnets that share firewall rules, such as a VLAN or an application security group. Groups come from the AddressGroups key and from --address-group, which adds ranges to the groups of the file (a group named in both, whatever the case of its name, holds the ranges of both). An address belongs to the group of its most precise range: with lab=192.168.10.0/24 and nas=192.168.10.20, the address 192.168.10.20 is in nas. A range given to two groups, whatever its spelling (192.168.10.0/24 and 192.168.10.9/24 are one range), stops the command before any log is read, with <range> is in two address groups, '<group>' and '<group>': give each address or subnet to one group only (AddressGroups, --address-group). and exit code 1; so does a range of AddressGroups that cannot be read, and a group named Any or named as an address or a subnet (192.168.1.50), since the matrix writes Any for the source of a listening socket and its address for a host in no group. The groups shape the group rule matrix only: the other matrices keep one row per address. Without --group-rules-output, the groups are not built, so a conflict or a reserved name in AddressGroups does not stop the command, and --address-group writes a warning on the error output.

The matrices are written once every log has been read. When an output option names one of the audit logs to read, given by its own path or found in a directory or a pattern, the command writes nothing and stops with The output '<path>' is also an audit log to read: choose another path for <option>. and exit code 1. When two output options name the same file, the command writes nothing and stops with <option> and <option> name the same file '<path>': choose two different paths. and exit code 1. File names are compared without case.

Configuration keys read: OutputCsvPath (default input), CsvDelimiter (also the separator of the matrices), AddressGroups, and the flow filter keys except MapExcludeEphemeralPorts. The map rendering keys, the recording filters and the name resolution keys are not read.

export ndjson

Converts CSV and NDJSON audit logs into a single NDJSON file, one JSON object per flow.

PortAtlas export ndjson [-i <path>]... [-o <path>]
Option Value Default Effect
-o, --output <path> portatlas_audit.ndjson NDJSON file (fields)

Family read: inputs (.csv, .ndjson, .jsonl). A .ndjson or .jsonl file is read as NDJSON, any other file as CSV. Input files are read sequentially and only the current file is held in memory. Each flow read gives one line: a flow held by two logs, such as the logs of two days, or a log and its enriched copy both given by their own path to --input, is written twice. An output given by its own path to --input stops the command with The output '<path>' is also an audit log to read: choose another --output. and exit code 1. An output of an earlier run found in a directory or a pattern is not read, and the command prints Skipped <path>: it is the output of this command.

A non-blank line of an NDJSON log that is not a JSON audit record, such as a line cut by a crash, is left out, and the command prints Skipped <n> line(s) of <path> that are not valid JSON: lines <numbers>. The output replaces the previous file only once every log has been read: a log that cannot be read, Ctrl+C or a full disk leaves the previous file as it was; after Ctrl+C, the partial output may stay beside it as <path>.pending.

Configuration keys read: OutputCsvPath (default input) and CsvDelimiter. No filter applies.

resolve

Merges DNS caches, resolves the addresses that still have no host name and writes enriched copies of the audit logs.

PortAtlas resolve [-i <path>]... [--cache <path>]... [options]
Option Value Default Effect
--cache <path>, repeatable DnsCacheFilePath DNS cache to merge: a file, a directory (its .json files) or a pattern
--output-cache <path> DnsCacheFilePath Merged cache to write
--in-place none off Overwrite the audit logs instead of writing <name>_enriched.<ext>
--force-resolve none off Resolve every address again, including those that already have a name, and replace the names already in the logs. A lookup that finds nothing never erases a known name
--rdap-delay-ms milliseconds, at least 0 200 Delay between two RDAP queries
--cache-only none off Send no DNS or RDAP query; use only the merged caches
--dns-timeout-ms milliseconds, at least 1 DnsResolutionTimeoutMs Time to wait for a reverse DNS answer
--rdap-timeout-ms milliseconds, at least 1 RdapTimeoutMs Time to wait for an RDAP answer, redirect to the registry included

Family read: inputs (.csv, .ndjson, .jsonl). When two caches give different names for the same address, the most recent entry is kept and the conflict is printed. An entry without a name never conflicts with a named one: the named entry is kept.

Audit logs are optional. When --cache is given without -i, the audit logs are left alone and the caches alone are resolved: the addresses of the merged cache are the ones to resolve. Without --force-resolve only the entries without a name are resolved; with it, every entry is. When audit logs are given, the addresses to resolve are those of the logs. The cache named by --output-cache is always merged before it is written, even when --cache does not list it, so that running resolve from several machines completes the cache instead of replacing it. If that cache exists but cannot be read (truncated or invalid JSON, file held by another program or not readable by the account), the command stops before any lookup with The DNS cache '<path>' cannot be read: … and exit code 1, and writes nothing, so that the cache is not replaced by the names of this run alone. The other caches listed in --cache are only read: one that cannot be read adds no names and the run goes on.

Ctrl+C, and SIGTERM on Linux and macOS, stop the lookups: the addresses resolved before the stop are saved to the cache named by --output-cache, the audit logs not processed yet are left as they were, and the command ends with exit code 130. Running it again resumes from that cache.

Configuration keys read: OutputCsvPath (default input), DnsCacheFilePath, DnsResolutionTimeoutMs, ResolveRemoteHostNames, ResolveIpOwners, RdapTimeoutMs, DnsExcludedNetworks and CustomHostAliases. CsvDelimiter is not read: each log is read with the separator of its first line, or , when that line does not tell. ResolveRemoteHostNames: false stops the reverse DNS queries only; RDAP queries still follow ResolveIpOwners, whereas in audit the same value turns off all naming. An address named by CustomHostAliases or within DnsExcludedNetworks receives no query, as in the audit, and an alias is not written to the cache. --cache-only sends no query whatever these keys say.

lookup

Prints a six-step diagnostic of the resolution of one address: classification, local cache, operating system DNS settings, reverse DNS query, RDAP query, resulting cache entry.

PortAtlas lookup <ip> [--dns-timeout-ms <milliseconds>] [--rdap-timeout-ms <milliseconds>]

<ip> must be an IPv4 or IPv6 address. The command reads CustomHostAliases, DnsExcludedNetworks, ExcludedRemoteIps, DnsCacheFilePath, DnsResolutionTimeoutMs and RdapTimeoutMs from the configuration. As in the audit, an address named by CustomHostAliases takes that name (step 1 cites the subnet when the alias names one), and an address of DnsExcludedNetworks gets no name: for both, the reverse DNS and RDAP steps print SKIPPED and send no query. Step 1 also says whether ExcludedRemoteIps makes the audit drop the flows of the address. Otherwise the command sends the reverse DNS query with the same timeout as audit, so that a SUCCESS here means the audit names the address too, and the RDAP query for public addresses only, with the same RdapTimeoutMs; ResolveIpOwners and --no-rdap do not apply to it, nor does ResolveRemoteHostNames. The pairs of audit --alias are not known to lookup, which reads CustomHostAliases from the file only.

config init

Writes a configuration file holding the default value of every key, with the 20 CSV columns listed.

PortAtlas config init [<path>]

<path> is a file or a directory; the default is portatlas.json in the working directory. An existing file is overwritten.

Configuration keys read: none.

Option matrix

Option audit map export rules export ndjson resolve lookup config init
--config ✔ ✔ ✔ ✔ ✔ ✔ ✔
-i, --input ✔ ✔ ✔ ✔
-o, --output ✔ ✔ ✔
Flow filters ✔ ✔ ✔
Map rendering, --html, --drawio ✔ ✔
--ndjson, --interval, --columns, --rewrite-interval ✔
--no-tui, --no-dns, --no-rdap ✔
--dns-timeout-ms, --rdap-timeout-ms ✔ ✔ ✔
--exclude-remote-ips, --alias ✔
--mask-server, --mask-ip, --obfuscate ✔
--min-observations, --active-within-days, --zone, --pairs-output, --flows-output, --host-rules-output, --listeners-output, --group-rules-output, --address-group ✔
--cache, --output-cache, --in-place, --force-resolve, --rdap-delay-ms, --cache-only ✔

config init accepts --config because the option is global, but does not read it.

Stopping through the standard input

When the environment variable PORTATLAS_STOP_ON_INPUT_END is 1 and the standard input is redirected, the end of that input stops the command as Ctrl+C does: audit writes its logs and saves its DNS cache, resolve saves the addresses already resolved and ends with exit code 130, and the other commands end at once with exit code 130. The end of the input is acted on as soon as it arrives, even when the input was empty from the start. The graphical interface sets the variable and closes the input to stop a command. Without the variable the input is not read, so a service started with an empty input keeps running.

Exit codes

Code Meaning
0 Success, or help printed
1 Invalid command line, no audit log found, invalid CSV column name or column list without the columns of the flow key, audit log that cannot be read back or replaced at start or written at stop (details), audit stopped by 10 failed scans in a row, a port or an address of a flow filter that cannot be read, a --config path that is empty or does not exist, or a configuration file that cannot be read, is not valid (see the configuration reference) or sets "IsolatedMode": true, audit DNS cache or resolve output cache that cannot be read, audit NDJSON log that holds flows while its CSV log is missing, output file of map, export rules, export ndjson or config init (or its folder) that cannot be created or written: the command prints The output '<path>' cannot be written: …
130 resolve stopped by Ctrl+C or SIGTERM, after saving the addresses already resolved; a command other than audit stopped through its input