Lesson 8 of 9

Extracting strings

Goal

The readable text among the bytes

01

Explanation

The first thing an analyst does with an unknown executable is pull out its strings, and that's exactly what the well-known strings tool does. Error messages, file names, URLs and any words written into the code stay inside the program as readable text among bytes you can't read.

The idea is simple. A printable ASCII character is anything from 0x20, the space, to 0x7e, the tilde ~. You walk through the bytes and collect every unbroken run of printable characters, and as soon as a non-printable byte comes along, the run ends. Random data contains a printable character or two by chance, so you drop any run shorter than a minimum length, which is four by default in strings:

blob = 00 01 'HELLO' 00 'ab' ff 'secret_key' 00
extract(blob)              ['HELLO', 'secret_key']
extract(blob, min_len=2)   ['HELLO', 'ab', 'secret_key']

Watch the end of the data: if it runs out while you're inside a long enough run, that's a string too, so don't forget it after the loop.

02

Your task

Write a function extract(data, min_len=4) that returns a list of the printable strings that are at least min_len characters long, in the order they appear in the data, with each one a str.

Checks

5 checks
  1. extract pulls out the long strings in order
  2. extract respects min_len
  3. extract works on another block
  4. The last string is caught even with no separator after it
  5. Noise doesn't count as text
main.py
OutputPython (Pyodide)

Loading Python. This only happens once. Your browser keeps it after that.

–/5

Checks start once the environment is ready.

5 checks listed under “Your task”.