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

public String base64_decode

Base64 decodes a string.

Meta Tags

Returns:

[String]

The base64 decoded form of the string.

[View source]


175
176
177
# File 'lib/ronin/formatting/extensions/binary/string.rb', line 175

def base64_decode
  Base64.decode64(self)
end

base64_encode

public String base64_encode

Base64 encodes a string.

Meta Tags

Returns:

[String]

The base64 encoded form of the string.

[View source]


165
166
167
# File 'lib/ronin/formatting/extensions/binary/string.rb', line 165

def base64_encode
  Base64.encode64(self)
end

common_postfix

public common_postfix(other)

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.

[View source]


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

public common_prefix(other)

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.

[View source]


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

public Integer depack(arch, address_length = arch.address_length)

Packs an Integer from a String, which was originally packed for a specific architecture and address-length.

Meta Tags

Examples

  0x41.pack(Arch('i686')) # => "A\000\000\000"
  0x41.pack(Arch('ppc'),2) # => "\000A"

Parameters:

[Ronin::Arch] arch

The architecture that the Integer was originally packed with.

[Integer] address_length

The number of bytes to depack.

Returns:

[Integer]

The depacked Integer.

[View source]


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

public String dump

Also known as: inspect

Dumps the string as a C-style string.

Meta Tags

Example:

  "hello\x00\073\x90\r\n".dump
  # => "hello\0;\x90\r\n"

Returns:

[String]

The C-style encoded version of the String.

[View source]


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

public String format_bytes(options = {}, &block) {|byte| ... }

Creates a new String by formatting each byte.

Meta Tags

Parameters:

Options Hash options
Key Name Default Value Accepted Types Description
:included 0x00..0xff [Array, Range]

The bytes to format.

:excluded N/A [Array, Range]

The bytes not to format.

Yields:

[byte]

The block which will return the formatted version of each byte within the String.

Yield Parameters:

[Integer] byte

The byte to format.

Returns:

[String]

The formatted version of the String.

