r/netsec • • Dec 21 '19

Exploiting Null Byte Buffer Overflow for a $40,000 bounty

https://samcurry.net/filling-in-the-blanks-exploiting-null-byte-buffer-overflow-for-a-40000-bounty/
384 Upvotes

7 comments sorted by

41

u/Koppis Dec 21 '19

I'd really like to see what kind of code would pass those parameters to the back end.

If the string gets stripped of null characters in javascript, the string length should obviously change as well, right?

11

u/my_name_still_jeff Dec 22 '19

Yep, it just never gets updated. Very cool find.

5

u/tchebb Dec 22 '19 edited Dec 22 '19

Because C uses NUL as its string terminator, any backend implementation that accidentally uses C string operations can have this issue without the frontend doing anything wrong. Something like

/* 
 * Converts "[email protected]" into "me". Input bytes and length are passed via `in`
 * and `in_len`, output bytes are put in a new buffer pointed to by `*out`, and output
 * length is returned.
 */
size_t strip_domain(char *in, size_t in_len, char** out) {
    size_t out_len;
    for (out_len = 0; out_len < in_len && in[out_len] != '@'; out_len++)
        ;

    /* This buffer must be filled with exactly out_len bytes to avoid memory leakage. */
    *out = malloc(out_len);
    /* Wrong! Should be a memcpy(). This will stop copying as soon as it hits a NUL. */
    strncpy(*out, in, out_len);

    return out_len;
}

An input exactly as given in the post, victim%00%00%[email protected], would cause this function to return a buffer containing victim followed by three bytes of uninitialized memory contents.

The fact that strncpy() won't insist on adding a NUL byte to its output if the length you give it is shorter than the input string makes something like this even easier to write accidentally (and for things like valgrind to miss).

2

u/NullCharacter Dec 22 '19

These are my favorite.

1

u/thehunter699 Dec 24 '19

That was a really enjoyable writeup. Hope to see some more.