forked from SerpicoProject/Serpico
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserpico.rb
2377 lines (1833 loc) · 65 KB
/
serpico.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# encoding: ASCII-8BIT
require 'rubygems'
require 'bundler/setup'
require 'sinatra'
require 'haml'
require 'zipruby'
require 'net/ldap'
require 'json'
require 'diffy'
#serpico handlers
require './model/master'
require './helpers/helper'
require './helpers/sinatra_ssl'
require './helpers/xslt_generation'
require './helpers/vuln_importer'
require './helpers/asciidoc_exporter'
# import config options
config_options = JSON.parse(File.read('./config.json'))
## SSL Settings
set :ssl_certificate, config_options["ssl_certificate"]
set :ssl_key, config_options["ssl_key"]
set :use_ssl, config_options["use_ssl"]
set :port, config_options["port"]
set :bind_address, config_options["bind_address"]
## Global variables
set :finding_types, config_options["finding_types"]
set :effort, ["Quick","Planned","Involved"]
set :assessment_types, ["External", "Internal", "Internal/External", "Wireless", "Web Application", "DoS"]
set :status, ["EXPLOITED"]
set :show_exceptions, false
# CVSS
set :av, ["Local","Local Network","Network"]
set :ac, ["High","Medium","Low"]
set :au, ["Multiple","Single","None"]
set :c, ["None","Partial","Complete"]
set :i, ["None","Partial","Complete"]
set :a, ["None","Partial","Complete"]
set :e, ["Not Defined","Unproven Exploit Exists","Proof-of-Concept Code","Functional Exploit Exists","High"]
set :rl, ["Not Defined","Official Fix","Temporary Fix","Workaround","Unavailable"]
set :rc, ["Not Defined","Unconfirmed","Uncorroborated","Confirmed"]
set :cdp, ["Not Defined","None","Low","Low-Medium","Medium-High","High"]
set :td, ["Not Defined","None","Low","Medium","High"]
set :cr, ["Not Defined","Low","Medium","High"]
set :ir, ["Not Defined","Low","Medium","High"]
set :ar, ["Not Defined","Low","Medium","High"]
## LDAP Settings
if config_options["ldap"].downcase == "true"
set :ldap, true
else
set :ldap, false
end
set :domain, config_options["ldap_domain"]
set :dc, config_options["ldap_dc"]
enable :sessions
set :session_secret, rand(36**12).to_s(36)
### Basic Routes
# Used for 404 responses
not_found do
"Sorry, I don't know this page."
end
# Error catches
error do
if settings.show_exceptions
"Error!"+ env['sinatra.error'].name
else
"Error!! Check the process dump for the error or turn show_exceptions on to show in the web interface."
end
end
# Run a session check on every route
["/info","/reports/*","/report/*","/","/logout","/admin/*","/master/*","/mapping/*"].each do |path|
before path do
next if request.path_info == "/reports/list"
redirect '/reports/list' unless valid_session?
end
end
before "/master/*" do
redirect to("/no_access") if not is_administrator?
end
before "/mapping/*" do
redirect to("/no_access") if not is_administrator?
end
#######
get '/' do
redirect to("/reports/list")
end
get '/login' do
redirect to("/reports/list")
end
# Handles the consultant information settings
get '/info' do
@user = User.first(:username => get_username)
if !@user
@user = User.new
@user.auth_type = "AD"
@user.username = get_username
@user.type = "User"
@user.save
end
haml :info, :encode_html => true
end
# Save the consultant information into the database
post '/info' do
user = User.first(:username => get_username)
if !user
user = User.new
user.auth_type = "AD"
user.username = get_username
user.type = "User"
end
user.consultant_email = params[:email]
user.consultant_phone = params[:phone]
user.consultant_title = params[:title]
user.consultant_name = params[:name]
user.consultant_company = params[:company]
user.save
redirect to("/info")
end
post '/login' do
user = User.first(:username => params[:username])
if user and user.auth_type == "Local"
usern = User.authenticate(params["username"], params["password"])
if usern and session[:session_id]
# replace the session in the session table
# TODO : This needs an expiration, session fixation
@del_session = Sessions.first(:username => "#{usern}")
@del_session.destroy if @del_session
@curr_session = Sessions.create(:username => "#{usern}",:session_key => "#{session[:session_id]}")
@curr_session.save
end
elsif user
if options.ldap
#try AD authentication
usern = params[:username]
data = url_escape_hash(request.POST)
if usern == "" or params[:password] == ""
redirect to("/")
end
user = "#{options.domain}\\#{data["username"]}"
ldap = Net::LDAP.new :host => "#{options.dc}", :port => 636, :encryption => :simple_tls, :auth => {:method => :simple, :username => user, :password => params[:password]}
if ldap.bind
# replace the session in the session table
@del_session = Sessions.first(:username => "#{usern}")
@del_session.destroy if @del_session
@curr_session = Sessions.create(:username => "#{usern}",:session_key => "#{session[:session_id]}")
@curr_session.save
end
end
end
redirect to("/")
end
## We use a persistent session table, one session per user; no end date
get '/logout' do
if session[:session_id]
sess = Sessions.first(:session_key => session[:session_id])
if sess
sess.destroy
end
end
redirect to("/")
end
# rejected access (admin functionality)
get "/no_access" do
return "Sorry. You Do Not have access to this resource."
end
######
# Admin Interfaces
######
get '/admin/' do
redirect to("/no_access") if not is_administrator?
@admin = true
haml :admin, :encode_html => true
end
get '/admin/add_user' do
redirect to("/no_access") if not is_administrator?
@admin = true
haml :add_user, :encode_html => true
end
# serve a copy of the code
get '/admin/pull' do
redirect to("/no_access") if not is_administrator?
if File.exists?("./export.zip")
send_file "./export.zip", :filename => "export.zip", :type => 'Application/octet-stream'
else
"No copy of the code available. Run scripts/make_export.sh."
end
end
#create DB backup
get '/admin/dbbackup' do
redirect to("/no_access") if not is_administrator?
bdate = Time.now()
filename = "master" + "-" + (bdate.strftime("%Y%m%d%H%M%S") +".bak")
FileUtils::copy_file("./db/master.db", "./tmp/#{filename}")
if not File.zero?("./tmp/#{filename}")
send_file "./tmp/#{filename}", :filename => "#{filename}", :type => 'Application/octet-stream'
else
"No copy of the database is available. Please try again."
sleep(5)
redirect to("/admin/")
end
end
# Create a new user
post '/admin/add_user' do
redirect to("/no_access") if not is_administrator?
user = User.first(:username => params[:username])
if user
if params[:password]
# we have to hardcode the input params to prevent param pollution
user.update(:type => params[:type], :auth_type => params[:auth_type], :password => params[:password])
else
# we have to hardcode the params to prevent param pollution
user.update(:type => params[:type], :auth_type => params[:auth_type])
end
else
user = User.new
user.username = params[:username]
user.password = params[:password]
user.type = params[:type]
user.auth_type = params[:auth_type]
user.save
end
redirect to('/admin/list_user')
end
get '/admin/list_user' do
redirect to("/no_access") if not is_administrator?
@admin = true
@users = User.all
haml :list_user, :encode_html => true
end
get '/admin/edit_user/:id' do
redirect to("/no_access") if not is_administrator?
@user = User.first(:id => params[:id])
haml :add_user, :encode_html => true
end
get '/admin/delete/:id' do
redirect to("/no_access") if not is_administrator?
@user = User.first(:id => params[:id])
@user.destroy if @user
redirect to('/admin/list_user')
end
get '/admin/add_user/:id' do
if not is_administrator?
id = params[:id]
unless get_report(id)
redirect to("/no_access")
end
end
@users = User.all(:order => [:username.asc])
@report = Reports.first(:id => params[:id])
if is_administrator?
@admin = true
end
haml :add_user_report, :encode_html => true
end
post '/admin/add_user/:id' do
if not is_administrator?
id = params[:id]
unless get_report(id)
redirect to("/no_access")
end
end
report = Reports.first(:id => params[:id])
if report == nil
return "No Such Report"
end
authors = report.authors
if authors
authors = authors.push(params[:author])
else
authors = ["#{params[:author]}"]
end
report.authors = authors
report.save
redirect to("/reports/list")
end
get '/admin/del_user_report/:id/:author' do
if not is_administrator?
id = params[:id]
unless get_report(id)
redirect to("/no_access")
end
end
report = Reports.first(:id => params[:id])
if report == nil
return "No Such Report"
end
authors = report.authors
if authors
authors = authors - ["#{params[:author]}"]
end
report.authors = authors
report.save
redirect to("/reports/list")
end
######
# Template Document Routes
######
# These are the master routes, they control the findings database
# List Available Templated Findings
get '/master/findings' do
@findings = TemplateFindings.all(:order => [:title.asc])
@master = true
@dread = config_options["dread"]
@cvss = config_options["cvss"]
haml :findings_list, :encode_html => true
end
# Create a new templated finding
get '/master/findings/new' do
@master = true
@dread = config_options["dread"]
@cvss = config_options["cvss"]
@nessusmap = config_options["nessusmap"]
haml :create_finding, :encode_html => true
end
# Create the finding in the DB
post '/master/findings/new' do
data = url_escape_hash(request.POST)
if(config_options["dread"])
data["dread_total"] = data["damage"].to_i + data["reproducability"].to_i + data["exploitability"].to_i + data["affected_users"].to_i + data["discoverability"].to_i
end
# update metadata
data["last_updated_by"] = get_username
# split out any nessus mapping data
nessusdata = Hash.new()
nessusdata["pluginid"] = data["pluginid"]
data.delete("pluginid")
@finding = TemplateFindings.new(data)
@finding.save
# find the id of the newly created finding so we can link mappings to it
@newfinding = TemplateFindings.first(:title => data["title"], :order => [:id.desc], :limit => 1)
# save nessus mapping
if(config_options["nessusmap"])
nessusdata["templatefindings_id"] = @newfinding.id
@nessus = NessusMapping.new(nessusdata)
@nessus.save
elsif(config_options["cvss"])
data = cvss(data)
end
redirect to('/master/findings')
end
# Edit the templated finding
get '/master/findings/:id/edit' do
@master = true
@dread = config_options["dread"]
@cvss = config_options["cvss"]
@nessusmap = config_options["nessusmap"]
@burpmap = config_options["burpmap"]
# Check for kosher name in report name
id = params[:id]
# Query for all Findings
@finding = TemplateFindings.first(:id => id)
@templates = Xslt.all()
if (@nessusmap)
@nessus = NessusMapping.all(:templatefindings_id => id)
end
if (@burpmap)
@burp = BurpMapping.all(:templatefindings_id => id)
end
if @finding == nil
return "No Such Finding"
end
@revisedfindings = RevisedFindings.all(:templatefindings_id => id)
haml :findings_edit, :encode_html => true
end
# Edit a finding
post '/master/findings/:id/edit' do
# Check for kosher name in report name
id = params[:id]
# Query for Finding
@finding = TemplateFindings.first(:id => id)
if @finding == nil
return "No Such Finding"
end
# make copy of finding for revision history
attr = @finding.attributes
attr["id"] = nil
attr["templatefindings_id"] = @finding.id
attr.delete(:affected_hosts)
@revisedfinding = RevisedFindings.new(attr)
@revisedfinding.save
data = url_escape_hash(request.POST)
if data["approved"] == "on"
data["approved"] = true
else
data["approved"] = false
end
if(config_options["dread"])
data["dread_total"] = data["damage"].to_i + data["reproducability"].to_i + data["exploitability"].to_i + data["affected_users"].to_i + data["discoverability"].to_i
elsif(config_options["cvss"])
data = cvss(data)
end
# update metadata
data["last_updated_by"] = get_username
# split out any nessus mapping data
nessusdata = Hash.new()
nessusdata["pluginid"] = data["nessus_pluginid"]
data.delete("nessus_pluginid")
nessusdata["templatefindings_id"] = id
# split out any burp mapping data
burpdata = Hash.new()
burpdata["pluginid"] = data["burp_pluginid"]
data.delete("burp_pluginid")
burpdata["templatefindings_id"] = id
# Update the finding with templated finding stuff
@finding.update(data)
# save nessus mapping data to db
if(config_options["nessusmap"])
@nessus = NessusMapping.new(nessusdata)
@nessus.save
end
# save burp mapping data to db
if(config_options["burpmap"])
@burp = BurpMapping.new(burpdata)
@burp.save
end
redirect to("/master/findings")
end
# Revision history
get '/master/findings/:id/history' do
@master = true
@currentfinding = TemplateFindings.first(:id => params[:id])
if @currentfinding == nil
return "No current/master finding exists to compare with"
end
@revisedfindings = RevisedFindings.all(:templatefindings_id => params[:id], :order => [:id.desc])
if @revisedfindings.size == 0
return "No history available"
end
haml :findings_history, :encode_html => true
end
# Revision history
post '/master/findings/:id/history' do
@master = true
id = params[:id]
finding_to_diff = params[:diff]
@currentfinding = TemplateFindings.first(:id => id)
@previousfinding = RevisedFindings.first(:templatefindings_id => id, :id => finding_to_diff)
@compare = Hash.new
@compare["title"] = Diffy::Diff.new(@previousfinding.title, @currentfinding.title, :include_plus_and_minus_in_html => true).to_s(:html)
@compare["type"] = Diffy::Diff.new(@previousfinding.type,@currentfinding.type, :include_plus_and_minus_in_html => true).to_s(:html)
@compare["risk"] = Diffy::Diff.new(@previousfinding.risk, @currentfinding.risk, :include_plus_and_minus_in_html => true).to_s(:html)
@compare["overview"] = Diffy::Diff.new(@previousfinding.overview, @currentfinding.overview, :include_plus_and_minus_in_html => true).to_s(:html)
@compare["remediation"] = Diffy::Diff.new(@previousfinding.remediation, @currentfinding.remediation, :include_plus_and_minus_in_html => true).to_s(:html)
@compare["references"] = Diffy::Diff.new( @previousfinding.references, @currentfinding.references, :include_plus_and_minus_in_html => true).to_s(:html)
if @previousfinding == nil or @currentfinding == nil
return "No finding exists or no history available"
end
haml :findings_history, :encode_html => true
end
# read only view of previously edited findings
get '/stale/findings/:id' do
@master = true
id = params[:id]
@revisedfinding = RevisedFindings.first(:id => id)
if @revisedfinding == nil
return "No such revised finding exists"
end
@dread = config_options["dread"]
haml :findings_readonly, :encode_html => true
end
# Delete a mapping from finding
get '/mapping/:id/nessus/:mappingid/delete' do
# Check for kosher name in report name
id = params[:id]
mappingid = params[:mappingid]
@map = NessusMapping.first(:templatefindings_id => id, :pluginid => mappingid)
@map.destroy
redirect to("/master/findings/#{id}/edit")
end
# Delete a mapping from finding
get '/mapping/:id/burp/:mappingid/delete' do
# Check for kosher name in report name
id = params[:id]
mappingid = params[:mappingid]
@map = BurpMapping.first(:templatefindings_id => id, :pluginid => mappingid)
@map.destroy
redirect to("/master/findings/#{id}/edit")
end
# Delete a template finding
get '/master/findings/:id/delete' do
# Check for kosher name in report name
id = params[:id]
# Query for all Findings
@finding = TemplateFindings.first(:id => id)
if @finding == nil
return "No Such Finding"
end
# Update the finding with templated finding stuff
@finding.destroy
redirect to("/master/findings")
end
# preview a finding
get '/master/findings/:id/preview' do
# Check for kosher name in report name
id = params[:id]
# Query for all Findings
@finding = TemplateFindings.first(:id => id)
if @finding == nil
return "No Such Finding"
end
## We have to do some hackery here for wordml
findings_xml = ""
findings_xml << "<findings_list>"
findings_xml << @finding.to_xml
findings_xml << "</findings_list>"
findings_xml = meta_markup_unencode(findings_xml, nil)
# this is the master db so we have to do a bait and switch
# The other option is creating a master finding specific docx
findings_xml = findings_xml.gsub("<template_findings>","<findings>")
findings_xml = findings_xml.gsub("</template_findings>;","</template_findings>")
report_xml = "#{findings_xml}"
xslt_elem = Xslt.first(:finding_template => true)
if xslt_elem
# Push the finding from XML to XSLT
xslt = Nokogiri::XSLT(File.read(xslt_elem.xslt_location))
docx_xml = xslt.transform(Nokogiri::XML(report_xml))
# We use a temporary file with a random name
rand_file = "./tmp/#{rand(36**12).to_s(36)}.docx"
# Create a temporary copy of the finding_template
FileUtils::copy_file(xslt_elem.docx_location,rand_file)
# A better way would be to create the zip file in memory and return to the user, this is not ideal
Zip::Archive.open(rand_file, Zip::CREATE) do |zipfile|
zipfile.add_or_replace_buffer('word/document.xml',
docx_xml.to_s)
end
send_file rand_file, :type => 'docx', :filename => "#{@finding.title}.docx"
else
"You don't have a Finding Template (did you delete the temp?) -_- ... If you're an admin go to <a href='/admin/templates/add'>here</a> to add one.'"
end
end
# Export a findings database
get '/master/export' do
json = ""
findings = TemplateFindings.all
local_filename = "./tmp/#{rand(36**12).to_s(36)}.json"
File.open(local_filename, 'w') {|f| f.write(JSON.pretty_generate(findings)) }
send_file local_filename, :type => 'json', :filename => "template_findings.json"
end
# Import a findings database
get '/master/import' do
haml :import_templates
end
# Import a findings database
post '/master/import' do
redirect to("/master/import") unless params[:file]
# reject if the file is above a certain limit
if params[:file][:tempfile].size > 1000000
return "File too large. 1MB limit"
end
json_file = params[:file][:tempfile].read
line = JSON.parse(json_file)
line.each do |j|
j["id"] = nil
finding = TemplateFindings.first(:title => j["title"])
if finding
#the finding title already exists in the database
if finding["overview"] == j["overview"] and finding["remediation"] == j["remediation"]
# the finding already exists, ignore it
else
# it's a modified finding
j["title"] = "#{j['title']} - [Uploaded Modified Templated Finding]"
params[:approved] !=nil ? j["approved"] = true : j["approved"] = false
f = TemplateFindings.create(j)
f.save
end
else
params[:approved] != nil ? j["approved"] = true : j["approved"] = false
f = TemplateFindings.first_or_create(j)
f.save
end
end
redirect to("/master/findings")
end
# Manage Templated Reports
get '/admin/templates' do
redirect to("/no_access") if not is_administrator?
@admin = true
# Query for all Findings
@templates = Xslt.all(:order => [:report_type.asc])
haml :template_list, :encode_html => true
end
# Manage Templated Reports
get '/admin/templates/add' do
redirect to("/no_access") if not is_administrator?
@admin = true
haml :add_template, :encode_html => true
end
# Manage Templated Reports
get '/admin/templates/:id/download' do
redirect to("/no_access") if not is_administrator?
@admin = true
xslt = Xslt.first(:id => params[:id])
send_file xslt.docx_location, :type => 'docx', :filename => "#{xslt.report_type}.docx"
end
get '/admin/delete/templates/:id' do
redirect to("/no_access") if not is_administrator?
@xslt = Xslt.first(:id => params[:id])
if @xslt
@xslt.destroy
File.delete(@xslt.xslt_location)
File.delete(@xslt.docx_location)
end
redirect to('/admin/templates')
end
# Manage Templated Reports
post '/admin/templates/add' do
redirect to("/no_access") if not is_administrator?
@admin = true
xslt_file = "./templates/#{rand(36**36).to_s(36)}.xslt"
redirect to("/admin/templates/add") unless params[:file]
# reject if the file is above a certain limit
if params[:file][:tempfile].size > 100000000
return "File too large. 10MB limit"
end
docx = "./templates/#{rand(36**36).to_s(36)}.docx"
File.open(docx, 'wb') {|f| f.write(params[:file][:tempfile].read) }
error = false
detail = ""
begin
xslt = generate_xslt(docx)
rescue ReportingError => detail
error = true
end
if error
"The report template you uploaded threw an error when parsing:<p><p> #{detail.errorString}"
else
# open up a file handle and write the attachment
File.open(xslt_file, 'wb') {|f| f.write(xslt) }
# delete the file data from the attachment
datax = Hash.new
# to prevent traversal we hardcode this
datax["docx_location"] = "#{docx}"
datax["xslt_location"] = "#{xslt_file}"
datax["description"] = params[:description]
datax["report_type"] = params[:report_type]
data = url_escape_hash(datax)
data["finding_template"] = params[:finding_template] ? true : false
data["status_template"] = params[:status_template] ? true : false
@current = Xslt.first(:report_type => data["report_type"])
if @current
@current.update(:xslt_location => data["xslt_location"], :docx_location => data["docx_location"], :description => data["description"])
else
@template = Xslt.new(data)
@template.save
end
redirect to("/admin/templates")
haml :add_template, :encode_html => true
end
end
# Manage Templated Reports
get '/admin/templates/:id/edit' do
redirect to("/no_access") if not is_administrator?
@admind = true
@template = Xslt.first(:id => params[:id])
haml :edit_template, :encode_html => true
end
# Manage Templated Reports
post '/admin/templates/edit' do
redirect to("/no_access") if not is_administrator?
@admin = true
template = Xslt.first(:id => params[:id])
xslt_file = template.xslt_location
redirect to("/admin/templates/#{params[:id]}/edit") unless params[:file]
# reject if the file is above a certain limit
if params[:file][:tempfile].size > 100000000
return "File too large. 10MB limit"
end
docx = "./templates/#{rand(36**36).to_s(36)}.docx"
File.open(docx, 'wb') {|f| f.write(params[:file][:tempfile].read) }
error = false
detail = ""
begin
xslt = generate_xslt(docx)
rescue ReportingError => detail
error = true
end
if error
"The report template you uploaded threw an error when parsing:<p><p> #{detail.errorString}"
else
# open up a file handle and write the attachment
File.open(xslt_file, 'wb') {|f| f.write(xslt) }
# delete the file data from the attachment
datax = Hash.new
# to prevent traversal we hardcode this
datax["docx_location"] = "#{docx}"
datax["xslt_location"] = "#{xslt_file}"
datax["description"] = params[:description]
datax["report_type"] = params[:report_type]
data = url_escape_hash(datax)
data["finding_template"] = params[:finding_template] ? true : false
data["status_template"] = params[:status_template] ? true : false
@current = Xslt.first(:report_type => data["report_type"])
if @current
@current.update(:xslt_location => data["xslt_location"], :docx_location => data["docx_location"], :description => data["description"])
else
@template = Xslt.new(data)
@template.save
end
redirect to("/admin/templates")
end
end
#####
# Reporting Routes
#####
# List current reports
get '/reports/list' do
@reports = get_reports
@admin = true if is_administrator?
# allow the user to set their logo in the configuration options
@logo = config_options["logo"]
haml :reports_list, :encode_html => true
end
# Create a report
get '/report/new' do
@templates = Xslt.all
haml :new_report, :encode_html => true
end
# Create a report
post '/report/new' do
data = url_escape_hash(request.POST)
data["owner"] = get_username
data["date"] = DateTime.now.strftime "%m/%d/%Y"
@report = Reports.new(data)
@report.save
redirect to("/report/#{@report.id}/edit")
end
# List attachments
get '/report/:id/attachments' do
id = params[:id]
# Query for the first report matching the id
@report = get_report(id)
if @report == nil
return "No Such Report"
end
@attachments = Attachments.all(:report_id => id)
haml :list_attachments, :encode_html => true
end
# upload nessus xml files to be processed
get '/report/:id/import_nessus' do
id = params[:id]
@nessusmap = config_options["nessusmap"]
# Query for the first report matching the id
@report = get_report(id)
haml :import_nessus, :encode_html => true
end
# auto add serpico findings if mapped to nessus ids
post '/report/:id/import_autoadd' do
type = params[:type]
xml = params[:file][:tempfile].read
if (xml =~ /^<NessusClientData_v2>/ && type == "nessus")
import_nessus = true
vulns = parse_nessus_xml(xml, config_options["threshold"])
elsif (xml =~ /^<issues burpVersion/ && type == "burp")
import_burp = true
vulns = parse_burp_xml(xml)
else
return "File does not contain valid XML import data"
end
# reject if the file is above a certain limit
#if params[:file][:tempfile].size > 1000000
# return "File too large. 1MB limit"
#end
# Check for kosher name in report name
id = params[:id]
add_findings = Array.new
dup_findings = Array.new
autoadd_hosts = Hash.new
# Query for the first report matching the report_name