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.

Parameters:

  • inst (String)

    The instruction name.

  • argc (Range, Integer)

    Count of arguments. Negative integer for doesn’t care the number of arguments.



17
18
19
20
21
22
23
24
# File 'lib/one_gadget/emulators/instruction.rb', line 17

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)

Returns Count of arguments.

Returns:

  • (Range)

    Count of arguments.



10
11
12
# File 'lib/one_gadget/emulators/instruction.rb', line 10

def argc
  @argc
end

#instString (readonly)

Returns The instruction name.

Returns:

  • (String)

    The instruction name.



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

def inst
  @inst
end

Instance Method Details

#fetch_args(cmd) ⇒ Array<String>

Extract arguments from command.

Parameters:

  • cmd (String)

Returns:

  • (Array<String>)

    Arguments.

Raises:



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/one_gadget/emulators/instruction.rb', line 30

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.

Parameters:

  • cmd (String)

Returns:

  • (Boolean)


47
48
49
# File 'lib/one_gadget/emulators/instruction.rb', line 47

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