ronin-rb/ronin-exploits

View on GitHub
lib/ronin/exploits/metadata/os.rb

Summary

Maintainability
A
0 mins
Test Coverage
# frozen_string_literal: true
#
# ronin-exploits - A Ruby library for ronin-rb that provides exploitation and
# payload crafting functionality.
#
# Copyright (c) 2007-2023 Hal Brodigan (postmodern.mod3 at gmail.com)
#
# ronin-exploits is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ronin-exploits is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with ronin-exploits.  If not, see <https://www.gnu.org/licenses/>.
#

module Ronin
  module Exploits
    module Metadata
      #
      # Metadata mixin that allows a exploit to define which
      # Operating System (OS) it specifically targets.
      #
      module OS
        #
        # Adds an {ClassMethods#os os} and {ClassMethods#os_version os_version}
        # metadata attributes to the exploit.
        #
        # @param [Class<Ronin::Exploits::Exploit>] exploit
        #   The exploit class that is including {Metadata::OS}.
        #
        # @api private
        #
        def self.included(exploit)
          exploit.extend ClassMethods
        end

        #
        # Class-methods.
        #
        module ClassMethods
          #
          # Gets or sets the exploit's targeted Operating System (OS).
          #
          # @param [:unix, :bsd, :freebsd, :openbsd, :netbsd, :linux, :macos, :windows, :android, nil] new_os
          #   The optional new Operating System (OS) to set.
          #
          # @return [:unix, :bsd, :freebsd, :openbsd, :netbsd, :linux, :macos, :windows, :android, nil]
          #   The exploit's Operating System (OS).
          #
          # @example
          #   os :linux
          #
          # @api public
          #
          def os(new_os=nil)
            if new_os
              @os = new_os
            else
              @os ||= if superclass.kind_of?(ClassMethods)
                        superclass.os
                      end
            end
          end

          #
          # Gets or sets the exploit's targeted Operating System (OS) version.
          #
          # @param [String, nil] new_os_version
          #   The optional new Operating System (OS) version to set.
          #
          # @return [String, nil]
          #   The exploit's Operating System (OS) version.
          #
          # @example
          #   os :linux
          #   os_version '5.x'
          #
          # @api public
          #
          def os_version(new_os_version=nil)
            if new_os_version
              @os_version = new_os_version
            else
              @os_version ||= if superclass.kind_of?(ClassMethods)
                                superclass.os_version
                              end
            end
          end
        end

        #
        # The Operating System (OS) that the exploit targets.
        #
        # @return [:unix, :bsd, :freebsd, :openbsd, :netbsd, :linux, :macos, :windows, :android, nil]
        #
        # @see ClassMethods#os
        #
        def os
          self.class.os
        end

        #
        # The Operating System (OS) version that the exploit targets.
        #
        # @return [String, nil]
        #
        # @see ClassMethods#os_version
        #
        def os_version
          self.class.os_version
        end
      end
    end
  end
end