Module: Msf::DBManager::Event

Included in:
Msf::DBManager
Defined in:
lib/msf/core/db_manager/event.rb

Constant Summary collapse

DEFAULT_ORDER =
:desc
DEFAULT_LIMIT =
100
DEFAULT_OFFSET =
0

Instance Method Summary collapse

Instance Method Details

#events(opts) ⇒ Array<Mdm::Event>|Mdm::Event::ActiveRecord_AssociationRelation

Retrieves events that are stored in the database.

Additional query options:

Parameters:

  • opts (Hash)

    Hash containing query key-value pairs based on the event model.

Options Hash (opts):

  • :id (Integer)

    A specific event ID. If specified, all other options are ignored.

  • :workspace (String)

    The workspace from which the data should be gathered from. (Required)

  • :order (Symbol|String)

    The event created_at sort order. Valid values: :asc, :desc, ‘asc’ or ‘desc’. Default: :desc

  • :limit (Integer)

    The maximum number of events that will be retrieved from the query. Default: 100

  • :offset (Integer)

    The number of events the query will begin reading from the start of the set. Default: 0

  • :search_term (String)

    Search regular expression used to filter results. All fields are converted to strings and results are returned if the pattern is matched.

Returns:

  • (Array<Mdm::Event>|Mdm::Event::ActiveRecord_AssociationRelation)

    events that are matched.



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
# File 'lib/msf/core/db_manager/event.rb', line 22

def events(opts)
::ApplicationRecord.connection_pool.with_connection {
  # If we have the ID, there is no point in creating a complex query.
  if opts[:id] && !opts[:id].to_s.empty?
    return Array.wrap(Mdm::Event.find(opts[:id]))
  end

  wspace = Msf::Util::DBManager.process_opts_workspace(opts, framework)
  opts = opts.clone()
  opts.delete(:workspace)

  order = opts.delete(:order)
  order = order.nil? ? DEFAULT_ORDER : order.to_sym

  limit = opts.delete(:limit) || DEFAULT_LIMIT
  offset = opts.delete(:offset) || DEFAULT_OFFSET

  search_term = opts.delete(:search_term)
  results = wspace.events.where(opts).order(created_at: order).offset(offset).limit(limit)

  if search_term && !search_term.empty?
    re_search_term = /#{search_term}/mi
    results = results.select { |event|
      event.attribute_names.any? { |a| event[a.intern].to_s.match(re_search_term) }
    }
  end
  results
}
end

#report_event(opts) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/msf/core/db_manager/event.rb', line 52

def report_event(opts)
  return if not active
::ApplicationRecord.connection_pool.with_connection {
  wspace = Msf::Util::DBManager.process_opts_workspace(opts, framework)
  return if not wspace # Temp fix?

  opts = opts.clone()
  opts.delete(:workspace)
  uname  = opts.delete(:username)

  if !opts[:host].nil? && !opts[:host].kind_of?(::Mdm::Host)
    opts[:host] = find_or_create_host(workspace: wspace, host: opts[:host])
  end

  ::Mdm::Event.create(opts.merge(:workspace_id => wspace[:id], :username => uname))
}
end