Module: OneGadget::CLI
- Defined in:
- lib/one_gadget/cli.rb
Overview
Methods for command line interface.
Constant Summary collapse
- USAGE =
Help message.
'Usage: one_gadget <FILE|-b BuildID> [options]'
- DEFAULT_OPTIONS =
Default options.
{ format: :pretty, force_file: false, level: 0, base: 0 }.freeze
Class Method Summary collapse
-
.display_gadgets(gadgets, format) ⇒ true
Writes gadgets to stdout.
-
.error(msg) ⇒ false
Logs error.
-
.execute(cmd) ⇒ void
Spawns and waits until the process end.
-
.handle_gadgets(gadgets, libc_file) ⇒ Boolean
Decides how to display fetched gadgets according to options.
-
.handle_near(libc_file, gadgets, near) ⇒ Object
Implements the –near feature.
-
.handle_script(gadgets, script) ⇒ true
Handles the –script feature.
-
.info_build_id(id) ⇒ Boolean
Displays libc information given BuildID.
-
.parser ⇒ OptionParser
The option parser.
-
.show(msg) ⇒ true
Writes
msg
to stdout and returnstrue
. -
.work(argv) ⇒ Boolean
Main method of CLI.
Class Method Details
.display_gadgets(gadgets, format) ⇒ true
Writes gadgets to stdout.
189 190 191 192 193 194 195 196 197 198 |
# File 'lib/one_gadget/cli.rb', line 189 def display_gadgets(gadgets, format) case format when :raw show(gadgets.map(&:value).join(' ')) when :pretty show(gadgets.map(&:inspect).join("\n")) when :json show(gadgets.to_json) end end |
.error(msg) ⇒ false
Logs error.
203 204 205 206 |
# File 'lib/one_gadget/cli.rb', line 203 def error(msg) OneGadget::Logger.error(msg) false end |
.execute(cmd) ⇒ void
This method returns an undefined value.
Spawns and waits until the process end.
166 167 168 |
# File 'lib/one_gadget/cli.rb', line 166 def execute(cmd) Process.wait(spawn(cmd)) end |
.handle_gadgets(gadgets, libc_file) ⇒ Boolean
Decides how to display fetched gadgets according to options.
64 65 66 67 68 69 70 |
# File 'lib/one_gadget/cli.rb', line 64 def handle_gadgets(gadgets, libc_file) return false if gadgets.empty? # error occurs when fetching gadgets return handle_script(gadgets, [:script]) if [:script] return handle_near(libc_file, gadgets, [:near]) if [:near] display_gadgets(gadgets, [:format]) end |
.handle_near(libc_file, gadgets, near) ⇒ Object
Implements the –near feature.
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/one_gadget/cli.rb', line 215 def handle_near(libc_file, gadgets, near) return error('Libc file must be given when using --near option') unless libc_file functions = if File.file?(near) && OneGadget::Helper.valid_elf_file?(near) OneGadget::Helper.got_functions(near) else near.split(',').map(&:strip) end function_offsets = OneGadget::Helper.function_offsets(libc_file, functions) return error('No functions for processing') if function_offsets.empty? collection = function_offsets.map do |function, offset| { near: function, near_offset: offset, gadgets: gadgets.sort_by { |gadget| (gadget.offset - offset).abs } } end return show(collection.to_json) if [:format] == :json collection.each do |c| colored_offset = OneGadget::Helper.colored_hex(c[:near_offset]) OneGadget::Logger.warn("Gadgets near #{OneGadget::Helper.colorize(c[:near])}(#{colored_offset}):") display_gadgets(c[:gadgets], [:format]) show("\n") end true end |
.handle_script(gadgets, script) ⇒ true
Handles the –script feature.
174 175 176 177 178 179 180 |
# File 'lib/one_gadget/cli.rb', line 174 def handle_script(gadgets, script) gadgets.map(&:offset).each do |offset| OneGadget::Logger.info("Trying #{OneGadget::Helper.colored_hex(offset)}...") execute("#{script} #{offset}") end true end |
.info_build_id(id) ⇒ Boolean
Displays libc information given BuildID.
93 94 95 96 97 98 99 |
# File 'lib/one_gadget/cli.rb', line 93 def info_build_id(id) result = OneGadget::Gadget.builds_info(id) return false if result.nil? # invalid form or BuildID not found OneGadget::Logger.info("Information of #{id}:\n#{result.join("\n")}") true end |
.parser ⇒ OptionParser
The option parser.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/one_gadget/cli.rb', line 103 def parser @parser ||= OptionParser.new do |opts| opts. = USAGE opts.on('-b', '--build-id BuildID', 'BuildID[sha1] of libc.') do |b| [:build_id] = b end opts.on('-f', '--[no-]force-file', 'Force search gadgets in file instead of build id first.') do |f| [:force_file] = f end opts.on('-l', '--level OUTPUT_LEVEL', Integer, 'The output level.', 'OneGadget automatically selects gadgets with higher successful probability.', 'Increase this level to ask OneGadget show more gadgets it found.', 'Default: 0') do |l| [:level] = l end opts.on('-n', '--near FUNCTIONS/FILE', 'Order gadgets by their distance to the given functions ' \ 'or to the GOT functions of the given file.') do |n| [:near] = n end opts.on('-o FORMAT', '--output-format FORMAT', i[pretty raw json], 'Output format. FORMAT should be one of <pretty|raw|json>.', 'Default: pretty') do |o| [:format] = o end opts.on('-r', '--raw', 'Alias of -o raw. Output gadgets offset only, split with one space.') do |_| [:format] = :raw end opts.on('-s', '--script exploit-script', 'Run exploit script with all possible gadgets.', 'The script will be run as \'exploit-script $offset\'.') do |s| [:script] = s end opts.on('--info BuildID', 'Show version information given BuildID.') do |b| [:info] = b end opts.on('--base BASE_ADDRESS', Integer, 'The base address of libc.', 'Default: 0') do |b| [:base] = b end opts.on('--version', 'Current gem version.') do |v| [:version] = v end end end |
.show(msg) ⇒ true
Writes msg
to stdout and returns true
.
158 159 160 161 |
# File 'lib/one_gadget/cli.rb', line 158 def show(msg) puts msg true end |
.work(argv) ⇒ Boolean
Main method of CLI.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/one_gadget/cli.rb', line 39 def work(argv) = DEFAULT_OPTIONS.dup parser.parse!(argv) return show("OneGadget Version #{OneGadget::VERSION}") if [:version] return info_build_id([:info]) if [:info] libc_file = argv.pop build_id = [:build_id] level = [:level] return error('Either FILE or BuildID can be passed') if libc_file && [:build_id] return show(parser.help) && false unless build_id || libc_file gadgets = if build_id OneGadget.gadgets(build_id:, details: true, level:) else # libc_file OneGadget.gadgets(file: libc_file, details: true, force_file: [:force_file], level:) end gadgets.each { |g| g.base = [:base] } handle_gadgets(gadgets, libc_file) end |