Skip to content

Commit e605318

Browse files
committed
Merge pull request rapid7#1813 from limhoff-r7/feature/mdm-module-namespace
Updates to MDM 0.11.2
2 parents feac292 + 4461aef commit e605318

File tree

27 files changed

+3296
-479
lines changed

27 files changed

+3296
-479
lines changed

Gemfile

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,48 @@ gem 'nokogiri'
1212
gem 'robots'
1313

1414
group :db do
15-
# Needed for Msf::DbManager
16-
gem 'activerecord'
17-
# Database models shared between framework and Pro.
18-
gem 'metasploit_data_models', '~> 0.6.16'
19-
# Needed for module caching in Mdm::ModuleDetails
20-
gem 'pg', '>= 0.11'
15+
# Needed for Msf::DbManager
16+
gem 'activerecord'
17+
# Database models shared between framework and Pro.
18+
gem 'metasploit_data_models', '~> 0.11.2'
19+
# Needed for module caching in Mdm::ModuleDetails
20+
gem 'pg', '>= 0.11'
2121
end
2222

2323
group :pcap do
24-
# For sniffer and raw socket modules
25-
gem 'pcaprub'
24+
# For sniffer and raw socket modules
25+
gem 'pcaprub'
2626
end
2727

2828
group :development do
29-
# Markdown formatting for yard
30-
gem 'redcarpet'
31-
# generating documentation
32-
gem 'yard'
29+
# Markdown formatting for yard
30+
gem 'redcarpet'
31+
# generating documentation
32+
gem 'yard'
3333
end
3434

3535
group :development, :test do
3636
# supplies factories for producing model instance for specs
37-
# Version 4.1.0 or newer is needed to support generate calls without the
38-
# 'FactoryGirl.' in factory definitions syntax.
39-
gem 'factory_girl', '>= 4.1.0'
40-
# running documentation generation tasks and rspec tasks
41-
gem 'rake'
37+
# Version 4.1.0 or newer is needed to support generate calls without the
38+
# 'FactoryGirl.' in factory definitions syntax.
39+
gem 'factory_girl', '>= 4.1.0'
40+
# running documentation generation tasks and rspec tasks
41+
gem 'rake'
4242
end
4343

4444
group :test do
4545
# Removes records from database created during tests. Can't use rspec-rails'
4646
# transactional fixtures because multiple connections are in use so
4747
# transactions won't work.
4848
gem 'database_cleaner'
49-
# testing framework
50-
gem 'rspec', '>= 2.12'
51-
# code coverage for tests
52-
# any version newer than 0.5.4 gives an Encoding error when trying to read the source files.
53-
gem 'simplecov', '0.5.4', :require => false
49+
# testing framework
50+
gem 'rspec', '>= 2.12'
51+
# add matchers from shoulda, such as query_the_database, which is useful for
52+
# testing that the Msf::DBManager activation is respected.
53+
gem 'shoulda-matchers'
54+
# code coverage for tests
55+
# any version newer than 0.5.4 gives an Encoding error when trying to read the source files.
56+
gem 'simplecov', '0.5.4', :require => false
57+
# Manipulate Time.now in specs
58+
gem 'timecop'
5459
end

Gemfile.lock

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,27 @@ GEM
1313
i18n (= 0.6.1)
1414
multi_json (~> 1.0)
1515
arel (3.0.2)
16+
bourne (1.4.0)
17+
mocha (~> 0.13.2)
1618
builder (3.0.4)
1719
database_cleaner (0.9.1)
1820
diff-lcs (1.2.2)
1921
factory_girl (4.2.0)
2022
activesupport (>= 3.0.0)
2123
i18n (0.6.1)
2224
json (1.7.7)
23-
metasploit_data_models (0.6.16)
25+
metaclass (0.0.1)
26+
metasploit_data_models (0.11.2)
2427
activerecord (>= 3.2.13)
2528
activesupport
2629
pg
30+
mocha (0.13.3)
31+
metaclass (~> 0.0.1)
2732
msgpack (0.5.4)
2833
multi_json (1.0.4)
2934
nokogiri (1.5.9)
3035
pcaprub (0.11.3)
31-
pg (0.15.0)
36+
pg (0.15.1)
3237
rake (10.0.4)
3338
redcarpet (2.2.2)
3439
robots (0.10.1)
@@ -40,10 +45,14 @@ GEM
4045
rspec-expectations (2.13.0)
4146
diff-lcs (>= 1.1.3, < 2.0)
4247
rspec-mocks (2.13.0)
48+
shoulda-matchers (1.5.2)
49+
activesupport (>= 3.0.0)
50+
bourne (~> 1.3)
4351
simplecov (0.5.4)
4452
multi_json (~> 1.0.3)
4553
simplecov-html (~> 0.5.3)
4654
simplecov-html (0.5.3)
55+
timecop (0.6.1)
4756
tzinfo (0.3.37)
4857
yard (0.8.5.2)
4958

