-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.rb
395 lines (304 loc) · 9.4 KB
/
app.rb
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# frozen_string_literal: true
#
# ronin-app - a local web app for Ronin.
#
# Copyright (C) 2023-2024 Hal Brodigan (postmodern.mod3@gmail.com)
#
# ronin-app is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ronin-app is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with ronin-app. If not, see <http://www.gnu.org/licenses/>.
#
$LOAD_PATH.unshift(File.join(__dir__,'lib'))
# classes
require 'sinatra/base'
require 'sinatra/content_for'
require 'sinatra/flash'
require 'sinatra/reloader'
# configuration
require_relative 'config/database'
require_relative 'config/sidekiq'
# ronin libraries
require 'ronin/repos'
require 'ronin/payloads'
require 'ronin/exploits'
require 'ronin/support/encoding'
# param validations
require 'ronin/app/validations/install_repo_params'
require 'ronin/app/validations/import_params'
require 'ronin/app/validations/http_params'
# schema builders
require 'ronin/app/schemas/payloads/encoders/encode_schema'
require 'ronin/app/schemas/payloads/build_schema'
# helpers
require 'ronin/app/helpers/html'
# worker classes
require_relative 'workers/install_repo'
require_relative 'workers/update_repo'
require_relative 'workers/update_repos'
require_relative 'workers/remove_repo'
require_relative 'workers/purge_repos'
require_relative 'workers/import'
require 'ronin/app/version'
require 'sidekiq/api'
# others
require 'pagy'
require 'pagy/extras/bulma'
#
# Main app class.
#
class App < Sinatra::Base
include Ronin::App
include Pagy::Backend
configure do
enable :sessions
enable :method_override
register Sinatra::Flash
helpers Sinatra::ContentFor
helpers Helpers::HTML
end
configure :development do
register Sinatra::Reloader
end
helpers do
include Pagy::Frontend
end
after do
ActiveRecord::Base.connection_handler.clear_active_connections!
end
get '/' do
erb :index
end
get '/repos' do
@repos = Ronin::Repos.cache_dir
erb :"repos/index"
end
get '/repos/install' do
erb :"repos/install"
end
post '/repos/install' do
result = Validations::InstallRepoParams.call(params)
if result.success?
Workers::InstallRepo.perform_async(result[:uri],result[:name])
flash[:success] = "Installing repo at #{result[:uri]}"
redirect '/repos'
else
@errors = result.errors
flash[:danger] = 'Failed to install repo!'
halt 400, erb(:"repos/install")
end
end
post '/repos/update' do
Workers::UpdateRepos.perform_async
flash[:success] = 'All repos will be updated'
redirect '/repos'
end
delete '/repos' do
Workers::PurgeRepos.perform_async
flash[:success] = 'All repos will be purged'
redirect '/repos'
end
get '/repos/:name' do
@repos = Ronin::Repos.cache_dir
begin
@repo = @repos[params[:name]]
erb :"repos/show"
rescue Ronin::Repos::RepositoryNotFound
halt 404
end
end
post '/repos/:name/update' do
@repo = Ronin::Repos.cache_dir[params[:name]]
Workers::UpdateRepo.perform_async(@repo.name)
flash[:success] = "Repo #{@repo.name} enqueued for update"
redirect "/repos/#{params[:name]}"
rescue Ronin::Repos::RepositoryNotFound
halt 404
end
delete '/repos/:name' do
@repo = Ronin::Repos.cache_dir[params[:name]]
Workers::RemoveRepo.perform_async(@repo.name)
flash[:success] = "Repo #{@repo.name} enqueued for removal"
redirect '/repos'
rescue Ronin::Repos::RepositoryNotFound
halt 404
end
get '/payloads' do
@payloads = Ronin::Payloads.list_files
erb :"payloads/index"
end
get '/payloads/encoders' do
@payload_encoders = Ronin::Payloads::Encoders.list_files
erb :"payloads/encoders/index"
end
get %r{/payloads/encoders/encode/(?<encoder_id>[a-z0-9_-]+(?:/[a-z0-9_-]+)*)} do
@encoder_class = Ronin::Payloads::Encoders.load_class(params[:encoder_id])
@encoder = @encoder_class.new
erb :"payloads/encoders/encode"
rescue Ronin::Core::ClassRegistry::ClassNotFound
halt 404
end
post %r{/payloads/encoders/encode/(?<encoder_id>[a-z0-9_-]+(?:/[a-z0-9_-]+)*)} do
@encoder_class = Ronin::Payloads::Encoders.load_class(params[:encoder_id])
@encoder = @encoder_class.new
form_schema = Schemas::Payloads::Encoders::EncodeSchema(@encoder_class)
result = form_schema.call(params)
if result.success?
encoder_params = result[:params].to_h
encoder_params.compact!
begin
@encoder.params = encoder_params
rescue Ronin::Core::Params::ParamError => error
flash[:error] = "Failed to set params: #{error.message}"
halt 400, erb(:"payloads/encoders/encode")
end
begin
@encoder.validate
rescue => error
flash[:error] = "Failed to encode encoder: #{error.message}"
halt 500, erb(:"payloads/encoders/encode")
end
@encoded_data = @encoder.encode(result[:data])
erb :"payloads/encoders/encode"
else
@params = params
@errors = result.errors
halt 400, erb(:"payloads/encoders/encode")
end
rescue Ronin::Core::ClassRegistry::ClassNotFound
halt 404
end
get %r{/payloads/encoders/(?<encoder_id>[a-z0-9_-]+(?:/[a-z0-9_-]+)*)} do
@encoder = Ronin::Payloads::Encoders.load_class(params[:encoder_id])
erb :"payloads/encoders/show"
rescue Ronin::Core::ClassRegistry::ClassNotFound
halt 404
end
get %r{/payloads/build/(?<payload_id>[a-z0-9_-]+(?:/[a-z0-9_-]+)*)} do
@payload_class = Ronin::Payloads.load_class(params[:payload_id])
@payload = @payload_class.new
erb :"payloads/build"
rescue Ronin::Core::ClassRegistry::ClassNotFound
halt 404
end
post %r{/payloads/build/(?<payload_id>[a-z0-9_-]+(?:/[a-z0-9_-]+)*)} do
@payload_class = Ronin::Payloads.load_class(params[:payload_id])
@payload = @payload_class.new
form_schema = Schemas::Payloads::BuildSchema(@payload_class)
result = form_schema.call(params)
if result.success?
payload_params = result[:params].to_h
payload_params.compact!
begin
@payload.params = payload_params
rescue Ronin::Core::Params::ParamError => error
flash[:error] = "Failed to set params: #{error.message}"
halt 400, erb(:"payloads/build")
end
begin
@payload.perform_validate
@payload.perform_build
rescue => error
flash[:error] = "Failed to build payload: #{error.message}"
halt 500, erb(:"payloads/build")
end
@built_payload = @payload.to_s
erb :"payloads/build"
else
@params = params
@errors = result.errors
halt 400, erb(:"payloads/build")
end
rescue Ronin::Core::ClassRegistry::ClassNotFound
halt 404
end
get %r{/payloads/(?<payload_id>[a-z0-9_-]+(?:/[a-z0-9_-]+)*)} do
@payload = Ronin::Payloads.load_class(params[:payload_id])
erb :"payloads/show"
rescue Ronin::Core::ClassRegistry::ClassNotFound
halt 404
end
get '/exploits' do
@exploits = Ronin::Exploits.list_files
erb :"exploits/index"
end
get %r{/exploits/(?<exploit_id>[A-Za-z0-9_-]+(?:/[A-Za-z0-9_-]+)*)} do
@exploit = Ronin::Exploits.load_class(params[:exploit_id])
erb :"exploits/show"
rescue Ronin::Core::ClassRegistry::ClassNotFound
halt 404
end
get '/import' do
erb :import
end
post '/import' do
result = Validations::ImportParams.call(params)
if result.success?
@jid = Workers::Import.perform_async(result.to_h)
type = result[:type]
path = result[:path]
flash[:success] = "Import of #{type} file #{path} enqueued"
redirect '/import'
else
@errors = result.errors
flash[:danger] = 'Failed to submit import job!'
halt 400, erb(:import)
end
end
get '/about' do
@lockfile = Bundler::LockfileParser.new(File.read(Bundler.default_lockfile))
erb :about
end
get '/queue' do
@workers = Sidekiq::Workers.new.map do |_process_id, _thread_id, worker|
payload = JSON.parse(worker["payload"])
{
queue: worker["queue"],
class: payload["class"],
args: payload["args"],
created_at: Time.at(payload["created_at"]),
enqueued_at: Time.at(payload["enqueued_at"]),
run_at: Time.at(worker["run_at"])
}
end
erb :queue
end
get '/network/http' do
erb :"network/http"
end
post '/network/http' do
result = Validations::HTTPParams.call(params)
if result.success?
kwargs = result.to_h
method = kwargs.delete(:method)
url = kwargs.delete(:url)
@http_response = Ronin::Support::Network::HTTP.request(method,url,**kwargs)
erb :"network/http"
else
@params = params
@errors = result.errors
halt 400, erb(:"network/http")
end
end
private
#
# Returns the hash of variables used to initialize the Pagy object.
#
def pagy_get_vars(collection, vars)
{
count: collection.count,
page: params["page"],
items: vars[:items] || 25
}
end
end
require './app/db'
require './app/scanning'