0

Could somebody explain me, why there are two various outputs?

CODE IN IRB(Interactive ruby shell):

irb(main):001:0> require 'base64'
=> true 
irb(main):002:0> cookie = "YXNkZmctLTBEAiAvi95NGgcgk1W0pyUKXFEo6IuEvdxhmrfLqNVpskDv5AIgVn8wfIWf0y41cb%2Bx9I0ah%2F4BIIeRJ54nX2qGcxw567Y%3D"
=> "YXNkZmctLTBEAiAvi95NGgcgk1W0pyUKXFEo6IuEvdxhmrfLqNVpskDv5AIgVn8wfIWf0y41cb%2Bx9I0ah%2F4BIIeRJ54nX2qGcxw567Y%3D" 
irb(main):003:0> decoded_cookie = Base64.urlsafe_decode64(URI.decode(cookie))
=> "asdfg--0D\x02 /\x8B\xDEM\x1A\a \x93U\xB4\xA7%\n\\Q(\xE8\x8B\x84\xBD\xDCa\x9A\xB7\xCB\xA8\xD5i\xB2@\xEF\xE4\x02 V\x7F0|\x85\x9F\xD3.5q\xBF\xB1\xF4\x8D\x1A\x87\xFE\x01 \x87\x91'\x9E'_j\x86s\x1C9\xEB\xB6"

Code from Linux terminal:

asd@asd:~# ruby script.rb
asdfg--0D /��M� �U��%
\Q(苄��a��˨�i�@�� V0|���.5q������ ��'�'_j�s9�

Script:

require 'base64'
require 'ecdsa'
cookie = "YXNkZmctLTBEAiAvi95NGgcgk1W0pyUKXFEo6IuEvdxhmrfLqNVpskDv5AIgVn8wfIWf0y41cb%2Bx9I0ah%2F4BIIeRJ54nX2qGcxw567Y%3D"

def decode_cookie(cookie)
  decoded_cookie = Base64.urlsafe_decode64(URI.decode(cookie))
end

puts (decode_cookie(cookie))

How can i get the same output in terminal? I need the output:

"asdfg--0D\x02 /\x8B\xDEM\x1A\a \x93U\xB4\xA7%\n\Q(\xE8\x8B\x84\xBD\xDCa\x9A\xB7\xCB\xA8\xD5i\xB2@\xEF\xE4\x02 V\x7F0|\x85\x9F\xD3.5q\xBF\xB1\xF4\x8D\x1A\x87\xFE\x01 \x87\x91'\x9E'_j\x86s\x1C9\xEB\xB6"

In Linux terminal.

  • You probably want to use `p` rather than `puts`. You'll find all the details here: https://stackoverflow.com/questions/1255324/p-vs-puts-in-ruby – ymbirtt Oct 08 '19 at 13:06

1 Answers1

0

A string like "\x8B" is a representation of character, not the literal \x8B. Ruby uses such representation if it's missing the font to display the character or if it messes with whitespacing (for example "\n" is a newline and not \ followed by a n).

The reason you get another output in irb is because you don't print the string using puts (like you do in your script). Simply calling decoded_cookie will return the string representation, not the actual content.

You can display the actual content by simply printing it to an output.

require 'base64'

cookie = "YXNkZmctLTBEAiAvi95NGgcgk1W0pyUKXFEo6IuEvdxhmrfLqNVpskDv5AIgVn8wfIWf0y41cb%2Bx9I0ah%2F4BIIeRJ54nX2qGcxw567Y%3D"
decoded_cookie = Base64.urlsafe_decode64(URI.decode(cookie))
puts decoded_cookie
# asdfg--0D /��M �U��%
# \Q(苄��a��˨�i�@�� V0|���.5q����� ��'�'_j�s9�
#=> nil

You can find more info about the "\xnn" representation here.

If you'd like the script to display the string representation use p instead of puts, or use puts decoded_cookie.inspect.

3limin4t0r
  • 19,353
  • 2
  • 31
  • 52