Module: Msf::Ui::Console::CommandDispatcher::Db::Certs

Included in:
Msf::Ui::Console::CommandDispatcher::Db
Defined in:
lib/msf/ui/console/command_dispatcher/db/certs.rb

Constant Summary collapse

@@certs_opts =
Rex::Parser::Arguments.new(
  ['-v', '--verbose'] => [false, 'Verbose output'],
  ['-d', '--delete'] => [ false, 'Delete *all* matching pkcs12 entries'],
  ['-h', '--help'] => [false, 'Help banner'],
  ['-i', '--index'] => [true, 'Pkcs12 entry ID(s) to search for, e.g. `-i 1` or `-i 1,2,3` or `-i 1 -i 2 -i 3`'],
  ['-a', '--activate'] => [false, 'Activates *all* matching pkcs12 entries'],
  ['-A', '--deactivate'] => [false, 'Deactivates *all* matching pkcs12 entries'],
  ['-e', '--export'] => [true, 'The file path where to export the matching pkcs12 entry']
)

Instance Method Summary collapse

Instance Method Details

#cmd_certs(*args) ⇒ Object



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
# File 'lib/msf/ui/console/command_dispatcher/db/certs.rb', line 45

def cmd_certs(*args)
  return unless active?

  entries_affected = 0
  mode = :list
  id_search = []
  username = nil
  verbose = false
  export_path = nil
  @@certs_opts.parse(args) do |opt, _idx, val|
    case opt
    when '-h', '--help'
      cmd_certs_help
      return
    when '-v', '--verbose'
      verbose = true
    when '-d', '--delete'
      mode = :delete
    when '-i', '--id'
      id_search = (id_search + val.split(/,\s*|\s+/)).uniq # allows 1 or 1,2,3 or "1 2 3" or "1, 2, 3"
    when '-a', '--activate'
      mode = :activate
    when '-A', '--deactivate'
      mode = :deactivate
    when '-e', '--export'
      export_path = val
    else
      # Anything that wasn't an option is a username to search for
      username = val
    end
  end

  pkcs12_results = pkcs12_search(username: username, id_search: id_search)

  print_line('Pkcs12')
  print_line('======')

  if mode == :delete
    result = pkcs12_storage.delete(ids: pkcs12_results.map(&:id))
    entries_affected = result.size
  end

  if mode == :activate || mode == :deactivate
    pkcs12_results = set_pkcs12_status(mode, pkcs12_results)
    entries_affected = pkcs12_results.size
  end

  if export_path
    if pkcs12_results.empty?
      print_error('No mathing Pkcs12 entry to export')
      return
    end
    if pkcs12_results.size > 1
      print_error('More than one mathing Pkcs12 entry found. Filter with `-i` and/or provide a username')
      return
    end

    raw_data = Base64.strict_decode64(pkcs12_results.first.private_cred.data)
    ::File.binwrite(::File.expand_path(export_path), raw_data)
    return
  end

  if pkcs12_results.empty?
    print_line('No Pkcs12')
    print_line
    return
  end

  if verbose
    pkcs12_results.each.with_index do |pkcs12_result, index|
      print_line "Certificate[#{index}]:"
      print_line pkcs12_result.openssl_pkcs12.certificate.to_s
      print_line pkcs12_result.openssl_pkcs12.certificate.to_text
      print_line
    end
  else
    tbl = Rex::Text::Table.new(
      {
        'Columns' => ['id', 'username', 'realm', 'subject', 'issuer', 'ADCS CA', 'ADCS Template', 'status'],
        'SortIndex' => -1,
        'WordWrap' => false,
        'Rows' => pkcs12_results.map do |pkcs12|
          [
            pkcs12.id,
            pkcs12.username,
            pkcs12.realm,
            pkcs12.openssl_pkcs12.certificate.subject.to_s,
            pkcs12.openssl_pkcs12.certificate.issuer.to_s,
            pkcs12.adcs_ca,
            pkcs12.adcs_template,
            pkcs12_status(pkcs12)
          ]
        end
      }
    )
    print_line(tbl.to_s)
  end

  case mode
  when :delete
    print_status("Deleted #{entries_affected} #{entries_affected > 1 ? 'entries' : 'entry'}") if entries_affected > 0
  when :activate
    print_status("Activated #{entries_affected} #{entries_affected > 1 ? 'entries' : 'entry'}") if entries_affected > 0
  when :deactivate
    print_status("Deactivated #{entries_affected} #{entries_affected > 1 ? 'entries' : 'entry'}") if entries_affected > 0
  end
end

#cmd_certs_helpObject



27
28
29
30
31
32
33
# File 'lib/msf/ui/console/command_dispatcher/db/certs.rb', line 27

def cmd_certs_help
  print_line 'List Pkcs12 certificate bundles in the database'
  print_line 'Usage: certs [options] [username[@domain_upn_format]]'
  print_line
  print @@certs_opts.usage
  print_line
end

#cmd_certs_tabs(str, words) ⇒ Object

Tab completion for the certs command

at least 1 when tab completion has reached this stage since the command itself has been completed

Parameters:

  • str (String)

    the string currently being typed before tab was hit

  • words (Array<String>)

    the previously completed words on the command line. words is always



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/msf/ui/console/command_dispatcher/db/certs.rb', line 10

def cmd_certs_tabs(str, words)
  tabs = []

  case words.length
  when 1
    tabs = @@certs_opts.option_keys.select { |opt| opt.start_with?(str) }
  when 2
    tabs = if words[1] == '-e' || words[1] == '--export'
             tab_complete_filenames(str, words)
           else
             []
           end
  end

  tabs
end

#pkcs12_search(username: nil, id_search: nil, workspace: framework.db.workspace) ⇒ Array<>

Parameters:

  • username (String, nil) (defaults to: nil)

    Search for pkcs12 associated with this username

  • id_search (Array<Integer>, nil) (defaults to: nil)

    List of pkcs12 IDs to search for

  • workspace (Workspace) (defaults to: framework.db.workspace)

    to search against

  • [Symbol] (Hash)

    a customizable set of options

Returns:

  • (Array<>)


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
# File 'lib/msf/ui/console/command_dispatcher/db/certs.rb', line 159

def pkcs12_search(username: nil, id_search: nil, workspace: framework.db.workspace)
  pkcs12_results = []

  if id_search.present?
    begin
      pkcs12_results += id_search.flat_map do |id|
        pkcs12_storage.pkcs12(
          workspace: workspace,
          id: id
        )
      end
    rescue ActiveRecord::RecordNotFound => e
      wlog("Record Not Found: #{e.message}")
      print_warning("Not all records with the ids: #{id_search} could be found.")
      print_warning('Please ensure all ids specified are available.')
    end
  elsif username.present?
    realm = nil
    if username.include?('@')
      username, realm = username.split('@', 2)
    end
    pkcs12_results += pkcs12_storage.pkcs12(
      workspace: workspace,
      username: username,
      realm: realm
    )
  else
    pkcs12_results += pkcs12_storage.pkcs12(
      workspace: workspace
    )
  end

  pkcs12_results.sort_by do |pkcs12|
    [pkcs12.realm, pkcs12.username]
  end
end