From 9ccc0a3070bef6906125d747a462dd1a16d2fdf0 Mon Sep 17 00:00:00 2001 From: h00die Date: Thu, 5 Dec 2024 15:40:57 -0500 Subject: [PATCH] lib spec progress --- lib/msf/core/post/linux/busy_box.rb | 2 ++ lib/msf/core/post/linux/priv.rb | 1 + lib/msf/core/post/linux/process.rb | 2 ++ lib/msf/core/post/linux/system.rb | 8 +++++--- spec/lib/msf/core/post/linux/compile_spec.rb | 4 +--- spec/lib/msf/core/post/linux/priv_spec.rb | 9 ++++++--- spec/lib/msf/core/post/linux/process_spec.rb | 3 ++- spec/lib/msf/core/post/linux/system_spec.rb | 6 ++++-- 8 files changed, 23 insertions(+), 12 deletions(-) diff --git a/lib/msf/core/post/linux/busy_box.rb b/lib/msf/core/post/linux/busy_box.rb index af84fee730ca..10310f5bc2eb 100644 --- a/lib/msf/core/post/linux/busy_box.rb +++ b/lib/msf/core/post/linux/busy_box.rb @@ -1,5 +1,7 @@ # -*- coding: binary -*- +require 'rex' + module Msf class Post module Linux diff --git a/lib/msf/core/post/linux/priv.rb b/lib/msf/core/post/linux/priv.rb index 59abd7ed5f15..97e085b67b0f 100644 --- a/lib/msf/core/post/linux/priv.rb +++ b/lib/msf/core/post/linux/priv.rb @@ -5,6 +5,7 @@ class Post module Linux module Priv include ::Msf::Post::Common + include ::Msf::Post::File # # Returns true if running as root, false if not. diff --git a/lib/msf/core/post/linux/process.rb b/lib/msf/core/post/linux/process.rb index bbc37aa4fbf9..cd4516292971 100644 --- a/lib/msf/core/post/linux/process.rb +++ b/lib/msf/core/post/linux/process.rb @@ -1,5 +1,7 @@ # -*- coding: binary -*- +require 'rex/post' + module Msf class Post module Linux diff --git a/lib/msf/core/post/linux/system.rb b/lib/msf/core/post/linux/system.rb index 03d321054901..f14da802a67f 100644 --- a/lib/msf/core/post/linux/system.rb +++ b/lib/msf/core/post/linux/system.rb @@ -299,10 +299,12 @@ def protected_symlinks? # def glibc_version raise 'glibc is not installed' unless command_exists? 'ldd' + begin - cmd_exec('ldd --version').scan(/^ldd\s+\(.*\)\s+([\d.]+)/).flatten.first - rescue StandardError - raise 'Could not determine glibc version' + cmd_exec('ldd --version').scan(/^ldd\s+\(.*\)\s+([\d.]+)/).flatten.first + rescue StandardError + raise 'Could not determine glibc version' + end end # diff --git a/spec/lib/msf/core/post/linux/compile_spec.rb b/spec/lib/msf/core/post/linux/compile_spec.rb index ae65103707b9..20e5feb5746b 100644 --- a/spec/lib/msf/core/post/linux/compile_spec.rb +++ b/spec/lib/msf/core/post/linux/compile_spec.rb @@ -61,7 +61,7 @@ it 'raises an error if the specified compiler is not available' do allow(subject).to receive(:datastore).and_return({ 'COMPILE' => 'True', 'COMPILER' => 'gcc' }) allow(subject).to receive(:has_gcc?).and_return(false) - expect { subject.live_compile? }.to raise_error(Msf::Module::Failure::BadConfig, 'gcc is not installed. Set COMPILE False to upload a pre-compiled executable') + expect { subject.live_compile? }.to raise_error(RuntimeError, 'bad-config: gcc is not installed. Set COMPILE False to upload a pre-compiled executable.') end end @@ -69,11 +69,9 @@ let(:source) { '/path/to/source.c' } let(:destination) { '/tmp/source.c' } let(:output) { '/tmp/output' } - let(:session) { double('session') } before do allow(subject).to receive(:get_compiler).and_return('gcc') - allow(subject).to receive(:session).and_return(session) end it 'uploads the source file and compiles it' do diff --git a/spec/lib/msf/core/post/linux/priv_spec.rb b/spec/lib/msf/core/post/linux/priv_spec.rb index 66fb223d15ea..2f0dbcf68c15 100644 --- a/spec/lib/msf/core/post/linux/priv_spec.rb +++ b/spec/lib/msf/core/post/linux/priv_spec.rb @@ -38,7 +38,7 @@ file_content = 'file content' allow(subject).to receive(:read_file).with(origin_file).and_return(file_content) - expect(subject).to receive(:cmd_exec).with("echo '#{file_content}' > #{final_file}") + expect(subject).to receive(:cmd_exec).with("echo '#{file_content}' > '#{final_file}'") subject.cp_cmd(origin_file, final_file) end @@ -83,7 +83,10 @@ file_content = "Hello\nWorld" allow(subject).to receive(:read_file).with(file).and_return(file_content) - expect(subject.nchars_file(file)).to eq(10) + # agrees with wc + # $ echo -n "Hello\nWorld" | wc -m + # 12 + expect(subject.nchars_file(file)).to eq(12) end end @@ -93,7 +96,7 @@ file_content = "Hello World\nThis is a test" allow(subject).to receive(:read_file).with(file).and_return(file_content) - expect(subject.nwords_file(file)).to eq(5) + expect(subject.nwords_file(file)).to eq(6) 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 72d4e97449d7..f66fcae0c899 100644 --- a/spec/lib/msf/core/post/linux/process_spec.rb +++ b/spec/lib/msf/core/post/linux/process_spec.rb @@ -12,9 +12,9 @@ let(:length) { 64 } let(:pid) { 1234 } let(:memory_content) { 'memory content' } - let(:PROCESS_READ) {(1 << 0)} 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) @@ -23,6 +23,7 @@ end it 'uses the default pid if not specified' do + 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) diff --git a/spec/lib/msf/core/post/linux/system_spec.rb b/spec/lib/msf/core/post/linux/system_spec.rb index ef339da2af4a..17853685313f 100644 --- a/spec/lib/msf/core/post/linux/system_spec.rb +++ b/spec/lib/msf/core/post/linux/system_spec.rb @@ -427,6 +427,7 @@ end it 'raises an error if unable to determine glibc version' do + allow(subject).to receive(:command_exists?).with('ldd').and_return(true) allow(subject).to receive(:cmd_exec).with('ldd --version').and_raise(StandardError) expect { subject.glibc_version }.to raise_error('Could not determine glibc version') end @@ -446,9 +447,10 @@ describe '#ips' do it 'returns all IP addresses of the device' do - fib_trie_content = " +-- 192.168.1.1/32 host LOCAL\n" + # content from https://medium.com/@linuxadminhacks/find-the-names-of-the-network-interfaces-by-their-ips-4ef82326e49e + fib_trie_content = "Main:\n +-- 0.0.0.0/0 3 0 5\n +-- 192.168.1.0/24 2 0 2\n +-- 192.168.1.0/30 2 0 2\n |-- 192.168.1.3\n /32 host LOCAL" allow(subject).to receive(:read_file).with('/proc/net/fib_trie').and_return(fib_trie_content) - expect(subject.ips).to eq(['192.168.1.1']) + expect(subject.ips).to eq(['192.168.1.3']) end end