Skip to content

Commit 9e86582

Browse files
author
Pedro Ribeiro
committed
Merge pull request #2 from rapid7/master
update
2 parents bcdea05 + 1cdf1c2 commit 9e86582

File tree

123 files changed

+2538
-684
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+2538
-684
lines changed

.rubocop.yml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ Style/MethodLength:
3939
often exceed 200 lines.
4040
Max: 300
4141

42+
# Basically everything in metasploit needs binary encoding, not UTF-8.
43+
# Disable this here and enforce it through msftidy
44+
Style/Encoding:
45+
Enabled: false
46+
4247
Style/NumericLiterals:
4348
Enabled: false
4449
Description: 'This often hurts readability for exploit-ish code.'
@@ -53,4 +58,22 @@ Style/StringLiterals:
5358

5459
Style/WordArray:
5560
Enabled: false
56-
Description: 'Metasploit prefers consistent use of []'
61+
Description: 'Metasploit prefers consistent use of []'
62+
63+
Style/RedundantBegin:
64+
Exclude:
65+
# this pattern is very common and somewhat unavoidable
66+
# def run_host(ip)
67+
# begin
68+
# ...
69+
# rescue ...
70+
# ...
71+
# ensure
72+
# disconnect
73+
# end
74+
# end
75+
- 'modules/**/*'
76+
77+
Documentation:
78+
Exclude:
79+
- 'modules/**/*'

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
env:
2+
- RAKE_TASK=cucumber
3+
- RAKE_TASK=spec
14
language: ruby
25
before_install:
36
- rake --version
@@ -14,6 +17,7 @@ before_script:
1417
- bundle exec rake --version
1518
- bundle exec rake db:create
1619
- bundle exec rake db:migrate
20+
script: "bundle exec rake $RAKE_TASK"
1721

1822
rvm:
1923
#- '1.8.7'

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ group :pcap do
4848
end
4949

5050
group :test do
51+
# cucumber extension for testing command line applications, like msfconsole
52+
gem 'aruba'
53+
# cucumber + automatic database cleaning with database_cleaner
54+
gem 'cucumber-rails'
5155
gem 'shoulda-matchers'
5256
# code coverage for tests
5357
# any version newer than 0.5.4 gives an Encoding error when trying to read the source files.

Gemfile.lock

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,41 @@ GEM
4545
arel (3.0.3)
4646
arel-helpers (2.0.1)
4747
activerecord (>= 3.1.0, < 5)
48+
aruba (0.6.1)
49+
childprocess (>= 0.3.6)
50+
cucumber (>= 1.1.1)
51+
rspec-expectations (>= 2.7.0)
4852
bcrypt (3.1.7)
4953
builder (3.0.4)
54+
capybara (2.4.1)
55+
mime-types (>= 1.16)
56+
nokogiri (>= 1.3.3)
57+
rack (>= 1.0.0)
58+
rack-test (>= 0.5.4)
59+
xpath (~> 2.0)
60+
childprocess (0.5.3)
61+
ffi (~> 1.0, >= 1.0.11)
5062
coderay (1.1.0)
63+
cucumber (1.2.1)
64+
builder (>= 2.1.2)
65+
diff-lcs (>= 1.1.3)
66+
gherkin (~> 2.11.0)
67+
json (>= 1.4.6)
68+
cucumber-rails (1.3.0)
69+
capybara (>= 1.1.2)
70+
cucumber (>= 1.1.8)
71+
nokogiri (>= 1.5.0)
5172
diff-lcs (1.2.5)
5273
erubis (2.7.0)
5374
factory_girl (4.4.0)
5475
activesupport (>= 3.0.0)
5576
factory_girl_rails (4.4.1)
5677
factory_girl (~> 4.4.0)
5778
railties (>= 3.0.0)
79+
ffi (1.9.3)
5880
fivemat (1.2.1)
81+
gherkin (2.11.6)
82+
json (>= 1.7.6)
5983
hike (1.2.3)
6084
i18n (0.6.11)
6185
journey (1.0.4)
@@ -80,6 +104,7 @@ GEM
80104
pg
81105
meterpreter_bins (0.0.7)
82106
method_source (0.8.2)
107+
mime-types (2.3)
83108
mini_portile (0.6.0)
84109
msgpack (0.5.8)
85110
multi_json (1.0.4)
@@ -150,13 +175,17 @@ GEM
150175
tilt (1.4.1)
151176
timecop (0.7.1)
152177
tzinfo (0.3.40)
178+
xpath (2.0.0)
179+
nokogiri (~> 1.3)
153180
yard (0.8.7.4)
154181

