diff --git a/lib/msf/core/post/linux/packages.rb b/lib/msf/core/post/linux/packages.rb index ba83baa19f7c..2a3f925893aa 100644 --- a/lib/msf/core/post/linux/packages.rb +++ b/lib/msf/core/post/linux/packages.rb @@ -15,11 +15,10 @@ module Packages def installed_package_version(package) info = get_sysinfo - if ['debian', 'ubuntu'].include?info[:distro] - package_version = cmd_exec("dpkg -l #{package} | grep \'^ii\'") - return nil unless package_version.start_with?('ii') + if ['debian', 'ubuntu'].include?(info[:distro]) + package_version = cmd_exec("dpkg-query -f='${Version}' -W #{package}") + return nil if package_version.include?('no packages found') - package_version = package_version.split(' ')[2] package_version = package_version.gsub('+', '.') return Rex::Version.new(package_version) elsif ['redhat', 'fedora'].include?(info[:distro]) @@ -64,7 +63,7 @@ def installed_package_version(package) package_version = package_version.match(/Version\s+:\s+(.+)/)[1] return Rex::Version.new(package_version) else - vprint_error('installed_package_version is being called on an unsupported OS') + vprint_error("installed_package_version is being called on an unsupported OS: #{info[:distro]}") end nil end diff --git a/spec/lib/msf/core/post/linux/compile_spec.rb b/spec/lib/msf/core/post/linux/compile_spec.rb index 20e5feb5746b..cd4a1c529c45 100644 --- a/spec/lib/msf/core/post/linux/compile_spec.rb +++ b/spec/lib/msf/core/post/linux/compile_spec.rb @@ -69,22 +69,33 @@ let(:source) { '/path/to/source.c' } let(:destination) { '/tmp/source.c' } let(:output) { '/tmp/output' } + let(:session) { double('Session', send: nil) } before do allow(subject).to receive(:get_compiler).and_return('gcc') end - it 'uploads the source file and compiles it' do + it 'uploads the source file and compiles it on meterpreter' do expect(subject).to receive(:upload_file).with(destination, source) expect(subject).to receive(:cmd_exec).with("gcc #{destination} -o #{output}") expect(subject).to receive(:write_file).and_return('/tmp/foo') - allow(session).to receive(:type).and_return('meterpreter') + expect(session).to receive(:type).and_return('meterpreter') + + subject.upload_and_compile(source, destination, output) + end + + it 'uploads the source file and compiles it on shell' do + expect(subject).to receive(:upload_file).with(destination, source) + expect(subject).to receive(:cmd_exec).with("PATH=\"$PATH:/usr/bin/\" gcc #{destination} -o #{output}") + expect(subject).to receive(:write_file).and_return('/tmp/foo') + expect(session).to receive(:type).and_return('shell') subject.upload_and_compile(source, destination, output) end it 'raises an error if no compiler is available' do allow(subject).to receive(:get_compiler).and_return(nil) + expect(session).to receive(:type).and_return('shell') expect { subject.upload_and_compile(source, destination, output) }.to raise_error('No compiler available on target') end diff --git a/spec/lib/msf/core/post/linux/packages_spec.rb b/spec/lib/msf/core/post/linux/packages_spec.rb index e389355ae029..438a2d0fb69a 100644 --- a/spec/lib/msf/core/post/linux/packages_spec.rb +++ b/spec/lib/msf/core/post/linux/packages_spec.rb @@ -26,7 +26,7 @@ context 'when the Ubuntu/Debian package is installed' do it 'returns 3.5-5ubuntu2.1' do allow(subject).to receive(:get_sysinfo).and_return({:kernel=>"Linux ubuntu22 5.15.0-25-generic #25-Ubuntu SMP Wed Mar 30 15:54:22 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux", :distro=>"ubuntu", :version=>"Ubuntu 22.04.5 LTS"}) - allow(subject).to receive(:cmd_exec).and_return('ii needrestart 3.5-5ubuntu2.1 all check which daemons need to be restarted after library upgrades') + allow(subject).to receive(:cmd_exec).and_return('3.5-5ubuntu2.1') expect(subject.installed_package_version('test')).to eq(Rex::Version.new('3.5-5ubuntu2.1')) end end @@ -34,7 +34,7 @@ context 'when the Ubuntu/Debian package is installed with a + in the version number' do it 'returns 1.34.dfsg.pre.1ubuntu0.1.22.04.2' do allow(subject).to receive(:get_sysinfo).and_return({:kernel=>"Linux ubuntu22 5.15.0-25-generic #25-Ubuntu SMP Wed Mar 30 15:54:22 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux", :distro=>"ubuntu", :version=>"Ubuntu 22.04.5 LTS"}) - allow(subject).to receive(:cmd_exec).and_return('ii tar 1.34+dfsg-1ubuntu0.1.22.04.2 amd64 GNU version of the tar archiving utility') + allow(subject).to receive(:cmd_exec).and_return('1.34+dfsg-1ubuntu0.1.22.04.2') expect(subject.installed_package_version('test')).to eq(Rex::Version.new("1.34.dfsg.pre.1ubuntu0.1.22.04.2")) end end diff --git a/spec/lib/msf/core/post/linux/process_spec.rb b/spec/lib/msf/core/post/linux/process_spec.rb index f66fcae0c899..52b96b5b75a7 100644 --- a/spec/lib/msf/core/post/linux/process_spec.rb +++ b/spec/lib/msf/core/post/linux/process_spec.rb @@ -12,11 +12,13 @@ let(:length) { 64 } let(:pid) { 1234 } let(:memory_content) { 'memory content' } + let(:mock_session) { double('Session', send: nil) } it 'reads memory from the specified base address and length' do expect(subject).to receive(:session) expect(subject).to receive(:open).with(pid, PROCESS_READ).and_return(1) expect(memory).to receive(:read).with(base_address, length).and_return(memory_content) + expect(mock_session).to receive(:type).and_return('meterpreter') result = subject.mem_read(base_address, length, pid: pid) expect(result).to eq(memory_content) @@ -26,6 +28,7 @@ expect(subject).to receive(:session) expect(subject).to receive(:open).with(0, PROCESS_READ).and_return(1) expect(memory).to receive(:read).with(base_address, length).and_return(memory_content) + expect(mock_session).to receive(:type).and_return('meterpreter') result = subject.mem_read(base_address, length) expect(result).to eq(memory_content)