cybersecurity · intermediate · ~15 min · safe pentest lab

Parse a sample Nmap-style report

Practise extracting structured fields from a text report.

Challenge

Extract the open ports from a saved Nmap report — pulling structured fields out of a text report.

Task

Implement size_t open_ports(const char *report, int *out, size_t out_cap) that stores each open port number into out (up to out_cap of them) and returns how many it stored.

Input

  • report: a NUL-terminated multi-line string in Nmap -oN format, baked into the harness. A line marks an open port when it looks like <port>/tcp<spaces>open<spaces>....
  • out, out_cap: the output array and its capacity.

Output

Returns size_t: the number of port numbers stored (the function never stores more than out_cap).

Example

report with 22/tcp open, 80/tcp open, 443/tcp closed, 8080/tcp open
open_ports(report, out, 10)   ->   3, out = {22, 80, 8080}

Edge cases

  • Lines that are not open TCP ports (headers, closed/filtered) are ignored.
  • Stop storing once out_cap is reached.

Rules

  • Walk the report line by line. On each line parse the leading digits, then require /tcp, whitespace, and open. Static fixture only — the exercise never runs Nmap.

Input format

A NUL-terminated Nmap -oN report, an output int array, and its capacity.

Output format

A size_t: the number of open port numbers stored in out (up to out_cap).

Constraints

Match lines like /tcpopen. Store at most out_cap ports. Static buffer only.

Starter code

#include <stdio.h>
#include <string.h>
#include <stddef.h>

size_t open_ports(const char *report, int *out, size_t out_cap) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.