This is just a write up on resolving picoCTF(2021) Glory of the Garden challenge.
First I tried running exiftool which showed me the metadata of the photo where I noticed that the lines with the following:
|
|
Meaning the photo has son binary data attached to it, now the question is
How do I extract that data?
After some digging on Youtube School I found various ways to do it
The challenge hint it is about using a hex editor which it will help you extract the data. Using that method you will have to scroll and found the flag.
Using strings command
The first way and the most easy one is using strings command. Using this command it will show you only the ASCII representation of the strings inside the binary data of the image.
strings - print the sequences of printable characters in files
Command:
|
|
Output:
|
|
Using xxd command
The second way to do it will be using the xxd command. Using it will start dumping in to the shell all the hex data of the image including the flag.
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.
Command:
|
|
Output:
|
|
And that’s it !! got the flag but not stopping I kept looking around the internet and found some extra steps to make the output of the strings fancier, Like only printing the flag passingtail command to the pipeline or getting only the flag without the extra text passing the cut command or making a simple bash script to automate everything.
Extra steps:
Passing the tail command to the pipeline
What does the tail command do?
tail - Print the last 10 lines of each FILE to standard output. With more than one FILE, precede each with a header giving the file name.
-n, --lines=[+]NUM
output the last NUM lines, instead of the last 10; or use -n +NUM to output starting with
line NUM
Since the flag is on the last line of the data I passed the following to the pipeline
|
|
Now the strings command should look like this:
|
|
and the Output:
|
|
Passing the cut command to the pipeline
What does the cut command do?
cut - remove sections from each line of files Print selected parts of lines from each FILE to standard output. - d, –delimiter=DELIM
use DELIM instead of TAB for field delimiter
- f, --fields=LIST
select only these fields; also print any line that contains no delimiter character, unless the -s option is specified
So as you can see the entire string has “Here is a flag” and the flag is surrounded by "" so using the -d and -f come in handy to tell the command what part of the string I want so by passing -d flag like this:
|
|
this way the cut command it’ll be set (") as the delimiter to split the string into fields, the double quotes is used to separate and distinguish one section of text from another within a larger body of text and the way cut -d works the -f flags needs to be specified or else will throw this error:
|
|
So the way to do this is finding on what field is the flag cut works kind of like this:
|
|
When cut processes the input string, it splits it into fields based on the delimiter. In the case of the string Here is the Flag “picoCTF{more_than_m33ts_the_3y3eBdBd2cc}", the delimiter (") occurs twice in the string, so cut splits it into three fields:
|
|
After cut has split the input string into fields, you can use the -f option to specify which fields you want to keep. In this case, we want to keep the second field, which contains the flag. So we use the -f 2 option to tell cut to keep only the second field.
So, the -d ‘"' option is not removing the double quotes from the fields. Rather, it is specifying the character that should be used as the delimiter to separate the input string into fields. The full command should look like this now:
Command:
|
|
Output:
|
|
And to automate all this I just pasted the command into a bash file so instead of writing all that line all I have to do is run the file and pass the name of the photo. So as for the file just create an .sh file and paste the code but instead of specifying the photo name I change for a positional parameter so it can be run this way:
|
|
So the modified command looks like this:
|
|
In this updated command, $1 is a positional parameter that refers to the first argument passed to the script when it is executed. So, if you execute the script with the command ./bashfile.sh garden.jpg, then $1 will be replaced with garden.jpg in the script. This allows you to run the script with different image file names without having to edit the script itself.