Class: OneGadget::Emulators::Instruction

Inherits:
Object
  • Object
show all
Defined in:
lib/one_gadget/emulators/instruction.rb

Overview

Define instruction name and it's argument count.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inst, argc) ⇒ Instruction

Instantiate a OneGadget::Emulators::Instruction object.


14
15
16
17
18
19
20
21
# File 'lib/one_gadget/emulators/instruction.rb', line 14

def initialize(inst, argc)
  @inst = inst
  @argc = case argc
          when -1 then 0..Float::INFINITY
          when Range then argc
          when Integer then argc..argc
          end
end

Instance Attribute Details

#argcRange (readonly)


8
9
10
# File 'lib/one_gadget/emulators/instruction.rb', line 8

def argc
  @argc
end

#instString (readonly)


7
8
9
# File 'lib/one_gadget/emulators/instruction.rb', line 7

def inst
  @inst
end

Instance Method Details

#fetch_args(cmd) ⇒ Array<String>

Extract arguments from command.


27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/one_gadget/emulators/instruction.rb', line 27

def fetch_args(cmd)
  idx = cmd.index(inst)
  cmd = cmd[0...cmd.rindex('//')] if cmd.rindex('//')
  cmd = cmd[0...cmd.rindex('#')] if cmd.rindex('#')
  args = parse_args(cmd[idx + inst.size..-1])
  unless argc.include?(args.size)
    raise OneGadget::Error::InstructionArgumentError, "Incorrect argument number in #{cmd}, expect: #{argc}"
  end

  args.map do |arg|
    arg.gsub(/XMMWORD|QWORD|DWORD|WORD|BYTE|PTR/, '').strip
  end
end

#match?(cmd) ⇒ Boolean

If the command contains this instruction.


44
45
46
# File 'lib/one_gadget/emulators/instruction.rb', line 44

def match?(cmd)
  (cmd =~ /#{inst}\s/) != nil
end