Class: String
Overview
Extension of the String class.
Constant Summary collapse
- HEX_VALUES =
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" + "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" + "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f" + "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f" + "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f" + "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f" + "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f" + "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f" + "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f" + "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f" + "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf" + "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf" + "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf" + "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf" + "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef" + "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"
Class Method Summary collapse
-
.generate_all_hex_values(upcase = true) ⇒ Object
generate all hex values and return them in a string.
Instance Method Summary collapse
-
#bin_to_array(upcase: true, radix: 16) ⇒ Object
Return the string as an array of base 16 values.
-
#borderize(char = '-') ⇒ Object
return the string with a border at the top and bottom of the string border character can be altered.
-
#decode64(strict = false) ⇒ Object
Base64 decde and return a duplicate string.
-
#obfuscate_case ⇒ Object
Return a duplicate of the string with the case altered randomly.
-
#obfuscate_string_insert(string) ⇒ Object
Inser string at a random place in the current string.
-
#prefix(symbol: '+', wrapper: ["[","]"], space_char: " ", space_rep: 1, ind_char: "", ind_rep: 1) ⇒ Object
Prefix the string with a symbol wrapped in two characters.
-
#prettyfi(max_len = 60) ⇒ Object
trim the string to a particular length and echo it to stdout as a script ready string.
-
#prettyfi_shellcode(prefix: '\x', limit: 16) ⇒ Object
same thing as prettyfi but converts contents of the string to x format.
-
#rando_insert(str) ⇒ Object
Insert a string at a random index within this string.
-
#simple_prefix(char) ⇒ Object
Returna a duplicate of the string with a character prefix.
-
#snake_name ⇒ Object
Convenience method for to_underscore().
-
#suffix(char) ⇒ Object
Return a duplicate of the string suffixed with a single character.
-
#to_base64(strict = false) ⇒ Object
Base64 encode and return a duplicate string.
-
#to_binary(prefix = '0b') ⇒ Object
Encode and return a duplicate of the string in 0b0010110 format.
-
#to_csv_value(comma_delimit = true) ⇒ Object
Convert the string to a CSV value.
-
#to_hex_string(literal: true, prefix: '\x') ⇒ Object
Returns a duplicate of the string unpacked in x literal form.
-
#to_instance_variable_name(snake_case = true, symbolize = true) ⇒ Object
Return a duplicate of the string prefixed with @ converted to a symbol.
-
#to_ivar_name(snake_case = true, symbolize = true) ⇒ Object
Convenience method for to_instance_variable_name().
-
#to_underscore ⇒ Object
- Return the string will all
-
and capital letters converted to underscores.
-
#to_url_encoded ⇒ Object
Poor mans URL encoding: each character is unpacked to it’s hex form and then suffixed with %.
-
#to_utf16(endianness = 'le') ⇒ Object
Return a duplicate string in literal from where each char is converted to utf16 literal format.
-
#to_utf8(prefix = '\u') ⇒ Object
Return a duplicate string in literal form with each character suffixed with u.
-
#utf_pad ⇒ Object
Pads each binary component in the string with x00.
-
#wrap(char) ⇒ Object
Return a duplicate of the string with a character on each side.
Class Method Details
.generate_all_hex_values(upcase = true) ⇒ Object
generate all hex values and return them in a string
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# 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 |
Instance Method Details
#bin_to_array(upcase: true, radix: 16) ⇒ Object
Return the string as an array of base 16 values. Base can be altered by updating the radix argument.
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# 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 |
#borderize(char = '-') ⇒ Object
return the string with a border at the top and bottom of the string border character can be altered
59 60 61 62 63 64 |
# File 'lib/aastdlib/string.rb', line 59 def borderize(char='-') border = char * self.length return "#{border}\n#{self}\n#{border}" end |
#decode64(strict = false) ⇒ Object
Base64 decde and return a duplicate string.
329 330 331 332 333 334 335 |
# 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 |
#obfuscate_case ⇒ Object
Return a duplicate of the string with the case altered randomly.
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# 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 |
#obfuscate_string_insert(string) ⇒ Object
Inser string at a random place in the current string.
371 372 373 374 375 |
# File 'lib/aastdlib/string.rb', line 371 def obfuscate_string_insert(string) return self.insert(rand(0..(self.length - 1)), string) end |
#prefix(symbol: '+', wrapper: ["[","]"], space_char: " ", space_rep: 1, ind_char: "", ind_rep: 1) ⇒ Object
Prefix the string with a symbol wrapped in two characters.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# 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 |
#prettyfi(max_len = 60) ⇒ Object
trim the string to a particular length and echo it to stdout as a script ready string
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# 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 |
#prettyfi_shellcode(prefix: '\x', limit: 16) ⇒ Object
same thing as prettyfi but converts contents of the string to x format
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 138 139 140 |
# 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 |
#rando_insert(str) ⇒ Object
Insert a string at a random index within this string
45 46 47 48 49 50 51 52 53 54 55 |
# 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 |
#simple_prefix(char) ⇒ Object
Returna a duplicate of the string with a character prefix.
159 160 161 162 163 |
# File 'lib/aastdlib/string.rb', line 159 def simple_prefix(char) return char+self end |
#snake_name ⇒ Object
Convenience method for to_underscore()
201 202 203 204 205 |
# File 'lib/aastdlib/string.rb', line 201 def snake_name() return self.to_underscore end |
#suffix(char) ⇒ Object
Return a duplicate of the string suffixed with a single character.
173 174 175 176 177 |
# File 'lib/aastdlib/string.rb', line 173 def suffix(char) return self+char end |
#to_base64(strict = false) ⇒ Object
Base64 encode and return a duplicate string.
320 321 322 323 324 325 326 |
# 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 |
#to_binary(prefix = '0b') ⇒ Object
Encode and return a duplicate of the string in 0b0010110 format.
338 339 340 341 342 343 344 345 346 347 348 349 |
# 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 |
#to_csv_value(comma_delimit = true) ⇒ Object
Convert the string to a CSV value. Setting comma delimit to false will return the string wrapped in double quotes.
181 182 183 184 185 |
# File 'lib/aastdlib/string.rb', line 181 def to_csv_value(comma_delimit=true) return comma_delimit ? self.wrap('"').suffix(',') : self.wrap('"') end |
#to_hex_string(literal: true, prefix: '\x') ⇒ Object
Returns a duplicate of the string unpacked in x literal form. Useful when dynamically generating shellcode.
260 261 262 263 264 265 266 267 268 269 270 271 272 |
# 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 |
#to_instance_variable_name(snake_case = true, symbolize = true) ⇒ Object
Return a duplicate of the string prefixed with @ converted to a symbol.
208 209 210 211 212 213 214 215 216 217 |
# 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 |
#to_ivar_name(snake_case = true, symbolize = true) ⇒ Object
Convenience method for to_instance_variable_name()
220 221 222 223 224 |
# File 'lib/aastdlib/string.rb', line 220 def to_ivar_name(snake_case=true, symbolize=true) to_instance_variable_name(snake_case, symbolize) end |
#to_underscore ⇒ Object
- Return the string will all
-
and capital letters converted to
underscores.
189 190 191 192 193 194 195 196 197 198 |
# 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 |
#to_url_encoded ⇒ Object
Poor mans URL encoding: each character is unpacked to it’s hex form and then suffixed with %.
315 316 317 |
# File 'lib/aastdlib/string.rb', line 315 def to_url_encoded() return '%' + self.bin_to_array.join('%') end |
#to_utf16(endianness = 'le') ⇒ Object
Return a duplicate string in literal from where each char is converted to utf16 literal format. Endianness can be altered via the endianness argument.
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# 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 |
#to_utf8(prefix = '\u') ⇒ Object
Return a duplicate string in literal form with each character suffixed with u
276 277 278 279 280 |
# File 'lib/aastdlib/string.rb', line 276 def to_utf8(prefix='\u') return prefix + self.bin_to_array.join(prefix) end |
#utf_pad ⇒ Object
Pads each binary component in the string with x00. A duplicate of the string is modified and returned.
228 229 230 231 232 233 234 |
# 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 |
#wrap(char) ⇒ Object
Return a duplicate of the string with a character on each side.
166 167 168 169 170 |
# File 'lib/aastdlib/string.rb', line 166 def wrap(char) return char+self+char end |