Skip to content

Instantly share code, notes, and snippets.

@oblivia-simplex
Last active March 11, 2016 03:59
Show Gist options
  • Save oblivia-simplex/a67b7aba554484513313 to your computer and use it in GitHub Desktop.
Save oblivia-simplex/a67b7aba554484513313 to your computer and use it in GitHub Desktop.
/**
* Calls an array of arbitrary machine instructions as a subroutine,
* and returns a serialized representation of the register state at
* the end of the array's execution.
* No security features at all have been implemented. Intended
* application: AIM-GP (automatic induction of machine code through
* genetic programming), or something like that.
**/
unsigned char * code_call (unsigned char *code){
/* cast the byte array as a function */
long (*ret)() = (long(*)())code;
/* This struct will be loaded by the tracer with a representation
* of all the registers at the end of the code's execution.
*/
struct user_regs_struct regs;
pid_t pid;
/* fork a new process in which to run the shellcode */
pid = fork();
if (pid == 0){ // if in child process (tracee)
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
ret(); // if you want to pass any params to the code, do it here
kill(getpid(), SIGSTOP); // to let the tracer catch up
exit(1); // we're done with this child, now
} else {
/* We're in the tracer process. It will observe the child and
* report back to the head office.
*/
int status;
wait(&status);
ptrace(PTRACE_GETREGS, pid, NULL, &regs);
}
/* We need to convert the register struct into an ordinary byte
* array, so that it can be sent back to lisp in a form cffi
* understands.
*/
unsigned char *rserial = calloc(sizeof(regs), sizeof(char));
memcpy(rserial, &regs, sizeof(regs));
return rserial; /* Remember to free after calling */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment