-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathmain.rb
More file actions
1634 lines (1316 loc) · 43.5 KB
/
Copy pathmain.rb
File metadata and controls
1634 lines (1316 loc) · 43.5 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
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
require 'sinatra'
require './helpers/sinatra_ssl.rb'
# we default to production env b/c i want to
if ENV['RACK_ENV'].nil?
set :environment, :production
ENV['RACK_ENV'] = 'production'
end
require 'sinatra/flash'
require 'haml'
require 'data_mapper'
require './model/master.rb'
require 'json'
require 'redis'
require 'resque'
require './jobs/jobq.rb'
require './helpers/hash_importer'
require './helpers/hc_stdout_parser.rb'
require './helpers/email.rb'
require 'pony'
set :bind, '0.0.0.0'
# Check to see if SSL cert is present, if not generate
unless File.exist?('cert/server.crt')
# Generate Cert
system('openssl req -x509 -nodes -days 365 -newkey RSA:2048 -subj "/CN=US/ST=Minnesota/L=Duluth/O=potatoFactory/CN=hashview" -keyout cert/server.key -out cert/server.crt')
end
set :ssl_certificate, 'cert/server.crt'
set :ssl_key, 'cert/server.key'
enable :sessions
redis = Redis.new
# to start the resque web queue run the following from the command prompt:
# resque-web
# to start the rake task do: TERM_CHILD=1 QUEUE=* rake resque:work
# ^^^ should probably make an upstart for that
# validate every session
before /^(?!\/(login|register|logout))/ do
if !validSession?
redirect to('/login')
else
settings = Settings.first
if (settings && settings.hcbinpath.nil?) or settings.nil?
flash[:warning] = "Annoying alert! You need to define hashcat\'s binary path in settings first. Do so <a href=/settings>HERE</a>"
end
end
end
get '/login' do
@users = User.all
if @users.empty?
redirect('/register')
else
haml :login
end
end
get '/logout' do
varWash(params)
if session[:session_id]
sess = Sessions.first(session_key: session[:session_id])
sess.destroy if sess
end
redirect to('/')
end
post '/login' do
varWash(params)
if !params[:username] || params[:username].nil?
flash[:error] = 'You must supply a username.'
redirect to('/login')
end
if !params[:password] || params[:password].nil?
flash[:error] = 'You must supply a password.'
redirect to('/login')
end
@user = User.first(username: params[:username])
if @user
usern = User.authenticate(params['username'], params['password'])
# if usern and session[:session_id]
unless usern.nil?
# only delete session if one exists
if 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
end
# Create new session
@curr_session = Sessions.create(username: "#{usern}", session_key: "#{session[:session_id]}")
@curr_session.save
redirect to('/home')
end
else
flash[:error] = 'Invalid credentials.'
redirect to('/not_authorized')
end
end
get '/protected' do
return 'This is a protected page, you must be logged in.'
end
get '/not_authorized' do
return 'You are not authorized.'
end
get '/' do
@users = User.all
if @users.empty?
redirect to('/register')
elsif !validSession?
redirect to('/login')
else
redirect to('/home')
end
end
############################
### Register controllers ###
get '/register' do
@users = User.all
# Prevent registering of multiple admins
redirect to('/') unless @users.empty?
haml :register
end
post '/register' do
varWash(params)
if !params[:username] || params[:username].nil? || params[:username].empty?
flash[:error] = 'You must have a username.'
redirect to('/register')
end
if !params[:password] || params[:password].nil? || params[:password].empty?
flash[:error] = 'You must have a password.'
redirect to('/register')
end
if !params[:confirm] || params[:confirm].nil? || params[:confirm].empty?
flash[:error] = 'You must have a password.'
redirect to('/register')
end
# validate that no other user account exists
@users = User.all
if @users.empty?
if params[:password] != params[:confirm]
flash[:error] = 'Passwords do not match.'
redirect to('/register')
else
new_user = User.new
new_user.username = params[:username]
new_user.password = params[:password]
new_user.email = params[:email] unless params[:email].nil? || params[:email].empty?
new_user.admin = 't'
new_user.save
flash[:success] = "User #{params[:username]} created successfully"
end
end
redirect to('/home')
end
############################
##### Home controllers #####
get '/home' do
@results = `ps awwux | grep -i Hashcat | egrep -v "(grep|screen|SCREEN|resque|^$)"`
@jobs = Jobs.all(:order => [:id.asc])
@jobtasks = Jobtasks.all
@tasks = Tasks.all
@recentlycracked = Targets.all(limit: 10, cracked: 1)
@customers = Customers.all
@active_jobs = Jobs.first(fields: [:id, :status], status: 'Running')
# status
# this cmd requires a sudo TODO:this isnt working due to X env
# username ALL=(ALL) NOPASSWD: /usr/bin/amdconfig --adapter=all --odgt
# nvidia works without sudo:
@gpustatus = `nvidia-settings -q \"GPUCoreTemp\" | grep Attribute | grep -v gpu | awk '{print $3,$4}'`
if @gpustatus.empty?
@gpustatus = `lspci | grep "VGA compatible controller" | cut -d: -f3 | sed 's/\(rev a1\)//g'`
end
@gpustatus = @gpustatus.split("\n")
@gpustat = []
@gpustatus.each do |line|
unless line.chomp.empty?
line = line.delete('.')
@gpustat << line
end
end
@jobs.each do |j|
if j.status == 'Running'
# gather info for statistics
@alltargets = Targets.count(hashfile_id: j.hashfile_id)
@crackedtargets = Targets.count(hashfile_id: j.hashfile_id, cracked: 1)
@progress = (@crackedtargets.to_f / @alltargets.to_f) * 100
# parse a hashcat status file
@hashcat_status = hashcatParser('control/outfiles/hcoutput_' + j.id.to_s + '.txt')
end
end
haml :home
end
############################
### customer controllers ###
get '/customers/list' do
@customers = Customers.all
@total_jobs = []
@total_hashfiles = []
@customers.each do |customer|
@total_jobs[customer.id] = Jobs.count(customer_id: customer.id)
@total_hashfiles[customer.id] = Hashfiles.count(customer_id: customer.id)
end
haml :customer_list
end
get '/customers/create' do
haml :customer_edit
end
post '/customers/create' do
varWash(params)
if !params[:name] || params[:name].nil?
flash[:error] = 'Customer must have a name.'
redirect to('/customers/create')
end
customer = Customers.new
customer.name = params[:name]
customer.description = params[:desc]
customer.save
redirect to('customers/list')
end
get '/customers/edit/:id' do
varWash(params)
@customer = Customers.first(id: params[:id])
haml :customer_edit
end
post '/customers/edit/:id' do
varWash(params)
if !params[:name] || params[:name].nil?
flash[:error] = 'Customer must have a name.'
redirect to('/customers/create')
end
customer = Customers.first(id: params[:id])
customer.name = params[:name]
customer.description = params[:desc]
customer.save
redirect to('customers/list')
end
get '/customers/delete/:id' do
varWash(params)
@customer = Customers.first(id: params[:id])
@customer.destroy unless @customer.nil?
@jobs = Jobs.all(customer_id: params[:id])
unless @jobs.nil?
@jobs.each do |job|
@jobtasks = Jobtasks.all(job_id: job.id)
@jobtasks.destroy unless @jobtasks.nil?
end
@jobs.destroy unless @jobs.nil?
end
@hashfiles = Hashfiles.all(customer_id: params[:id])
@hashfiles.destroy unless @hashfiles.nil?
redirect to('/customers/list')
end
post '/customers/upload/hashfile' do
varWash(params)
if params[:hf_name].nil? || params[:hf_name].empty?
flash[:error] = 'You must specificy a name for this hash file.'
redirect to("/jobs/assign_hashfile?custid=#{params[:custid]}&jobid=#{params[:jobid]}")
end
if params[:file].nil? || params[:file].empty?
flash[:error] = 'You must specify a hashfile.'
redirect to("/jobs/assign_hashfile?custid=#{params[:custid]}&jobid=#{params[:jobid]}")
end
@job = Jobs.first(id: params[:jobid])
return 'No such job exists' unless @job
# temporarily save file for testing
hash = rand(36**8).to_s(36)
hashfile = "control/hashes/hashfile_upload_jobid-#{@job.id}-#{hash}.txt"
# Parse uploaded file into an array
hash_array = []
whole_file_as_string_object = params[:file][:tempfile].read
File.open(hashfile, 'w') { |f| f.write(whole_file_as_string_object) }
whole_file_as_string_object.each_line do |line|
hash_array << line
end
# save location of tmp hash file
hashfile = Hashfiles.new
hashfile.name = params[:hf_name]
hashfile.customer_id = params[:custid]
hashfile.hash_str = hash
hashfile.save
@job.save
redirect to("/customers/upload/verify_filetype?custid=#{params[:custid]}&jobid=#{params[:jobid]}&hashid=#{hashfile.id}")
end
get '/customers/upload/verify_filetype' do
varWash(params)
hashfile = Hashfiles.first(id: params[:hashid])
@filetypes = detectHashfileType("control/hashes/hashfile_upload_jobid-#{params[:jobid]}-#{hashfile.hash_str}.txt")
@job = Jobs.first(id: params[:jobid])
haml :verify_filetypes
end
post '/customers/upload/verify_filetype' do
varWash(params)
redirect to("/customers/upload/verify_hashtype?custid=#{params[:custid]}&jobid=#{params[:jobid]}&hashid=#{params[:hashid]}&filetype=#{params[:filetype]}")
end
get '/customers/upload/verify_hashtype' do
varWash(params)
hashfile = Hashfiles.first(id: params[:hashid])
@hashtypes = detectHashType("control/hashes/hashfile_upload_jobid-#{params[:jobid]}-#{hashfile.hash_str}.txt", params[:filetype])
@job = Jobs.first(id: params[:jobid])
haml :verify_hashtypes
end
post '/customers/upload/verify_hashtype' do
varWash(params)
if !params[:filetype] || params[:filetype].nil?
flash[:error] = 'You must specify a valid hashfile type.'
redirect to("/customers/upload/verify_hashtype?custid=#{params[:custid]}&jobid=#{params[:jobid]}&hashid=#{params[:hashid]}&filetype=#{params[:filetype]}")
end
filetype = params[:filetype]
hashfile = Hashfiles.first(id: params[:hashid])
if params[:hashtype] == '99999'
hashtype = params[:manualHash]
else
hashtype = params[:hashtype]
end
hash_file = "control/hashes/hashfile_upload_jobid-#{params[:jobid]}-#{hashfile.hash_str}.txt"
hash_array = []
File.open(hash_file, 'r').each do |line|
hash_array << line
end
@job = Jobs.first(id: params[:jobid])
customer_id = @job.customer_id
@job.hashfile_id = hashfile.id
@job.save
unless importHash(hash_array, customer_id, hashfile.id, filetype, hashtype)
flash[:error] = 'Error importing hashes'
redirect to("/customers/upload/verify_hashtype?custid=#{params[:custid]}&jobid=#{params[:jobid]}&hashid=#{params[:hashid]}&filetype=#{params[:filetype]}")
end
# detect if hash was previously cracked
# build hash of hashes and plains
if params[:retro_crack]
puts "detecting previously cracked hashes"
cracks = {}
@all_cracked_targets = Targets.all(cracked: 1)
@all_cracked_targets.each do |ct|
cracks[ct.originalhash.chomp.to_s] = ct.plaintext
end
# modify hash array if it is a pwdump
if filetype == "pwdump"
hash_array.map! {|item| item = item.split(":")[3].downcase}
end
# match already cracked hashes against hashesl to be uploaded, update db
# matches = []
count = 0
hash_array.each do |hash|
hash = hash.chomp.downcase.to_s
if cracks.key?(hash)
Targets.all(originalhash: hash, cracked: 0).update(cracked: 1, plaintext: cracks[hash])
count = count + 1
end
end
if count > 0
flash[:success] = "Hashview has previous cracked #{count} of these hashes"
end
end
# Delete file, no longer needed
File.delete(hash_file)
if params[:edit]
redirect to("/jobs/assign_tasks?jobid=#{params[:jobid]}&edit=1")
else
redirect to("/jobs/assign_tasks?jobid=#{params[:jobid]}")
end
end
############################
### Account controllers ####
get '/accounts/list' do
@users = User.all
haml :account_list
end
get '/accounts/create' do
haml :account_edit
end
post '/accounts/create' do
varWash(params)
if params[:username].nil? || params[:username].empty?
flash[:error] = 'You must have username.'
redirect to('/accounts/create')
end
if params[:password].nil? || params[:password].empty?
flash[:error] = 'You must have a password.'
redirect to('/accounts/create')
end
if params[:confirm].nil? || params[:confirm].empty?
flash[:error] = 'You must have a password.'
redirect to('/accounts/create')
end
# validate that no other user account exists
@users = User.all(username: params[:username])
if @users.empty?
if params[:password] != params[:confirm]
flash[:error] = 'Passwords do not match'
redirect to('/accounts/create')
else
new_user = User.new
new_user.username = params[:username]
new_user.password = params[:password]
new_user.email = params[:email] unless params[:email].nil? || params[:email].empty?
new_user.admin = 't'
new_user.save
end
else
flash[:error] = 'User account already exists.'
redirect to('/accounts/create')
end
redirect to('/accounts/list')
end
get '/accounts/edit/:account_id' do
varWash(params)
@user = User.first(id: params[:account_id])
haml :account_edit
end
post '/accounts/save' do
varWash(params)
if params[:account_id].nil? || params[:account_id].empty?
flash[:error] = 'Invalid account.'
redirect to('/accounts/list')
end
if params[:username].nil? || params[:username].empty?
flash[:error] = 'Invalid username.'
redirect to("/accounts/edit/#{params[:account_id]}")
end
if params[:password] != params[:confirm]
flash[:error] = 'Passwords do not match'
redirect to("/accounts/edit/#{params[:account_id]}")
end
user = User.first(id: params[:account_id])
user.username = params[:username]
user.password = params[:password] unless params[:password].nil? || params[:password].empty?
user.email = params[:email] unless params[:email].nil? || params[:email].empty?
user.save
flash[:success] = 'Account successfuly updated.'
redirect to('/accounts/list')
end
get '/accounts/delete/:id' do
varWash(params)
@user = User.first(id: params[:id])
@user.destroy unless @user.nil?
redirect to('/accounts/list')
end
############################
##### task controllers #####
get '/tasks/list' do
@tasks = Tasks.all
@wordlists = Wordlists.all
haml :task_list
end
get '/tasks/delete/:id' do
varWash(params)
@task = Tasks.first(id: params[:id])
if @task
@task.destroy
else
return 'No such task exists.'
end
redirect to('/tasks/list')
end
get '/tasks/edit/:id' do
varWash(params)
@task = Tasks.first(id: params[:id])
@wordlists = Wordlists.all
@settings = Settings.first
@rules = []
# list wordlists that can be used
Dir.foreach('control/rules/') do |item|
next if item == '.' || item == '..'
@rules << item
end
haml :task_edit
end
post '/tasks/edit/:id' do
varWash(params)
if !params[:name] || params[:name].nil?
flash[:error] = 'The task requires a name.'
redirect to("/tasks/edit/#{params[:id]}")
end
settings = Settings.first
wordlist = Wordlists.first(id: params[:wordlist])
if settings && !settings.hcbinpath
flash[:error] = 'No hashcat binary path is defined in global settings.'
redirect to('/settings')
end
task = Tasks.first(id: params[:id])
task.name = params[:name]
task.hc_attackmode = params[:attackmode]
if params[:attackmode] == 'dictionary'
task.wl_id = wordlist.id
task.hc_rule = params[:rule]
task.hc_mask = 'NULL'
elsif params[:attackmode] == 'maskmode'
task.wl_id = 'NULL'
task.hc_rule = 'NULL'
task.hc_mask = params[:mask]
end
task.save
redirect to('/tasks/list')
end
get '/tasks/create' do
varWash(params)
@settings = Settings.first
# TODO present better error msg
flash[:warning] = 'You must define hashcat\'s binary path in global settings first.' if @settings && @settings.hcbinpath.nil?
@rules = []
# list wordlists that can be used
Dir.foreach('control/rules/') do |item|
next if item == '.' || item == '..'
@rules << item
end
@wordlists = Wordlists.all
haml :task_edit
end
post '/tasks/create' do
varWash(params)
settings = Settings.first
if settings && !settings.hcbinpath
flash[:error] = 'No hashcat binary path is defined in global settings.'
redirect to('/settings')
end
if !params[:name] || params[:name].empty?
flash[:error] = 'You must provide a name for your task!'
redirect to('/tasks/create')
end
@tasks = Tasks.all(name: params[:name])
unless @tasks.nil?
@tasks.each do |task|
if task.name == params[:name]
flash[:error] = 'Name already in use, pick another'
redirect to('/tasks/create')
end
end
end
wordlist = Wordlists.first(id: params[:wordlist])
# mask field cannot be empty
if params[:attackmode] == 'maskmode'
if !params[:mask] || params[:mask].empty?
flash[:error] = 'Mask field cannot be left empty'
redirect to('/tasks/create')
end
end
task = Tasks.new
task.name = params[:name]
task.hc_attackmode = params[:attackmode]
if params[:attackmode] == 'dictionary'
task.wl_id = wordlist.id
task.hc_rule = params[:rule]
elsif params[:attackmode] == 'maskmode'
task.hc_mask = params[:mask]
end
task.save
flash[:success] = "Task #{task.name} successfully created"
redirect to('/tasks/list')
end
############################
##### job controllers #####
get '/jobs/list' do
@targets_cracked = {}
@customer_names = {}
@jobs = Jobs.all(order: [:id.desc])
@tasks = Tasks.all
@jobtasks = Jobtasks.all
@jobs.each do |entry|
@customers = Customers.first(id: [entry.customer_id])
@customer_names[entry.customer_id] = @customers.name
end
haml :job_list
end
get '/jobs/delete/:id' do
varWash(params)
@job = Jobs.first(id: params[:id])
if !@job
return 'No such job exists.'
else
@jobtasks = Jobtasks.all(job_id: params[:id])
@jobtasks.each do |jobtask|
jobtask.destroy unless jobtask.nil?
end
@job.destroy
end
redirect to('/jobs/list')
end
get '/jobs/create' do
varWash(params)
@customers = Customers.all
@job = Jobs.first(id: params[:jobid])
haml :job_edit
end
post '/jobs/create' do
varWash(params)
if !params[:job_name] || params[:job_name].empty?
flash[:error] = 'You must provide a name for your job.'
if params[:edit] == '1'
redirect to("/jobs/create?custid=#{:custid}&jobid=#{:jobid}&edit=1")
else
redirect to('/jobs/create')
end
end
if !params[:customer] || params[:customer].empty?
if !params[:cust_name] || params[:cust_name].empty?
flash[:error] = 'You must provide a customer for your job.'
if params[:edit] == '1'
redirect to("/jobs/create?custid=#{params[:custid]}&jobid=#{params[:jobid]}&edit=1")
else
redirect to('/jobs/create')
end
end
end
if params[:customer] && params[:customer] == 'add_new'
if !params[:cust_name] || params[:cust_name].empty?
flash[:error] = 'You must provide a customer name.'
if params[:edit] == '1'
redirect to("/jobs/create?custid=#{params[:custid]}&jobid=#{params[:jobid]}&edit=1")
else
redirect to('/jobs/create')
end
end
end
# Create a new customer if selected
if params[:customer] == 'add_new' || params[:customer].nil?
customer = Customers.new
customer.name = params[:cust_name]
customer.description = params[:cust_desc]
customer.save
end
if params[:customer] == 'add_new' || params[:customer].nil?
cust_id = customer.id
else
cust_id = params[:customer]
end
# create new or update existing job
if params[:edit] == '1'
job = Jobs.first(id: params[:jobid])
else
job = Jobs.new
end
job.name = params[:job_name]
job.last_updated_by = getUsername
job.customer_id = cust_id
if params[:notify] == 'on'
job.notify_completed = '1'
else
job.notify_completed = '0'
end
job.save
if params[:edit] == '1'
redirect to("/jobs/assign_hashfile?custid=#{cust_id}&jobid=#{job.id}&edit=1")
else
redirect to("/jobs/assign_hashfile?custid=#{cust_id}&jobid=#{job.id}")
end
end
get '/jobs/assign_hashfile' do
varWash(params)
@hashfiles = Hashfiles.all(customer_id: params[:custid])
@customer = Customers.first(id: params[:custid])
@job = Jobs.first(id: params[:jobid])
return 'No such job exists' unless @job
haml :assign_hashfile
end
post '/jobs/assign_hashfile' do
varWash(params)
if params[:hash_file] != 'add_new'
job = Jobs.first(id: params[:jobid])
job.hashfile_id = params[:hash_file]
job.save
end
if params[:edit] == '1'
job = Jobs.first(id: params[:jobid])
job.hashfile_id = params[:hash_file]
job.save
end
if params[:edit]
redirect to("/jobs/assign_tasks?jobid=#{params[:jobid]}&custid=#{params[:custid]}&hashid=#{params[:hash_file]}&edit=1")
else
redirect to("/jobs/assign_tasks?jobid=#{params[:jobid]}&custid=#{params[:custid]}&hashid=#{params[:hash_file]}")
end
end
get '/jobs/assign_tasks' do
varWash(params)
@job = Jobs.first(id: params[:jobid])
@jobtasks = Jobtasks.all(job_id: params[:jobid])
@tasks = Tasks.all
# we do this so we can embedded ruby into js easily
# js handles adding/selecting tasks associated with new job
taskhashforjs = {}
@tasks.each do |task|
taskhashforjs[task.id] = task.name
end
@taskhashforjs = taskhashforjs.to_json
haml :assign_tasks
end
post '/jobs/assign_tasks' do
varWash(params)
if !params[:tasks] || params[:tasks].nil?
if !params[:edit] || params[:edit].nil?
flash[:error] = 'You must assign atleast one task'
redirect to("/jobs/assign_tasks?jobid=#{params[:jobid]}&custid=#{params[:custid]}&hashid=#{params[:hash_file]}")
end
end
job = Jobs.first(id: params[:jobid])
job.status = 'Stopped'
job.save
# assign tasks to the job
if params[:tasks] && !params[:tasks].nil?
assignTasksToJob(params[:tasks], job.id)
end
# Resets jobtasks tables
if params[:edit] && !params[:edit].nil?
@jobtasks = Jobtasks.all(job_id: params[:jobid])
@jobtasks.each do |jobtask|
jobtask.status = 'READY'
jobtask.save
end
end
flash[:success] = 'Successfully created job.'
redirect to('/jobs/list')
end
get '/jobs/start/:id' do
varWash(params)
tasks = []
@job = Jobs.first(id: params[:id])
if !@job
return 'No such job exists.'
else
@jobtasks = Jobtasks.all(job_id: params[:id])
if !@jobtasks
return 'This job has no tasks to run.'
else
@jobtasks.each do |jt|
tasks << Tasks.first(id: jt.task_id)
end
end
end
tasks.each do |task|
jt = Jobtasks.first(task_id: task.id, job_id: @job.id)
# do not start tasks if they have already been completed.
# set all other tasks to status of queued
unless jt.status == 'Completed'
# set jobtask status to queued
jt.status = 'Queued'
jt.save
# toggle the job status to run
@job.status = 'Queued'
@job.save
cmd = buildCrackCmd(@job.id, task.id)
cmd = cmd + ' | tee -a control/outfiles/hcoutput_' + @job.id.to_s + '.txt'
p 'ENQUE CMD: ' + cmd
Resque.enqueue(Jobq, jt.id, cmd)
end
end
if @job.status == 'Completed'
flash[:error] = 'All tasks for this job have been completed. To prevent overwriting your results, you will need to create a new job with the same tasks in order to rerun the job.'
redirect to('/jobs/list')
end
redirect to('/home')
end
get '/jobs/stop/:id' do
varWash(params)
tasks = []
@job = Jobs.first(id: params[:id])
if !@job
return 'No such job exists.'
else
@jobtasks = Jobtasks.all(job_id: params[:id])
if !@jobtasks
return 'This job has no tasks to stop.'
else
@jobtasks.each do |jt|
tasks << Tasks.first(id: jt.task_id)
end
end
end
@job.status = 'Paused'
@job.save
tasks.each do |task|
jt = Jobtasks.first(task_id: task.id, job_id: @job.id)
# do not stop tasks if they have already been completed.
# set all other tasks to status of Canceled
if not jt.status == 'Completed' and not jt.status == 'Running'
jt.status = 'Canceled'
jt.save
cmd = buildCrackCmd(@job.id, task.id)
cmd = cmd + ' | tee -a control/outfiles/hcoutput_' + @job.id.to_s + '.txt'
puts 'STOP CMD: ' + cmd
Resque::Job.destroy('hashcat', Jobq, jt.id, cmd)
end
end
tasks.each do |task|
jt = Jobtasks.first(task_id: task.id, job_id: @job.id)
if jt.status == 'Running'
redirect to("/jobs/stop/#{jt.job_id}/#{jt.task_id}")
end
end
redirect to('/jobs/list')
end
get '/jobs/stop/:jobid/:taskid' do
varWash(params)
# validate if running
jt = Jobtasks.first(job_id: params[:jobid], task_id: params[:taskid])
unless jt.status == 'Running'
return 'That specific Job and Task is not currently running.'
end
# find pid
pid = `ps -ef | grep hashcat | grep hc_cracked_#{params[:jobid]}_#{params[:taskid]}.txt | grep -v 'ps -ef' | grep -v 'sh \-c' | awk '{print $2}'`
pid = pid.chomp
# update jobtasks to "canceled"
jt.status = 'Canceled'
jt.save
# Kill jobtask
`kill -9 #{pid}`
referer = request.referer.split('/')
if referer[3] == 'home'