SMC (Self modifing code) under Linux with GAS

April 14, 2009 – 2:18 am

With the Gnu Assembler (also known as GAS) it is quite simple to write self modifying assembly code. The only issue is that you can’t make the text section directly self modifiable you have to create a new section:

.section .text
.globl _start
_start:
  jmp modifing # directly jump in the smc section where code is modifiable

.section .smc, "awx" # set the section as allocatable, writable and executable
modifing:
  movl $1, %eax
  addl $17, modified (,%eax,) # Set the return code as 17 instead of 0.

modified:
  movl $0, %ebx    # This is the instruction that is modified.
                   # The $0 will be changed in $17.
                   # If you look at it in machine code the first byte is
                   # the instruction "movl into ebx"
                   # and the other 4 next bytes are the number $0.
  movl $1, %eax
  int $0x80

Post a Comment