Skip to content

Commit 492b081

Browse files
committed
Msf::DBManager::Export#extract_module_detail_info spec
[#47979793]
1 parent 3bf3cfc commit 492b081

File tree

7 files changed

+254
-26
lines changed

7 files changed

+254
-26
lines changed

lib/msf/core/db_export.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,17 @@ def create_xml_element(key,value)
358358
return el
359359
end
360360

361-
361+
# @note there is no single root element output by
362+
# {#extract_module_detail_info}, so if calling {#extract_module_detail_info}
363+
# directly, it is the caller's responsibility to add an opening and closing
364+
# tag to report_file around the call to {#extract_module_detail_info}.
365+
#
366+
# Writes a module_detail element to the report_file for each
367+
# Mdm::ModuleDetail.
368+
#
369+
# @param report_file [#write, #flush] IO stream to which to write the
370+
# module_detail elements.
371+
# @return [void]
362372
def extract_module_detail_info(report_file)
363373
Mdm::ModuleDetail.all.each do |m|
364374
report_file.write("<module_detail>\n")
@@ -371,6 +381,7 @@ def extract_module_detail_info(report_file)
371381
end
372382

373383
# Authors sub-elements
384+
# @todo https://www.pivotaltracker.com/story/show/48451001
374385
report_file.write(" <module_authors>\n")
375386
m.authors.find(:all).each do |d|
376387
d.attributes.each_pair do |k,v|
@@ -381,6 +392,7 @@ def extract_module_detail_info(report_file)
381392
report_file.write(" </module_authors>\n")
382393

383394
# Refs sub-elements
395+
# @todo https://www.pivotaltracker.com/story/show/48451001
384396
report_file.write(" <module_refs>\n")
385397
m.refs.find(:all).each do |d|
386398
d.attributes.each_pair do |k,v|
@@ -392,6 +404,7 @@ def extract_module_detail_info(report_file)
392404

393405

394406
# Archs sub-elements
407+
# @todo https://www.pivotaltracker.com/story/show/48451001
395408
report_file.write(" <module_archs>\n")
396409
m.archs.find(:all).each do |d|
397410
d.attributes.each_pair do |k,v|
@@ -403,6 +416,7 @@ def extract_module_detail_info(report_file)
403416

404417

405418
# Platforms sub-elements
419+
# @todo https://www.pivotaltracker.com/story/show/48451001
406420
report_file.write(" <module_platforms>\n")
407421
m.platforms.find(:all).each do |d|
408422
d.attributes.each_pair do |k,v|
@@ -414,6 +428,7 @@ def extract_module_detail_info(report_file)
414428

415429

416430
# Targets sub-elements
431+
# @todo https://www.pivotaltracker.com/story/show/48451001
417432
report_file.write(" <module_targets>\n")
418433
m.targets.find(:all).each do |d|
419434
d.attributes.each_pair do |k,v|
@@ -424,6 +439,7 @@ def extract_module_detail_info(report_file)
424439
report_file.write(" </module_targets>\n")
425440

426441
# Actions sub-elements
442+
# @todo https://www.pivotaltracker.com/story/show/48451001
427443
report_file.write(" <module_actions>\n")
428444
m.actions.find(:all).each do |d|
429445
d.attributes.each_pair do |k,v|
@@ -434,6 +450,7 @@ def extract_module_detail_info(report_file)
434450
report_file.write(" </module_actions>\n")
435451

436452
# Mixins sub-elements
453+
# @todo https://www.pivotaltracker.com/story/show/48451001
437454
report_file.write(" <module_mixins>\n")
438455
m.mixins.find(:all).each do |d|
439456
d.attributes.each_pair do |k,v|

spec/factories/mdm/module_details.rb

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
FactoryGirl.define do
2+
type_directory_by_type = {
3+
'auxiliary' => 'auxiliary',
4+
'encoder' => 'encoders',
5+
'exploit' => 'exploits',
6+
'nop' => 'nops',
7+
'payload' => 'payloads',
8+
'post' => 'posts'
9+
}
10+
11+
sequence :mdm_module_detail_disclosure_date do |n|
12+
# @todo https://www.pivotaltracker.com/story/show/48450593
13+
Date.today - n
14+
end
15+
16+
sequence :mdm_module_detail_description do |n|
17+
"Module Description #{n}"
18+
end
19+
20+
sequence :mdm_module_detail_license do |n|
21+
"Module License v#{n}"
22+
end
23+
24+
privileges = [false, true]
25+
privilege_count = privileges.length
26+
27+
sequence :mdm_module_detail_privileged do |n|
28+
privileges[n % privilege_count]
29+
end
30+
31+
sequence :mdm_module_detail_mtime do |n|
32+
Time.now.utc - n.seconds
33+
end
34+
35+
types = type_directory_by_type.keys
36+
type_count = types.length
37+
38+
sequence :mdm_module_detail_mtype do |n|
39+
types[n % type_count]
40+
end
41+
42+
sequence :mdm_module_detail_name do |n|
43+
"Module Name #{n}"
44+
end
45+
46+
sequence :mdm_module_detail_rank do |n|
47+
(100 * n)
48+
end
49+
end
50+
51+
modules_pathname = Metasploit::Framework.root.join('modules')
52+
type_directory_by_type = {
53+
'auxiliary' => 'auxiliary',
54+
'encoder' => 'encoders',
55+
'exploit' => 'exploits',
56+
'nop' => 'nops',
57+
'payload' => 'payloads',
58+
'post' => 'posts'
59+
}
60+
61+
FactoryGirl.modify do
62+
factory :mdm_module_detail do
63+
description { generate :mdm_module_detail_description }
64+
disclosure_date { generate :mdm_module_detail_disclosure_date }
65+
license { generate :mdm_module_detail_license }
66+
mtime { generate :mdm_module_detail_mtime }
67+
mtype { generate :mdm_module_detail_mtype }
68+
privileged { generate :mdm_module_detail_privileged }
69+
name { generate :mdm_module_detail_name }
70+
rank { generate :mdm_module_detail_rank }
71+
refname { generate :mdm_module_detail_refname }
72+
fullname { "#{mtype}/#{refname}" }
73+
74+
file {
75+
type_directory = type_directory_by_type[mtype]
76+
77+
modules_pathname.join(
78+
type_directory,
79+
"#{refname}.rb"
80+
).to_path
81+
}
82+
end
83+
end

spec/lib/msf/core/db_export_spec.rb

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
require 'spec_helper'
2+
3+
require 'msf/core/db_export'
4+
5+
describe Msf::DBManager::Export do
6+
include_context 'Msf::DBManager'
7+
8+
subject(:export) do
9+
described_class.new(workspace)
10+
end
11+
12+
let(:active) do
13+
true
14+
end
15+
16+
let(:workspace) do
17+
FactoryGirl.create(
18+
:mdm_workspace
19+
)
20+
end
21+
22+
context '#extract_module_detail_info' do
23+
let(:report_file) do
24+
StringIO.new
25+
end
26+
27+
subject(:extract_module_detail_info) do
28+
export.extract_module_detail_info(report_file)
29+
end
30+
31+
context 'with Mdm::ModuleDetails' do
32+
let(:document) do
33+
Nokogiri::XML(report_file.string)
34+
end
35+
36+
let(:module_detail_count) do
37+
2
38+
end
39+
40+
let(:root) do
41+
document.root
42+
end
43+
44+
let!(:module_details) do
45+
FactoryGirl.create_list(
46+
:mdm_module_detail,
47+
module_detail_count
48+
)
49+
end
50+
51+
before(:each) do
52+
report_file.write("<root>")
53+
extract_module_detail_info
54+
report_file.write("</root>")
55+
end
56+
57+
it 'should have module_detail tag for each Mdm::ModuleDetail' do
58+
nodes = root.xpath('module_detail')
59+
60+
nodes.length.should == module_detail_count
61+
end
62+
63+
context 'module_detail' do
64+
let(:module_detail) do
65+
module_details.first
66+
end
67+
68+
subject(:module_detail_node) do
69+
root.at_xpath('module_detail')
70+
end
71+
72+
it_should_behave_like 'Msf::DBManager::Export#extract_module_detail_info module_detail child', 'description'
73+
74+
context '/disclosure-date' do
75+
it 'should have Mdm::ModuleDetail#disclosure_date present' do
76+
module_detail.disclosure_date.should be_present
77+
end
78+
79+
it 'should have Mdm::ModuleDetail#disclosure_date from disclosure-date content' do
80+
node = module_detail_node.at_xpath('disclosure-date')
81+
82+
Date.parse(node.content).should == module_detail.disclosure_date
83+
end
84+
end
85+
86+
it_should_behave_like 'Msf::DBManager::Export#extract_module_detail_info module_detail child', 'file'
87+
it_should_behave_like 'Msf::DBManager::Export#extract_module_detail_info module_detail child', 'fullname'
88+
it_should_behave_like 'Msf::DBManager::Export#extract_module_detail_info module_detail child', 'license'
89+
it_should_behave_like 'Msf::DBManager::Export#extract_module_detail_info module_detail child', 'mtime'
90+
it_should_behave_like 'Msf::DBManager::Export#extract_module_detail_info module_detail child', 'mtype'
91+
it_should_behave_like 'Msf::DBManager::Export#extract_module_detail_info module_detail child', 'name'
92+
it_should_behave_like 'Msf::DBManager::Export#extract_module_detail_info module_detail child', 'privileged'
93+
it_should_behave_like 'Msf::DBManager::Export#extract_module_detail_info module_detail child', 'rank'
94+
it_should_behave_like 'Msf::DBManager::Export#extract_module_detail_info module_detail child', 'refname'
95+
96+
# @todo https://www.pivotaltracker.com/story/show/48451001
97+
end
98+
end
99+
100+
context 'without Mdm::ModuleDetails' do
101+
it 'should not write anything to report_file' do
102+
extract_module_detail_info
103+
104+
report_file.string.should be_empty
105+
end
106+
end
107+
end
108+
end

spec/lib/msf/core/db_spec.rb

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,15 @@
1212
require 'msf/core'
1313

1414
describe Msf::DBManager do
15-
include_context 'Msf::Simple::Framework'
15+
include_context 'Msf::DBManager'
1616

17-
subject(:db_manager) do
18-
framework.db
17+
subject do
18+
db_manager
1919
end
2020

2121
it_should_behave_like 'Msf::DBManager::ImportMsfXml'
2222

2323
context '#report_session' do
24-
include_context 'DatabaseCleaner'
25-
2624
let(:options) do
2725
{}
2826
end
@@ -31,17 +29,6 @@
3129
db_manager.report_session(options)
3230
end
3331

34-
before(:each) do
35-
configurations = Metasploit::Framework::Database.configurations
36-
spec = configurations[Metasploit::Framework.env]
37-
38-
# Need to connect or ActiveRecord::Base.connection_pool will raise an
39-
# error.
40-
db_manager.connect(spec)
41-
42-
db_manager.stub(:active => active)
43-
end
44-
4532
context 'with active' do
4633
let(:active) do
4734
true
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
shared_context 'Msf::DBManager' do
2+
include_context 'DatabaseCleaner'
3+
include_context 'Msf::Simple::Framework'
4+
5+
let(:db_manager) do
6+
framework.db
7+
end
8+
9+
before(:each) do
10+
configurations = Metasploit::Framework::Database.configurations
11+
spec = configurations[Metasploit::Framework.env]
12+
13+
# Need to connect or ActiveRecord::Base.connection_pool will raise an
14+
# error.
15+
db_manager.connect(spec)
16+
17+
db_manager.stub(:active => active)
18+
end
19+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
shared_examples_for 'Msf::DBManager::Export#extract_module_detail_info module_detail child' do |child_node_name|
2+
attribute_name = child_node_name.underscore
3+
4+
subject(:child_node) do
5+
module_detail_node.at_xpath(child_node_name)
6+
end
7+
8+
let(:attribute) do
9+
module_detail.send(attribute_name)
10+
end
11+
12+
it "should have Mdm::ModuleDetail##{attribute_name} present" do
13+
attribute.should be_present
14+
end
15+
16+
it "should have Mdm::ModuleDetail##{attribute_name} for #{child_node_name} content" do
17+
child_node.content.should == attribute.to_s
18+
end
19+
end

spec/support/shared/examples/msf/db_manager/import_msf_xml.rb

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ def with_info
2121
subject
2222
end
2323

24+
let(:active) do
25+
true
26+
end
27+
2428
let(:allow_yaml) do
2529
false
2630
end
@@ -83,15 +87,6 @@ def with_info
8387
Builder::XmlMarkup.new(:indent => 2)
8488
end
8589

86-
before(:each) do
87-
configurations = Metasploit::Framework::Database.configurations
88-
spec = configurations[Metasploit::Framework.env]
89-
90-
# Need to connect or Msf::DBManager#active will be false and
91-
# Msf::DBManager#report_* methods won't create any records.
92-
db_manager.connect(spec)
93-
end
94-
9590
it 'should include methods from module so method can be overridden easier in pro' do
9691
db_manager.should be_a Msf::DBManager::ImportMsfXml
9792
end

0 commit comments

Comments
 (0)