forked from SecGen/SecGen
-
Notifications
You must be signed in to change notification settings - Fork 328
Expand file tree
/
Copy pathlocal_string_generator.rb
More file actions
145 lines (120 loc) · 3.09 KB
/
local_string_generator.rb
File metadata and controls
145 lines (120 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
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
138
139
140
141
142
143
144
require 'getoptlong'
require_relative '../helpers/constants'
require 'base64'
# Inherited by local string generators
# stdout used to return value
# use Print.local to print status messages (formatted to stdout)
# A nice side-effect is that each of these modules is also an executable script
class StringGenerator
require_relative '../helpers/print.rb'
attr_accessor :module_name
attr_accessor :has_base64_inputs
attr_accessor :outputs
# override this
def initialize
# default values
self.module_name = 'Null generator'
self.has_base64_inputs = false
self.outputs = []
end
# override this
def generate
self.outputs << ''
end
def read_arguments
# Get command line arguments
Print.local 'Reading args from STDIN'
if ARGV.size == 0
begin
args_array = []
ARGF.each do |arg|
arg.strip.split(' ').each do |split|
args_array << split
end
end
ARGV.unshift(*args_array)
rescue
# Do nothing...
end
end
opts = get_options
# process option arguments
opts.each do |opt, arg|
# Check if base64 decoding is required and set instance variable
if opt == '--b64'
self.has_base64_inputs = true
end
# Decode if required
argument = self.has_base64_inputs ? Base64.strict_decode64(arg) : arg
process_options(opt, argument)
end
end
# Override this when using read_fact's in your module
def get_options
GetoptLong.new(*get_options_array)
end
def get_options_array
[['--help', '-h', GetoptLong::NO_ARGUMENT],
['--b64', GetoptLong::OPTIONAL_ARGUMENT]]
end
# Override this when using read_fact's in your module. Always call super first
def process_options(opt, arg)
unless option_is_valid(opt)
Print.err "Argument not valid: #{arg}"
usage
exit
end
case opt
when '--help'
usage
when '--b64'
# do nothing
end
end
def usage
Print.err "Usage:
#{$0} [--options]
OPTIONS:
--strings_to_encode [string]
"
exit
end
def run
Print.local module_name
read_arguments
Print.local_verbose "Generating..."
generate
# print the first 1000 chars to screen
output = self.outputs.to_s
length = output.length
if length < 1000
Print.local_verbose "Generated: #{output}..."
else
Print.local_verbose "Generated: #{output.to_s[0..1000]}..."
Print.local_verbose "(Displaying 1000/#{length} length output)"
end
enforce_utf8(self.outputs)
print_outputs
end
def enforce_utf8(values)
values.map { |o| o.force_encoding('UTF-8') }
end
def print_outputs
puts base64_encode_outputs
end
def base64_encode_outputs
self.outputs.map { |o| Base64.strict_encode64 o }
end
def option_is_valid(opt_to_check)
arg_validity = false
valid_arguments = get_options_array
valid_arguments.each{ |valid_arg_array|
valid_arg_array.each_with_index { |valid_arg|
if valid_arg == opt_to_check
arg_validity = true
end
}
}
arg_validity
end
end