module OneGadget::Gadget::ClassMethods

Define class methods here.

Constants

BUILDS

Record.

BUILDS_PATH

Path to the pre-build files.

Public Instance Methods

add(build_id, offset, **options) click to toggle source

Add a gadget, for scripts in builds/ to use. @param [String] build_id The target’s build id. @param [Integer] offset The relative address offset of this gadget. @param [Hash] options See {Gadget::Gadget#initialize} for more information. @return [void]

# File lib/one_gadget/gadget.rb, line 173
def add(build_id, offset, **options)
  BUILDS[build_id] << OneGadget::Gadget::Gadget.new(offset, **options)
end
builds(build_id, remote: true) click to toggle source

Get gadgets from pre-defined corpus. @param [String] build_id Desired build id. @param [Boolean] remote

When local not found, try search in latest version?

@return [Array<Gadget::Gadget>?] Gadgets.

# File lib/one_gadget/gadget.rb, line 122
def builds(build_id, remote: true)
  ret = find_build(build_id)
  return ret unless ret.nil?
  return build_not_found unless remote

  # fetch remote builds
  table = OneGadget::Helper.remote_builds.find { |c| c.include?(build_id) }
  return build_not_found if table.nil? # remote doesn't have this one either.

  # builds found in remote! Ask update gem and download remote gadgets.
  OneGadget::Logger.ask_update(msg: 'The desired one-gadget can be found in lastest version!')
  tmp_file = OneGadget::Helper.download_build(table)
  require tmp_file.path
  tmp_file.unlink
  BUILDS[build_id]
end
builds_info(build_id) click to toggle source

Returns the comments in builds/libc-*-<build_id>*.rb @param [String] build_id

Supports give only few starting bytes, but a warning will be shown
if multiple BulidIDs are matched.

@return [String?]

Lines of comments.

@example

puts OneGadget::Gadget.builds_info('3bbdc')
# https://gitlab.com/libcdb/libcdb/blob/master/libc/libc6-amd64-2.19-18+deb8u4/lib64/libc-2.19.so
#
# Advanced Micro Devices X86-64
# ...
# File lib/one_gadget/gadget.rb, line 151
def builds_info(build_id)
  raise Error::ArgumentError, "Invalid BuildID #{build_id.inspect}" if build_id =~ /[^0-9a-f]/

  files = Dir.glob(File.join(BUILDS_PATH, "*-#{build_id}*.rb")).sort
  return OneGadget::Logger.not_found(build_id) && nil if files.empty?

  if files.size > 1
    OneGadget::Logger.warn("Multiple BuildIDs match /^#{build_id}/\n")
    show = files.map do |f|
      File.basename(f, '.rb').reverse.split('-', 2).join(' ').reverse
    end
    OneGadget::Logger.warn("Candidates are:\n#{show * "\n"}\n")
    return nil
  end
  OneGadget::Helper.comments_of_file(files.first)
end

Private Instance Methods

build_not_found() click to toggle source
# File lib/one_gadget/gadget.rb, line 188
def build_not_found
  nil
end
find_build(id) click to toggle source
# File lib/one_gadget/gadget.rb, line 179
def find_build(id)
  return BUILDS[id] if BUILDS.key?(id)

  Dir.glob(File.join(BUILDS_PATH, "*-#{id}.rb")).sort.each do |dic|
    require dic
  end
  BUILDS[id] if BUILDS.key?(id)
end