cybersecurity · intermediate · ~15 min · safe pentest lab
Combine a sockets exercise with the local-only safety pattern.
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.
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.
port: the loopback port of the banner server the harness started.out, out_sz: the destination buffer and its size.Returns int: 0 on success (with out holding the NUL-terminated banner bytes read), or -1 on any failure (socket, connect, or read error).
server emits "SSH-2.0-TestServer\r\n"
read_local_banner(<port>, buf, 128) -> 0, buf contains "SSH-"
out_sz == 0: return -1.127.0.0.1 — never a third-party host.A loopback port, a destination buffer out, and its size out_sz.
0 on success with out holding the NUL-terminated banner; -1 on failure.
Connect only to 127.0.0.1; read at most out_sz-1 bytes and NUL-terminate.
#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.