Conversion
String conversion and/or encoding is an important part of exploitation and firewall bypasses.
Convert String/Binary to Hex
If no prefix is needed, you just do the following
Otherwise, see the below ways
For a single character
Note: the symbols *""
are equal of .join
or
or
or
or
or
or
or
or
Convert Hex to String/Binary
or
or for raw socket
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
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
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
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
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.
Convert to Unicode Escape
Hexadecimal unicode escape
Or using unpack
A shorter way
Octal unicode escape
An octal escape is exactly the same, except we convert the string to octal instead of hexadecimal
Escape Sequences in Double-Quoted Strings
En/Decode base-64 String
We'll present this in a few ways.
Encode string
or
Decode
or
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 .
En/Decode URL String
URL encoding/decoding is well known. From a hacker's point of view, we need it often for client-side vulnerabilities.
Encoding string
Decoding string
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 URI.encode_www_form_component
HTML En/Decode
Encoding HTML
Returns
Decoding HTML
Returns
En/Decode SAML String
Decoding SAML
Returns
Resources
Last updated