Class: String
Ronin - A Ruby platform for exploit development and security research.
Copyright © 2006-2009 Hal Brodigan (postmodern.mod3 at gmail.com)
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Public Visibility
Public Instance Method Summary
| #base64_decode |
Base64 decodes a string. Returns: String |
|---|---|
| #base64_encode |
Base64 encodes a string. Returns: String |
| #common_postfix(other) |
Returns the common postfix of the string and the specified other. |
| #common_prefix(other) |
Returns the common prefix of the string and the specified other. |
| #depack(arch, address_length = arch.address_length) |
Packs an Integer from a String, which was originally packed for. Returns: Integer |
| #dump #inspect |
Dumps the string as a C-style string. Returns: String |
| #format_bytes(options = {}, &block) {|byte| ... } |
Creates a new String by formatting each byte. Returns: String |
| #format_chars(options = {}, &block) {|char| ... } |
Creates a new String by formatting each character. Returns: String |
| #format_http(options = {}) |
Returns: String |
| #hex_escape(options = {}) |
Returns: String |
| #hex_unescape |
Returns: String |
| #md5 |
Returns: String |
| #pad(padding, max_length = self.length) |
Creates a new String by padding the String with repeating text,. Returns: String |
| #random_case(options = {}) |
Creates a new String by randomizing the case of each character in the. |
| #sha1 |
Returns: String |
| #sha2 |
Returns: String |
| #sha256 |
Returns: String |
| #sha512 |
Returns: String |
| #uncommon_substring(other) |
Returns the uncommon substring within the specified other string,. |
| #unhexdump(options = {}) |
Converts a multitude of hexdump formats back into the original. Returns: String |
| #uri_decode |
Returns: String |
| #uri_encode |
Returns: String |
| #uri_escape |
Returns: String |
| #uri_unescape |
Returns: String |
| #xor(key) |
XOR encodes the String. Returns: String |
Public Instance Method Details
base64_decode
Base64 decodes a string.
175 176 177 |
# File 'lib/ronin/formatting/extensions/binary/string.rb', line 175 def base64_decode Base64.decode64(self) end |
base64_encode
Base64 encodes a string.
165 166 167 |
# File 'lib/ronin/formatting/extensions/binary/string.rb', line 165 def base64_encode Base64.encode64(self) end |
common_postfix
Returns the common postfix of the string and the specified other string. If no common postfix can be found an empty string will be returned.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ronin/extensions/string.rb', line 45 def common_postfix(other) min_length = [length, other.length].min (min_length - 1).times do |i| index = (length - i - 1) other_index = (other.length - i - 1) if self[index] != other[other_index] return self[(index + 1)..-1] end end return '' end |
common_prefix
Returns the common prefix of the string and the specified other string. If no common prefix can be found an empty string will be returned.
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/ronin/extensions/string.rb', line 28 def common_prefix(other) min_length = [length, other.length].min min_length.times do |i| if self[i] != other[i] return self[0...i] end end return self[0...min_length] end |
depack
Packs an Integer from a String, which was originally packed for a specific architecture and address-length.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/ronin/formatting/extensions/binary/string.rb', line 48 def depack(arch,address_length=arch.address_length) integer = 0x0 byte_index = 0 if arch.endian == 'little' mask = lambda { |b| b << (byte_index * 8) } elsif arch.endian == 'big' mask = lambda { |b| b << ((address_length - byte_index - 1) * 8) } end self.each_byte do |b| break if byte_index >= address_length integer |= mask.call(b) byte_index += 1 end return integer end |
dump
Also known as: inspect
Dumps the string as a C-style string.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/ronin/extensions/string.rb', line 82 def dump c_string = '' each_byte do |b| c_string << case b when 0x00 "\\0" when 0x07 "\\a" when 0x08 "\\b" when 0x09 "\\t" when 0x0a "\\n" when 0x0b "\\v" when 0x0c "\\f" when 0x0d "\\r" when 0x22 "\\\"" when 0x5c "\\\\" when (0x20..0x7e) b.chr else ("\\x%.2x" % b) end end return "\"#{c_string}\"" end |
format_bytes
Creates a new String by formatting each byte.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ronin/formatting/extensions/text/string.rb', line 47 def format_bytes(={},&block) included = ([:included] || (0x00..0xff)) excluded = ([:excluded] || []) formatted = '' self.each_byte do |b| c = b.chr if ((included.include?(b) || included.include?(c)) \ && !(excluded.include?(b) || excluded.include?(c))) formatted << block.call(b) else formatted << b end end return formatted end |
format_chars
Creates a new String by formatting each character.
89 90 91 92 93 |
# File 'lib/ronin/formatting/extensions/text/string.rb', line 89 def format_chars(={},&block) format_bytes() do |b| block.call(b.chr) end end |
format_http
84 85 86 |
# File 'lib/ronin/formatting/extensions/http/string.rb', line 84 def format_http(={}) format_bytes() { |b| "%%%x" % b } end |
hex_escape
78 79 80 |
# File 'lib/ronin/formatting/extensions/binary/string.rb', line 78 def hex_escape(={}) format_bytes() { |b| "\\x%.2x" % b } end |
hex_unescape
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/ronin/formatting/extensions/binary/string.rb', line 90 def hex_unescape buffer = '' hex_index = 0 hex_length = length while (hex_index < hex_length) hex_substring = self[hex_index..-1] if hex_substring =~ /^\\[0-7]{3}/ buffer << hex_substring[0..3].to_i(8) hex_index += 3 elsif hex_substring =~ /^\\x[0-9a-fA-F]{1,2}/ hex_substring[2..-1].scan(/^[0-9a-fA-F]{1,2}/) do |hex_byte| buffer << hex_byte.to_i(16) hex_index += (2 + hex_byte.length) end elsif hex_substring =~ /^\\./ escaped_char = hex_substring[1..1] buffer << case escaped_char when '0' "\0" when 'a' "\a" when 'b' "\b" when 't' "\t" when 'n' "\n" when 'v' "\v" when 'f' "\f" when 'r' "\r" else escaped_char end hex_index += 2 else buffer << hex_substring[0] hex_index += 1 end end return buffer end |
md5
35 36 37 |
# File 'lib/ronin/formatting/extensions/digest/string.rb', line 35 def md5 Digest::MD5.hexdigest(self) end |
pad
Creates a new String by padding the String with repeating text, out to a specified length.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/ronin/formatting/extensions/text/string.rb', line 144 def pad(padding,max_length=self.length) padding = padding.to_s if max_length >= self.length max_length -= self.length else max_length = 0 end padded = self + (padding * (max_length / padding.length)) unless (remaining = max_length % padding.length) == 0 padded += padding[0...remaining] end return padded end |
random_case
Creates a new String by randomizing the case of each character in the String.
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/ronin/formatting/extensions/text/string.rb', line 115 def random_case(={}) prob = ([:probability] || 0.5) format_chars() do |c| if rand <= prob c.swapcase else c end end end |
sha1
47 48 49 |
# File 'lib/ronin/formatting/extensions/digest/string.rb', line 47 def sha1 Digest::SHA1.hexdigest(self) end |
sha2
59 60 61 |
# File 'lib/ronin/formatting/extensions/digest/string.rb', line 59 def sha2 Digest::SHA2.hexdigest(self) end |
sha256
71 72 73 |
# File 'lib/ronin/formatting/extensions/digest/string.rb', line 71 def sha256 Digest::SHA256.hexdigest(self) end |
sha512
83 84 85 |
# File 'lib/ronin/formatting/extensions/digest/string.rb', line 83 def sha512 Digest::SHA512.hexdigest(self) end |
uncommon_substring
Returns the uncommon substring within the specified other string, which does not occur within the string. If no uncommon substring can be found, an empty string will be returned.
65 66 67 68 69 70 |
# File 'lib/ronin/extensions/string.rb', line 65 def uncommon_substring(other) prefix = common_prefix(other) postfix = self[prefix.length..-1].common_postfix(other[prefix.length..-1]) return self[prefix.length...(length - postfix.length)] end |
unhexdump
Converts a multitude of hexdump formats back into the original raw-data.
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/ronin/formatting/extensions/binary/string.rb', line 203 def unhexdump(={}) case (format = [:format]) when :od address_base = base = 8 word_size = 2 when :hexdump address_base = base = 16 word_size = 2 else address_base = base = 16 word_size = 1 end case [:encoding] when :binary base = 2 when :octal, :octal_bytes, :octal_shorts, :octal_ints, :octal_quads base = 8 when :decimal, :decimal_bytes, :decimal_shorts, :decimal_ints, :decimal_quads base = 10 when :hex, :hex_bytes, :hex_shorts, :hex_ints, :hex_quads base = 16 end case [:encoding] when :binary, :octal_bytes, :decimal_bytes, :hex_bytes word_size = 1 when :octal_shorts, :decimal_shorts, :hex_shorts word_size = 2 when :octal_ints, :decimal_ints, :hex_ints word_size = 4 when :octal_quads, :decimal_quads, :hex_quads word_size = 8 end current_addr = last_addr = first_addr = nil repeated = false segment_length = ([:segment] || 16) segment = [] buffer = [] each_line do |line| if format == :hexdump line = line.gsub(/\s+\|.+\|\s*$/,'') end words = line.split if words.first == '*' repeated = true elsif words.length > 0 current_addr = words.shift.to_i(address_base) first_addr ||= current_addr if repeated (((current_addr - last_addr) / segment.length) - 1).times do buffer += segment end repeated = false end segment.clear words.each do |word| if (base != 10 && word =~ /^(\\[0abtnvfr\\]|.)$/) word.hex_unescape.each_byte { |b| segment << b } else segment += word.to_i(base).bytes(word_size) end end segment = segment[0...segment_length] buffer += segment last_addr = current_addr end end return buffer[0...(last_addr - first_addr)] end |
uri_decode
48 49 50 |
# File 'lib/ronin/formatting/extensions/http/string.rb', line 48 def uri_decode URI.decode(self) end |
uri_encode
36 37 38 |
# File 'lib/ronin/formatting/extensions/http/string.rb', line 36 def uri_encode URI.encode(self) end |
uri_escape
60 61 62 |
# File 'lib/ronin/formatting/extensions/http/string.rb', line 60 def uri_escape CGI.escape(self) end |
uri_unescape
72 73 74 |
# File 'lib/ronin/formatting/extensions/http/string.rb', line 72 def uri_unescape CGI.unescape(self) end |
xor
XOR encodes the String.
152 153 154 155 156 157 |
# File 'lib/ronin/formatting/extensions/binary/string.rb', line 152 def xor(key) encoded = '' each_byte { |b| encoded << (b ^ key).chr } return encoded end |