cybersecurity · intermediate · ~15 min · safe pentest lab

Read a banner from a local service

Combine a sockets exercise with the local-only safety pattern.

Challenge

Read a service banner from a loopback-only server. The harness spawns a tiny banner-emitting server in a background thread, so you connect to 127.0.0.1 and never leave the machine.

Task

Implement int read_local_banner(int port, char *out, size_t out_sz) that connects to 127.0.0.1:port, reads up to out_sz - 1 bytes, NUL-terminates out, and closes the socket.

Input

  • port: the loopback port of the banner server the harness started.
  • out, out_sz: the destination buffer and its size.

Output

Returns int: 0 on success (with out holding the NUL-terminated banner bytes read), or -1 on any failure (socket, connect, or read error).

Example

server emits "SSH-2.0-TestServer\r\n"
read_local_banner(<port>, buf, 128)   ->   0, buf contains "SSH-"

Edge cases

  • out_sz == 0: return -1.
  • A read error returns -1; a successful read NUL-terminates at the byte count read.

Rules

  • Connect only to 127.0.0.1 — never a third-party host.

Input format

A loopback port, a destination buffer out, and its size out_sz.

Output format

0 on success with out holding the NUL-terminated banner; -1 on failure.

Constraints

Connect only to 127.0.0.1; read at most out_sz-1 bytes and NUL-terminate.

Starter code

#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stddef.h>

int read_local_banner(int port, char *out, size_t out_sz) {
    /* TODO */
    return -1;
}

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