|
| 1 | +# -*- coding: binary -*- |
| 2 | +require 'spec_helper' |
| 3 | + |
| 4 | +require 'msf/core/post/windows/mssql' |
| 5 | + |
| 6 | +describe Msf::Post::Windows::MSSQL do |
| 7 | + let(:subject) do |
| 8 | + mod = Module.new |
| 9 | + mod.extend described_class |
| 10 | + stubs = [ :vprint_status, :print_status, :vprint_good, :print_good, :print_error ] |
| 11 | + stubs.each { |meth| mod.stub(meth) } |
| 12 | + mod.stub(:service_info).and_return({}) |
| 13 | + mod |
| 14 | + end |
| 15 | + |
| 16 | + let(:running_pid) do |
| 17 | + 6541 |
| 18 | + end |
| 19 | + |
| 20 | + let(:stopped_pid) do |
| 21 | + 0 |
| 22 | + end |
| 23 | + |
| 24 | + let(:named_instance) do |
| 25 | + 'NamedInstance' |
| 26 | + end |
| 27 | + |
| 28 | + # http://blogs.technet.com/b/fort_sql/archive/2010/05/31/list-of-sql-server-service-names.aspx |
| 29 | + let(:sql_server_7_display) do |
| 30 | + 'MSSQLServer' |
| 31 | + end |
| 32 | + |
| 33 | + let(:sql_server_2000_display) do |
| 34 | + 'MSSQLServer' |
| 35 | + end |
| 36 | + |
| 37 | + let(:sql_server_2000_named_display) do |
| 38 | + "MSSQL$#{named_instance}" |
| 39 | + end |
| 40 | + |
| 41 | + # Affects 7 and 2000 |
| 42 | + let(:sql_server_analysis_services_display) do |
| 43 | + 'MSSQLServerOLAPService' |
| 44 | + end |
| 45 | + |
| 46 | + let(:sql_server_2005_display) do |
| 47 | + 'SQL Server (MSSQLSERVER)' |
| 48 | + end |
| 49 | + |
| 50 | + let(:sql_server_2005_named_display) do |
| 51 | + "MSSQLServer#{named_instance}" |
| 52 | + end |
| 53 | + |
| 54 | + let(:sql_server_2008_display) do |
| 55 | + 'SQL Server (MSSQLSERVER)' |
| 56 | + end |
| 57 | + |
| 58 | + let(:sql_server_2008_named_display) do |
| 59 | + "SQL Server (#{named_instance})" |
| 60 | + end |
| 61 | + |
| 62 | + # Affects 2005/2008 |
| 63 | + let(:sql_server_agent_display) do |
| 64 | + "SQL Server Agent (MSSQLServer)" |
| 65 | + end |
| 66 | + |
| 67 | + let(:stopped_2k8_sql_instance) do |
| 68 | + { display: sql_server_2008_display, pid: stopped_pid } |
| 69 | + end |
| 70 | + |
| 71 | + let(:running_2k8_sql_instance) do |
| 72 | + { display: sql_server_2008_display, pid: running_pid } |
| 73 | + end |
| 74 | + |
| 75 | + let(:running_named_2k8_sql_instance) do |
| 76 | + { display: sql_server_2008_named_display, pid: running_pid } |
| 77 | + end |
| 78 | + |
| 79 | + let(:stopped_named_2k8_sql_instance) do |
| 80 | + { display: sql_server_2008_named_display, pid: stopped_pid } |
| 81 | + end |
| 82 | + |
| 83 | + let(:running_sql_server_agent_service) do |
| 84 | + { display: sql_server_agent_display, pid: running_pid } |
| 85 | + end |
| 86 | + |
| 87 | + let(:running_2k5_sql_instance) do |
| 88 | + { display: sql_server_2005_display, pid: running_pid } |
| 89 | + end |
| 90 | + |
| 91 | + let(:running_named_2k5_sql_instance) do |
| 92 | + { display: sql_server_2005_named_display, pid: running_pid } |
| 93 | + end |
| 94 | + |
| 95 | + let(:running_2k_sql_instance) do |
| 96 | + { display: sql_server_2000_display, pid: running_pid } |
| 97 | + end |
| 98 | + |
| 99 | + let(:running_named_2k_sql_instance) do |
| 100 | + { display: sql_server_2000_named_display, pid: running_pid } |
| 101 | + end |
| 102 | + |
| 103 | + let(:running_7_sql_instance) do |
| 104 | + { display: sql_server_7_display, pid: running_pid } |
| 105 | + end |
| 106 | + |
| 107 | + let(:running_analysis_service) do |
| 108 | + { display: sql_server_analysis_services_display, pid: running_pid } |
| 109 | + end |
| 110 | + |
| 111 | + let(:normal_service) do |
| 112 | + { display: 'blah', pid: running_pid } |
| 113 | + end |
| 114 | + |
| 115 | + describe "#check_for_sqlserver" do |
| 116 | + let(:instance) do |
| 117 | + nil |
| 118 | + end |
| 119 | + |
| 120 | + context "when instance is nil" do |
| 121 | + it "should return nil if unable to locate any SQL instance" do |
| 122 | + allow(subject).to receive(:each_service).and_yield(normal_service) |
| 123 | + result = subject.check_for_sqlserver(instance) |
| 124 | + result.should be_nil |
| 125 | + end |
| 126 | + |
| 127 | + it "should identify a running SQL instance" do |
| 128 | + allow(subject).to receive(:each_service).and_yield(normal_service).and_yield(running_2k8_sql_instance) |
| 129 | + result = subject.check_for_sqlserver(instance) |
| 130 | + result.should eq running_2k8_sql_instance |
| 131 | + end |
| 132 | + |
| 133 | + it "shouldn't identify a non running SQL instance" do |
| 134 | + allow(subject).to receive(:each_service).and_yield(normal_service).and_yield(stopped_2k8_sql_instance).and_yield(running_2k8_sql_instance) |
| 135 | + result = subject.check_for_sqlserver(instance) |
| 136 | + result.should eq running_2k8_sql_instance |
| 137 | + end |
| 138 | + end |
| 139 | + |
| 140 | + context "when SQL Server 7 and instance is nil" do |
| 141 | + it "should identify a running SQL instance" do |
| 142 | + allow(subject).to receive(:each_service).and_yield(normal_service).and_yield(running_analysis_service).and_yield(running_7_sql_instance) |
| 143 | + result = subject.check_for_sqlserver(instance) |
| 144 | + result.should eq running_7_sql_instance |
| 145 | + end |
| 146 | + end |
| 147 | + |
| 148 | + context "when SQL Server 2000 and instance is nil" do |
| 149 | + it "should identify a running SQL instance" do |
| 150 | + allow(subject).to receive(:each_service).and_yield(normal_service).and_yield(running_analysis_service).and_yield(running_2k_sql_instance) |
| 151 | + result = subject.check_for_sqlserver(instance) |
| 152 | + result.should eq running_2k_sql_instance |
| 153 | + end |
| 154 | + |
| 155 | + it "should identify a named SQL instance" do |
| 156 | + allow(subject).to receive(:each_service).and_yield(normal_service).and_yield(running_analysis_service).and_yield(running_named_2k_sql_instance) |
| 157 | + result = subject.check_for_sqlserver(instance) |
| 158 | + result.should eq running_named_2k_sql_instance |
| 159 | + end |
| 160 | + end |
| 161 | + |
| 162 | + context "when SQL Server 2005 and instance is nil" do |
| 163 | + it "should identify a running SQL instance" do |
| 164 | + allow(subject).to receive(:each_service).and_yield(normal_service).and_yield(running_sql_server_agent_service).and_yield(running_2k5_sql_instance) |
| 165 | + result = subject.check_for_sqlserver(instance) |
| 166 | + result.should eq running_2k5_sql_instance |
| 167 | + end |
| 168 | + |
| 169 | + it "should identify a named SQL instance" do |
| 170 | + allow(subject).to receive(:each_service).and_yield(normal_service).and_yield(running_sql_server_agent_service).and_yield(running_named_2k5_sql_instance) |
| 171 | + result = subject.check_for_sqlserver(instance) |
| 172 | + result.should eq running_named_2k5_sql_instance |
| 173 | + end |
| 174 | + end |
| 175 | + |
| 176 | + context "when SQL Server 2008 and instance is nil" do |
| 177 | + it "should identify a running SQL instance" do |
| 178 | + allow(subject).to receive(:each_service).and_yield(normal_service).and_yield(running_sql_server_agent_service).and_yield(running_2k8_sql_instance) |
| 179 | + result = subject.check_for_sqlserver(instance) |
| 180 | + result.should eq running_2k8_sql_instance |
| 181 | + end |
| 182 | + |
| 183 | + it "should identify a named SQL instance" do |
| 184 | + allow(subject).to receive(:each_service).and_yield(normal_service).and_yield(running_sql_server_agent_service).and_yield(running_named_2k8_sql_instance) |
| 185 | + result = subject.check_for_sqlserver(instance) |
| 186 | + result.should eq running_named_2k8_sql_instance |
| 187 | + end |
| 188 | + end |
| 189 | + |
| 190 | + context "when instance is supplied" do |
| 191 | + let(:instance) do |
| 192 | + named_instance |
| 193 | + end |
| 194 | + |
| 195 | + it "should return nil if unable to locate any SQL instance" do |
| 196 | + allow(subject).to receive(:each_service).and_yield(normal_service) |
| 197 | + result = subject.check_for_sqlserver(instance) |
| 198 | + result.should be_nil |
| 199 | + end |
| 200 | + |
| 201 | + it "should identify a running SQL instance" do |
| 202 | + allow(subject).to receive(:each_service).and_yield(normal_service).and_yield(running_named_2k8_sql_instance) |
| 203 | + result = subject.check_for_sqlserver(instance) |
| 204 | + result.should eq running_named_2k8_sql_instance |
| 205 | + end |
| 206 | + |
| 207 | + it "shouldn't identify a non running SQL instance" do |
| 208 | + allow(subject).to receive(:each_service).and_yield(normal_service).and_yield(stopped_named_2k8_sql_instance).and_yield(running_named_2k8_sql_instance) |
| 209 | + result = subject.check_for_sqlserver(instance) |
| 210 | + result.should eq running_named_2k8_sql_instance |
| 211 | + end |
| 212 | + |
| 213 | + it "should only identify that instance" do |
| 214 | + allow(subject).to receive(:each_service).and_yield(normal_service).and_yield(running_2k8_sql_instance).and_yield(running_named_2k8_sql_instance) |
| 215 | + result = subject.check_for_sqlserver(instance) |
| 216 | + result.should eq running_named_2k8_sql_instance |
| 217 | + end |
| 218 | + end |
| 219 | + |
| 220 | + context "when SQL Server 7 and instance is supplied" do |
| 221 | + let(:instance) do |
| 222 | + 'MSSQLServer' |
| 223 | + end |
| 224 | + |
| 225 | + it "should identify a running SQL instance" do |
| 226 | + allow(subject).to receive(:each_service).and_yield(normal_service).and_yield(running_analysis_service).and_yield(running_7_sql_instance) |
| 227 | + result = subject.check_for_sqlserver(instance) |
| 228 | + result.should eq running_7_sql_instance |
| 229 | + end |
| 230 | + end |
| 231 | + |
| 232 | + context "when SQL Server 2000 and instance is supplied" do |
| 233 | + let(:instance) do |
| 234 | + named_instance |
| 235 | + end |
| 236 | + |
| 237 | + it "should identify only a named SQL instance" do |
| 238 | + allow(subject).to receive(:each_service).and_yield(normal_service).and_yield(running_analysis_service). |
| 239 | + and_yield(running_2k_sql_instance).and_yield(running_named_2k_sql_instance) |
| 240 | + result = subject.check_for_sqlserver(instance) |
| 241 | + result.should eq running_named_2k_sql_instance |
| 242 | + end |
| 243 | + end |
| 244 | + |
| 245 | + context "when SQL Server 2005 and instance is supplied" do |
| 246 | + let(:instance) do |
| 247 | + named_instance |
| 248 | + end |
| 249 | + |
| 250 | + it "should identify only a named SQL instance" do |
| 251 | + allow(subject).to receive(:each_service).and_yield(normal_service).and_yield(running_analysis_service). |
| 252 | + and_yield(running_2k5_sql_instance).and_yield(running_named_2k5_sql_instance) |
| 253 | + result = subject.check_for_sqlserver(instance) |
| 254 | + result.should eq running_named_2k5_sql_instance |
| 255 | + end |
| 256 | + end |
| 257 | + |
| 258 | + context "when SQL Server 2008 and instance is supplied" do |
| 259 | + let(:instance) do |
| 260 | + named_instance |
| 261 | + end |
| 262 | + |
| 263 | + it "should identify only a named SQL instance" do |
| 264 | + allow(subject).to receive(:each_service).and_yield(normal_service).and_yield(running_analysis_service). |
| 265 | + and_yield(running_2k8_sql_instance).and_yield(running_named_2k8_sql_instance) |
| 266 | + result = subject.check_for_sqlserver(instance) |
| 267 | + result.should eq running_named_2k8_sql_instance |
| 268 | + end |
| 269 | + end |
| 270 | + end |
| 271 | +end |
0 commit comments