Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH Base64 Encode script not encoding right

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=
like image 425
jfreak53 Avatar asked Sep 05 '12 22:09

jfreak53


People also ask

Can Base64 encoding fail?

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.

How Base64 ends with ==?

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).

Is Base64 obfuscated?

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).


1 Answers

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?)

like image 71
Gordon Davisson Avatar answered Sep 28 '22 10:09

Gordon Davisson