Class: Msf::RPC::RPC_Db

Inherits:
RPC_Base show all
Defined in:
lib/msf/core/rpc/v10/rpc_db.rb

Instance Attribute Summary

Attributes inherited from RPC_Base

#framework, #service, #tokens, #users

Instance Method Summary collapse

Methods inherited from RPC_Base

#error, #initialize

Constructor Details

This class inherits a constructor from Msf::RPC::RPC_Base

Instance Method Details

#rpc_add_workspace(wspace) ⇒ Object


260
261
262
263
264
265
266
267
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 260

def rpc_add_workspace(wspace)
::ActiveRecord::Base.connection_pool.with_connection {
  db_check
  wspace = self.framework.db.add_workspace(wspace)
  return { 'result' => 'success' } if(wspace)
  { 'result' => 'failed' }
}
end

#rpc_clients(xopts) ⇒ Object


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
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 926

def rpc_clients(xopts)
::ActiveRecord::Base.connection_pool.with_connection {
  opts, wspace = init_db_opts_workspace(xopts)
  limit = opts.delete(:limit) || 100
  offset = opts.delete(:offset) || 0

  conditions = {}
  conditions[:ua_name] = opts[:ua_name] if opts[:ua_name]
  conditions[:ua_ver] = opts[:ua_ver] if opts[:ua_ver]
  conditions["hosts.address"] = opts[:addresses] if opts[:addresses]

  ret = {}
  ret[:clients] = []

  wspace.clients.all(:include => :host, :conditions => conditions,
      :limit => limit, :offset => offset).each do |c|
    client = {}
    client[:host] = c.host.address.to_s if c.host
    client[:ua_string] = c.ua_string
    client[:ua_name] = c.ua_name
    client[:ua_ver] = c.ua_ver
    client[:created_at] = c.created_at.to_i
    client[:updated_at] = c.updated_at.to_i
    ret[:clients] << client
  end
  ret
}
end

#rpc_connect(xopts) ⇒ Object


1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 1013

def rpc_connect(xopts)
  opts = fix_options(xopts)
  if(not self.framework.db.driver and not opts[:driver])
    return { :result => 'failed' }
  end

  if opts[:driver]
    if self.framework.db.drivers.include?(opts[:driver])
      self.framework.db.driver = opts[:driver]
    else
      return { :result => 'failed' }
    end
  end

  driver = self.framework.db.driver

  case driver
  when 'postgresql'
    opts['adapter'] = 'postgresql'
  else
    return { :result => 'failed' }
  end

  if (not self.framework.db.connect(opts))
    return { :result => 'failed' }
  end
  return { :result => 'success' }

end

#rpc_creds(xopts) ⇒ Object

right now workspace is the only option supported


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
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 842

def rpc_creds(xopts)
::ActiveRecord::Base.connection_pool.with_connection {
  opts, wspace = init_db_opts_workspace(xopts)
  limit = opts.delete(:limit) || 100
  offset = opts.delete(:offset) || 0

  ret = {}
  ret[:creds] = []
  ::Mdm::Cred.find(:all, :include => {:service => :host}, :conditions => ["hosts.workspace_id = ?",
      framework.db.workspace.id ], :limit => limit, :offset => offset).each do |c|
    cred = {}
    cred[:host] = c.service.host.address if(c.service.host)
    cred[:updated_at] = c.updated_at.to_i
    cred[:port] = c.service.port
    cred[:proto] = c.service.proto
    cred[:sname] = c.service.name
    cred[:type] = c.ptype
    cred[:user] = c.user
    cred[:pass] = c.pass
    cred[:active] = c.active
    ret[:creds] << cred
  end
  ret
}
end

#rpc_current_workspaceObject


208
209
210
211
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 208

def rpc_current_workspace
  db_check
  { "workspace" => self.framework.db.workspace.name }
end

#rpc_del_client(xopts) ⇒ Object


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
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 955

