Last active
March 11, 2016 03:59
-
-
Save oblivia-simplex/a67b7aba554484513313 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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, ®s); | |
} | |
/* 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, ®s, 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