-
Notifications
You must be signed in to change notification settings - Fork 46
/
noir.cr
224 lines (199 loc) · 5.83 KB
/
noir.cr
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
require "../detector/detector.cr"
require "../analyzer/analyzer.cr"
require "../deliver/*"
require "../output_builder/*"
require "./endpoint.cr"
require "./logger.cr"
require "../utils/string_extension.cr"
require "json"
class NoirRunner
@options : Hash(Symbol, String)
@techs : Array(String)
@endpoints : Array(Endpoint)
@logger : NoirLogger
@send_proxy : String
@send_req : String
@send_es : String
@is_debug : Bool
@is_color : Bool
@is_log : Bool
@concurrency : Int32
@config_file : String
macro define_getter_methods(names)
{% for name, index in names %}
def {{name.id}}
@{{name.id}}
end
{% end %}
end
define_getter_methods [options, techs, endpoints, logger]
def initialize(options)
@options = options
@config_file = @options[:config_file]
if @config_file != ""
config = YAML.parse(File.read(@config_file))
@options.each do |key, _|
string_key = key.to_s
begin
if config[string_key] != "" && string_key != "base"
@options[key] = "yes" if config[string_key] == true
@options[key] = "no" if config[string_key] == false
@options[key] = config[string_key].as_s
end
rescue
end
end
end
@techs = [] of String
@endpoints = [] of Endpoint
@send_proxy = @options[:send_proxy]
@send_req = @options[:send_req]
@send_es = @options[:send_es]
@is_debug = str_to_bool(@options[:debug])
@is_color = str_to_bool(@options[:color])
@is_log = str_to_bool(@options[:nolog])
@concurrency = @options[:concurrency].to_i
@logger = NoirLogger.new @is_debug, @is_color, @is_log
if @options[:techs].size > 0
techs_tmp = @options[:techs].split(",")
@logger.info "Setting #{techs_tmp.size} techs from command line."
techs_tmp.each do |tech|
@techs << NoirTechs.similar_to_tech(tech)
@logger.debug "Added #{tech} to techs."
end
end
end
def run
puts @techs
end
def detect
detected_techs = detect_techs options[:base], options, @logger
@techs += detected_techs
if @is_debug
@logger.debug("CodeLocator Table:")
locator = CodeLocator.instance
locator.show_table
end
end
def analyze
@endpoints = analysis_endpoints options, @techs, @logger
optimize_endpoints
combine_url_and_endpoints
deliver
end
def optimize_endpoints
@logger.system "Optimizing endpoints."
final = [] of Endpoint
@endpoints.each do |endpoint|
tiny_tmp = endpoint
if endpoint.params.size > 0
tiny_tmp.params = [] of Param
endpoint.params.each do |param|
if !param.name.includes? " "
if @options[:set_pvalue] != ""
param.value = @options[:set_pvalue]
end
tiny_tmp.params << param
end
end
end
if tiny_tmp.url != ""
is_new = true
final.each do |dup|
if dup.method == tiny_tmp.method && dup.url == tiny_tmp.url
is_new = false
tiny_tmp.params.each do |param|
dup.params << param
end
end
end
if is_new || final.size == 0
final << tiny_tmp
end
end
end
@endpoints = final
end
def combine_url_and_endpoints
tmp = [] of Endpoint
target_url = @options[:url]
if target_url != ""
@logger.system "Combining url and endpoints."
@endpoints.each do |endpoint|
tmp_endpoint = endpoint
if tmp_endpoint.url.includes? target_url
tmp_endpoint.url = tmp_endpoint.url.gsub(target_url, "")
end
tmp_endpoint.url = tmp_endpoint.url.gsub_repeatedly("//", "/")
if tmp_endpoint.url != ""
if target_url[-1] == '/' && tmp_endpoint.url[0] == '/'
tmp_endpoint.url = tmp_endpoint.url[1..]
elsif target_url[-1] != '/' && tmp_endpoint.url[0] != '/'
tmp_endpoint.url = "/#{tmp_endpoint.url}"
end
end
tmp_endpoint.url = target_url + tmp_endpoint.url
tmp << tmp_endpoint
end
@endpoints = tmp
end
end
def deliver
if @send_proxy != ""
@logger.system "Sending requests with proxy #{@send_proxy}."
deliver = SendWithProxy.new(@options)
deliver.run(@endpoints)
end
if @send_req != "no"
@logger.system "Sending requests without proxy."
deliver = SendReq.new(@options)
deliver.run(@endpoints)
end
if @send_es != ""
@logger.system "Sending requests to Elasticsearch."
deliver = SendElasticSearch.new(@options)
deliver.run(@endpoints, @send_es)
end
end
def report
case options[:format]
when "yaml"
puts @endpoints.to_yaml
when "json"
puts @endpoints.to_json
when "jsonl"
builder = OutputBuilderJsonl.new @options
builder.print @endpoints
when "markdown-table"
builder = OutputBuilderMarkdownTable.new @options
builder.print @endpoints
when "httpie"
builder = OutputBuilderHttpie.new @options
builder.print @endpoints
when "curl"
builder = OutputBuilderCurl.new @options
builder.print @endpoints
when "oas2"
buidler = OutputBuilderOas2.new @options
buidler.print @endpoints
when "oas3"
buidler = OutputBuilderOas3.new @options
buidler.print @endpoints
when "only-url"
builder = OutputBuilderOnlyUrl.new @options
builder.print @endpoints
when "only-param"
builder = OutputBuilderOnlyParam.new @options
builder.print @endpoints
when "only-header"
builder = OutputBuilderOnlyHeader.new @options
builder.print @endpoints
when "only-cookie"
builder = OutputBuilderOnlyCookie.new @options
builder.print @endpoints
else
builder = OutputBuilderCommon.new @options
builder.print @endpoints
end
end
end