Thanks for the comment, I was fighting with the code for a while until I read your comment and I realized I didn’t understood the question, and I was answering something different.
Hmmm… Here are my thoughts:
- I struggled for an hour or two on this using an ubuntu to simulate the output and it gave me one output with counter just literally echoing {1…40} and the wc output (i’m not sure why)
- I used a local toolbox called mobaxterm to run the code, and it ran the basic code there, and echoed everything
- you dont really need the if-else, because you only need the answer.
enjoy (if you can)
I’ve spent more time than I should on this f*cking exercise.
So, here’s my code:
#!/bin/bash
var="nef892na9s1p9asn2aJs71nIsm"
for counter in {1..5}
do
var=$(echo -n $var | base64)
echo $var
done
echo -n $var | wc -m
If you run that snippet, you will get up to 5 iteration and get the base64 hash for that 5th iteration along with the number of characters.
That being said. you can change 5 for 35, and you should get the right answer.
However, the answer is wrong – according to HTB.
So, I give up. Aint got time to spend more than 2 hours on this.
As someone mentioned in this thread, results will differ depending if you run your code from Linux, or macOS, and perhaps Windows?
That being said. answer can be found below, you’re welcome.
#!/bin/bash
# Variable to encode
var="nef892na9s1p9asn2aJs71nIsm"
for counter in {1..40}; do
var=$(echo $var | base64)
# Check if the loop is at the 35th iteration
if [ $counter -eq 35 ]; then
# Print the number of characters in the variable
echo $var | wc -c
fi
done
I had trouble creating the file and puting the content into it. I was able to figure it out through vim command.
vim filename.sh
then copy the code from the exercise script and then add if condition after var=$(echo var | base64) line
if [ counter -eq 35 ];then
count = $(echo “$var” | wc -c)
echo “$count”
fi
and make sure you have ‘done’ at the end.
Finally, hit esc and then :wq to save and exit out of the file. To execute it:
bash filename.sh
This worked for me hope it was helpful.
did you run this on a linux system?
I had the same problem, running it on a mac it gave a different answer than on linux. the value from linux was accepted
About the difference in MacOS and Linux is because of the base64 Encoding:
- On macOS, the base64 command does not break lines by default.
- On Linux distributions, the base64 inserts line breaks by default after 76 characters.
Can match output either by
base64 -b 76
on MacOS: to put line breaks after 76 chars
ORbase64 -w 0
on Linux: to remove line breaks