class String

Extension of the String class.

Constants

HEX_VALUES

Public Class Methods

generate_all_hex_values(upcase=true) click to toggle source

generate all hex values and return them in a string

# File lib/aastdlib/string.rb, line 26
def self.generate_all_hex_values(upcase=true)

        s = ""

        256.times do |n|

                n = n.to_s(16)
                n = n.upcase if upcase
                n = '0' + n if n.length < 2
                s += '\x' + n

        end

        return s

end

Public Instance Methods

bin_to_array(upcase: true, radix: 16) click to toggle source

Return the string as an array of base 16 values. Base can be altered by updating the radix argument.

# File lib/aastdlib/string.rb, line 228
def bin_to_array(upcase: true, radix: 16)

        return self.bytes.map do |b|

                                if upcase
                                        b = b.to_s(radix).upcase
                                else
                                        b = b.to_s(radix)
                                end

                                if b.length < 2
                                        b = '0' + b
                                else
                                        b
                                end

                        end

end
borderize(char='-') click to toggle source

return the string with a border at the top and bottom of the string border character can be altered

# File lib/aastdlib/string.rb, line 58
def borderize(char='-')

        border = char * self.length
        return "#{border}\n#{self}\n#{border}"

end
decode64(strict=false) click to toggle source

Base64 decde and return a duplicate string.

# File lib/aastdlib/string.rb, line 319
def decode64(strict=false)
        if strict
                return Base64::strict_decode64(self)
        else
                return Base64::decode64(self)
        end
end
obfuscate_case() click to toggle source

Return a duplicate of the string with the case altered randomly.

# File lib/aastdlib/string.rb, line 342
def obfuscate_case()

        return self.split('').map do |c|

                if rand(0..1) > 0

                        c = c.upcase

                else

                        c = c.downcase

                end

        end.join('')

end
obfuscate_string_insert(string) click to toggle source

Inser string at a random place in the current string.

# File lib/aastdlib/string.rb, line 361
def obfuscate_string_insert(string)

        return self.insert(rand(0..(self.length - 1)), string)

end
prefix(symbol: '+', wrapper: ["[","]"], space_char: " ", rep: 1) click to toggle source

Prefix the string with a symbol wrapped in two characters.

# File lib/aastdlib/string.rb, line 142
def prefix(symbol: '+', wrapper: ["[","]"], space_char: " ", rep: 1)

    return (wrapper[0] + symbol + wrapper[1]) + (space_char * rep) + self

end
prettyfi(max_len=60) click to toggle source

trim the string to a particular length and echo it to stdout as a script ready string

# File lib/aastdlib/string.rb, line 67
def prettyfi(max_len=60)

        if self.length < max_len

                out = self

        else

                # holds output fragments
                buff = []

                start, _end = 0, (max_len-1)

                ind,counter = -1, 0

                chunk = ""
                while c = self[ind+=1]

                        counter += 1
                        chunk += c

                        if counter == (max_len) or ind == (self.length-1)

                                buff << '"' + chunk + '"'
                                chunk = ""
                                counter = 0

                        end

                end

                out = buff.join(" +\n")

        end

        return out
        
end
prettyfi_shellcode(prefix: '\x', limit: 16) click to toggle source

same thing as prettyfi but converts contents of the string to x format

# File lib/aastdlib/string.rb, line 108
def prettyfi_shellcode(prefix: '\x', limit: 16)

        ary = self.bin_to_array

        output = ""

        # Maximum index position of split array
        max = (ary.length - 1)

        # Bottom of slice range
        floor = 0

        # Top of slice range
        ceiling = (limit-1)

        while true

                slice = prefix + ary.slice(floor..ceiling).join(prefix)

                output += '"' + slice + '"'

                break if ceiling >= max

                floor += limit
                ceiling += limit
                output += "+\n"

        end

        return output

end
rando_insert(str) click to toggle source

Insert a string at a random index within this string

# File lib/aastdlib/string.rb, line 44
def rando_insert(str)

        unless self.length > 1

                raise "String length is too short! length must be 2 or greater (len >= 2)"

        end

        return self.dup.insert(rand(0..(self.length-1)), str)

end
simple_prefix(char) click to toggle source

Returna a duplicate of the string with a character prefix.

# File lib/aastdlib/string.rb, line 149
def simple_prefix(char)

    return char+self

end
snake_name() click to toggle source

Convenience method for #to_underscore()

# File lib/aastdlib/string.rb, line 191
def snake_name()

        return self.to_underscore