@@ -56,7 +65,7 @@ DEPENDENCIES
5665
database_cleaner
5766
factory_girl (>= 4.1.0)
5867
json
59-
metasploit_data_models (~> 0.6.16)
68+
metasploit_data_models (~> 0.11.2)
6069
msgpack
6170
nokogiri
6271
pcaprub
@@ -65,5 +74,7 @@ DEPENDENCIES
6574
redcarpet
6675
robots
6776
rspec (>= 2.12)
77+
shoulda-matchers
6878
simplecov (= 0.5.4)
79+
timecop
6980
yard

Rakefile

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ else
3636
task :default => :spec
3737
end
3838

39+
# Require yard before loading metasploit_data_models rake tasks as the yard tasks won't be defined if
40+
# YARD is not defined when yard.rake is loaded.
41+
begin
42+
require 'yard'
43+
rescue LoadError
44+
puts "yard not in bundle, so can't set up yard tasks. " \
45+
"To generate documentation ensure to install the development group."
46+
47+
print_without = true
48+
end
49+
3950
begin
4051
require 'metasploit_data_models'
4152
rescue LoadError
@@ -58,14 +69,6 @@ else
5869
end
5970
end
6071

61-
begin
62-
require 'yard'
63-
rescue LoadError
64-
puts "yard not in bundle, so can't set up yard tasks. " \
65-
"To generate documentation ensure to install the development group."
66-
67-
print_without = true
68-
end
6972

7073

7174
if print_without

db/schema.rb

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#
1212
# It's strongly recommended to check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(:version => 20130228214900) do
14+
ActiveRecord::Schema.define(:version => 20130430162145) do
1515

1616
create_table "api_keys", :force => true do |t|
1717
t.text "token"
@@ -135,7 +135,7 @@
135135

136136
create_table "hosts", :force => true do |t|
137137
t.datetime "created_at"
138-
t.string "address", :limit => nil
138+
t.string "address", :limit => nil, :null => false
139139
t.string "mac"
140140
t.string "comm"
141141
t.string "name"
@@ -145,7 +145,7 @@
145145
t.string "os_sp"
146146
t.string "os_lang"
147147
t.string "arch"
148-
t.integer "workspace_id"
148+
t.integer "workspace_id", :null => false
149149
t.datetime "updated_at"
150150
t.text "purpose"
151151
t.string "info", :limit => 65536
@@ -157,14 +157,15 @@
157157
t.integer "service_count", :default => 0
158158
t.integer "host_detail_count", :default => 0
159159
t.integer "exploit_attempt_count", :default => 0
160+
t.integer "cred_count", :default => 0
160161
end
161162

162-
add_index "hosts", ["address"], :name => "index_hosts_on_address"
163163
add_index "hosts", ["name"], :name => "index_hosts_on_name"
164164
add_index "hosts", ["os_flavor"], :name => "index_hosts_on_os_flavor"
165165
add_index "hosts", ["os_name"], :name => "index_hosts_on_os_name"
166166
add_index "hosts", ["purpose"], :name => "index_hosts_on_purpose"
167167
add_index "hosts", ["state"], :name => "index_hosts_on_state"
168+
add_index "hosts", ["workspace_id", "address"], :name => "index_hosts_on_workspace_id_and_address", :unique => true
168169

169170
create_table "hosts_tags", :id => false, :force => true do |t|
170171
t.integer "host_id"
@@ -223,26 +224,26 @@
223224
end
224225

