aastdlib 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +6 -0
- data/aastdlib.gemspec +37 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/doc/Aastdlib.html +110 -0
- data/doc/Array.html +164 -0
- data/doc/Dir.html +96 -0
- data/doc/Gemfile.html +95 -0
- data/doc/Hash.html +197 -0
- data/doc/Integer.html +162 -0
- data/doc/LICENSE_txt.html +112 -0
- data/doc/Object.html +501 -0
- data/doc/README_md.html +155 -0
- data/doc/Rakefile.html +96 -0
- data/doc/String.html +1240 -0
- data/doc/Symbol.html +197 -0
- data/doc/aastdlib_gemspec.html +127 -0
- data/doc/bin/setup.html +96 -0
- data/doc/created.rid +19 -0
- data/doc/css/fonts.css +167 -0
- data/doc/css/rdoc.css +590 -0
- data/doc/fonts/Lato-Light.ttf +0 -0
- data/doc/fonts/Lato-LightItalic.ttf +0 -0
- data/doc/fonts/Lato-Regular.ttf +0 -0
- data/doc/fonts/Lato-RegularItalic.ttf +0 -0
- data/doc/fonts/SourceCodePro-Bold.ttf +0 -0
- data/doc/fonts/SourceCodePro-Regular.ttf +0 -0
- data/doc/images/add.png +0 -0
- data/doc/images/arrow_up.png +0 -0
- data/doc/images/brick.png +0 -0
- data/doc/images/brick_link.png +0 -0
- data/doc/images/bug.png +0 -0
- data/doc/images/bullet_black.png +0 -0
- data/doc/images/bullet_toggle_minus.png +0 -0
- data/doc/images/bullet_toggle_plus.png +0 -0
- data/doc/images/date.png +0 -0
- data/doc/images/delete.png +0 -0
- data/doc/images/find.png +0 -0
- data/doc/images/loadingAnimation.gif +0 -0
- data/doc/images/macFFBgHack.png +0 -0
- data/doc/images/package.png +0 -0
- data/doc/images/page_green.png +0 -0
- data/doc/images/page_white_text.png +0 -0
- data/doc/images/page_white_width.png +0 -0
- data/doc/images/plugin.png +0 -0
- data/doc/images/ruby.png +0 -0
- data/doc/images/tag_blue.png +0 -0
- data/doc/images/tag_green.png +0 -0
- data/doc/images/transparent.png +0 -0
- data/doc/images/wrench.png +0 -0
- data/doc/images/wrench_orange.png +0 -0
- data/doc/images/zoom.png +0 -0
- data/doc/index.html +114 -0
- data/doc/js/darkfish.js +161 -0
- data/doc/js/jquery.js +4 -0
- data/doc/js/navigation.js +142 -0
- data/doc/js/navigation.js.gz +0 -0
- data/doc/js/search.js +109 -0
- data/doc/js/search_index.js +1 -0
- data/doc/js/search_index.js.gz +0 -0
- data/doc/js/searcher.js +229 -0
- data/doc/js/searcher.js.gz +0 -0
- data/doc/table_of_contents.html +282 -0
- data/lib/aastdlib.rb +12 -0
- data/lib/aastdlib/array.rb +20 -0
- data/lib/aastdlib/file.rb +23 -0
- data/lib/aastdlib/hash.rb +22 -0
- data/lib/aastdlib/integer.rb +16 -0
- data/lib/aastdlib/object.rb +200 -0
- data/lib/aastdlib/string.rb +377 -0
- data/lib/aastdlib/symbol.rb +19 -0
- data/lib/aastdlib/version.rb +3 -0
- data/pkg/aastdlib-0.0.0.gem +0 -0
- data/pkg/aastdlib-0.0.1.gem +0 -0
- data/pkg/aastdlib-0.0.2.gem +0 -0
- metadata +179 -0
data/lib/aastdlib.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "terminal-table"
|
2
|
+
require "aastdlib/version"
|
3
|
+
|
4
|
+
path = __FILE__.split('/')
|
5
|
+
path = path[0..(path.count - 2)].join('/')
|
6
|
+
path += '/aastdlib/'
|
7
|
+
|
8
|
+
Dir::entries(path).keep_if {|e| e =~ /\.rb$/}.each { |f| require(path+f) }
|
9
|
+
|
10
|
+
module Aastdlib
|
11
|
+
# Your code goes here...
|
12
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class Array
|
2
|
+
|
3
|
+
# Accepts a value that is check against each element
|
4
|
+
# in the array itself and returns a new array containing
|
5
|
+
# the index for each matching element.
|
6
|
+
def indices(value)
|
7
|
+
counter = 0
|
8
|
+
indices = []
|
9
|
+
|
10
|
+
self.each do |v|
|
11
|
+
indices << counter if value == v
|
12
|
+
counter+=1
|
13
|
+
end
|
14
|
+
|
15
|
+
return indices
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class File
|
2
|
+
|
3
|
+
def self.check_and_open(filename, mode="r+")
|
4
|
+
|
5
|
+
raise "Error: File doesn't exist (#{filename})" if !File::exist?(filename)
|
6
|
+
|
7
|
+
file = File::open(filename, mode)
|
8
|
+
|
9
|
+
if block_given?
|
10
|
+
|
11
|
+
yield(file)
|
12
|
+
file.close
|
13
|
+
return
|
14
|
+
|
15
|
+
else
|
16
|
+
|
17
|
+
return file
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class Hash
|
2
|
+
|
3
|
+
# Return a copy of the hash with each key converted
|
4
|
+
# to a string.
|
5
|
+
def stringify_keys()
|
6
|
+
|
7
|
+
stringified = {}
|
8
|
+
self.each {|k,v| stringified[k.to_s] = v}
|
9
|
+
return stringified
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
# _Hash is modified inline!_ Converts each key to a string.
|
14
|
+
def stringify_keys!()
|
15
|
+
|
16
|
+
stringified = self.stringify_keys()
|
17
|
+
self.clear
|
18
|
+
stringified.each {|k,v| self[k] = v}
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class Integer
|
2
|
+
|
3
|
+
# Return a copy of the integer in base 16. The
|
4
|
+
# pack argument determines whether or not the value
|
5
|
+
# should be packed to binary form using Array::pack().
|
6
|
+
def to_hex_string(pack=false)
|
7
|
+
|
8
|
+
out = self.to_s(16)
|
9
|
+
out = ("0"+out) if out.length == 1
|
10
|
+
out = [out].pack('H*') if pack
|
11
|
+
|
12
|
+
return out
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -0,0 +1,200 @@
|
|
1
|
+
class Object
|
2
|
+
|
3
|
+
# Declare and define an instance variable for a given object.
|
4
|
+
# Name is the instance variable name and value is the item assigned
|
5
|
+
# to that instance variable. Convenience determines whether getter/setter
|
6
|
+
# methods are defined as well.
|
7
|
+
def declare_and_define_ivar(name:, value:, convenience: true)
|
8
|
+
|
9
|
+
if name =~ /^@/
|
10
|
+
|
11
|
+
iv_handle = name
|
12
|
+
name = name.to_s.gsub('@','')
|
13
|
+
|
14
|
+
else
|
15
|
+
|
16
|
+
iv_handle = ('@'+name)
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
iv_handle = iv_handle.to_sym if iv_handle.respond_to?(:to_sym)
|
21
|
+
|
22
|
+
self.instance_variable_set(iv_handle, value)
|
23
|
+
|
24
|
+
if convenience
|
25
|
+
|
26
|
+
# getter
|
27
|
+
self.define_singleton_method(name) do
|
28
|
+
self.instance_variable_get(iv_handle)
|
29
|
+
end
|
30
|
+
|
31
|
+
# setter
|
32
|
+
self.define_singleton_method(name+'=') do |value|
|
33
|
+
self.instance_variable_set(iv_handle, value)
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
# Convenience method for declare_and_define_ivar()
|
41
|
+
def define_ivar(name:, value:, convenience: true)
|
42
|
+
|
43
|
+
self.declare_and_define_ivar(name: name, value: value)
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
# Using terminal-table, format all instance variables in a
|
48
|
+
# visually appealing text-based table.
|
49
|
+
def instance_variable_table()
|
50
|
+
|
51
|
+
rows = []
|
52
|
+
table = Terminal::Table.new()
|
53
|
+
table.headings = ['Instance Variable', 'Object']
|
54
|
+
table.title = "#{self.class}: Instance Vairables"
|
55
|
+
|
56
|
+
self.instance_variables.each do |iv|
|
57
|
+
|
58
|
+
rows << [iv.to_s,self.instance_variable_get(iv)]
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
table.rows = rows
|
63
|
+
|
64
|
+
return table
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
# Inspect the object all pretty like.
|
69
|
+
def pretty_inspect()
|
70
|
+
|
71
|
+
tab_count = 0
|
72
|
+
|
73
|
+
out = self.inspect.gsub("@","\n\t@")
|
74
|
+
.lines
|
75
|
+
|
76
|
+
expanded = ""
|
77
|
+
|
78
|
+
counter = out.length
|
79
|
+
(0..counter-1).each do |n|
|
80
|
+
|
81
|
+
line = out[n]
|
82
|
+
|
83
|
+
expanded += ("\t"*tab_count) + line
|
84
|
+
|
85
|
+
tab_count += 1 if line =~ /\=\#\</
|
86
|
+
|
87
|
+
if line =~ /\>\,/ and tab_count != 0
|
88
|
+
|
89
|
+
tab_count -= 1
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
return expanded
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
# Pretty inspect and print it to stdout.
|
100
|
+
def pi(obj)
|
101
|
+
|
102
|
+
puts
|
103
|
+
puts obj.pretty_inspect
|
104
|
+
puts
|
105
|
+
|
106
|
+
end
|
107
|
+
|
108
|
+
# Using terminal-table, print a visually appealing table detailing
|
109
|
+
# methods offered by the object. Valid arguments to _type_ include:
|
110
|
+
# - private
|
111
|
+
# - public
|
112
|
+
# - protected
|
113
|
+
def method_table(type=nil)
|
114
|
+
|
115
|
+
mlists = {
|
116
|
+
methods: [],
|
117
|
+
private: [],
|
118
|
+
protected: [],
|
119
|
+
public: []
|
120
|
+
}
|
121
|
+
|
122
|
+
if type
|
123
|
+
|
124
|
+
case type
|
125
|
+
|
126
|
+
when :private
|
127
|
+
|
128
|
+
self.private_methods.sort.each {|m| mlists[:private] << m}
|
129
|
+
|
130
|
+
when :protected
|
131
|
+
|
132
|
+
self.protected_methods.sort.each {|m| mlists[:protected] << m}
|
133
|
+
|
134
|
+
when :public
|
135
|
+
|
136
|
+
self.public_methods.sort.each {|m| mlists[:public] << m}
|
137
|
+
|
138
|
+
end
|
139
|
+
|
140
|
+
else
|
141
|
+
|
142
|
+
self.methods.sort.each {|m| mlists[:methods] << m} unless type
|
143
|
+
self.private_methods.sort.each {|m| mlists[:private] << m}
|
144
|
+
self.protected_methods.sort.each {|m| mlists[:protected] << m}
|
145
|
+
self.public_methods.sort.each {|m| mlists[:public] << m}
|
146
|
+
|
147
|
+
end
|
148
|
+
|
149
|
+
mlists.delete_if {|k,v| v == []}
|
150
|
+
|
151
|
+
if mlists.count < 1
|
152
|
+
|
153
|
+
if type
|
154
|
+
|
155
|
+
puts "[+] No methods of #{type} type available!"
|
156
|
+
|
157
|
+
else
|
158
|
+
|
159
|
+
puts "[+] No methods available!"
|
160
|
+
|
161
|
+
end
|
162
|
+
|
163
|
+
return
|
164
|
+
|
165
|
+
end
|
166
|
+
|
167
|
+
lengths = []
|
168
|
+
headings = ['#']
|
169
|
+
|
170
|
+
mlists.each do |k,v|
|
171
|
+
headings << k.to_s.gsub('_',' ').capitalize
|
172
|
+
lengths << v.length
|
173
|
+
end
|
174
|
+
|
175
|
+
max_len = lengths.uniq.sort.last
|
176
|
+
|
177
|
+
table = Terminal::Table.new()
|
178
|
+
table.headings = headings
|
179
|
+
table.title = "Methods: #{self.class}"
|
180
|
+
|
181
|
+
rows = []
|
182
|
+
|
183
|
+
max_len.times do |n|
|
184
|
+
|
185
|
+
row = []
|
186
|
+
|
187
|
+
row << n
|
188
|
+
mlists.each { |k,v| row << mlists[k][n] }
|
189
|
+
|
190
|
+
rows << row
|
191
|
+
|
192
|
+
end
|
193
|
+
|
194
|
+
table.rows = rows
|
195
|
+
|
196
|
+
return table
|
197
|
+
|
198
|
+
end
|
199
|
+
|
200
|
+
end
|
@@ -0,0 +1,377 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
# encoding: ASCII-8BIT
|
3
|
+
|
4
|
+
# Extension of the String class.
|
5
|
+
class String
|
6
|
+
|
7
|
+
require 'base64'
|
8
|
+
|
9
|
+
HEX_VALUES = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" +
|
10
|
+
"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" +
|
11
|
+
"\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f" +
|
12
|
+
"\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f" +
|
13
|
+
"\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f" +
|
14
|
+
"\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f" +
|
15
|
+
"\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f" +
|
16
|
+
"\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f" +
|
17
|
+
"\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f" +
|
18
|
+
"\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f" +
|
19
|
+
"\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf" +
|
20
|
+
"\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf" +
|
21
|
+
"\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf" +
|
22
|
+
"\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf" +
|
23
|
+
"\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef" +
|
24
|
+
"\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"
|
25
|
+
|
26
|
+
# generate all hex values and return them in a string
|
27
|
+
def self.generate_all_hex_values(upcase=true)
|
28
|
+
|
29
|
+
s = ""
|
30
|
+
|
31
|
+
256.times do |n|
|
32
|
+
|
33
|
+
n = n.to_s(16)
|
34
|
+
n = n.upcase if upcase
|
35
|
+
n = '0' + n if n.length < 2
|
36
|
+
s += '\x' + n
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
return s
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
# Insert a string at a random index within this string
|
45
|
+
def rando_insert(str)
|
46
|
+
|
47
|
+
unless self.length > 1
|
48
|
+
|
49
|
+
raise "String length is too short! length must be 2 or greater (len >= 2)"
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
return self.dup.insert(rand(0..(self.length-1)), str)
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
# return the string with a border at the top and bottom of the string
|
58
|
+
# border character can be altered
|
59
|
+
def borderize(char='-')
|
60
|
+
|
61
|
+
border = char * self.length
|
62
|
+
return "#{border}\n#{self}\n#{border}"
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
# trim the string to a particular length and echo it to stdout as a
|
67
|
+
# script ready string
|
68
|
+
def prettyfi(max_len=60)
|
69
|
+
|
70
|
+
if self.length < max_len
|
71
|
+
|
72
|
+
out = self
|
73
|
+
|
74
|
+
else
|
75
|
+
|
76
|
+
# holds output fragments
|
77
|
+
buff = []
|
78
|
+
|
79
|
+
start, _end = 0, (max_len-1)
|
80
|
+
|
81
|
+
ind,counter = -1, 0
|
82
|
+
|
83
|
+
chunk = ""
|
84
|
+
while c = self[ind+=1]
|
85
|
+
|
86
|
+
counter += 1
|
87
|
+
chunk += c
|
88
|
+
|
89
|
+
if counter == (max_len) or ind == (self.length-1)
|
90
|
+
|
91
|
+
buff << '"' + chunk + '"'
|
92
|
+
chunk = ""
|
93
|
+
counter = 0
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
out = buff.join(" +\n")
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
return out
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
# same thing as prettyfi but converts contents of the string
|
108
|
+
# to \x format
|
109
|
+
def prettyfi_shellcode(prefix: '\x', limit: 16)
|
110
|
+
|
111
|
+
ary = self.bin_to_array
|
112
|
+
|
113
|
+
output = ""
|
114
|
+
|
115
|
+
# Maximum index position of split array
|
116
|
+
max = (ary.length - 1)
|
117
|
+
|
118
|
+
# Bottom of slice range
|
119
|
+
floor = 0
|
120
|
+
|
121
|
+
# Top of slice range
|
122
|
+
ceiling = (limit-1)
|
123
|
+
|
124
|
+
while true
|
125
|
+
|
126
|
+
slice = prefix + ary.slice(floor..ceiling).join(prefix)
|
127
|
+
|
128
|
+
output += '"' + slice + '"'
|
129
|
+
|
130
|
+
break if ceiling >= max
|
131
|
+
|
132
|
+
floor += limit
|
133
|
+
ceiling += limit
|
134
|
+
output += "+\n"
|
135
|
+
|
136
|
+
end
|
137
|
+
|
138
|
+
return output
|
139
|
+
|
140
|
+
end
|
141
|
+
|
142
|
+
# Prefix the string with a symbol wrapped in two characters.
|
143
|
+
def prefix(symbol: '+', wrapper: ["[","]"], space_char: " ", space_rep: 1, ind_char: "", ind_rep: 1)
|
144
|
+
|
145
|
+
# build the indention string
|
146
|
+
indention = ind_char * ind_rep
|
147
|
+
|
148
|
+
# build the spacer string
|
149
|
+
spacer = space_char * space_rep
|
150
|
+
|
151
|
+
# build the symbol string
|
152
|
+
symbol = indention + wrapper.insert(1,symbol).join('') + spacer
|
153
|
+
|
154
|
+
return symbol + self
|
155
|
+
|
156
|
+
end
|
157
|
+
|
158
|
+
# Returna a duplicate of the string with a character prefix.
|
159
|
+
def simple_prefix(char)
|
160
|
+
|
161
|
+
return char+self
|
162
|
+
|
163
|
+
end
|
164
|
+
|
165
|
+
# Return a duplicate of the string with a character on each side.
|
166
|
+
def wrap(char)
|
167
|
+
|
168
|
+
return char+self+char
|
169
|
+
|
170
|
+
end
|
171
|
+
|
172
|
+
# Return a duplicate of the string suffixed with a single character.
|
173
|
+
def suffix(char)
|
174
|
+
|
175
|
+
return self+char
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
# Convert the string to a CSV value. Setting comma delimit to false
|
180
|
+
# will return the string wrapped in double quotes.
|
181
|
+
def to_csv_value(comma_delimit=true)
|
182
|
+
|
183
|
+
return comma_delimit ? self.wrap('"').suffix(',') : self.wrap('"')
|
184
|
+
|
185
|
+
end
|
186
|
+
|
187
|
+
# Return the string will all :: and capital letters converted to
|
188
|
+
# underscores.
|
189
|
+
def to_underscore()
|
190
|
+
|
191
|
+
# sauce: https://stackoverflow.com/questions/1509915/converting-camel-case-to-underscore-case-in-ruby
|
192
|
+
return self.gsub(/::/, '/')
|
193
|
+
.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
|
194
|
+
.gsub(/([a-z\d])([A-Z])/,'\1_\2')
|
195
|
+
.tr("-", "_")
|
196
|
+
.downcase
|
197
|
+
|
198
|
+
end
|
199
|
+
|
200
|
+
# Convenience method for to_underscore()
|
201
|
+
def snake_name()
|
202
|
+
|
203
|
+
return self.to_underscore
|
204
|
+
|
205
|
+
end
|
206
|
+
|
207
|
+
# Return a duplicate of the string prefixed with @ converted to a symbol.
|
208
|
+
def to_instance_variable_name(snake_case=true, symbolize=true)
|
209
|
+
|
210
|
+
name = self.to_s
|
211
|
+
name = name.to_underscore if snake_case
|
212
|
+
name = ('@' + name) if name !~ /^@/
|
213
|
+
name = name.to_sym if symbolize
|
214
|
+
|
215
|
+
return name
|
216
|
+
|
217
|
+
end
|
218
|
+
|
219
|
+
# Convenience method for to_instance_variable_name()
|
220
|
+
def to_ivar_name(snake_case=true, symbolize=true)
|
221
|
+
|
222
|
+
to_instance_variable_name(snake_case, symbolize)
|
223
|
+
|
224
|
+
end
|
225
|
+
|
226
|
+
# Pads each binary component in the string with \x00. A duplicate
|
227
|
+
# of the string is modified and returned.
|
228
|
+
def utf_pad()
|
229
|
+
|
230
|
+
return self.unpack('H2'*self.bytesize)
|
231
|
+
.map {|l| l = "\x00" + [l].pack('H2')}
|
232
|
+
.join('')
|
233
|
+
|
234
|
+
end
|
235
|
+
|
236
|
+
# Return the string as an array of base 16 values. Base can be altered by
|
237
|
+
# updating the _radix_ argument.
|
238
|
+
def bin_to_array(upcase: true, radix: 16)
|
239
|
+
|
240
|
+
return self.bytes.map do |b|
|
241
|
+
|
242
|
+
if upcase
|
243
|
+
b = b.to_s(radix).upcase
|
244
|
+
else
|
245
|
+
b = b.to_s(radix)
|
246
|
+
end
|
247
|
+
|
248
|
+
if b.length < 2
|
249
|
+
b = '0' + b
|
250
|
+
else
|
251
|
+
b
|
252
|
+
end
|
253
|
+
|
254
|
+
end
|
255
|
+
|
256
|
+
end
|
257
|
+
|
258
|
+
# Returns a duplicate of the string unpacked in \x literal form. Useful
|
259
|
+
# when dynamically generating shellcode.
|
260
|
+
def to_hex_string(literal: true, prefix: '\x')
|
261
|
+
|
262
|
+
out = self.bin_to_array()
|
263
|
+
|
264
|
+
if literal
|
265
|
+
out = prefix + out.join(prefix)
|
266
|
+
else
|
267
|
+
out = out.map! {|e| [e].pack("H2") }.join('')
|
268
|
+
end
|
269
|
+
|
270
|
+
return out
|
271
|
+
|
272
|
+
end
|
273
|
+
|
274
|
+
# Return a duplicate string in literal form with each character
|
275
|
+
# suffixed with \u
|
276
|
+
def to_utf8(prefix='\u')
|
277
|
+
|
278
|
+
return prefix + self.bin_to_array.join(prefix)
|
279
|
+
|
280
|
+
end
|
281
|
+
|
282
|
+
# Return a duplicate string in literal from where each char is
|
283
|
+
# converted to utf16 literal format. Endianness can be altered
|
284
|
+
# via the _endianness_ argument.
|
285
|
+
def to_utf16(endianness='le')
|
286
|
+
|
287
|
+
if endianness !~ /^(le|be)$/i
|
288
|
+
|
289
|
+
raise "Error: valid arguments to endianness parameter: le or be"
|
290
|
+
|
291
|
+
end
|
292
|
+
|
293
|
+
pad = '00'
|
294
|
+
prefix = '\u'
|
295
|
+
|
296
|
+
# get a copy of the string in hex form
|
297
|
+
# and pad each byte
|
298
|
+
buff = prefix + self.bin_to_array
|
299
|
+
.map do |b|
|
300
|
+
|
301
|
+
if endianness =~ /^le$/i
|
302
|
+
(b = b + pad)
|
303
|
+
else
|
304
|
+
(b = pad + b)
|
305
|
+
end
|
306
|
+
|
307
|
+
end.join(prefix)
|
308
|
+
|
309
|
+
return buff
|
310
|
+
|
311
|
+
end
|
312
|
+
|
313
|
+
# Poor mans URL encoding: each character is unpacked to
|
314
|
+
# it's hex form and then suffixed with %.
|
315
|
+
def to_url_encoded()
|
316
|
+
return '%' + self.bin_to_array.join('%')
|
317
|
+
end
|
318
|
+
|
319
|
+
# Base64 encode and return a duplicate string.
|
320
|
+
def to_base64(strict=false)
|
321
|
+
if strict
|
322
|
+
return Base64::strict_encode64(self)
|
323
|
+
else
|
324
|
+
return Base64::encode64(self)
|
325
|
+
end
|
326
|
+
end
|
327
|
+
|
328
|
+
# Base64 decde and return a duplicate string.
|
329
|
+
def decode64(strict=false)
|
330
|
+
if strict
|
331
|
+
return Base64::strict_decode64(self)
|
332
|
+
else
|
333
|
+
return Base64::decode64(self)
|
334
|
+
end
|
335
|
+
end
|
336
|
+
|
337
|
+
# Encode and return a duplicate of the string in _0b0010110_ format.
|
338
|
+
def to_binary(prefix='0b')
|
339
|
+
|
340
|
+
return prefix+self.bytes
|
341
|
+
.map do |b|
|
342
|
+
|
343
|
+
b = b.to_s(2)
|
344
|
+
b = '0' * (8 - b.length) + b
|
345
|
+
|
346
|
+
end
|
347
|
+
.join(prefix)
|
348
|
+
|
349
|
+
end
|
350
|
+
|
351
|
+
# Return a duplicate of the string with the case altered randomly.
|
352
|
+
def obfuscate_case()
|
353
|
+
|
354
|
+
return self.split('').map do |c|
|
355
|
+
|
356
|
+
if rand(0..1) > 0
|
357
|
+
|
358
|
+
c = c.upcase
|
359
|
+
|
360
|
+
else
|
361
|
+
|
362
|
+
c = c.downcase
|
363
|
+
|
364
|
+
end
|
365
|
+
|
366
|
+
end.join('')
|
367
|
+
|
368
|
+
end
|
369
|
+
|
370
|
+
# Inser _string_ at a random place in the current string.
|
371
|
+
def obfuscate_string_insert(string)
|
372
|
+
|
373
|
+
return self.insert(rand(0..(self.length - 1)), string)
|
374
|
+
|
375
|
+
end
|
376
|
+
|
377
|
+
end
|