def rpc_del_client(xopts)
::ActiveRecord::Base.connection_pool.with_connection {
  db_check
  opts = fix_options(xopts)
  wspace = find_workspace(opts[:workspace])
  hosts = []
  clients = []

  if opts[:host] or opts[:address] or opts[:addresses]
    hosts = opts_to_hosts(opts)
  else
    hosts = wspace.hosts
  end

  hosts.each do |h|
    cret = nil
    if opts[:ua_name] or opts[:ua_ver]
      conditions = {}
      conditions[:ua_name] = opts[:ua_name] if opts[:ua_name]
      conditions[:ua_ver] = opts[:ua_ver] if opts[:ua_ver]
      cret = h.clients.all(:conditions => conditions)
    else
      cret = h.clients
    end
    next if cret == nil
    clients << cret if cret.class == ::Mdm::Client
    clients |= cret if cret.class == Array
  end

  deleted = []
  clients.each do |c|
    dent = {}
    dent[:address] = c.host.address.to_s
    dent[:ua_string] = c.ua_string
    deleted << dent
    c.destroy
  end

  { :result => 'success', :deleted => deleted }
}
end

#rpc_del_host(xopts) ⇒ Object


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
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 720

def rpc_del_host(xopts)
::ActiveRecord::Base.connection_pool.with_connection {
  db_check
  opts = fix_options(xopts)
  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 { :result => 'failed' } if hent == nil or hent.class != ::Mdm::Host
    hosts << hent
  elsif opts[:addresses]
    return { :result => 'failed' } if opts[:addresses].class != Array
    conditions = { :address => opts[:addresses] }
    hent = wspace.hosts.all(:conditions => conditions)
    return { :result => 'failed' } if hent == nil
    hosts |= hent if hent.class == Array
    hosts << hent if hent.class == ::Mdm::Host
  end
  deleted = []
  hosts.each do |h|
    deleted << h.address.to_s
    h.destroy
  end

  return { :result => 'success', :deleted => deleted }
}
end

#rpc_del_note(xopts) ⇒ Object


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
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 596

def rpc_del_note(xopts)
::ActiveRecord::Base.connection_pool.with_connection {
  opts, wspace = init_db_opts_workspace(xopts)
  hosts  = []
  services = []
  notes = []

  if opts[:host] or opts[:address] or opts[:addresses]
    hosts = opts_to_hosts(opts)
  end

  if opts[:port] or opts[:proto]
    if opts[:host] or opts[:address] or opts[:addresses]
      services = opts_to_services(hosts,opts)
    else
      services = opts_to_services([],opts)
    end
  end

  if opts[:port] or opts[:proto]
    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
  elsif opts[:address] or opts[:host] or opts[:addresses]
    hosts.each do |h|
      nret = nil
      if opts[:ntype]
        nret = h.notes.find_by_ntype(opts[:ntype])
      else
        nret = h.notes
      end
      next if nret == nil
      notes << nret if nret.class == ::Mdm::Note
      notes |= nret if nret.class == Array
    end
  else
    nret = nil
    if opts[:ntype]
      nret = wspace.notes.find_by_ntype(opts[:ntype])
    else
      nret = wspace.notes
    end
    notes << nret if nret.class == ::Mdm::Note
    notes |= nret if nret.class == Array
  end
  deleted = []
  notes.each do |n|
    dent = {}
    dent[:address] = n.host.address.to_s if n.host
    dent[:port] = n.service.port if n.service
    dent[:proto] = n.service.proto if n.service
    dent[:ntype] = n.ntype
    deleted << dent
    n.destroy
  end

  return { :result => 'success', :deleted => deleted }
}
end

#rpc_del_service(xopts) ⇒ Object


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
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 664

