Class: Ronin::Code::ASM::Program

Inherits:
Object
  • Object
show all
Defined in:
lib/ronin/code/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(arch: :x86, os: nil, define: {}) { ... } ⇒ 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:

  • arch (Symbol) (defaults to: :x86)

    The Architecture to target.

  • os (Symbol, nil) (defaults to: nil)

    The Operating System to target.

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

    Constants to define in the program.

Yields:

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



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

def initialize(arch: :x86, os: nil, define: {}, &block)
  @arch = arch

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

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

  extend arch

  @syscalls = {}

  if os
    @os       = os
    @syscalls = OS::SYSCALLS[@os][@arch]

    extend OS[@os]
  end

  define.each do |name,value|
    instance_variable_set("@#{name}",value)
  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.



492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/ronin/code/asm/program.rb', line 492

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_registersArray<Symbol> (readonly)

The registers used by the program

Returns:

  • (Array<Symbol>)


81
82
83
# File 'lib/ronin/code/asm/program.rb', line 81

def allocated_registers
  @allocated_registers
end

#archSymbol (readonly)

The targeted architecture

Returns:

  • (Symbol)


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

def arch
  @arch
end

#instructionsArray<Instruction> (readonly)

The instructions of the program

Returns:



86
87
88
# File 'lib/ronin/code/asm/program.rb', line 86

def instructions
  @instructions
end

#osSymbol? (readonly)

The targeted Operating System

Returns:

  • (Symbol, nil)


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

def os
  @os
end

#registersHash{Symbol => Register} (readonly)

The registers available to the program

Returns:

  • (Hash{Symbol => Register})

    The names and registers.



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

def registers
  @registers
end

#syscallsHash{Symbol => Integer} (readonly)

The syscalls available to the program

Returns:

  • (Hash{Symbol => Integer})

    The syscall names and numbers.



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

def syscalls
  @syscalls
end

#word_sizeInteger (readonly)

The default word size

Returns:

  • (Integer)


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

def word_size
  @word_size
end

Instance Method Details

#assemble(output, syntax: :intel, format: :bin) ⇒ String

Assembles the program.

Parameters:

  • output (String)

    The path for the assembled program.

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

    The syntax to compile the program to.

  • format (Symbol) (defaults to: :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.

Raises:

  • (ArgumentError)

    The given syntax was not :intel or :att.



458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/ronin/code/asm/program.rb', line 458

def assemble(output, syntax: :intel, format: :bin)
  parser = PARSERS.fetch(syntax) do
    raise(ArgumentError,"unknown ASM syntax: #{syntax.inspect}")
  end

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

  YASM::Command.run(
    file:          source.path,
    parser:        parser,
    target:        @arch,
    output_format: format,
    output:        output
  )

  return output
end

#byte(op) ⇒ MemoryOperand, ImmediateOperand

Creates an operand of size 1 (byte).

Parameters:

Returns:



207
208
209
210
211
212
213
214
# File 'lib/ronin/code/asm/program.rb', line 207

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.



390
391
392
393
394
395
396
# File 'lib/ronin/code/asm/program.rb', line 390

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:



243
244
245
246
247
248
249
250
# File 'lib/ronin/code/asm/program.rb', line 243

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.



404
405
406
# File 'lib/ronin/code/asm/program.rb', line 404

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:



191
192
193
194
195
196
# File 'lib/ronin/code/asm/program.rb', line 191

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.



299
300
# File 'lib/ronin/code/asm/program.rb', line 299

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.



283
284
285
286
287
288
289
# File 'lib/ronin/code/asm/program.rb', line 283

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:



261
262
263
264
265
266
267
268
# File 'lib/ronin/code/asm/program.rb', line 261

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.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/ronin/code/asm/program.rb', line 166

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.



150
151
152
# File 'lib/ronin/code/asm/program.rb', line 150

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



340
341
# File 'lib/ronin/code/asm/program.rb', line 340

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



376
377
# File 'lib/ronin/code/asm/program.rb', line 376

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



365
366
# File 'lib/ronin/code/asm/program.rb', line 365

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

  • value (Register, ImmediateOperand, Integer)

    The new value for the register.



354
355
# File 'lib/ronin/code/asm/program.rb', line 354

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



329
330
# File 'lib/ronin/code/asm/program.rb', line 329

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.



318
319
# File 'lib/ronin/code/asm/program.rb', line 318

def stack_push(value)
end

#syscallObject

This method is abstract.

Generic method for generating the instruction for invoking a syscall.



307
308
# File 'lib/ronin/code/asm/program.rb', line 307

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.



414
415
416
# File 'lib/ronin/code/asm/program.rb', line 414

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

#to_sObject

See Also:



421
422
423
# File 'lib/ronin/code/asm/program.rb', line 421

def to_s
  to_asm
end

#word(op) ⇒ MemoryOperand, ImmediateOperand

Creates a operand of size 2 (bytes).

Parameters:

Returns:



225
226
227
228
229
230
231
232
# File 'lib/ronin/code/asm/program.rb', line 225

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