155182
PLATFORMS
156183
ruby
157184

158185
DEPENDENCIES
159186
activerecord (>= 3.0.0, < 4.0.0)
187+
aruba
188+
cucumber-rails
160189
factory_girl (>= 4.1.0)
161190
factory_girl_rails
162191
fivemat (= 1.2.1)

config/application.rb

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,14 @@
2626
#
2727

2828
require 'metasploit/framework/common_engine'
29-
require 'msf/base/config'
29+
require 'metasploit/framework/database'
3030

3131
module Metasploit
3232
module Framework
3333
class Application < Rails::Application
3434
include Metasploit::Framework::CommonEngine
3535

36-
environment_database_yaml = ENV['MSF_DATABASE_CONFIG']
37-
38-
if environment_database_yaml
39-
# DO NOT check if the path exists: if the environment variable is set, then the user meant to use this path
40-
# and if it doesn't exist then an error should occur so the user knows the environment variable points to a
41-
# non-existent file.
42-
config.paths['config/database'] = environment_database_yaml
43-
else
44-
user_config_root = Pathname.new(Msf::Config.get_config_root)
45-
user_database_yaml = user_config_root.join('database.yml')
46-
47-
# DO check if the path exists as in test environments there may be no config root, in which case the normal
48-
# rails location, `config/database.yml`, should contain the database config.
49-
if user_database_yaml.exist?
50-
config.paths['config/database'] = [user_database_yaml.to_path]
51-
end
52-
end
36+
config.paths['config/database'] = [Metasploit::Framework::Database.configurations_pathname.try(:to_path)]
5337
end
5438
end
5539
end