def rpc_del_service(xopts)
::ActiveRecord::Base.connection_pool.with_connection {
  opts, wspace = init_db_opts_workspace(xopts)
  hosts  = []
  services = []
  if opts[:host] or opts[:address]
    host = opts[:host] || opts[:address]
    hent = wspace.hosts.find_by_address(host)
    return { :result => 'failed' } if hent == nil or hent.class != ::Mdm::Host
    hosts << hent
  elsif opts[:addresses]
    return { :result => 'failed' } if opts[:addresses].class != Array
    conditions = { :address => opts[:addresses] }
    hent = wspace.hosts.all(:conditions => conditions)
    return { :result => 'failed' } if hent == nil
    hosts |= hent if hent.class == Array
    hosts << hent if hent.class == ::Mdm::Host
  end
  if opts[:addresses] or opts[:address] or opts[:host]
    hosts.each do |h|
      sret = nil
      if opts[:port] or opts[:proto]
        conditions = {}
        conditions[:port] = opts[:port] if opts[:port]
        conditions[:proto] = opts[:proto] if opts[:proto]
        sret = h.services.all(:conditions => conditions)
        next if sret == nil
        services << sret if sret.class == ::Mdm::Service
        services |= sret if sret.class == Array
      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.all(:conditions => conditions)
    services << sret if sret and sret.class == ::Mdm::Service
    services |= sret if sret and sret.class == Array
  end

  deleted = []
  services.each do |s|
    dent = {}
    dent[:address] = s.host.address.to_s
    dent[:port] = s.port
    dent[:proto] = s.proto
    deleted << dent
    s.destroy
  end

  return { :result => 'success', :deleted => deleted }
}
end

#rpc_del_vuln(xopts) ⇒ Object


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
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 527

def rpc_del_vuln(xopts)
::ActiveRecord::Base.connection_pool.with_connection {
  opts, wspace = init_db_opts_workspace(xopts)
  hosts  = []
  services = []
  vulns = []

  if opts[:host] or opts[:address] or opts[:addresses]
    hosts = opts_to_hosts(opts)
  end

  if opts[:port] or opts[:proto]
    if opts[:host] or opts[:address] or opts[:addresses]
      services = opts_to_services(hosts,opts)
    else
      services = opts_to_services([],opts)
    end
  end

  if opts[:port] or opts[:proto]
    services.each do |s|
      vret = nil
      if opts[:name]
        vret = s.vulns.find_by_name(opts[:name])
      else
        vret = s.vulns
      end
      next if vret == nil
      vulns << vret if vret.class == ::Mdm::Vuln
      vulns |= vret if vret.class == Array
    end
  elsif opts[:address] or opts[:host] or opts[:addresses]
    hosts.each do |h|
      vret = nil
      if opts[:name]
        vret = h.vulns.find_by_name(opts[:name])
      else
        vret = h.vulns
      end
      next if vret == nil
      vulns << vret if vret.class == ::Mdm::Vuln
      vulns |= vret if vret.class == Array
    end
  else
    vret = nil
    if opts[:name]
      vret = wspace.vulns.find_by_name(opts[:name])
    else
      vret = wspace.vulns
    end
    vulns << vret if vret.class == ::Mdm::Vuln
    vulns |= vret if vret.class == Array
  end

  deleted = []
  vulns.each do |v|
    dent = {}
    dent[:address] = v.host.address.to_s if v.host
    dent[:port] = v.service.port if v.service
    dent[:proto] = v.service.proto if v.service
    dent[:name] = v.name
    deleted << dent
    v.destroy
  end

  return { :result => 'success', :deleted => deleted }
}
end

#rpc_del_workspace(wspace) ⇒ Object


240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 240

