/
rpc_db.rb
1990 lines (1823 loc) · 71.8 KB
/
rpc_db.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
# -*- coding: binary -*-
module Msf
module RPC
class RPC_Db < RPC_Base
private
include Metasploit::Credential::Creation
def db
self.framework.db.active
end
def find_workspace(wspace = nil)
if wspace and wspace != ""
return self.framework.db.find_workspace(wspace) || error(500, "Invalid workspace")
end
self.framework.db.workspace
end
def fix_cred_options(opts)
new_opts = fix_options(opts)
# Convert some of the raw data back to symbols
if new_opts[:origin_type]
new_opts[:origin_type] = new_opts[:origin_type].to_sym
end
if new_opts[:private_type]
new_opts[:private_type] = new_opts[:private_type].to_sym
end
new_opts
end
def fix_options(opts)
newopts = {}
opts.each do |k,v|
newopts[k.to_sym] = v
end
newopts
end
def opts_to_hosts(opts)
::ApplicationRecord.connection_pool.with_connection {
wspace = find_workspace(opts[:workspace])
hosts = []
if opts[:host] or opts[:address]
host = opts[:host] || opts[:address]
hent = wspace.hosts.find_by_address(host)
return hosts if hent == nil
hosts << hent if hent.class == ::Mdm::Host
hosts |= hent if hent.class == Array
elsif opts[:addresses]
return hosts if opts[:addresses].class != Array
conditions = {}
conditions[:address] = opts[:addresses]
hent = wspace.hosts.where(conditions)
hosts |= hent if hent.class == Array
end
return hosts
}
end
def opts_to_services(hosts,opts)
::ApplicationRecord.connection_pool.with_connection {
wspace = find_workspace(opts[:workspace])
services = []
if opts[:host] or opts[:address] or opts[:addresses]
return services if hosts.count < 1
hosts.each do |h|
if opts[:port] or opts[:proto]
conditions = {}
conditions[:port] = opts[:port] if opts[:port]
conditions[:proto] = opts[:proto] if opts[:proto]
sret = h.services.where(conditions)
next if sret == nil
services |= sret if sret.class == Array
services << sret if sret.class == ::Mdm::Service
else
services |= h.services
end
end
elsif opts[:port] or opts[:proto]
conditions = {}
conditions[:port] = opts[:port] if opts[:port]
conditions[:proto] = opts[:proto] if opts[:proto]
sret = wspace.services.where(conditions)
services |= sret if sret.class == Array
services << sret if sret.class == ::Mdm::Service
end
return services
}
end
def db_check
error(500, "Database Not Loaded") if not db
end
def init_db_opts_workspace(xopts)
db_check
opts = fix_options(xopts)
opts[:workspace] = find_workspace(opts[:workspace])
return opts, opts[:workspace]
end
def get_notes(xopts)
::ApplicationRecord.connection_pool.with_connection {
opts, wspace = init_db_opts_workspace(xopts)
notes = []
host = self.framework.db.get_host(opts)
return notes if not host
if opts[:proto] && opts[:port]
services = []
nret = host.services.find_by_proto_and_port(opts[:proto], opts[:port])
return ret if nret == nil
services << nret if nret.class == ::Mdm::Service
services |= nret if nret.class == Array
services.each do |s|
nret = nil
if opts[:ntype]
nret = s.notes.find_by_ntype(opts[:ntype])
else
nret = s.notes
end
next if nret == nil
notes << nret if nret.class == ::Mdm::Note
notes |= nret if nret.class == Array
end
else
notes = host.notes
end
notes
}
end
public
# Creates a cracked credential.
#
# @param [Hash] xopts Credential options. (See #create_credential Documentation)
# @return [Metasploit::Credential::Core]
# @see https://github.com/rapid7/metasploit-credential/blob/master/lib/metasploit/credential/creation.rb#L107 #create_credential Documentation.
# @see #rpc_create_credential
# @example Here's how you would use this from the client:
# opts = {
# username: username,
# password: password,
# core_id: core_id
# }
# rpc.call('db.create_cracked_credential', opts)
def rpc_create_cracked_credential(xopts)
opts = fix_cred_options(xopts)
create_cracked_credential(opts)
end
# Creates a credential.
#
# @param [Hash] xopts Credential options. (See #create_credential Documentation)
# @return [Hash] Credential data. It contains the following keys:
# * 'username' [String] Username saved.
# * 'private' [String] Password saved.
# * 'private_type' [String] Password type.
# * 'realm_value' [String] Realm.
# * 'realm_key' [String] Realm key.
# * 'host' [String] Host (Only available if there's a :last_attempted_at and :status)
# * 'sname' [String] Service name (only available if there's a :last_attempted_at and :status)
# * 'status' [Status] Login status (only available if there's a :last_attempted_at and :status)
# @see https://github.com/rapid7/metasploit-credential/blob/master/lib/metasploit/credential/creation.rb#L107 #create_credential Documentation.
# @example Here's how you would use this from the client:
# opts = {
# origin_type: :service,
# address: '192.168.1.100',
# port: 445,
# service_name: 'smb',
# protocol: 'tcp',
# module_fullname: 'auxiliary/scanner/smb/smb_login',
# workspace_id: myworkspace_id,
# private_data: 'password1',
# private_type: :password,
# username: 'Administrator'
# }
# rpc.call('db.create_cracked_credential', opts)
def rpc_create_credential(xopts)
opts = fix_cred_options(xopts)
core = create_credential(opts)
ret = {
username: core.public.try(:username),
private: core.private.try(:data),
private_type: core.private.try(:type),
realm_value: core.realm.try(:value),
realm_key: core.realm.try(:key)
}
if opts[:last_attempted_at] && opts[:status]
opts[:core] = core
opts[:last_attempted_at] = opts[:last_attempted_at].to_datetime
login = create_credential_login(opts)
ret[:host] = login.service.host.address,
ret[:sname] = login.service.name
ret[:status] = login.status
end
ret
end
# Sets the status of a login credential to a failure.
#
# @param [Hash] xopts Credential data (See #invalidate_login Documentation)
# @raise [Msf::RPC::Exception] If there's an option missing.
# @return [void]
# @see https://github.com/rapid7/metasploit-credential/blob/master/lib/metasploit/credential/creation.rb#L492 #invalidate_login Documentation
# @see https://github.com/rapid7/metasploit-model/blob/master/lib/metasploit/model/login/status.rb Status symbols.
# @example Here's how you would use this from the client:
# opts = {
# address: '192.168.1.100',
# port: 445,
# protocol: 'tcp',
# public: 'admin',
# private: 'password1',
# status: 'Incorrect'
# }
# rpc.call('db.invalidate_login', opts)
def rpc_invalidate_login(xopts)
opts = fix_cred_options(xopts)
invalidate_login(opts)
end
# Returns login credentials from a specific workspace.
#
# @param [Hash] xopts Options:
# @option xopts [String] :workspace Name of the workspace.
# @raise [Msf::RPC::ServerException] You might get one of these errors:
# * 500 ActiveRecord::ConnectionNotEstablished. Try: rpc.call('console.create').
# * 500 Database not loaded. Try: rpc.call('console.create')
# * 500 Invalid workspace.
# @return [Hash] Credentials with the following hash key:
# * 'creds' [Array<Hash>] An array of credentials. Each hash in the array will have the following:
# * 'user' [String] Username.
# * 'pass' [String] Password.
# * 'updated_at' [Integer] Last updated at.
# * 'type' [String] Password type.
# * 'host' [String] Host.
# * 'port' [Integer] Port.
# * 'proto' [String] Protocol.
# * 'sname' [String] Service name.
# @example Here's how you would use this from the client:
# rpc.call('db.creds', {})
def rpc_creds(xopts)
::ApplicationRecord.connection_pool.with_connection {
ret = {}
ret[:creds] = []
opts, wspace = init_db_opts_workspace(xopts)
limit = opts.delete(:limit) || 100
offset = opts.delete(:offset) || 0
query = Metasploit::Credential::Core.where(
workspace_id: wspace
).offset(offset).limit(limit)
query.each do |cred|
host = ''
port = 0
proto = ''
sname = ''
unless cred.logins.empty?
login = cred.logins.first
host = login.service.host.address.to_s
sname = login.service.name.to_s if login.service.name.present?
port = login.service.port.to_i
proto = login.service.proto.to_s
end
updated_at = nil
pass = nil
type = nil
unless cred.private.nil?
updated_at = cred.private.updated_at.to_i
pass = cred.private.data.to_s
type = cred.private.type.to_s
else
updated_at = cred.public.updated_at.to_i
end
ret[:creds] << {
:user => cred.public.username.to_s,
:pass => pass,
:updated_at => updated_at,
:type => type,
:host => host,
:port => port,
:proto => proto,
:sname => sname
}
end
ret
}
end
# Delete credentials from a specific workspace.
#
# @param [Hash] xopts Options:
# @option xopts [String] :workspace Name of the workspace.
# @raise [Msf::RPC::ServerException] You might get one of these errors:
# * 500 ActiveRecord::ConnectionNotEstablished. Try: rpc.call('console.create').
# * 500 Database not loaded. Try: rpc.call('console.create')
# * 500 Invalid workspace.
# @return [Hash] Credentials with the following hash key:
# * 'result' [String] A message that says 'success'.
# * 'deleted' [Array<Hash>] An array of credentials. Each hash in the array will have the following:
# * 'user' [String] Username.
# * 'pass' [String] Password.
# * 'updated_at' [Integer] Last updated at.
# * 'type' [String] Password type.
# * 'host' [String] Host.
# * 'port' [Integer] Port.
# * 'proto' [String] Protocol.
# * 'sname' [String] Service name.
# @example Here's how you would use this from the client:
# rpc.call('db.del_creds', {})
def rpc_del_creds(xopts)
::ApplicationRecord.connection_pool.with_connection {
deleted = []
ret = {}
ret[:creds] = []
opts, wspace = init_db_opts_workspace(xopts)
limit = opts.delete(:limit) || 100
offset = opts.delete(:offset) || 0
query = Metasploit::Credential::Core.where(
workspace_id: wspace
).offset(offset).limit(limit)
query.each do |cred|
host = ''
port = 0
proto = ''
sname = ''
unless cred.logins.empty?
login = cred.logins.first
host = login.service.host.address.to_s
sname = login.service.name.to_s if login.service.name.present?
port = login.service.port.to_i
proto = login.service.proto.to_s
end
ret[:creds] << {
:user => cred.public.username.to_s,
:pass => cred.private.data.to_s,
:updated_at => cred.private.updated_at.to_i,
:type => cred.private.type.to_s,
:host => host,
:port => port,
:proto => proto,
:sname => sname}
deleted << ret
cred.destroy
end
return { :result => 'success', :deleted => deleted }
}
end
# Returns information about hosts.
#
# @param [Hash] xopts Options:
# @option xopts [String] :workspace Name of the workspace.
# @raise [Msf::RPC::ServerException] You might get one of these errors:
# * 500 ActiveRecord::ConnectionNotEstablished. Try: rpc.call('console.create').
# * 500 Database not loaded. Try: rpc.call('console.create')
# * 500 Invalid workspace.
# @return [Hash] Host information that starts with the following hash key:
# * 'hosts' [Array<Hash>] An array of hosts. Each hash in the array will have the following:
# * 'created_at' [Integer] Creation date.
# * 'address' [String] IP address.
# * 'mac' [String] MAC address.
# * 'name' [String] Computer name.
# * 'state' [String] Host's state.
# * 'os_name' [String] Name of the operating system.
# * 'os_flavor' [String] OS flavor.
# * 'os_sp' [String] Service pack.
# * 'os_lang' [String] OS language.
# * 'updated_at' [Integer] Last updated at.
# * 'purpose' [String] Host purpose (example: server)
# * 'info' [String] Additional information about the host.
# @example Here's how you would use this from the client:
# rpc.call('db.hosts', {})
def rpc_hosts(xopts)
::ApplicationRecord.connection_pool.with_connection {
opts, wspace = init_db_opts_workspace(xopts)
conditions = {}
conditions[:state] = [Msf::HostState::Alive, Msf::HostState::Unknown] if opts[:only_up]
conditions[:address] = opts[:addresses] if opts[:addresses]
limit = opts.delete(:limit) || 100
offset = opts.delete(:offset) || 0
ret = {}
ret[:hosts] = []
wspace.hosts.where(conditions).offset(offset).order(:address).limit(limit).each do |h|
host = {}
host[:created_at] = h.created_at.to_i
host[:address] = h.address.to_s
host[:mac] = h.mac.to_s
host[:name] = h.name.to_s
host[:state] = h.state.to_s
host[:os_name] = h.os_name.to_s
host[:os_flavor] = h.os_flavor.to_s
host[:os_sp] = h.os_sp.to_s
host[:os_lang] = h.os_lang.to_s
host[:updated_at] = h.updated_at.to_i
host[:purpose] = h.purpose.to_s
host[:info] = h.info.to_s
ret[:hosts] << host
end
ret
}
end
# Returns information about services.
#
# @param [Hash] xopts Options:
# @option xopts [String] :workspace Name of workspace.
# @option xopts [Integer] :limit Limit.
# @option xopts [Integer] :offset Offset.
# @option xopts [String] :proto Protocol.
# @option xopts [String] :address Address.
# @option xopts [String] :ports Port range.
# @option xopts [String] :names Names (Use ',' as the separator).
# @raise [Msf::RPC::ServerException] You might get one of these errors:
# * 500 ActiveRecord::ConnectionNotEstablished. Try: rpc.call('console.create').
# * 500 Database not loaded. Try: rpc.call('console.create')
# * 500 Invalid workspace.
# @return [Hash] A hash with the following keys:
# * 'services' [Array<Hash>] In each hash of the array, you will get these keys:
# * 'host' [String] Host.
# * 'created_at' [Integer] Last created at.
# * 'updated_at' [Integer] Last updated at.
# * 'port' [Integer] Port.
# * 'proto' [String] Protocol.
# * 'state' [String] Service state.
# * 'name' [String] Service name.
# * 'info' [String] Additional information about the service.
# @example Here's how you would use this from the client:
# rpc.call('db.services', {})
def rpc_services( xopts)
::ApplicationRecord.connection_pool.with_connection {
opts, wspace = init_db_opts_workspace(xopts)
limit = opts.delete(:limit) || 100
offset = opts.delete(:offset) || 0
conditions = {}
conditions[:state] = [Msf::ServiceState::Open] if opts[:only_up]
conditions[:proto] = opts[:proto] if opts[:proto]
conditions["hosts.address"] = opts[:addresses] if opts[:addresses]
conditions[:port] = Rex::Socket.portspec_to_portlist(opts[:ports]) if opts[:ports]
conditions[:name] = opts[:names].strip().split(",") if opts[:names]
ret = {}
ret[:services] = []
wspace.services.includes(:host).where(conditions).offset(offset).limit(limit).each do |s|
service = {}
host = s.host
service[:host] = host.address || "unknown"
service[:created_at] = s[:created_at].to_i
service[:updated_at] = s[:updated_at].to_i
service[:port] = s[:port]
service[:proto] = s[:proto].to_s
service[:state] = s[:state].to_s
service[:name] = s[:name].to_s
service[:info] = s[:info].to_s
ret[:services] << service
end
ret
}
end
# Returns information about reported vulnerabilities.
#
# @param [Hash] xopts Options:
# @option xopts [String] :workspace Name of workspace.
# @option xopts [Integer] :limit Limit.
# @option xopts [Integer] :offset Offset.
# @option xopts [String] :proto Protocol.
# @option xopts [String] :address Address.
# @option xopts [String] :ports Port range.
# @raise [Msf::RPC::ServerException] You might get one of these errors:
# * 500 ActiveRecord::ConnectionNotEstablished. Try: rpc.call('console.create').
# * 500 Database not loaded. Try: rpc.call('console.create')
# * 500 Invalid workspace.
# @return [Hash] A hash with the following key:
# * 'vulns' [Array<Hash>] In each hash of the array, you will get these keys:
# * 'port' [Integer] Port.
# * 'proto' [String] Protocol.
# * 'time' [Integer] Time reported.
# * 'host' [String] Vulnerable host.
# * 'name' [String] Exploit that was used.
# * 'refs' [String] Vulnerability references.
# @example Here's how you would use this from the client:
# rpc.call('db.vulns', {})
def rpc_vulns(xopts)
::ApplicationRecord.connection_pool.with_connection {
opts, wspace = init_db_opts_workspace(xopts)
limit = opts.delete(:limit) || 100
offset = opts.delete(:offset) || 0
conditions = {}
conditions["hosts.address"] = opts[:address] if opts[:address]
conditions[:name] = opts[:names].strip().split(",") if opts[:names]
conditions["services.port"] = Rex::Socket.portspec_to_portlist(opts[:ports]) if opts[:port]
conditions["services.proto"] = opts[:proto] if opts[:proto]
ret = {}
ret[:vulns] = []
wspace.vulns.includes(:service).where(conditions).offset(offset).limit(limit).each do |v|
vuln = {}
reflist = v.refs.map { |r| r.name }
if v.service
vuln[:port] = v.service.port
vuln[:proto] = v.service.proto
else
vuln[:port] = nil
vuln[:proto] = nil
end
vuln[:time] = v.created_at.to_i
vuln[:host] = v.host.address || nil
vuln[:name] = v.name
vuln[:refs] = reflist.join(',')
ret[:vulns] << vuln
end
ret
}
end
# Returns information about workspaces.
#
# @raise [Msf::RPC::Exception] Database not loaded.
# @return [Hash] A hash with the following key:
# * 'workspaces' [Array<Hash>] In each hash of the array, you will get these keys:
# * 'id' [Integer] Workspace ID.
# * 'name' [String] Workspace name.
# * 'created_at' [Integer] Last created at.
# * 'updated_at' [Integer] Last updated at.
# @example Here's how you would use this from the client:
# rpc.call('db.workspaces')
def rpc_workspaces
db_check
res = {}
res[:workspaces] = []
self.framework.db.workspaces.each do |j|
ws = {}
ws[:id] = j.id
ws[:name] = j.name
ws[:created_at] = j.created_at.to_i
ws[:updated_at] = j.updated_at.to_i
res[:workspaces] << ws
end
res
end
# Returns the current workspace.
#
# @raise [Msf::RPC::Exception] Database not loaded. Try: rpc.call('console.create')
# @return [Hash] A hash with the following keys:
# * 'workspace' [String] Workspace name.
# * 'workspace_id' [Integer] Workspace ID.
# @example Here's how you would use this from the client:
# rpc.call('db.current_workspace')
def rpc_current_workspace
db_check
{ "workspace" => self.framework.db.workspace.name, "workspace_id" => self.framework.db.workspace.id }
end
# Returns the current workspace.
#
# @param [String] wspace workspace name.
# @raise [Msf::RPC::Exception] You might get one of the following errors:
# * 500 Database not loaded.
# * 500 Invalid workspace.
# @return [Hash] A hash with the following key:
# * 'workspace' [Array<Hash>] In each hash of the array, you will get these keys:
# * 'name' [String] Workspace name.
# * 'id' [Integer] Workspace ID.
# * 'created_at' [Integer] Last created at.
# * 'updated_at' [Integer] Last updated at.
# @example Here's how you would use this from the client:
# rpc.call('db.get_workspace', 'default')
def rpc_get_workspace(wspace)
db_check
wspace = find_workspace(wspace)
ret = {}
ret[:workspace] = []
if wspace
w = {}
w[:name] = wspace.name
w[:id] = wspace.id
w[:created_at] = wspace.created_at.to_i
w[:updated_at] = wspace.updated_at.to_i
ret[:workspace] << w
end
ret
end
# Sets a workspace.
#
# @param [String] wspace Workspace name.
# @raise [Msf::RPC::ServerException] You might get one of these errors:
# * 500 ActiveRecord::ConnectionNotEstablished. Try: rpc.call('console.create').
# * 500 Database not loaded. Try: rpc.call('console.create')
# * 500 Invalid workspace
# * 404 Workspace not found.
# @return [Hash] A hash indicating whether the action was successful or not. You will get:
# * 'result' [String] A message that says either 'success' or 'failed'
# @example Here's how you would use this from the client:
# # This will set the current workspace to 'default'
# rpc.call('db.set_workspace', 'default')
def rpc_set_workspace(wspace)
::ApplicationRecord.connection_pool.with_connection {
db_check
workspace = find_workspace(wspace)
if workspace
self.framework.db.workspace = workspace
return { 'result' => "success" }
end
{ 'result' => 'failed' }
}
end
# Deletes a workspace.
#
# @param [String] wspace Workspace name.
# @raise [Msf::RPC::ServerException] You might get one of these errors:
# * 500 ActiveRecord::ConnectionNotEstablished. Try: rpc.call('console.create').
# * 500 Database not loaded. Try: rpc.call('console.create')
# * 404 Workspace not found.
# @return [Hash] A hash indicating the action was successful. It contains the following:
# * 'result' [String] A message that says 'success'.
# @example Here's how you would use this from the client:
# rpc.call('db.del_workspace', 'temp_workspace')
def rpc_del_workspace(wspace)
::ApplicationRecord.connection_pool.with_connection {
db_check
# Delete workspace
workspace = find_workspace(wspace)
if workspace.nil?
error(404, "Workspace not found: #{wspace}")
elsif workspace.default?
workspace.destroy
workspace = self.framework.db.add_workspace(workspace.name)
else
# switch to the default workspace if we're about to delete the current one
self.framework.db.workspace = self.framework.db.default_workspace if self.framework.db.workspace.name == workspace.name
# now destroy the named workspace
workspace.destroy
end
{ 'result' => "success" }
}
end
# Adds a new workspace.
#
# @param [String] wspace Workspace name.
# @raise [Msf::RPC::ServerException] You might get one of these errors:
# * 500 ActiveRecord::ConnectionNotEstablished. Try: rpc.call('console.create').
# * 500 Database not loaded. Try: rpc.call('console.create')
# * 500 Invalid workspace.
# @return [Hash] A hash indicating whether the action was successful or not. You get:
# * 'result' [String] A message that says either 'success' or 'failed'.
# @example Here's how you would use this from the client:
# * rpc.call('db.add_workspace', 'my_new_workspace')
def rpc_add_workspace(wspace)
::ApplicationRecord.connection_pool.with_connection {
db_check
wspace = self.framework.db.add_workspace(wspace)
return { 'result' => 'success' } if wspace
{ 'result' => 'failed' }
}
end
# Returns information about a host.
#
# @param [Hash] xopts Options (:addr, :address, :host are the same thing, and you only need one):
# @option xopts [String] :workspace Name of the workspace.
# @option xopts [String] :addr Host address.
# @option xopts [String] :address Same as :addr.
# @option xopts [String] :host Same as :address.
# @raise [Msf::RPC::ServerException] You might get one of these errors:
# * 500 ActiveRecord::ConnectionNotEstablished. Try: rpc.call('console.create').
# * 500 Database not loaded. Try: rpc.call('console.create')
# * 500 Invalid workspace.
# @return [Hash] A hash that contains the following:
# * 'host' [Array<Hash>] Each hash in the array contains the following:
# * 'created_at' [Integer] Last created at.
# * 'address' [String] Address.
# * 'mac' [String] Mac address.
# * 'name' [String] Host name.
# * 'state' [String] Host state.
# * 'os_name' [String] OS name.
# * 'os_flavor' [String] OS flavor.
# * 'os_sp' [String] OS service pack.
# * 'os_lang' [String] OS language.
# * 'updated_at' [Integer] Last updated at.
# * 'purpose' [String] Purpose. Example: 'server'.
# * 'info' [String] Additional information.
# @example Here's how you would use this from the client:
# rpc.call('db.get_host', {:workspace => 'default', :host => ip})
def rpc_get_host(xopts)
::ApplicationRecord.connection_pool.with_connection {
opts, wspace = init_db_opts_workspace(xopts)
ret = {}
ret[:host] = []
opts = fix_options(xopts)
h = self.framework.db.get_host(opts)
if h
host = {}
host[:created_at] = h.created_at.to_i
host[:address] = h.address.to_s
host[:mac] = h.mac.to_s
host[:name] = h.name.to_s
host[:state] = h.state.to_s
host[:os_name] = h.os_name.to_s
host[:os_flavor] = h.os_flavor.to_s
host[:os_sp] = h.os_sp.to_s
host[:os_lang] = h.os_lang.to_s
host[:updated_at] = h.updated_at.to_i
host[:purpose] = h.purpose.to_s
host[:info] = h.info.to_s
ret[:host] << host
end
ret
}
end
# Returns analysis of module suggestions for known data about a host.
#
# @param [Hash] xopts Options (:addr, :address, :host are the same thing, and you only need one):
# @option xopts [String] :workspace Name of the workspace.
# @option xopts [String] :addr Host address.
# @option xopts [String] :address Same as :addr.
# @option xopts [String] :host Same as :address.
# @option xopts [Map<String, Object>] :analyze_options All returned modules will support these options
# * [Array<String>] :payloads Modules returned will be compatible with at least one payload
# @raise [Msf::RPC::ServerException] You might get one of these errors:
# * 500 ActiveRecord::ConnectionNotEstablished. Try: rpc.call('console.create').
# * 500 Database not loaded. Try: rpc.call('console.create')
# * 500 Invalid workspace.
# @return [Hash] A hash that contains the following:
# * 'host' [Array<Hash>] Each hash in the array contains the following:
# * 'address' [String] Address.
# * 'modules' [Array<Hash>] Each hash in the array modules contains the following:
# * 'mtype' [String] Module type.
# * 'mname' [String] Module name. For example: 'windows/wlan/wlan_profile'
# @example Here's how you would use this from the client:
# rpc.call('db.analyze_host', {:workspace => 'default', :host => ip})
def rpc_analyze_host(xopts)
::ApplicationRecord.connection_pool.with_connection {
_opts, _wspace = init_db_opts_workspace(xopts)
ret = {
host: {}
}
opts = fix_options(xopts)
opts.deep_symbolize_keys!
host = self.framework.db.get_host(opts)
# consider expanding to support a list of hosts?
return ret unless host
# as a consumer options should be a filter object not a top level param
analyze_options = {}
analyze_options = opts[:analyze_options] if opts[:analyze_options]
analyze_result = self.framework.analyze.host(host, **analyze_options)
# this should just serialize the result as hosts details why did it end up limited?
module_suggestions = analyze_result[:results].sort_by { |result| result.mod.fullname }
host_detail = {
address: host.address,
modules: module_suggestions.map do |result|
mod = result.mod
{
mtype: mod.type,
mname: mod.fullname,
state: result.state,
description: result.description,
options: {
invalid: result.invalid,
missing: result.missing
}
}
end
}
ret[:host] = host_detail
ret
}
end
# Reports a new host to the database.
#
# @param [Hash] xopts Information to report about the host. See below:
# @option xopts [String] :workspace Name of the workspace.
# @option xopts [String] :host IP address. You must supply this.
# @option xopts [String] :state One of the Msf::HostState constants. (See Most::HostState Documentation)
# @option xopts [String] :os_name Something like "Windows", "Linux", or "Mac OS X".
# @option xopts [String] :os_flavor Something like "Enterprise", "Pro", or "Home".
# @option xopts [String] :os_sp Something like "SP2".
# @option xopts [String] :os_lang Something like "English", "French", or "en-US".
# @option xopts [String] :arch one of the ARCH_* constants. (see ARCH Documentation)
# @option xopts [String] :mac Mac address.
# @option xopts [String] :scope Interface identifier for link-local IPv6.
# @option xopts [String] :virtual_host The name of the VM host software, eg "VMWare", "QEMU", "Xen", etc.
# @see https://github.com/rapid7/metasploit-framework/blob/master/lib/msf/core/host_state.rb Most::HostState Documentation.
# @see https://github.com/rapid7/metasploit-framework/blob/master/lib/rex/constants.rb#L66 ARCH Documentation.
# @raise [Msf::RPC::ServerException] You might get one of these errors:
# * 500 ActiveRecord::ConnectionNotEstablished. Try: rpc.call('console.create').
# * 500 Database not loaded. Try: rpc.call('console.create')
# * 500 Invalid workspace.
# @return [Hash] A hash indicating whether the action was successful or not. It contains the following:
# * 'result' [String] A message that says either 'success' or 'failed'.
# @example Here's how you would use this from the client:
# rpc.call('db.report_host', {:host => ip})
def rpc_report_host(xopts)
::ApplicationRecord.connection_pool.with_connection {
opts, wspace = init_db_opts_workspace(xopts)
res = self.framework.db.report_host(opts)
return { :result => 'success' } if res
{ :result => 'failed' }
}
end
# Reports a service to the database.
#
# @param [Hash] xopts Information to report about the service. See below:
# @option xopts [String] :workspace Name of the workspace.
# @option xopts [String] :host Required. The host where this service is running.
# @option xopts [String] :port Required. The port where this service listens.
# @option xopts [String] :proto Required. The transport layer protocol (e.g. tcp, udp).
# @option xopts [String] :name The application layer protocol (e.g. ssh, mssql, smb).
# @option xopts [String] :sname An alias for the above
# @raise [Msf::RPC::ServerException] You might get one of these errors:
# * 500 ActiveRecord::ConnectionNotEstablished. Try: rpc.call('console.create').
# * 500 Database not loaded. Try: rpc.call('console.create')
# * 500 Invalid workspace.
# @return [Hash] A hash indicating whether the action was successful or not. It contains:
# * 'result' [String] A message that says either 'success' or 'failed'.
# @example Here's how you would use this from the client:
# rpc.call('db.report_service', {:host=>ip, :port=>8181, :proto=>'tcp', :name=>'http'})
def rpc_report_service(xopts)
::ApplicationRecord.connection_pool.with_connection {
opts, wspace = init_db_opts_workspace(xopts)
res = self.framework.db.report_service(opts)
return { :result => 'success' } if res
{ :result => 'failed' }
}
end
# Returns information about a service.
#
# @param [Hash] xopts Filters for the search, see below:
# @option xopts [String] :workspace Workspace name.
# @option xopts [String] :proto Protocol.
# @option xopts [Integer] :port Port.
# @option xopts [String] :names Service names.
# @raise [Msf::RPC::ServerException] You might get one of these errors:
# * 500 ActiveRecord::ConnectionNotEstablished. Try: rpc.call('console.create').
# * 500 Database not loaded. Try: rpc.call('console.create')
# * 500 Invalid workspace.
# @return [Hash] A hash that contains the following key:
# * 'service' [Array<Hash>] Each hash in the array contains the following:
# * 'host' [String] Host address.
# * 'created_at' [Integer] Creation date.
# * 'port' [Integer] Port.
# * 'proto' [String] Protocol.
# * 'state' [String] Service state.
# * 'name' [String] Service name.
# * 'info' [String] Additional information.
# @example Here's how you would use this from the client:
# rpc.call('db.get_service', {:workspace=>'default', :proto=>'tcp', :port=>443})
def rpc_get_service(xopts)
::ApplicationRecord.connection_pool.with_connection {
opts, wspace = init_db_opts_workspace(xopts)
ret = {}
ret[:service] = []
host = self.framework.db.get_host(opts)
services = []
sret = nil
if host && opts[:proto] && opts[:port]
sret = host.services.find_by_proto_and_port(opts[:proto], opts[:port])
elsif opts[:proto] && opts[:port]
conditions = {}
conditions[:state] = [Msf::ServiceState::Open] if opts[:up]
conditions[:proto] = opts[:proto] if opts[:proto]
conditions[:port] = opts[:port] if opts[:port]
conditions[:name] = opts[:names] if opts[:names]
sret = wspace.services.where(conditions).order("hosts.address, port").to_a()
elsif host
sret = host.services.to_a()
end
return ret if sret == nil
services << sret if sret.class == ::Mdm::Service
services |= sret if sret.class == Array
services.each do |s|
service = {}
host = s.host
service[:host] = host.address || "unknown"
service[:created_at] = s[:created_at].to_i
service[:updated_at] = s[:updated_at].to_i
service[:port] = s[:port]
service[:proto] = s[:proto].to_s
service[:state] = s[:state].to_s
service[:name] = s[:name].to_s
service[:info] = s[:info].to_s
ret[:service] << service
end
ret
}
end
# Returns a note.
#
# @param [Hash] xopts Options.
# @option xopts [String] :workspace Workspace name.
# @option xopts [String] :addr Host address.
# @option xopts [String] :address Same as :addr.
# @option xopts [String] :host Same as :address.
# @option xopts [String] :proto Protocol.
# @option xopts [Integer] :port Port.
# @option xopts [String] :ntype Note type.
# @raise [Msf::RPC::ServerException] You might get one of these errors:
# * 500 ActiveRecord::ConnectionNotEstablished. Try: rpc.call('console.create').
# * 500 Database not loaded. Try: rpc.call('console.create')
# * 500 Invalid workspace.
# @return [Hash] A hash that contains the following:
# * 'note' [Array<Hash>] Each hash in the array contains the following:
# * 'host' [String] Host.
# * 'port' [Integer] Port.
# * 'proto' [String] Protocol.
# * 'created_at' [Integer] Last created at.
# * 'updated_at' [Integer] Last updated at.
# * 'ntype' [String] Note type.
# * 'data' [String] Note data.
# * 'critical' [String] A boolean indicating criticality.
# * 'seen' [String] A boolean indicating whether the note has been seen before.
# @example Here's how you would use this from the client:
# rpc.call('db.get_note', {:proto => 'tcp', :port => 80})
def rpc_get_note(xopts)
ret = {}
ret[:note] = []
notes = get_notes(xopts)
notes.each do |n|
note = {}
host = n.host
note[:host] = host.address || "unknown"
if n.service
note[:port] = n.service.port
note[:proto] = n.service.proto
end
note[:created_at] = n[:created_at].to_i
note[:updated_at] = n[:updated_at].to_i
note[:ntype] = n[:ntype].to_s
note[:data] = n[:data]
note[:critical] = n[:critical].to_s
note[:seen] = n[:seen].to_s
ret[:note] << note
end
ret
end
# Returns information about a client connection.
#
# @param [Hash] xopts Options:
# @option xopts [String] :workspace Workspace name.