Class: Ronin::ASM::Program

Inherits:
Object
  • Object
show all
Defined in:
lib/ronin/asm/program.rb

Overview

Represents a full Assembly program.

Direct Known Subclasses

Shellcode

Constant Summary collapse

SYNTAX =

Supported Assembly Syntaxs

{
  att:   Syntax::ATT,
  intel: Syntax::Intel
}
PARSERS =

The Assembly Parsers

{
  att:   :gas,
  intel: :nasm
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) { ... } ⇒ Program

Initializes a new Assembly Program.

Examples:

Program.new(arch: :amd64) do
  push  rax
  push  rbx

  mov   rsp,     rax
  mov   rax[8],  rbx
end

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :arch (String, Symbol) — default: :x86

    The Architecture to target.

  • :os (String, Symbol)

    The Operating System to target.

  • :define (Hash{Symbol => Object})

    Constants to define in the program.

Yields:

  • [] The given block will be evaluated within the program.



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
# File 'lib/ronin/asm/program.rb', line 105

def initialize(options={},&block)
  @arch = options.fetch(:arch,:x86).to_sym

  arch = Archs.const_get(@arch.to_s.upcase)

  @word_size = arch::WORD_SIZE
  @registers = arch::REGISTERS

  extend Archs.const_get(@arch.to_s.upcase)

  @syscalls = {}

  if options.has_key?(:os)
    @os       = options[:os].to_s
    @syscalls = OS::SYSCALLS[@os][@arch]

    extend OS.const_get(@os)
  end

  if options[:define]
    options[:define].each do |name,value|
      instance_variable_set("@#{name}",value)
    end
  end

  @allocated_registers = []
  @instructions = []

  instance_eval(&block) if block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *arguments, &block) ⇒ Object (protected)

Allows adding unknown instructions to the program.

Parameters:

  • name (Symbol)

    The name of the instruction.

  • arguments (Array)

    Additional operands.



487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/ronin/asm/program.rb', line 487

def method_missing(name,*arguments,&block)
  if (block && arguments.empty?)
    label(name,&block)
  elsif block.nil?
    if (arguments.empty? && register?(name))
      register(name)
    else
      instruction(name,*arguments)
    end
  else
    super(name,*arguments,&block)
  end
end

Instance Attribute Details

#allocated_registersObject (readonly)

The registers used by the program



73
74
75
# File 'lib/ronin/asm/program.rb', line 73

def allocated_registers
  @allocated_registers
end

#archObject (readonly)

The targeted architecture



52
53
54
# File 'lib/ronin/asm/program.rb', line 52

def arch
  @arch
end

#instructionsObject (readonly)

The instructions of the program



76
77
78
# File 'lib/ronin/asm/program.rb', line 76

def instructions
  @instructions
end

#osObject (readonly)

The targeted Operating System



55
56
57
# File 'lib/ronin/asm/program.rb', line 55

def os
  @os
end

#registersHash{Symbol => Register} (readonly)

The registers available to the program

Returns:

  • (Hash{Symbol => Register})

    The names and registers.



64
65
66
# File 'lib/ronin/asm/program.rb', line 64

def registers
  @registers
end

#syscallsHash{Symbol => Integer} (readonly)

The syscalls available to the program

Returns:

  • (Hash{Symbol => Integer})

    The syscall names and numbers.



70
71
72
# File 'lib/ronin/asm/program.rb', line 70

def syscalls
  @syscalls
end

#word_sizeObject (readonly)

The default word size



58
59
60
# File 'lib/ronin/asm/program.rb', line 58

def word_size
  @word_size
end

Instance Method Details

#assemble(output, options = {}) ⇒ String

Assembles the program.