225226
create_table "module_actions", :force => true do |t|
226-
t.integer "module_detail_id"
227+
t.integer "detail_id"
227228
t.text "name"
228229
end
229230

230-
add_index "module_actions", ["module_detail_id"], :name => "index_module_actions_on_module_detail_id"
231+
add_index "module_actions", ["detail_id"], :name => "index_module_actions_on_module_detail_id"
231232

232233
create_table "module_archs", :force => true do |t|
233-
t.integer "module_detail_id"
234+
t.integer "detail_id"
234235
t.text "name"
235236
end
236237

237-
add_index "module_archs", ["module_detail_id"], :name => "index_module_archs_on_module_detail_id"
238+
add_index "module_archs", ["detail_id"], :name => "index_module_archs_on_module_detail_id"
238239

239240
create_table "module_authors", :force => true do |t|
240-
t.integer "module_detail_id"
241+
t.integer "detail_id"
241242
t.text "name"
242243
t.text "email"
243244
end
244245

245-
add_index "module_authors", ["module_detail_id"], :name => "index_module_authors_on_module_detail_id"
246+
add_index "module_authors", ["detail_id"], :name => "index_module_authors_on_module_detail_id"
246247

247248
create_table "module_details", :force => true do |t|
248249
t.datetime "mtime"
@@ -268,34 +269,34 @@
268269
add_index "module_details", ["refname"], :name => "index_module_details_on_refname"
269270

270271
create_table "module_mixins", :force => true do |t|
271-
t.integer "module_detail_id"
272+
t.integer "detail_id"
272273
t.text "name"
273274
end
274275

275-
add_index "module_mixins", ["module_detail_id"], :name => "index_module_mixins_on_module_detail_id"
276+
add_index "module_mixins", ["detail_id"], :name => "index_module_mixins_on_module_detail_id"
276277

277278
create_table "module_platforms", :force => true do |t|
278-
t.integer "module_detail_id"
279+
t.integer "detail_id"
279280
t.text "name"
280281
end
281282

282-
add_index "module_platforms", ["module_detail_id"], :name => "index_module_platforms_on_module_detail_id"
283+
add_index "module_platforms", ["detail_id"], :name => "index_module_platforms_on_module_detail_id"
283284

284285
create_table "module_refs", :force => true do |t|
285-
t.integer "module_detail_id"
286+
t.integer "detail_id"
286287
t.text "name"
287288
end
288289

289-
add_index "module_refs", ["module_detail_id"], :name => "index_module_refs_on_module_detail_id"
290+
add_index "module_refs", ["detail_id"], :name => "index_module_refs_on_module_detail_id"
290291
add_index "module_refs", ["name"], :name => "index_module_refs_on_name"
291292

292293
create_table "module_targets", :force => true do |t|
293-
t.integer "module_detail_id"
294+
t.integer "detail_id"
294295
t.integer "index"
295296
t.text "name"
296297
end
297298

298-
add_index "module_targets", ["module_detail_id"], :name => "index_module_targets_on_module_detail_id"
299+
add_index "module_targets", ["detail_id"], :name => "index_module_targets_on_module_detail_id"
299300

300301
create_table "nexpose_consoles", :force => true do |t|
301302
t.datetime "created_at", :null => false

lib/msf/core/db.rb

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -645,12 +645,69 @@ def get_session(opts)
645645
}
646646
end
647647