end
suffix(char) click to toggle source

Return a duplicate of the string suffixed with a single character.

# File lib/aastdlib/string.rb, line 163
def suffix(char)

    return self+char

end
to_base64(strict=false) click to toggle source

Base64 encode and return a duplicate string.

# File lib/aastdlib/string.rb, line 310
def to_base64(strict=false)
        if strict
                return Base64::strict_encode64(self)
        else
                return Base64::encode64(self)
        end
end
to_binary(prefix='0b') click to toggle source

Encode and return a duplicate of the string in 0b0010110 format.

# File lib/aastdlib/string.rb, line 328
def to_binary(prefix='0b')

        return prefix+self.bytes
                        .map do |b|

                                b = b.to_s(2)
                                b = '0' * (8 - b.length) + b

                        end
                        .join(prefix)

end
to_csv_value(comma_delimit=true) click to toggle source

Convert the string to a CSV value. Setting comma delimit to false will return the string wrapped in double quotes.

# File lib/aastdlib/string.rb, line 171
def to_csv_value(comma_delimit=true)

    return comma_delimit ? self.wrap('"').suffix(',') : self.wrap('"')

end
to_hex_string(literal: true, prefix: '\x') click to toggle source

Returns a duplicate of the string unpacked in x literal form. Useful when dynamically generating shellcode.

# File lib/aastdlib/string.rb, line 250
def to_hex_string(literal: true, prefix: '\x')

        out = self.bin_to_array()

        if literal
                out = prefix + out.join(prefix)
        else
    out = out.map!  {|e| [e].pack("H2") }.join('')
        end

        return out

end
to_instance_variable_name(snake_case=true, symbolize=true) click to toggle source

Return a duplicate of the string prefixed with @ converted to a symbol.

# File lib/aastdlib/string.rb, line 198
def to_instance_variable_name(snake_case=true, symbolize=true)

        name = self.to_s
        name = name.to_underscore if snake_case
        name = ('@' + name) if name !~ /^@/ 
        name = name.to_sym if symbolize

        return name

end
to_ivar_name(snake_case=true, symbolize=true) click to toggle source

Convenience method for #to_instance_variable_name()

# File lib/aastdlib/string.rb, line 210
def to_ivar_name(snake_case=true, symbolize=true)

        to_instance_variable_name(snake_case, symbolize)

end
to_underscore() click to toggle source
Return the string will all

and capital letters converted to

underscores.

# File lib/aastdlib/string.rb, line 179
def to_underscore()

        # sauce: https://stackoverflow.com/questions/1509915/converting-camel-case-to-underscore-case-in-ruby
        return self.gsub(/::/, '/')
                .gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
                .gsub(/([a-z\d])([A-Z])/,'\1_\2')
                .tr("-", "_")
                .downcase

end
to_url_encoded() click to toggle source

Poor mans URL encoding: each character is unpacked to it's hex form and then suffixed with %.

# File lib/aastdlib/string.rb, line 305
def to_url_encoded()
        return '%' + self.bin_to_array.join('%')
end
to_utf16(endianness='le') click to toggle source

Return a duplicate string in literal from where each char is converted to utf16 literal format. Endianness can be altered via the endianness argument.

# File lib/aastdlib/string.rb, line 275
def to_utf16(endianness='le')

        if endianness !~ /^(le|be)$/i

                raise "Error: valid arguments to endianness parameter: le or be"

        end

        pad = '00'
        prefix = '\u'

        # get a copy of the string in hex form
        # and pad each byte
        buff = prefix + self.bin_to_array
                        .map do |b|

                                if endianness =~ /^le$/i 
                                        (b = b + pad)
                                else
                                        (b = pad + b)
                                end

                        end.join(prefix)

        return buff

end
to_utf8(prefix='\u') click to toggle source

Return a duplicate string in literal form with each character suffixed with u

# File lib/aastdlib/string.rb, line 266
def to_utf8(prefix='\u')

        return prefix + self.bin_to_array.join(prefix)

end
utf_pad() click to toggle source

Pads each binary component in the string with x00. A duplicate of the string is modified and returned.

# File lib/aastdlib/string.rb, line 218
def utf_pad()

        return self.unpack('H2'*self.bytesize)
                .map {|l| l = "\x00" + [l].pack('H2')}
                .join('')

end
wrap(char) click to toggle source

Return a duplicate of the string with a character on each side.

# File lib/aastdlib/string.rb, line 156
def wrap(char)

    return char+self+char

end