config/cucumber.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<%
2+
rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
3+
rerun_opts = rerun.to_s.strip.empty? ? "--format #{ENV['CUCUMBER_FORMAT'] || 'progress'} features" : "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}"
4+
std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --tags ~@wip"
5+
%>
6+
default: <%= std_opts %> features
7+
wip: --tags @wip:3 --wip features
8+
rerun: <%= rerun_opts %> --format rerun --out rerun.txt --strict --tags ~@wip
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
Feature: `msfconsole` `database.yml`
2+
3+
In order to connect to the database in `msfconsole`
4+
As a user calling `msfconsole` from a terminal
5+
I want to be able to set the path of the `database.yml` in one of 4 locations (in order of precedence):
6+
7+
1. An explicit argument to the `-y` flag to `msfconsole`
8+
2. The MSF_DATABASE_CONFIG environment variable
9+
3. The user's `~/.msf4/database.yml`
10+
4. `config/database.yml` in the metasploit-framework checkout location.
11+
12+
Scenario: With all 4 locations, --yaml wins
13+
Given a file named "command_line.yml" with:
14+
"""
15+
test:
16+
adapter: postgresql
17+
database: command_line_metasploit_framework_test
18+
username: command_line_metasploit_framework_test
19+
"""
20+
And a file named "msf_database_config.yml" with:
21+
"""
22+
test:
23+
adapter: postgresql
24+
database: environment_metasploit_framework_test
25+
username: environment_metasploit_framework_test
26+
"""
27+
And I set the environment variables to:
28+
| variable | value |
29+
| MSF_DATABASE_CONFIG | msf_database_config.yml |
30+
And a directory named "home"
31+
And I cd to "home"
32+
And a mocked home directory
33+
And a directory named ".msf4"
34+
And I cd to ".msf4"
35+
And a file named "database.yml" with:
36+
"""
37+
test:
38+
adapter: postgresql
39+
database: user_metasploit_framework_test
40+
username: user_metasploit_framework_test
41+
"""
42+
And I cd to "../.."
43+
And the project "database.yml" exists with:
44+
"""
45+
test:
46+
adapter: postgresql
47+
database: project_metasploit_framework_test
48+
username: project_metasploit_framework_test
49+
"""
50+
When I run `msfconsole --environment test --yaml command_line.yml` interactively
51+
And I wait for stdout to contain "Free Metasploit Pro trial: http://r-7.co/trymsp"
52+
And I type "exit"
53+
Then the output should contain "command_line_metasploit_framework_test"
54+
55+
Scenario: Without --yaml, MSF_DATABASE_CONFIG wins
56+
Given a file named "msf_database_config.yml" with:
57+
"""
58+
test:
59+
adapter: postgresql
60+
database: environment_metasploit_framework_test
61+
username: environment_metasploit_framework_test
62+
"""
63+
And I set the environment variables to:
64+
| variable | value |
65+
| MSF_DATABASE_CONFIG | msf_database_config.yml |
66+
And a directory named "home"
67+
And I cd to "home"
68+
And a mocked home directory
69+
And a directory named ".msf4"
70+
And I cd to ".msf4"
71+
And a file named "database.yml" with:
72+
"""
73+
test:
74+
adapter: postgresql
75+
database: user_metasploit_framework_test
76+
username: user_metasploit_framework_test
77+
"""
78+
And I cd to "../.."
79+
And the project "database.yml" exists with:
80+
"""
81+
test:
82+
adapter: postgresql
83+
database: project_metasploit_framework_test
84+
username: project_metasploit_framework_test
85+
"""
86+
When I run `msfconsole --environment test` interactively
87+
And I wait for stdout to contain "Free Metasploit Pro trial: http://r-7.co/trymsp"
88+
And I type "exit"
89+
Then the output should contain "environment_metasploit_framework_test"
90+
91+
Scenario: Without --yaml or MSF_DATABASE_CONFIG, ~/.msf4/database.yml wins
92+
Given I unset the environment variables:
93+
| variable |
94+
| MSF_DATABASE_CONFIG |
95+
And a directory named "home"
96+
And I cd to "home"
97+
And a mocked home directory
98+
And a directory named ".msf4"
99+
And I cd to ".msf4"
100+
And a file named "database.yml" with:
101+
"""
102+
test:
103+
adapter: postgresql
104+
database: user_metasploit_framework_test
105+
username: user_metasploit_framework_test
106+
"""
107+
And I cd to "../.."
108+
And the project "database.yml" exists with:
109+
"""
110+
test:
111+
adapter: postgresql
112+
database: project_metasploit_framework_test
113+
username: project_metasploit_framework_test
114+
"""
115+
When I run `msfconsole --environment test` interactively
116+
And I wait for stdout to contain "Free Metasploit Pro trial: http://r-7.co/trymsp"
117+
And I type "exit"
118+
Then the output should contain "user_metasploit_framework_test"
119+
120+
Scenario: Without --yaml, MSF_DATABASE_CONFIG or ~/.msf4/database.yml, project "database.yml" wins
121+
Given I unset the environment variables:
122+
| variable |
123+
| MSF_DATABASE_CONFIG |
124+
And a directory named "home"
125+
And I cd to "home"
126+
And a mocked home directory
127+
And I cd to "../.."
128+
And the project "database.yml" exists with:
129+
"""
130+
test:
131+
adapter: postgresql
132+
database: project_metasploit_framework_test
133+
username: project_metasploit_framework_test
134+
"""
135+
When I run `msfconsole --environment test` interactively
136+
And I wait for stdout to contain "Free Metasploit Pro trial: http://r-7.co/trymsp"
137+
And I type "exit"
138+
Then the output should contain "project_metasploit_framework_test"
139+
140+
141+
Scenario: Without --yaml, MSF_DATABASE_CONFIG, ~/.msf4/database.yml, or project "database.yml", no database connection
142+
Given I unset the environment variables:
143+
| variable |
144+
| MSF_DATABASE_CONFIG |
145+
And a directory named "home"
146+
And I cd to "home"
147+
And a mocked home directory
148+
And I cd to "../.."
149+
And the project "database.yml" does not exist
150+
When I run `msfconsole --environment test` interactively
151+
And I wait for stdout to contain "Free Metasploit Pro trial: http://r-7.co/trymsp"
152+
And I type "db_status"
153+
And I type "exit"
154+
Then the output should not contain "command_line_metasploit_framework_test"
155+
And the output should not contain "environment_metasploit_framework_test"
156+
And the output should not contain "user_metasploit_framework_test"
157+
And the output should not contain "project_metasploit_framework_test"
158+
And the output should contain "[*] postgresql selected, no connection"