648-
# Record a new session in the database
649-
#
650-
# opts MUST contain either
651-
# +:session+:: the Msf::Session object we are reporting
652-
# +:host+:: the Host object we are reporting a session on.
653-
#
648+
# @note The Mdm::Session#desc will be truncated to 255 characters.
649+
# @todo https://www.pivotaltracker.com/story/show/48249739
650+
#
651+
# @overload report_session(opts)
652+
# Creates an Mdm::Session from Msf::Session. If +via_exploit+ is set on the
653+
# +session+, then an Mdm::Vuln and Mdm::ExploitAttempt is created for the
654+
# session's host. The Mdm::Host for the +session_host+ is created using
655+
# The session.session_host, +session.arch+ (if +session+ responds to arch),
656+
# and the workspace derived from opts or the +session+. The Mdm::Session is
657+
# assumed to be +last_seen+ and +opened_at+ at the time report_session is
658+
# called. +session.exploit_datastore['ParentModule']+ is used for the
659+
# Mdm::Session#via_exploit if +session.via_exploit+ is
660+
# 'exploit/multi/handler'.
661+
#
662+
# @param opts [Hash{Symbol => Object}] options
663+
# @option opt [Msf::Session, #datastore, #platform, #type, #via_exploit, #via_payload] :session
664+
# The in-memory session to persist to the database.
665+
# @option opts [Mdm::Workspace] :workspace The workspace for in which the
666+
# :session host is contained. Also used as the workspace for the
667+
# Mdm::ExploitAttempt and Mdm::Vuln. Defaults to Mdm::Worksapce with
668+
# Mdm::Workspace#name equal to +session.workspace+.
669+
# @return [nil] if {Msf::DBManager#active} is +false+.
670+
# @return [Mdm::Session] if session is saved
671+
# @raise [ArgumentError] if :session is not an {Msf::Session}.
672+
# @raise [ActiveRecord::RecordInvalid] if session is invalid and cannot be
673+
# saved, in which case, the Mdm::ExploitAttempt and Mdm::Vuln will not be
674+
# created, but the Mdm::Host will have been. (There is no transaction
675+
# to rollback the Mdm::Host creation.)
676+
# @see #find_or_create_host
677+
# @see #normalize_host
678+
# @see #report_exploit_success
679+
# @see #report_vuln
680+
#
681+
# @overload report_session(opts)
682+
# Creates an Mdm::Session from Mdm::Host.
683+
#
684+
# @param opts [Hash{Symbol => Object}] options
685+
# @option opts [DateTime, Time] :closed_at The date and time the sesion was
686+
# closed.
687+
# @option opts [String] :close_reason Reason the session was closed.
688+
# @option opts [Hash] :datastore {Msf::DataStore#to_h}.
689+
# @option opts [String] :desc Session description. Will be truncated to 255
690+
# characters.
691+
# @option opts [Mdm::Host] :host The host on which the session was opened.
692+
# @option opts [DateTime, Time] :last_seen The last date and time the
693+
# session was seen to be open. Defaults to :closed_at's value.
694+
# @option opts [DateTime, Time] :opened_at The date and time that the
695+
# session was opened.
696+
# @option opts [String] :platform The platform of the host.
697+
# @option opts [Array] :routes ([]) The routes through the session for
698+
# pivoting.
699+
# @option opts [String] :stype Session type.
700+
# @option opts [String] :via_exploit The {Msf::Module#fullname} of the
701+
# exploit that was used to open the session.
702+
# @option option [String] :via_payload the {MSf::Module#fullname} of the
703+
# payload sent to the host when the exploit was successful.
704+
# @return [nil] if {Msf::DBManager#active} is +false+.
705+
# @return [Mdm::Session] if session is saved.
706+
# @raise [ArgumentError] if :host is not an Mdm::Host.
707+
# @raise [ActiveRecord::RecordInvalid] if session is invalid and cannot be
708+
# saved.
709+
#
710+
# @raise ArgumentError if :host and :session is +nil+
654711
def report_session(opts)
655712
return if not active
656713
::ActiveRecord::Base.connection_pool.with_connection {
@@ -719,13 +776,11 @@ def report_session(opts)
719776

720777
# If this is a live session, we know the host is vulnerable to something.
721778
if opts[:session] and session.via_exploit
722-
return unless host
723-
724779
mod = framework.modules.create(session.via_exploit)
725780

726781
if session.via_exploit == "exploit/multi/handler" and sess_data[:datastore]['ParentModule']
727782
mod_fullname = sess_data[:datastore]['ParentModule']
728-
mod_name = ::Mdm::ModuleDetail.find_by_fullname(mod_fullname).name
783+
mod_name = ::Mdm::Module::Detail.find_by_fullname(mod_fullname).name
729784
else
730785
mod_name = mod.name
731786
mod_fullname = mod.fullname

0 commit comments

Comments
 (0)