Writing Ronin Ruby Scripts - Encoding

Table of Contents

Base64

ronin-support adds methods to Ruby’s String class for encoding/decoding Base64 data:

require 'ronin/support/encoding/base64'

"foo bar\n".base64_encode
# => "Zm9vIGJhcgo=\n"

"czNjcjN0\n".base64_decode
# => "s3cr3t"

Base32

ronin-support adds Base32 encoding/decoding methods to Ruby’s built-in String class:

require 'ronin/support/encoding/base32'

"foo bar\n".base32_encode
# => "MZXW6IDCMFZAU==="

"OMZWG4RTOQ======".base32_decode
# => "s3cr3t"

Base16

ronin-support adds Base16 encoding/decoding methods to Ruby’s built-in String class:

require 'ronin/support/encoding/base16'

"foo bar\n".base16_encode
# => "666f6f206261720a"

"733363723374".base16_decode
# => "s3cr3t"

C

ronin-support adds C string encoding/decoding methods to Ruby’s built-in String class:

require 'ronin/support/encoding/c'

"hello\nworld\n".c_escape
# => "hello\\nworld\\n"

"hello".c_encode
# => "\\x68\\x65\\x6c\\x6c\\x6f"

"hello\nworld\n".c_string
# => "\"hello\\nworld\\n\""

"\\x68\\x65\\x6c\\x6c\\x6f\\x20\\x77\\x6f\\x72\\x6c\\x64".c_unescape
# => "hello world"

"\"hello\\nworld\"".c_unquote
# => "hello\nworld"

ronin-support also adds c_encode and c_escape methods to Ruby’s built-in Integer class:

0x41.c_encode
# => "\\x41"
0x100.c_encode
# => "\\u1000"
0x10000.c_encode
# => "\\U000100000"

0x41.c_escape
# => "A"
0x22.c_escape
# => "\\\""
0x7f.c_escape
# => "\\x7F"

Hex

ronin-support adds hex encoding/decoding methods to Ruby’s built-in String class:

require 'ronin/support/encoding/hex'

"hello\nworld".hex_escape
# => "hello\\nworld"

"hello".hex_encode
# => "68656C6C6F"

"hello\nworld".hex_string
# => "\"hello\\nworld\""

"hello\\nworld".hex_unescape
# => "hello\nworld"

"68656C6C6F".hex_decode
# => "hello"

"\"hello\\nworld\"".hex_unquote
# => "hello\nworld"

ronin-support also adds hex_encode, hex_escape, and hex_int methods to Ruby’s built-in Integer class:

0x41.hex_encode
# => "41"

42.hex_char
# => "\\x2a"

42.hex_int
# => "0x2e"

HTML

ronin-support adds HTML encoding/decoding methods to Ruby’s built-in String class:

require 'ronin/support/encoding/html'

"one & two".html_escape
# => "one & two"

"abc".html_encode
# => "abc"

"<p>one <span>two</span></p>".html_unescape
# => "<p>one <span>two</span></p>"

ronin-support also adds html_encode and html_escape methods to Ruby’s built-in Integer class:

0x26.html_escape
# => "&amp;"

0x41.html_encode
# => "&#65;"

HTTP

ronin-support adds HTTP encoding/decoding methods to Ruby’s built-in String class:

require 'ronin/support/encoding/http'

"x > y".http_escape
# => "x+%3E+y"

"hello".http_encode
# => "%68%65%6c%6c%6f"

"sweet+%26+sour".http_unescape
# => "sweet & sour"

ronin-support also adds http_encode and http_escape methods to Ruby’s built-in Integer class:

62.http_escape
# => "%3E"

0x41.http_encode
# => "%41"

JavaScript

ronin-support adds JavaScript string encoding/decoding methods to Ruby’s built-in String class:

require 'ronin/support/encoding/js'

"hello\nworld\n".js_escape
# => "hello\\nworld\\n"

"hello".js_encode
# => "\\u0068\\u0065\\u006C\\u006C\\u006F"

"hello\nworld\n".js_string
# => "\"hello\\nworld\\n\""

"\\u0068\\u0065\\u006C\\u006C\\u006F world".js_unescape
# => "hello world"

"\"hello\\nworld\"".js_unquote
# => "hello\nworld"

ronin-support also adds js_encode and js_escape methods to Ruby’s built-in Integer class:

0x41.js_escape
# => "A"
0x22.js_escape
# => "\\\""
0x7f.js_escape
# => "\\x7F"

0x41.js_encode
# => "\\x41"

PowerShell

ronin-support adds PowerShell string encoding/decoding methods to Ruby’s built-in String class:

require 'ronin/support/encoding/powershell'

"hello\nworld".powershell_escape
# => "hello`nworld"

"hello world".powershell_encode
# => "$([char]0x68)$([char]0x65)$([char]0x6c)$([char]0x6c)$([char]0x6f)$([char]0x20)$([char]0x77)$([char]0x6f)$([char]0x72)$([char]0x6c)$([char]0x64)"

"hello\nworld".powershell_string
# => "\"hello`nworld\""

