Files
metasploit-gs/lib/msf/core/post/linux/packages.rb
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

73 lines
2.9 KiB
Ruby
Raw Normal View History

2024-11-26 19:00:14 -05:00
# -*- coding: binary -*-
module Msf
class Post
module Linux
module Packages
include ::Msf::Post::Linux::System
#
# Determines the version of an installed package
#
# @param package The package name to check for
# @return [Rex::Version] nil if OS is not supported or package is not installed
#
2024-11-27 16:07:40 -05:00
def installed_package_version(package)
2024-11-26 19:00:14 -05:00
info = get_sysinfo
2024-12-11 17:52:07 -05:00
if ['debian', 'ubuntu'].include?(info[:distro])
package_version = cmd_exec("dpkg-query -f='${Version}' -W #{package}")
2025-02-27 09:25:46 -05:00
# The "no package" error is language based, but "dpkg-query:" starting is not
2025-02-18 16:43:19 -05:00
return nil if package_version.start_with?('dpkg-query:')
2024-11-26 19:00:14 -05:00
2024-11-27 16:07:40 -05:00
package_version = package_version.gsub('+', '.')
return Rex::Version.new(package_version)
2025-02-18 16:43:19 -05:00
elsif ['redhat', 'fedora', 'centos'].include?(info[:distro])
2024-11-27 16:07:40 -05:00
package_version = cmd_exec("rpm -q #{package}")
2025-02-19 15:15:18 -05:00
return nil unless package_version.start_with?(package)
2025-02-18 16:43:19 -05:00
2024-11-27 16:07:40 -05:00
# dnf-4.18.0-2.fc39.noarch
# remove package name at the beginning
package_version = package_version.split("#{package}-")[1]
# remove arch at the end
package_version = package_version.sub(/\.[^.]*$/, '')
return Rex::Version.new(package_version)
2025-02-26 18:21:10 +00:00
elsif ['solaris', 'oracle', 'freebsd'].include?(info[:distro])
2024-11-27 16:07:40 -05:00
package_version = cmd_exec("pkg info #{package}")
return nil unless package_version.include?('Version')
2025-02-18 16:43:19 -05:00
2024-11-27 16:07:40 -05:00
package_version = package_version.match(/Version\s+:\s+(.+)/)[1]
return Rex::Version.new(package_version)
elsif ['gentoo'].include?(info[:distro])
# https://wiki.gentoo.org/wiki/Equery
2025-02-19 15:15:18 -05:00
if command_exists?('equery')
package_version = cmd_exec("equery --quiet list #{package}")
# https://wiki.gentoo.org/wiki/Q_applets
elsif command_exists?('qlist')
package_version = cmd_exec("qlist -Iv #{package}")
else
vprint_error("installed_package_version couldn't find qlist and equery on gentoo")
return nil
end
return nil if package_version.strip.empty?
2025-02-18 16:43:19 -05:00
2024-11-27 16:07:40 -05:00
package_version = package_version.split('/')[1]
# make gcc-1.1 to 1.1
2025-02-18 16:43:19 -05:00
package_version = package_version.sub(/.*?-/, '')
2024-11-27 16:07:40 -05:00
return Rex::Version.new(package_version)
elsif ['arch'].include?(info[:distro])
package_version = cmd_exec("pacman -Qi #{package}")
return nil unless package_version.include?('Version')
2025-02-18 16:43:19 -05:00
2024-11-27 16:07:40 -05:00
package_version = package_version.match(/Version\s+:\s+(.+)/)[1]
2025-02-18 16:43:19 -05:00
return Rex::Version.new(package_version)
2024-11-26 19:00:14 -05:00
else
2024-12-11 17:52:07 -05:00
vprint_error("installed_package_version is being called on an unsupported OS: #{info[:distro]}")
2024-11-26 19:00:14 -05:00
end
nil
end
end
end
end
end