def rpc_del_workspace(wspace)
::ActiveRecord::Base.connection_pool.with_connection {
  db_check
  # Delete workspace
  workspace = self.framework.db.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

#rpc_disconnectObject


1064
1065
1066
1067
1068
1069
1070
1071
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 1064

def rpc_disconnect
  if (self.framework.db)
    self.framework.db.disconnect()
    return { :result => 'success' }
  else
    return { :result => 'failed' }
  end
end

#rpc_driver(xopts) ⇒ Object


997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 997

def rpc_driver(xopts)
  opts = fix_options(xopts)
  if opts[:driver]
    if self.framework.db.drivers.include?(opts[:driver])
      self.framework.db.driver = opts[:driver]
      return { :result => 'success' }
    else
      return { :result => 'failed' }

    end
  else
    return { :driver => self.framework.db.driver.to_s }
  end
  return { :result => 'failed' }
end

#rpc_events(xopts) ⇒ Object


760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 760

def rpc_events(xopts)
::ActiveRecord::Base.connection_pool.with_connection {
  opts, wspace = init_db_opts_workspace(xopts)
  limit = opts.delete(:limit) || 100
  offset = opts.delete(:offset) || 0

  ret = {}
  ret[:events] = []

  wspace.events.all(:limit => limit, :offset => offset).each do |e|
    event = {}
    event[:host] = e.host.address if(e.host)
    event[:created_at] = e.created_at.to_i
    event[:updated_at] = e.updated_at.to_i
    event[:name] = e.name
    event[:critical] = e.critical if(e.critical)
    event[:username] = e.username if(e.username)
    event[:info] = e.info
    ret[:events] << event
  end
  ret
}
end

#rpc_get_auth_info(xopts) ⇒ Object


502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 502

def rpc_get_auth_info(xopts)
::ActiveRecord::Base.connection_pool.with_connection {
  opts, wspace = init_db_opts_workspace(xopts)
  ret = {}
  ret[:auth_info] = []
  # XXX: This method doesn't exist...
  ai = self.framework.db.get_auth_info(opts)
  ai.each do |i|
    info = {}
    i.each do |k,v|
      info[k.to_sym] = v
    end
    ret[:auth_info] << info
  end
  ret
}
end

#rpc_get_client(xopts) ⇒ Object


414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 414

def rpc_get_client(xopts)
::ActiveRecord::Base.connection_pool.with_connection {
  opts, wspace = init_db_opts_workspace(xopts)
  ret = {}
  ret[:client] = []
  c = self.framework.db.get_client(opts)
  if(c)
    client = {}
    host = c.host
    client[:host] = host.address
    client[:created_at] = c.created_at.to_i
    client[:updated_at] = c.updated_at.to_i
    client[:ua_string] = c.ua_string.to_s
    client[:ua_name] = c.ua_name.to_s
    client[:ua_ver] = c.ua_ver.to_s
    ret[:client] << client
  end
  ret
}
end

#rpc_get_host(xopts) ⇒ Object


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
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 269

def rpc_get_host(xopts)
::ActiveRecord::Base.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

#rpc_get_note(xopts) ⇒ Object


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
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 362

def rpc_get_note(xopts)
::ActiveRecord::Base.connection_pool.with_connection {
  opts, wspace = init_db_opts_workspace(xopts)

  ret = {}
  ret[:note] = []

  host = self.framework.db.get_host(opts)

  return ret if( not host)
  notes = []
  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.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

#rpc_get_ref(name) ⇒ Object


520
521
522
523
524
525
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 520

def rpc_get_ref(name)
::ActiveRecord::Base.connection_pool.with_connection {
  db_check
  self.framework.db.get_ref(name)
}
end

#rpc_get_service(xopts) ⇒ Object


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
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 316

def rpc_get_service(xopts)
::ActiveRecord::Base.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] = [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.all(:conditions => conditions, :order => "hosts.address, port")
  else
    sret = host.services
  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

#rpc_get_vuln(xopts) ⇒ Object


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
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 876

def rpc_get_vuln(xopts)
::ActiveRecord::Base.connection_pool.with_connection {
  opts, wspace = init_db_opts_workspace(xopts)

  ret = {}
  ret[:vuln] = []

  host = self.framework.db.get_host(opts)

  return ret if( not host)
  vulns = []

  if(opts[:proto] && opts[:port])
    services = []
    sret = host.services.find_by_proto_and_port(opts[:proto], opts[:port])
    return ret if sret == nil
    services << sret if sret.class == ::Mdm::Service
    services |= sret if sret.class == Array

    services.each do |s|
      vulns |= s.vulns
    end
  else
    vulns = host.vulns
  end

  return ret if (not vulns)

  vulns.each do |v|
    vuln= {}
    host= v.host
    vuln[:host] = host.address || "unknown"
    if v.service
      vuln[:port] = v.service.port
      vuln[:proto] = v.service.proto
    end
    vuln[:created_at] = v[:created_at].to_i
    vuln[:updated_at] = v[:updated_at].to_i
    vuln[:name] = v[:name].to_s
    vuln[:info] = v[:info].to_s
    vuln[:refs] = []
    v.refs.each do |r|
      vuln[:refs] << r.name
    end
    ret[:vuln] << vuln
  end
  ret
}
end

#rpc_get_workspace(wspace) ⇒ Object


213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 213

def rpc_get_workspace(wspace)
  db_check
  wspace = find_workspace(wspace)
  ret = {}
  ret[:workspace] = []
  if(wspace)
    w = {}
    w[:name] = wspace.name
    w[:created_at] = wspace.created_at.to_i
    w[:updated_at] = wspace.updated_at.to_i
    ret[:workspace] << w
  end
  ret
end

#rpc_hosts(xopts) ⇒ Object


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
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 91

def rpc_hosts(xopts)
::ActiveRecord::Base.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.all(:conditions => conditions, :order => :address,
      :limit => limit, :offset => offset).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

#rpc_import_data(xopts) ⇒ Object


868
869
870
871
872
873
874
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 868

def rpc_import_data(xopts)
::ActiveRecord::Base.connection_pool.with_connection {
  opts, wspace = init_db_opts_workspace(xopts)
  self.framework.db.import(opts)
  return { :result => 'success' }
}
end

#rpc_loots(xopts) ⇒ Object


806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 806

def rpc_loots(xopts)
::ActiveRecord::Base.connection_pool.with_connection {
  opts, wspace = init_db_opts_workspace(xopts)
  limit = opts.delete(:limit) || 100
  offset = opts.delete(:offset) || 0

  ret = {}
  ret[:loots] = []
  wspace.loots.all(:limit => limit, :offset => offset).each do |l|
    loot = {}
    loot[:host] = l.host.address if(l.host)
    loot[:service] = l.service.name || l.service.port  if(l.service)
    loot[:ltype] = l.ltype
    loot[:ctype] = l.content_type
    loot[:data] = l.data
    loot[:created_at] = l.created_at.to_i
    loot[:updated_at] = l.updated_at.to_i
    loot[:name] = l.name
    loot[:info] = l.info
    ret[:loots] << loot
  end
  ret
}
end

#rpc_notes(xopts) ⇒ Object


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
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 462

def rpc_notes(xopts)
::ActiveRecord::Base.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[:addresses] if opts[:addresses]
  conditions[:name] = opts[:names].strip().split(",") if opts[:names]
  conditions[:ntype] = opts[:ntype] if opts[:ntype]
  conditions["services.port"] = Rex::Socket.portspec_to_portlist(opts[:ports]) if opts[:port]
  conditions["services.proto"] = opts[:proto] if opts[:proto]

  ret = {}
  ret[:notes] = []
  wspace.notes.all(:include => [:host, :service], :conditions => conditions,
      :limit => limit, :offset => offset).each do |n|
    note = {}
    note[:time] = n.created_at.to_i
    note[:host] = ""
    note[:service] = ""
    note[:host] = n.host.address if(n.host)
    note[:service] = n.service.name || n.service.port  if(n.service)
    note[:type ] = n.ntype.to_s
    note[:data] = n.data.inspect
    ret[:notes] << note
  end
  ret
}
end

#rpc_report_auth_info(xopts) ⇒ Object


493
494
495
496
497
498
499
500
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 493

def rpc_report_auth_info(xopts)
::ActiveRecord::Base.connection_pool.with_connection {
  opts, wspace = init_db_opts_workspace(xopts)
  res = self.framework.db.report_auth_info(opts)
  return { :result => 'success' } if(res)
  { :result => 'failed' }
}
end

#rpc_report_client(xopts) ⇒ Object


435
436
437
438
439
440
441
442
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 435

def rpc_report_client(xopts)
::ActiveRecord::Base.connection_pool.with_connection {
  opts, wspace = init_db_opts_workspace(xopts)
  res = self.framework.db.report_client(opts)
  return { :result => 'success' } if(res)
  { :result => 'failed' }
}
end

#rpc_report_cred(xopts) ⇒ Object

requires host, port, user, pass, ptype, and active


832
833
834
835
836
837
838
839
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 832

def rpc_report_cred(xopts)
::ActiveRecord::Base.connection_pool.with_connection {
  opts, wspace = init_db_opts_workspace(xopts)
  res = framework.db.find_or_create_cred(opts)
  return { :result => 'success' } if res
  { :result => 'failed' }
}
end

#rpc_report_event(xopts) ⇒ Object


784
785
786
787
788
789
790
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 784

def rpc_report_event(xopts)
::ActiveRecord::Base.connection_pool.with_connection {
  opts, wspace = init_db_opts_workspace(xopts)
  res = self.framework.db.report_event(opts)
  { :result => 'success' } if(res)
}
end

#rpc_report_host(xopts) ⇒ Object


297
298
299
300
301
302
303
304
305
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 297

def rpc_report_host(xopts)
::ActiveRecord::Base.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

#rpc_report_loot(xopts) ⇒ Object

NOTE Path is required NOTE To match a service need host, port, proto


794
795
796
797
798
799
800
801
802
803
804
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 794

def rpc_report_loot(xopts)
::ActiveRecord::Base.connection_pool.with_connection {
  opts, wspace = init_db_opts_workspace(xopts)
  if opts[:host] && opts[:port] && opts[:proto]
    opts[:service] = self.framework.db.find_or_create_service(opts)
  end

  res = self.framework.db.report_loot(opts)
  { :result => 'success' } if(res)
}
end

#rpc_report_note(xopts) ⇒ Object

DOC NOTE: :data and :ntype are REQUIRED


445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 445

def rpc_report_note(xopts)
::ActiveRecord::Base.connection_pool.with_connection {
  opts, wspace = init_db_opts_workspace(xopts)
  if (opts[:host] or opts[:address]) and opts[:port] and opts[:proto]
    addr = opts[:host] || opts[:address]
    wspace = opts[:workspace] || self.framework.db.workspace
    host = wspace.hosts.find_by_address(addr)
    service = host.services.find_by_proto_and_port(opts[:proto],opts[:port]) if host.services.count > 0
    opts[:service] = service if service
  end

  res = self.framework.db.report_note(opts)
  return { :result => 'success' } if(res)
  { :result => 'failed' }
}
end

#rpc_report_service(xopts) ⇒ Object


307
308
309
310
311
312
313
314
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 307

def rpc_report_service(xopts)
::ActiveRecord::Base.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

#rpc_report_vuln(xopts) ⇒ Object


749
750
751
752
753
754
755
756
757
758
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 749

def rpc_report_vuln(xopts)
::ActiveRecord::Base.connection_pool.with_connection {
  db_check
  opts = fix_options(xopts)
  opts[:workspace] = find_workspace(opts[:workspace]) if opts[:workspace]
  res = self.framework.db.report_vuln(opts)
  return { :result => 'success' } if(res)
  { :result => 'failed' }
}
end

#rpc_services(xopts) ⇒ Object


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
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 125

def rpc_services( xopts)
::ActiveRecord::Base.connection_pool.with_connection {
  opts, wspace = init_db_opts_workspace(xopts)
  limit = opts.delete(:limit) || 100
  offset = opts.delete(:offset) || 0

  conditions = {}
  conditions[:state] = [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.all(:include => :host, :conditions => conditions,
      :limit => limit, :offset => offset).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

#rpc_set_workspace(wspace) ⇒ Object


228
229
230
231
232
233
234
235
236
237
238
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 228

def rpc_set_workspace(wspace)
::ActiveRecord::Base.connection_pool.with_connection {
  db_check
  workspace = self.framework.db.find_workspace(wspace)
  if(workspace)
    self.framework.db.workspace = workspace
    return { 'result' => "success" }
  end
  { 'result' => 'failed' }
}
end

#rpc_statusObject


1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 1043

def rpc_status
  if (not self.framework.db.driver)
    return {:driver => 'None' }
  end

  cdb = ""
  if ::ActiveRecord::Base.connected?
    ::ActiveRecord::Base.connection_pool.with_connection { |conn|
      if conn.respond_to? :current_database
        cdb = conn.current_database
      else
        cdb = conn.instance_variable_get(:@config)[:database]
      end
    }
    return {:driver => self.framework.db.driver.to_s , :db => cdb }
  else
    return {:driver => self.framework.db.driver.to_s}
  end
  {:driver => 'None' }
end

#rpc_vulns(xopts) ⇒ Object


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
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 159

def rpc_vulns(xopts)
::ActiveRecord::Base.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[:addresses] if opts[:addresses]
  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.all(:include => :service, :conditions => conditions, :limit => limit, :offset => offset).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

#rpc_workspacesObject


193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/msf/core/rpc/v10/rpc_db.rb', line 193

def rpc_workspaces
  db_check

  res = {}
  res[:workspaces] = []
  self.framework.db.workspaces.each do |j|
    ws = {}
    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