Module: OneGadget::Gadget::ClassMethods

Included in:
OneGadget::Gadget
Defined in:
lib/one_gadget/gadget.rb

Overview

Define class methods here.

Constant Summary collapse

BUILDS_PATH =

Path to the pre-build files.

File.join(__dir__, 'builds').freeze
BUILDS =

Record.

Hash.new { |h, k| h[k] = [] }

Instance Method Summary collapse

Instance Method Details

#add(build_id, offset, **options) ⇒ void

This method returns an undefined value.

Add a gadget, for scripts in builds/ to use.

Parameters:

  • build_id (String)

    The target's build id.

  • offset (Integer)

    The relative address offset of this gadget.

  • options (Hash)

    See Gadget#initialize for more information.


153
154
155
# File 'lib/one_gadget/gadget.rb', line 153

def add(build_id, offset, **options)
  BUILDS[build_id] << OneGadget::Gadget::Gadget.new(offset, **options)
end

#builds(build_id, remote: true) ⇒ Array<Gadget::Gadget>?

Get gadgets from pre-defined corpus.

Parameters:

  • build_id (String)

    Desired build id.

  • remote (Boolean) (defaults to: true)

    When local not found, try search in latest version?

Returns:


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/one_gadget/gadget.rb', line 102

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) ⇒ String?

Returns the comments in builds/libc-*-<build_id>*.rb

Examples:

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
# ...

Parameters:

  • build_id (String)

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

Returns:

  • (String?)

    Lines of comments.

Raises:


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/one_gadget/gadget.rb', line 131

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