Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Compile this with:
- * $ gcc example.c -o example -z execstack
- * */
- #include <stdio.h>
- #include <stdlib.h>
- typedef int (*FunctionThatReturnsInt)();
- FunctionThatReturnsInt GenerateAFunctionThatReturnsX(int x) {
- void *buf = malloc(6);
- // mov eax, x
- *(unsigned char *) buf = 0xb8;
- *(int *) (buf + 1) = x;
- // ret
- *(unsigned char *) (buf + 5) = 0xc3;
- return (FunctionThatReturnsInt) buf;
- }
- int main() {
- int i;
- for (i = 0; i < 10; i++) {
- FunctionThatReturnsInt f = GenerateAFunctionThatReturnsX(i);
- printf("%d\n", f());
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement