I am trying to get a base64 encode to work and output to a variable in a bash script. The regular cli syntax is:
echo -ne "\[email protected]\0mypass" | base64
But when I try putting this into a variable in a script it outputs, but a very small encoding, so I know it's not working. My code in the script is:
auth=$(echo -ne "\0$user@$host\0$pass" | base64);
I know it has something to do with the quotes, but I've tried a myriad of thing's with different quotes and singles and backslashes with no go.
Any thoughts?
EDIT: A bit more for the info. This should output with the user/pass/host above:
AG15dXNlckBteWhvc3QuY29tAG15cGFzcw==
But in the script it outputs:
LW5lIAo=
You can encode arbitrary bytes in base64 (which is why the encoding functions don't return errors). Only decoding can fail. The whole point of Base64 encoding is to take arbitrary bytes and reduce them to printable ASCII characters. There is no such thing as an invalid character for encoding, only for decoding.
Base64 deals with the first block (producing 4 characters) and the second (as they are complete). But for the third, it will add a double == in the output in order to complete the 4 needed characters. Thus, the result will be QUJD REVG Rw== (without spaces).
Base64 encodingBase64 is the most common form of obfuscation across our detection data. Administrators and developers use Base64 encoding to pass scripts to subprocesses or remote systems and to conceal sensitive information (think: passwords).
Different versions of echo
behave very differently when you give them anything other than a plain string. Some interpret command options (like -ne
), while some just print them as output; some interpret escape sequences in the string (even if not given a -e
option), some don't.
If you want consistent behavior, use printf
instead:
user=myuser
pass=mypass
host=myhost.com
auth=$(printf "\0%s@%s\0%s" "$user" "$host" "$pass" | base64)
As a bonus, since the password (and username and host) are in plain strings rather than the format string, it won't make a mess trying to interpret escape sequences in them (does your real password have a backslash in it?)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With