Module: Ronin::Support::Encoding::Shell
- Defined in:
- lib/ronin/support/encoding/shell.rb
Overview
Contains methods for encoding/decoding escaping/unescaping Shell data.
Core-Ext Methods
Constant Summary collapse
- ESCAPE_BYTES =
Special shell bytes and their escaped Strings.
{ 0x00 => "\\0", # $'\0' 0x07 => "\\a", # $'\a' 0x08 => "\\b", # $'\b' 0x09 => "\\t", # $'\t' 0x0a => "\\n", # $'\n' 0x0b => "\\v", # $'\v' 0x0c => "\\f", # $'\f' 0x0d => "\\r", # $'\r' 0x1B => "\\e", # $'\e' 0x22 => "\\\"", # \" 0x23 => "\\#", # \# 0x5c => "\\\\" # \\ }
- BACKSLASHED_CHARS =
Shell characters that must be back-slashed.
{ '0' => "\0", 'a' => "\a", 'b' => "\b", 'e' => "\e", 't' => "\t", 'n' => "\n", 'v' => "\v", 'f' => "\f", 'r' => "\r", "'" => "'", '"' => '"' }
Class Method Summary collapse
-
.decode(data) ⇒ String
Alias for Shell.unescape.
-
.encode(data) ⇒ String
Shell encodes every character in the given data.
-
.encode_byte(byte) ⇒ String
Encodes the byte as a shell character.
-
.escape(data) ⇒ String
Shell escapes any special characters in the given data.
-
.escape_byte(byte) ⇒ String
Escapes the byte as a shell character.
-
.quote(data) ⇒ String
Converts the given data into a double-quoted shell escaped String.
-
.unescape(data) ⇒ String
Shell unescapes the given data.
-
.unquote(data) ⇒ String
Removes the quotes an unescapes a shell string.
Class Method Details
.decode(data) ⇒ String
Alias for unescape.
243 244 245 |
# File 'lib/ronin/support/encoding/shell.rb', line 243 def self.decode(data) unescape(data) end |
.encode(data) ⇒ String
Shell encodes every character in the given data.
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/ronin/support/encoding/shell.rb', line 216 def self.encode(data) encoded = String.new if data.valid_encoding? data.each_codepoint do |codepoint| encoded << encode_byte(codepoint) end else data.each_byte do |byte| encoded << encode_byte(byte) end end return encoded end |
.encode_byte(byte) ⇒ String
Encodes the byte as a shell character.
79 80 81 82 83 84 85 86 87 |
# File 'lib/ronin/support/encoding/shell.rb', line 79 def self.encode_byte(byte) if byte >= 0x00 && byte <= 0xff "\\x%.2x" % byte elsif byte > 0xff "\\u%x" % byte else raise(RangeError,"#{byte.inspect} out of char range") end end |
.escape(data) ⇒ String
Shell escapes any special characters in the given data.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/ronin/support/encoding/shell.rb', line 155 def self.escape(data) escaped = String.new if data.valid_encoding? data.each_codepoint do |codepoint| escaped << escape_byte(codepoint) end else data.each_byte do |byte| escaped << escape_byte(byte) end end return escaped end |
.escape_byte(byte) ⇒ String
Escapes the byte as a shell character.
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/ronin/support/encoding/shell.rb', line 113 def self.escape_byte(byte) if byte >= 0x00 && byte <= 0xff ESCAPE_BYTES.fetch(byte) do if byte >= 0x20 && byte <= 0x7e byte.chr else encode_byte(byte) end end else encode_byte(byte) end end |
.quote(data) ⇒ String
Converts the given data into a double-quoted shell escaped String.
262 263 264 265 266 267 268 |
# File 'lib/ronin/support/encoding/shell.rb', line 262 def self.quote(data) if data =~ /[^[:print:]]/ "$'#{escape(data)}'" else "\"#{escape(data)}\"" end end |
.unescape(data) ⇒ String
Shell unescapes the given data.
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/ronin/support/encoding/shell.rb', line 184 def self.unescape(data) unescaped = String.new scanner = StringScanner.new(data) until scanner.eos? unescaped << if (backslash_char = scanner.scan(/\\[0abetnvfr\'\"]/)) # \n BACKSLASHED_CHARS[backslash_char[1..]] elsif (hex_char = scanner.scan(/\\x[0-9a-fA-F]+/)) # \XX hex_char[2..].to_i(16).chr elsif (unicode_char = scanner.scan(/\\u[0-9a-fA-F]+/)) # \uXXXX unicode_char[2..].to_i(16).chr(Encoding::UTF_8) else scanner.getch end end return unescaped end |
.unquote(data) ⇒ String
Removes the quotes an unescapes a shell string.
288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/ronin/support/encoding/shell.rb', line 288 def self.unquote(data) if (data[0,2] == "$'" && data[-1] == "'") unescape(data[2..-2]) elsif (data[0] == '"' && data[-1] == '"') data[1..-2].gsub("\\\"",'"') elsif (data[0] == "'" && data[-1] == "'") data[1..-2].gsub("\\'","'") else data end end |