Parameters:

  • output (String)

    The path for the assembled program.

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :syntax (Symbol, String) — default: :intel

    The syntax to compile the program to.

  • :format (Symbol) — default: :bin

    The format of the assembled executable. May be one of:

    • :dbg - Trace of all info passed to object format module.
    • :bin - Flat format binary.
    • :dosexe - DOS .EXE format binary.
    • :elf - ELF.
    • :elf32 - ELF (32-bit).
    • :elf64 - ELF (64-bit).
    • :coff - COFF (DJGPP).
    • :macho - Mac OS X ABI Mach-O File Format.
    • :macho32 - Mac OS X ABI Mach-O File Format (32-bit).
    • :macho64 - Mac OS X ABI Mach-O File Format (64-bit).
    • :rdf - Relocatable Dynamic Object File Format (RDOFF) v2.0.
    • :win32 - Win32.
    • :win64 / :x64 - Win64.
    • :xdf - Extended Dynamic Object.

Returns:

  • (String)

    The path to the assembled program.



453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/ronin/asm/program.rb', line 453

def assemble(output,options={})
  syntax  = options.fetch(:syntax,:intel)
  format  = options.fetch(:format,:bin)
  parser  = PARSERS[syntax]

  source = Tempfile.new(['ronin-asm', '.s'])
  source.write(to_asm(syntax))
  source.close

  YASM::Program.assemble(
    file:          source.path,
    parser:        PARSERS[syntax],
    target:        @arch,
    output_format: format,
    output:        output
  )

  return output
end

#byte(op) ⇒ MemoryOperand, ImmediateOperand

Creates an operand of size 1 (byte).

Parameters:

Returns:



202
203
204
205
206
207
208
209
# File 'lib/ronin/asm/program.rb', line 202

def byte(op)
  case op
  when MemoryOperand
    MemoryOperand.new(op.base,op.offset,op.index,op.scale,1)
  else
    ImmediateOperand.new(op,1)
  end
end

#critical(*regs) { ... } ⇒ Object

Defines a critical region, where the specified Registers should be saved and then reloaded.

Parameters:

  • regs (Array<Symbol>)

    The registers to save and reload.

Yields:

  • [] The given block will be evaluated after the registers have been saved.



385
386
387
388
389
390
391
# File 'lib/ronin/asm/program.rb', line 385

def critical(*regs,&block)
  regs.each { |name| register_save(name) }

  instance_eval(&block)

  regs.reverse_each { |name| register_load(name) }
end

#dword(op) ⇒ ImmediateOperand

Creates a operand of size 4 (bytes).

Parameters:

Returns:



238
239
240
241
242
243
244
245
# File 'lib/ronin/asm/program.rb', line 238

def dword(op)
  case op
  when MemoryOperand
    MemoryOperand.new(op.base,op.offset,op.index,op.scale,4)
  else
    ImmediateOperand.new(op,4)
  end
end

#eval { ... } ⇒ Object

Evaluates code within the Program.

Yields:

  • [] The code to evaluate.



399
400
401
# File 'lib/ronin/asm/program.rb', line 399

def eval(&block)
  instance_eval(&block)
end

#instruction(name, *operands) ⇒ Instruction

Adds a new instruction to the program.

Parameters:

  • name (String, Symbol)
  • operands (Array)

Returns:



186
187
188
189
190
191
# File 'lib/ronin/asm/program.rb', line 186

def instruction(name,*operands)
  insn = Instruction.new(name.to_sym,operands)

  @instructions << insn
  return insn
end

#interrupt(number) ⇒ Object

This method is abstract.

Generic method for generating the instruction for causing an interrupt.

Parameters:

  • number (Integer)

    The interrupt number to call.



294
295
# File 'lib/ronin/asm/program.rb', line 294

def interrupt(number)
end

#label(name) { ... } ⇒ Symbol

Adds a label to the program.

Parameters:

  • name (Symbol, String)

    The name of the label.

Yields:

  • [] The given block will be evaluated after the label has been added.

Returns:

  • (Symbol)

    The label name.



278
279
280
281
282
283
284
# File 'lib/ronin/asm/program.rb', line 278

def label(name,&block)
  name = name.to_sym

  @instructions << name
  instance_eval(&block)
  return name
end

