Simvik

Find the strings in a dump

List the runs of printable characters in a binary, as the strings tool does. An easy embedded C problem (Reading a firmware dump), run and graded in the…

easy · Reading a firmware dump · reverse-engineering · strings · parsing

The fastest way into an unknown image is its printable strings. Write find_strings(data, n, min), which prints every run of at least min printable ASCII characters (0x20 to 0x7E) in the n bytes, one per line as <offset in hex, 4 digits>: <text>, where the offset is that of the run's first byte. Return how many it printed.

Starter code

int find_strings(const uint8_t *data, unsigned n, unsigned min) {
  int count = 0;
  for (unsigned i = 0; i < n; i++) {
    if (data[i] >= 0x20 && data[i] <= 0x7E) {
      printf("%04X: %c\n", i, data[i]);
      count++;
    }
  }
  (void)min;
  return count;
}

Example tests

A key left in the image

const uint8_t dump[] = { 0x00, 0x10, 'w', 'i', 'f', 'i', '=', 'H', 'o', 'm', 'e', 0x00, 0xFF, 'o', 'k', 0x00,
                         'p', 'a', 's', 's', ':', 'h', 'u', 'n', 't', 'e', 'r', '2', 0x00 };
printf("found %d\n", find_strings(dump, sizeof dump, 4));

Expected output:

0002: wifi=Home
0010: pass:hunter2
found 2

Learn it first

  • Reading a firmware dump · Vector tables, strings and disassembly, what attackers look for, and protecting the image.

More Reading a firmware dump problems

  • Read a vector table · Read a Cortex-M image's initial stack pointer and reset handler, and tell a real image from noise.