xxd is a very handy command line tool that converts its input (file or standard input) into hexadecimal output. I came across it when I was trying to figure out an easy to way to take some binary input and convert it to ASCII. From the xxd man page…

xxd creates a hex dump of a given file or standard input. It can also convert a hex dump back to its original binary form. Like uuencode(1) and uudecode(1) it allows the transmission of binary data in a ‘mail-safe’ ASCII representation, but has the advantage of decoding to standard output. Moreover, it can be used to perform binary file patching.

Lets imagine you want to send an image file to your friend Alice. This is obviously very straight forward I here you say. But what if the following restrictions were put in place:

  1. You have to break the file up into chunks and send it piece by piece
  2. You have to send the individual pieces of the file by email
  3. You cannot send an attachment so the file data must be contained within the email body

Here is were xxd comes in really handy. We can use xxd to convert the image file into chunks of hexadecimal values and then send each chunk of hex values to Alice by email. Lets have a look at some examples.

Basic use of xxd

xxd countries.png

The basic use of xxd simply takes the input file and gives a stand hex output as shown in the following snippet:

0000000: 8950 4e47 0d0a 1a0a 0000 000d 4948 4452 .PNG........IHDR
0000010: 0000 0708 0000 03fa 0806 0000 0084 8609 ................
0000020: 8700 0000 0473 4249 5408 0808 087c 0864 .....sBIT....|.d
0000030: 8800 0000 0970 4859 7300 000f 6100 000f .....pHYs...a...
0000040: 6101 a83f a769 0000 2000 4944 4154 789c a..?.i.. .IDATx.

So this is good but we can do better. By using the -p switch xxd will strip out all the fancy output and just give us the chunks of hex data.

xxd with the -p switch

xxd -p countries.png

See the output snippet below

89504e470d0a1a0a0000000d4948445200000708000003fa080600000084
8609870000000473424954080808087c086488000000097048597300000f
6100000f6101a83fa7690000200049444154789cecdd77785465fefef17b
269364d27b028492105ae84d1488d4d004140b0888820828b81640458165
557455d41faeac88b82a8214294ab32c526d80ab805495127a4f269336a9

When using the -p switch xxd defaults to 16 octets per line as shown above. This can be controlled by using the -c switch and specifying the number of octets per line as shown in the next example.

xxd -p -c 8 countries.png

See the output snippet below

89504e470d0a1a0a
0000000d49484452
00000708000003fa
0806000000848609
8700000004734249
54080808087c0864

So using this approach we can split our image file up into chunks and then send it piece by piece to Alice. Since the output of xxd is ASCII safe we can embed our data straight into the email body. Alice will have a bit of work to do since she will have to keep track of the hex file chunks that we send her in order reassemble them into a file on her local machine in the right order. For the moment lets assume Alice is really patient and the information we are sending her is so important that she doesn’t mind this extra work. So once Alice has assembled all of the hex chunks into a local file all she has to do is run xxd in reverse using the -r switch in order to translate the hex back into binary so she can look at the image file. The end to end process might look like this.

Encoding (Bob)

xxd -p -c 32 countries.png > encoded.dta

Decoding (Alice)

xxd -r -p encoded.dta > countries.png

We can make life even easier if we write a little bash script to automate the process.

for b in $(xxd -p  -c 32 ./countries.png); do echo $b | mail -s "FILE CHUNK" alice@secret.com; done

Now this is still not ideal as it will bombard Alice with emails so in reality we should really build in a delay between emails so as Alice’s Sys Admin doesn’t get suspicious. But I will leave that for another day.

For anyone interested in security, xxd is a very useful tool to have in the bag for converting hex back and forth. Given the fact that it works with stdin we can automate and extend its usefulness in an unlimited number of ways.

Until next time, hex out!

Stephen Sheridan