Skip to content

Commit f7e3abf

Browse files
committed
sqlcmd specs and fixes
1 parent 3b651ae commit f7e3abf

File tree

2 files changed

+64
-8
lines changed

2 files changed

+64
-8
lines changed

lib/msf/core/post/windows/mssql.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,16 @@ def get_sql_client
7575
#
7676
# @return [Boolean] true if osql is present
7777
def check_osql
78-
running_services1 = run_cmd("osql -?")
79-
services_array1 = running_services1.split("\n")
80-
services_array1.join =~ /(SQL Server Command Line Tool)|(usage: osql)/i
78+
result = run_cmd('osql -?')
79+
result =~ /(SQL Server Command Line Tool)|(usage: osql)/i
8180
end
8281

8382
# Attempts to run the sqlcmd command line tool
8483
#
8584
# @return [Boolean] true if sqlcmd is present
8685
def check_sqlcmd
87-
running_services = run_cmd("sqlcmd -?")
88-
services_array = running_services.split("\n")
89-
services_array.each do |service|
90-
return true if service =~ /SQL Server Command Line Tool/i
91-
end
86+
result = run_cmd('sqlcmd -?')
87+
result =~ /SQL Server Command Line Tool/i
9288
end
9389

9490
# Runs a SQL query using the identified command line tool

spec/lib/msf/core/post/windows/mssql_spec.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,4 +268,64 @@
268268
end
269269
end
270270
end
271+
272+
describe "#run_cmd" do
273+
274+
end
275+
276+
let(:osql) do
277+
'osql'
278+
end
279+
280+
let(:sql_command) do
281+
'sqlcmd'
282+
end
283+
284+
describe "#check_osql" do
285+
it "should return nil if no osql" do
286+
expect(subject).to receive(:run_cmd).with('osql -?').and_return('blah')
287+
subject.check_osql.should be_falsey
288+
end
289+
290+
it "should return true if present" do
291+
expect(subject).to receive(:run_cmd).with('osql -?').and_return('(usage: osql)')
292+
subject.check_osql.should be_truthy
293+
end
294+
end
295+
296+
describe "#check_sqlcmd" do
297+
it "should return nil if no sqlcmd" do
298+
expect(subject).to receive(:run_cmd).and_return('blah')
299+
subject.check_sqlcmd.should be_falsey
300+
end
301+
302+
it "should return true if present" do
303+
expect(subject).to receive(:run_cmd).and_return('SQL Server Command Line Tool')
304+
subject.check_sqlcmd.should be_truthy
305+
end
306+
307+
end
308+
309+
describe "#get_sql_client" do
310+
it "should return nil if no client is available" do
311+
expect(subject).to receive(:check_sqlcmd).and_return(false)
312+
expect(subject).to receive(:check_osql).and_return(false)
313+
subject.get_sql_client.should be_nil
314+
subject.sql_client.should be_nil
315+
end
316+
317+
it "should return 'osql' if osql is available" do
318+
expect(subject).to receive(:check_sqlcmd).and_return(false)
319+
expect(subject).to receive(:check_osql).and_return(true)
320+
subject.get_sql_client.should eq osql
321+
subject.sql_client.should eq osql
322+
end
323+
324+
it "should return 'sqlcmd' if sqlcmd is available" do
325+
allow(subject).to receive(:check_osql).and_return(true)
326+
expect(subject).to receive(:check_sqlcmd).and_return(true)
327+
subject.get_sql_client.should eq sql_command
328+
subject.sql_client.should eq sql_command
329+
end
330+
end
271331
end

0 commit comments

Comments
 (0)