Skip to content

Commit

Permalink
more specs and progress
Browse files Browse the repository at this point in the history
  • Loading branch information
h00die committed Dec 11, 2024
1 parent 9ccc0a3 commit 80d15ae
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
9 changes: 4 additions & 5 deletions lib/msf/core/post/linux/packages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions spec/lib/msf/core/post/linux/compile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/msf/core/post/linux/packages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
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

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
Expand Down
3 changes: 3 additions & 0 deletions spec/lib/msf/core/post/linux/process_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 80d15ae

Please sign in to comment.