[View source]


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(options={},&block)
  included = (options[:included] || (0x00..0xff))
  excluded = (options[: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

public String format_chars(options = {}, &block) {|char| ... }

Creates a new String by formatting each character.

Meta Tags

Parameters:

Options Hash options
Key Name Default Value Accepted Types Description
:included 0x00..0xff [Array, Range]

The bytes to format.

:excluded N/A [Array, Range]

The bytes not to format.

Yields:

[char]

The block which will return the formatted version of each character within the String.

Yield Parameters:

[String] char

The character to format.

Returns:

[String]

The formatted version of the String.

[View source]


89
90
91
92
93
# File 'lib/ronin/formatting/extensions/text/string.rb', line 89

def format_chars(options={},&block)
  format_bytes(options) do |b|
    block.call(b.chr)
  end
end

format_http

public String format_http(options = {})

Meta Tags

Example:

  "hello".format_http
  # => "&#104;&#101;&#108;&#108;&#111;"

Returns:

[String]

The HTTP hexidecimal encoded form of the String.

[View source]


84
85
86
# File 'lib/ronin/formatting/extensions/http/string.rb', line 84

def format_http(options={})
  format_bytes(options) { |b| "%%%x" % b }
end

hex_escape

public String hex_escape(options = {})

Meta Tags

Example:

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

Returns:

[String]

The hex escaped version of the String.

[View source]


78
79
80
# File 'lib/ronin/formatting/extensions/binary/string.rb', line 78

def hex_escape(options={})
  format_bytes(options) { |b| "\\x%.2x" % b }
end

hex_unescape

public String hex_unescape

Meta Tags

Example:

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

Returns:

[String]

The unescaped version of the hex escaped String.

[View source]


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

public String md5

Meta Tags

Example:

  "hello".md5
  # => "5d41402abc4b2a76b9719d911017c592"

Returns:

[String]

The MD5 checksum of the String.

[View source]


35
36
37
# File 'lib/ronin/formatting/extensions/digest/string.rb', line 35

def md5
  Digest::MD5.hexdigest(self)
end

pad

public String pad(padding, max_length = self.length)

Creates a new String by padding the String with repeating text, out to a specified length.

Meta Tags

Example:

  "hello".pad('A',50)
  # => "helloAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"

Parameters:

[String] padding

The text to pad the new String with.

[String] max_length

The maximum length to pad the new String out to.

Returns:

[String]

The padded version of the String.

[View source]


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

public random_case(options = {})

Creates a new String by randomizing the case of each character in the String.

Meta Tags

Example:

  "get out your checkbook".random_case
  # => "gEt Out YOur CHEckbook"

Parameters:

Options Hash options
Key Name Default Value Accepted Types Description
:included 0x00..0xff [Array, Range]

The bytes to format.

:excluded N/A [Array, Range]

The bytes not to format.

:probability 0.5 [Float]

The probability that a character will have it’s case changed.

[View source]


115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ronin/formatting/extensions/text/string.rb', line 115

def random_case(options={})
  prob = (options[:probability] || 0.5)

  format_chars(options) do |c|
    if rand <= prob
      c.swapcase 
    else
      c
    end
  end
end

sha1

public String sha1

Meta Tags

Example:

  "hello".sha1
  # => "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d"

Returns:

[String]

The SHA1 checksum of the String.

[View source]


47
48
49
# File 'lib/ronin/formatting/extensions/digest/string.rb', line 47

def sha1
  Digest::SHA1.hexdigest(self)
end

sha2

public String sha2

Meta Tags

Example:

  "hello".sha2
  # => "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"

Returns:

[String]

The SHA2 checksum of the String.

[View source]


59
60
61
# File 'lib/ronin/formatting/extensions/digest/string.rb', line 59

def sha2
  Digest::SHA2.hexdigest(self)
end

sha256

public String sha256

Meta Tags

Example:

  "hello".sha256
  # => "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"

Returns:

[String]

The SHA256 checksum of the String.

[View source]


71
72
73
# File 'lib/ronin/formatting/extensions/digest/string.rb', line 71

def sha256
  Digest::SHA256.hexdigest(self)
end

sha512

public String sha512

Meta Tags

Example:

  "hello".sha512
  # => "9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043"

Returns:

[String]

The SHA512 checksum of the String.

[View source]


83
84
85
# File 'lib/ronin/formatting/extensions/digest/string.rb', line 83

def sha512
  Digest::SHA512.hexdigest(self)
end

uncommon_substring

public uncommon_substring(other)

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.

[View source]


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

public String unhexdump(options = {})

Converts a multitude of hexdump formats back into the original raw-data.

Meta Tags

Parameters:

Options Hash options
Key Name Default Value Accepted Types Description
:format N/A [Symbol]

The expected format of the hexdump. Must be either :od or :hexdump.

:encoding N/A [Symbol]

Denotes the encoding used for the bytes within the hexdump. Must be one of the following: :binary, :octal, :octal_bytes :octal_shorts, :octal_ints, :octal_quads+, :decimal, :decimal_bytes, :decimal_shorts, :decimal_ints, :decimal_quads, :hex :hex_bytes, :hex_shorts, :hex_ints or :hex_quads.

:segment 16 [Integer]

The length in bytes of each segment in the hexdump.

Returns:

[String]

The raw-data from the hexdump.

[View source]


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(options={})
  case (format = options[: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 options[: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 options[: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 = (options[: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

public String uri_decode

Meta Tags

Example:

  "genre%3f".uri_decode
  # => "genre?"

Returns:

[String]

The decoded URI form of the String.

[View source]


48
49
50
# File 'lib/ronin/formatting/extensions/http/string.rb', line 48

def uri_decode
  URI.decode(self)
end

uri_encode

public String uri_encode

Meta Tags

Example:

  "art is graffiti".uri_encode
  # => "art%20is%20graffiti"

Returns:

[String]

The URI encoded form of the String.

[View source]


36
37
38
# File 'lib/ronin/formatting/extensions/http/string.rb', line 36

def uri_encode
  URI.encode(self)
end

uri_escape

public String uri_escape

Meta Tags

Example:

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

Returns:

[String]

The URI escaped form of the String.

[View source]


60
61
62
# File 'lib/ronin/formatting/extensions/http/string.rb', line 60

def uri_escape
  CGI.escape(self)
end

uri_unescape

public String uri_unescape

Meta Tags

Example:

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

Returns:

[String]

The unescaped URI form of the String.

[View source]


72
73
74
# File 'lib/ronin/formatting/extensions/http/string.rb', line 72

def uri_unescape
  CGI.unescape(self)
end

xor

public String xor(key)

XOR encodes the String.

Meta Tags

Example:

  "hello".xor(0x41)
  # => ")$--."

Parameters:

[Integer] key

The byte to XOR against each byte in the String.

Returns:

[String]

The XOR encoded String.

[View source]


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
Generated on Friday, September 25 2009 at 02:57:34 PM by YARD 0.2.3.5 (ruby-1.8.6).