features/step_definitions/env.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Given /^I unset the environment variables:$/ do |table|
2+
table.hashes.each do |row|
3+
variable = row['variable'].to_s.upcase
4+
5+
# @todo add extension to Announcer
6+
announcer.instance_eval do
7+
if @options[:env]
8+
print "$ unset #{variable}"
9+
end
10+
end
11+
12+
current_value = ENV.delete(variable)
13+
14+
# if original_env already has the key, then the true original was already recorded from a previous unset or set,
15+
# so don't record the current value as it will cause ENV not to be restored after the Scenario.
16+
unless original_env.key? variable
17+
original_env[variable] = current_value
18+
end
19+
end
20+
end

features/step_definitions/project.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require 'metasploit/framework/database/cucumber'
2+
3+
Given /^the project "database.yml" does not exist$/ do
4+
Metasploit::Framework::Database::Cucumber.backup_project_configurations
5+
end
6+
7+
Given /^the project "database.yml" exists with:$/ do |file_content|
8+
Metasploit::Framework::Database::Cucumber.backup_project_configurations
9+
write_file(Metasploit::Framework::Database::Cucumber.project_configurations_path, file_content)
10+
end
11+
12+
After do
13+
Metasploit::Framework::Database::Cucumber.restore_project_configurations
14+
end

features/support/bin/stty

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env ruby
2+
3+
case ARGV[0]
4+
when 'size'
5+
puts "30 134"
6+
when '-a'
7+
puts <<EOS
8+
speed 38400 baud; 30 rows; 134 columns;
9+
lflags: icanon isig iexten echo echoe echok echoke -echonl echoctl
10+
-echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo
11+
-extproc
12+
iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel iutf8
13+
-ignbrk brkint -inpck -ignpar -parmrk
14+
oflags: opost onlcr -oxtabs -onocr -onlret
15+
cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow
16+
-dtrflow -mdmbuf
17+
cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;
18+
eol2 = <undef>; erase = ^?; intr = ^C; kill = ^U; lnext = ^V;
19+
min = 1; quit = ^\; reprint = ^R; start = ^Q; status = ^T;
20+
stop = ^S; susp = ^Z; time = 0; werase = ^W;
21+
EOS
22+
when '-g'
23+
puts "gfmt1:cflag=4b00:iflag=6b02:lflag=200005cf:oflag=3:discard=f:dsusp=19:eof=4:eol=ff:eol2=ff:erase=7f:intr=3:kill=15:lnext=16:min=1:quit=1c:reprint=12:start=11:status=14:stop=13:susp=1a:time=0:werase=17:ispeed=38400:ospeed=38400"
24+
end
25+
26+
exit 0

0 commit comments

Comments
 (0)