class String
Extension of the String
class.
Constants
- HEX_VALUES
Public Class Methods
generate all hex values and return them in a string
# File lib/aastdlib/string.rb, line 27 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
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 238 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
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 59 def borderize(char='-') border = char * self.length return "#{border}\n#{self}\n#{border}" end
Base64 decde and return a duplicate string.
# File lib/aastdlib/string.rb, line 329 def decode64(strict=false) if strict return Base64::strict_decode64(self) else return Base64::decode64(self) end end
Return a duplicate of the string with the case altered randomly.
# File lib/aastdlib/string.rb, line 352 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
Inser string at a random place in the current string.
# File lib/aastdlib/string.rb, line 371 def obfuscate_string_insert(string) return self.insert(rand(0..(self.length - 1)), string) end
Prefix the string with a symbol wrapped in two characters.
# File lib/aastdlib/string.rb, line 143 def prefix(symbol: '+', wrapper: ["[","]"], space_char: " ", space_rep: 1, ind_char: "", ind_rep: 1) # build the indention string indention = ind_char * ind_rep # build the spacer string spacer = space_char * space_rep # build the symbol string symbol = indention + wrapper.insert(1,symbol).join('') + spacer return symbol + self end
trim the string to a particular length and echo it to stdout as a script ready string
# File lib/aastdlib/string.rb, line 68 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
same thing as prettyfi but converts contents of the string to x format
# File lib/aastdlib/string.rb, line 109 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
Insert a string at a random index within this string
# File lib/aastdlib/string.rb, line 45 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
Returna a duplicate of the string with a character prefix.
# File lib/aastdlib/string.rb, line 159 def simple_prefix(char) return char+self end
Convenience method for to_underscore
()
# File lib/aastdlib/string.rb, line 201 def snake_name() return self.to_underscore end
Return a duplicate of the string suffixed with a single character.
# File lib/aastdlib/string.rb, line 173 def suffix(char) return self+char end
Base64 encode and return a duplicate string.
# File lib/aastdlib/string.rb, line 320 def to_base64(strict=false) if strict return Base64::strict_encode64(self) else return Base64::encode64(self) end end
Encode and return a duplicate of the string in 0b0010110 format.
# File lib/aastdlib/string.rb, line 338 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
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 181 def to_csv_value(comma_delimit=true) return comma_delimit ? self.wrap('"').suffix(',') : self.wrap('"') end
Returns a duplicate of the string unpacked in x literal form. Useful when dynamically generating shellcode.
# File lib/aastdlib/string.rb, line 260 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
Return a duplicate of the string prefixed with @ converted to a symbol.
# File lib/aastdlib/string.rb, line 208 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
Convenience method for to_instance_variable_name
()
# File lib/aastdlib/string.rb, line 220 def to_ivar_name(snake_case=true, symbolize=true) to_instance_variable_name(snake_case, symbolize) end
- Return the string will all
-
and capital letters converted to
underscores.
# File lib/aastdlib/string.rb, line 189 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
Poor mans URL encoding: each character is unpacked to it’s hex form and then suffixed with %.
# File lib/aastdlib/string.rb, line 315 def to_url_encoded() return '%' + self.bin_to_array.join('%') end
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 285 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
Return a duplicate string in literal form with each character suffixed with u
# File lib/aastdlib/string.rb, line 276 def to_utf8(prefix='\u') return prefix + self.bin_to_array.join(prefix) end
Pads each binary component in the string with x00. A duplicate of the string is modified and returned.
# File lib/aastdlib/string.rb, line 228 def utf_pad() return self.unpack('H2'*self.bytesize) .map {|l| l = "\x00" + [l].pack('H2')} .join('') end
Return a duplicate of the string with a character on each side.
# File lib/aastdlib/string.rb, line 166 def wrap(char) return char+self+char end