#qword(op) ⇒ MemoryOperand, ImmediateOperand

Creates a operand of size 8 (bytes).

Parameters:

Returns:



256
257
258
259
260
261
262
263
# File 'lib/ronin/asm/program.rb', line 256

def qword(op)
  case op
  when MemoryOperand
    MemoryOperand.new(op.base,op.offset,op.index,op.scale,8)
  else
    ImmediateOperand.new(op,8)
  end
end

#register(name) ⇒ Register

Accesses a register.

Parameters:

  • name (String, Symbol)

    The name of the register.

Returns:

Raises:

  • (ArgumentError)

    The register could not be found.



161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/ronin/asm/program.rb', line 161

def register(name)
  name = name.to_sym

  unless register?(name)
    raise(ArgumentError,"unknown register: #{name}")
  end

  unless @allocated_registers.include?(name)
    # mark the register as being used, when it was first accessed
    @allocated_registers << name
  end

  return @registers[name]
end

#register?(name) ⇒ Boolean

Determines if a register exists.

Parameters:

  • name (Symbol)

    The name of the register.

Returns:

  • (Boolean)

    Specifies whether the register exists.



145
146
147
# File 'lib/ronin/asm/program.rb', line 145

def register?(name)
  @registers.has_key?(name.to_sym)
end

#register_clear(name) ⇒ Object

This method is abstract.

Generic method for clearing a register.

Parameters:

  • name (Symbol)

    The name of the reigster.



335
336
# File 'lib/ronin/asm/program.rb', line 335

def register_clear(name)
end

#register_load(name) ⇒ Object

This method is abstract.

Generic method for loading a register.

Parameters:

  • name (Symbol)

    The name of the reigster.



371
372
# File 'lib/ronin/asm/program.rb', line 371

def register_load(name)
end

#register_save(name) ⇒ Object

This method is abstract.

Generic method for saving a register.

Parameters:

  • name (Symbol)

    The name of the reigster.



360
361
# File 'lib/ronin/asm/program.rb', line 360

def register_save(name)
end

#register_set(name, value) ⇒ Object

This method is abstract.

Generic method for setting a register.

Parameters:

  • name (Symbol)

    The name of the reigster.

  • value (Register, Immediate, Integer)

    The new value for the register.



349
350
# File 'lib/ronin/asm/program.rb', line 349

def register_set(name,value)
end

#stack_pop(name) ⇒ Object

This method is abstract.

Generic method for popping off the stack.

Parameters:

  • name (Symbol)

    The name of the reigster.



324
325
# File 'lib/ronin/asm/program.rb', line 324

def stack_pop(name)
end

#stack_push(value) ⇒ Object

This method is abstract.

Generic method for pushing onto the stack.

Parameters:

  • value (Register, Integer)

    The value to push.



313
314
# File 'lib/ronin/asm/program.rb', line 313

def stack_push(value)
end

#syscallObject

This method is abstract.

Generic method for generating the instruction for invoking a syscall.



302
303
# File 'lib/ronin/asm/program.rb', line 302

def syscall
end

#to_asm(syntax = :intel) ⇒ Object

Converts the program to Assembly Source Code.

Parameters:

  • syntax (Symbol) (defaults to: :intel)

    The syntax to compile the program to.



409
410
411
# File 'lib/ronin/asm/program.rb', line 409

def to_asm(syntax=:intel)
  SYNTAX[syntax].emit_program(self)
end

#to_sObject

See Also:



416
417
418
# File 'lib/ronin/asm/program.rb', line 416

def to_s
  to_asm
end

#word(op) ⇒ MemoryOperand, ImmediateOperand

Creates a operand of size 2 (bytes).

Parameters:

Returns:



220
221
222
223
224
225
226
227
# File 'lib/ronin/asm/program.rb', line 220

def word(op)
  case op
  when MemoryOperand
    MemoryOperand.new(op.base,op.offset,op.index,op.scale,2)
  else
    ImmediateOperand.new(op,2)
  end
end