ZW Steg is a python command-line tool implemented to hide secret messages through zero-width Unicode characters. This tool was inspired by Steganographr, 330k and the TryHackMe Room The Impossible Challenge.
The idea of zero-width character stenography is converting a private message into binary data. Then the binary characters space, zero and one are mapped to three different zero-width characters. This will be appended to a public message. The result is that only the public message will be visible.
This method is relatively hard to detect because most text editors display these Unicode characters with zero width - as it’s supposed to. However, there are add-ons for different browsers that transform these characters into emojis (Exp. ZeroWidth Detection by Mikkel D.).
Now let’s take a look at the code:
Now that we understand what the tool does, we can take a look at how I coded the tool. You can check out the GitHub repository to get the full code.
First, I collected a list of zero-width characters. I mainly used the list from 330k for this.
Next, I created the encoding function. This is the function that takes the secret and public message and hides the secret in the public message through these zero-width characters.
For the encoding, I needed a way to map the binary characters to the zero-width characters. I decided to create two lists and map the positions. This could have also been done with dictionaries.
The first step is to transform the secret message into binary. In Python, the
ord() function transforms a character into an ASCII value/integer. The integer can then be transformed into binary with
format(x, 'b'). Each binary representation of a character will be joined with a space.
Finally, we have a binary string that only contains spaces, zeros and ones. These characters can then be mapped to the zero-width characters.
bin_list.index(b) returns the position of the current character. The equivalent position in the
char_list is the zero-width character that represents the character from the binary string.
The zero-width characters are appended to the end of the public message. This could be varied. The characters could be written before or in-between as well.
Here are the steps shown for an example:
- Secret message:
- Public message:
- Secret message to integer:
83 101 99 114 101 116 32 77 101 115 115 97 103 101 33
- Integer to binary:
1010011 1100101 1100011 1110010 1100101 1110100 100000 1001101 1100101 1110011 1110011 1100001 1100111 1100101 100001
- Binary to zero-width characters:
- Zero-width characters appended to public message:
The decoding function works oppositely to the encoding function. First, we only want to look at the zero-width characters. The characters from the public message can be discarded. Then we access the two lists in exactly the opposite order.
The result will be the binary string, each character separated by a space. To transform the binary back into the character, we transform the binary into an integer with
int(b,2) and then the ASCII integer into the corresponding character with the
char() function. This results in the secret message.
Brute Force Decoding
Since there is a whole list of zero-width characters and the mapping to the binary characters can be done arbitrarily, I decided to implement a simple ‘brute fore’ decoding.
First, the three used zero-width characters have to be determined. It has to be exactly three because there are three binary characters that need to have a corresponding zero-width character.
Next, we need to decode the string for each possible order/permutation of the three zero-width characters. There are six options. I used the
itertools library to generate all possible permutations. Some of these permutations could lead to very big binary numbers that are too big to be an integer. In this case, the particular permutation cannot be the original solution and they are simply skipped.
The result of the function will be a maximum of six different strings. It lays by the user to figure out what was the original secret message. Generally, this should be obvious.
I implemented a helper function to display the lists with zero-width characters. It is necessary to use
.encode() with these characters to see the Unicode representation like
"\u2060", since the whole point of these characters is that they are not visible.
Finally to make a functional tool, I combined all of the functions above. For this, I decided to create a simple command-line tool. The tool first queries the options based on the choice it will ask for additionally necessary input.
For the encoding function I added that the encoded result will be saved in a file. This makes it easier to copy the message and not lose the zero-width characters. This could have potentially been avoided if I had placed the characters inside the public message instead of behind it.
I also implemented the possibility to change what zero-width characters are used for the encoding and decoding. This allows for more flexibility.
This was a very fun afternoon project. It made me revise my python skill and I learned something new about stenography, all while creating a tool that could be helpful in future CTF challenges. I hope you enjoyed my breakdown of the project.
I have hidden a secret message in the GitHub repository of ZW Steg. Can you find and encode it?