"hello`nworld".powershell_unescape
# => "hello\nworld"

"\"hello`nworld\"".powershell_unquote
# => "hello\nworld"
"'hello''world'".powershell_unquote
# => "hello'world"

ronin-support also adds powershell_encode and powershell_escape methods to Ruby’s built-in Integer class:

0x41.powershell_escape
# => "A"
0x08.powershell_escape
# => "`b"
0xff.powershell_escape
# => "[char]0xff"

0x41.powershell_encode
# => "[char]0x41"
0x0a.powershell_encode
# => "`n"

Punycode

ronin-support adds Punycode string encoding/decoding methods to Ruby’s built-in String class:

require 'ronin/support/encoding/punycode'

"詹姆斯".punycode_encode
# => "xn--8ws00zhy3a"

"xn--8ws00zhy3a".punycode_decode
# => "詹姆斯"

Quoted Printable

ronin-support adds Quoted Printable string encoding/decoding methods to Ruby’s built-in String class:

require 'ronin/support/encoding/quoted_printable'

'<a href="https://example.com/">link</a>'.quoted_printable_escape
# => "<a href=3D\"https://example.com/\">link</a>=\n"

"詹姆斯".punycode_encode
# => "xn--8ws00zhy3a"

"<a href=3D\"https://example.com/\">link</a>=\n".quoted_printable_unescape
# => "<a href=\"https://example.com/\">link</a>"

Ruby

ronin-support adds Ruby string encoding/decoding methods to Ruby’s built-in String class:

require 'ronin/support/encoding/ruby'

"hello\nworld\n".ruby_escape
# => "hello\\nworld\\n"

"hello".ruby_encode
# => "\\x68\\x65\\x6c\\x6c\\x6f"

"hello\nworld\n".ruby_string
# => "\"hello\\nworld\\n\""

"\x68\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64".ruby_unescape
# => "hello world"

"\"hello\\nworld\"".ruby_unquote
# => "hello\nworld"

Shell

ronin-support adds Shell string encoding/decoding methods to Ruby’s built-in String class:

require 'ronin/support/encoding/shell'

"hello\nworld".shell_escape
# => "hello\\nworld"

"hello world".shell_encode
# => "\\x68\\x65\\x6c\\x6c\\x6f\\x0a\\x77\\x6f\\x72\\x6c\\x64"

"hello world".shell_string
# => "\"hello world\""
"hello\nworld".shell_string
# => "$'hello\\nworld'"

"hello\\nworld".shell_unescape
# => "hello\nworld"

"\"hello \\\"world\\\"\"".shell_unquote
# => "hello \"world\""
"'hello\\'world'".shell_unquote
# => "hello'world"
"$'hello\\nworld'".shell_unquote
# => "hello\nworld"

ronin-support also adds shell_encode and shell_escape methods to Ruby’s built-in Integer class:

0x41.shell_escape
# => "A"
0x08.shell_escape
# => "\b"
0xff.shell_escape
# => "\xff"

0x41.shell_encode
# => "\\x41"
0x0a.shell_encode
# => "\\n"

SQL

ronin-support adds SQL string encoding/decoding methods to Ruby’s built-in String class:

require 'ronin/support/encoding/sql'

"O'Brian".sql_escape
# => "'O''Brian'"

"/etc/passwd".sql_encode
# => "0x2f6574632f706173737764"

"'O''Brian'".sql_unescape
# => "O'Brian"

"'Conan O''Brian'".sql_decode
# => "Conan O'Brian"
"2f6574632f706173737764".sql_decode
# => "/etc/passwd"

URI

ronin-support adds URI string encoding/decoding methods to Ruby’s built-in String class:

require 'ronin/support/encoding/uri'

"x > y".uri_escape
# => "x%20%3E%20y"

"plain text".uri_encode
# => "%70%6C%61%69%6E%20%74%65%78%74"

"sweet%20%26%20sour".uri_unescape
# => "sweet & sour"

ronin-support also adds uri_encode and uri_escape methods to Ruby’s built-in Integer class:

0x41.uri_escape
# => "A"
0x3d.uri_escape
# => "%3D"

0x41.uri_encode
# => "%41"

UUencoding

ronin-support adds UUencoding encoding/decoding methods to Ruby’s built-in String class:

require 'ronin/support/encoding/uuencoding'

"hello world".uu_encode
# => "+:&5L;&\\@=V]R;&0`\n"

"+:&5L;&\\@=V]R;&0`\n".uu_decode
# => "hello world"

XML

ronin-support adds XML encoding/decoding methods to Ruby’s built-in String class:

require 'ronin/support/encoding/xml'

"one & two".xml_escape
# => "one &amp; two"

"abc".xml_encode
# => "&#97;&#98;&#99;"

"&lt;p&gt;one &lt;span&gt;two&lt;/span&gt;&lt;/p&gt;".xml_unescape
# => "<p>one <span>two</span></p>"

ronin-support also adds xml_encode and xml_escape methods to Ruby’s built-in Integer class:

0x26.xml_escape
# => "&amp;"

0x41.xml_encode
# => "&#65;"