in-case of binary that is out of .chr range. For example you may need to convert an IP-address to hex raw then send it through the socket. The case of just converting it to hex would not work for you
>> ip ="192.168.100.10"=>"192.168.100.10">> ip.split(".").map {|c|'\x%02x'% c.to_i}.join =>"\\xc0\\xa8\\x64\\x0a"
As you can see, Ruby reads returns "\\xc0\\xa8\\x64\\x0a" which doesn't equal "\xc0\xa8\x64\x0a". Try to enter this value (with double-quotes) "\xc0\xa8\x64\x0a" into your irb directly and you'll notice that the return is "\xC0\xA8d\n" which is what should be passed to the raw socket, not the "\\xc0\\xa8\\x64\\x0a". The main cause is ruby escapes the backslash(\).
To solve this issue, use pack to convert integers to 8-bit unsigned (unsigned char)
Note about hex: Sometimes you might face non-printable characters, especially when dealing with binary raw. In this case, append (# -*- coding: binary -*-) at the top of your file to fix any interpretation issues.
Convert Hex (Return address) to Little-Endian format
Little-endian format is simply reversing the string such as reversing/backwarding "Rubyfu" to "ufybuR" which can be done by calling the reverse method of the String class
"Rubyfu".reverse
In exploitation, this is not as simple as that since we're dealing with hex values that may not represent printable characters.
So assume we have 0x77d6b141 as the return address which we want to convert to Little-Endian format to allow the CPU to read it correctly.
Generally speaking, it's really a trivial task to convert 0x77d6b141 to \x41\xb1\xd6\x77 since it's a one time process, but this is not the case if you have a ROP chain that has to be staged in your exploit. To do so simply pack it as an array
[0x77d6b141].pack('V')
It happens that sometimes you get an error because of a non-Unicode string issue. To solve this issue, just force encoding to UTF-8, but most of the time you will not face this issue
[0x77d6b141].pack('V').force_encoding("UTF-8")
If you have a ROP chain, then it's not decent to apply this each time - so you can use the first way and append (# -*- coding: binary -*-) at top of your exploit file.
TIP: The string unpack method is incredibly useful for converting data we read as strings back to their original form. To read more, visit the String class reference at www.ruby-doc.org/core/classes/String.html .
You can encode/decode any non-URL string, of-course.
The above way will encode any non-URL standard strings only (ex. <>"{}) however if you want to encode the full string use one of the following methods:
# SAML Request saml ="fZJNT%2BMwEIbvSPwHy%2Fd8tMvHympSdUGISuwS0cCBm%2BtMUwfbk%2FU4zfLvSVMq2Euv45n3fd7xzOb%2FrGE78KTRZXwSp5yBU1hpV2f8ubyLfvJ5fn42I2lNKxZd2Lon%2BNsBBTZMOhLjQ8Y77wRK0iSctEAiKLFa%2FH4Q0zgVrceACg1ny9uMy7rCdaM2%2Bs0BWrtppK2UAdeoVjW2ruq1bevGImcvR6zpHmtJ1MHSUZAuDKU0vY7Si2h6VU5%2BiMuJuLx65az4dPql3SHBKaz1oYnEfVkWUfG4KkeBna7A%2Fxm6M14j1gZihZazBRH4MODcoKPOgl%2BB32kFz08PGd%2BG0JJIkr7v46%2BhRCaEpod17DCRivYZCkmkd4N28B3wfNyrGKP5bws9DS6PKDz%2FMpsl36Tyz%2F%2Fax1jeFmi0emcLY7C%2F8SDD0Z7dobcynHbbV3QVbcZW0TlqQemNhoqzJD%2B4%2Fn8Yw7l8AA%3D%3D"require'cgi'require'base64'require'zlib'inflated =Base64::decode64(CGI.unescape(saml))# You don't need below code if it's not deflated/compressedzlib =Zlib::Inflate.new(-Zlib::MAX_WBITS)zlib.inflate(inflated)