WCU / Cybersecurity
~/CSC 472/Class 06/KP 10
Class 06 · KP 10 / 18

The {%n

printf(buf)buf = user inputRSIRDXRCXR8R9then values pulled from the stack →stack: %p %p %p ... %n
%p leaks these slots; %n writes to them — an arbitrary read/write primitive.

Specifier}

%n is the dangerous one: it does not print anything. It writes

the number of characters printed so far to the int* argument.

int count;
printf("hello%n", &count);   /* count becomes 5 */
  • Give %n an attacker-controlled pointer ⇒ write to an arbitrary address.
  • Control the value by controlling how many bytes were printed before the %n —- width specifiers do this cheaply.
  • Size variants avoid printing billions of bytes:
  • %n writes 4 bytes (an int).
  • %hn writes 2 bytes (a short).
  • %hhn writes 1 byte.