Is there any way to copy null bytes (ASCII 0x00) to the clipboard on Windows?

12

5

Null bytes (ASCII 0x00) don't appear to be copyable (Ctrl+C-able) on Windows. For a demonstration of this, open up your browser's dev console and do console.log('a\x00b'). If you try and copy-paste the resulting string on a Windows 8 machine (and probably other versions of Windows too), you'll find that only the a gets copied. The \x00 and everything after it is ignored.

Is there any way to copy the entirety of a string that contains null bytes? Can the clipboard even hold null bytes?

(Auxiliary question: why can't null bytes be copied? E.g. is there a security-related reason for this, or is it just Windows being silly?)

senshin

Posted 2015-07-28T19:39:49.233

Reputation: 546

good question.. be interesting to see if it's putting it into the clipboard xxd can output nulls `C:>echo 000000| xxd -r -p|xxd -p<ENTER> 000000

C:>echo 000000| xxd -r -p|clip<ENTER>

`

– barlop – 2015-07-28T20:07:47.213

using unxutils gclip and pclip `C:>echo 000000| xxd -r -p|gclip<ENTER>

C:>pclip|xxd -p<ENTER> C:>pclip>a.a<ENTER>` <-- it not clear to me whether the nulls are going into the clipboard.. or whether they're in but not pasting out. – barlop – 2015-07-28T20:16:49.070

2Somewhere in the chain, a program is using null-terminated string functions/representations (i.e. C string functions). Very common. My meager understanding of such things is "yes" there are potential security issues with embedding nulls, mostly due to the potential for mis-identification of string length and buffer overflow(??). – Yorik – 2015-07-28T20:45:17.397

It's a puzzler: I copied the contents of a JPG to the clipboard, and when I read it back it was truncated at the first null, which suggests the clipboard is text-based. Yet I can screen dump or cut and paste images between graphics packages without a problem. – AFH – 2015-07-29T10:09:34.730

I guess it depends on format used. Clipboard supports different formats. If you copy text, them most likely null-characters aren't supported because they're used as string terminator. If you use a binary clipboard format, then you can copy and paste null bytes. – Alexey Ivanov – 2016-04-01T08:33:57.393

Answers

9

No, you cannot put text with embedded null characters on the clipboard. Let's look at the list of standard Windows clipboard formats. There are a few formats that hold things generally understood as text:

  • CF_TEXT (1)
  • CF_OEMTEXT (7)
  • CF_UNICODETEXT (13)

Every single one of those has this sentence in its definition:

A null character signals the end of the data.

Now, CF_UNICODETEXT keeps its data as UTF-16LE, so it will more than likely have some null bytes, but null characters (two null bytes in a row, basically) will still end the string.

We can only speculate about why null characters aren't allowed in clipboard text, but more than likely it's just because the most commonly-used string processing functions in Windows assume a null character signals the end. The only other way to know where a string stops would be to prefix it with its length.

You can hold graphics on the clipboard even though they likely have null bytes because they get passed around in different clipboard formats (e.g. CF_BITMAP), which have to be understood differently by programs.

Ben N

Posted 2015-07-28T19:39:49.233

Reputation: 32 973

Thanks - great answer. I definitely learned some good stuff about the Windows clipboard. – senshin – 2016-04-29T19:13:39.120