Skip to content

Instantly share code, notes, and snippets.

@akoskovacs
Created January 5, 2013 15:14
Show Gist options
  • Save akoskovacs/4462044 to your computer and use it in GitHub Desktop.
Save akoskovacs/4462044 to your computer and use it in GitHub Desktop.
32bit self-modifying multiplying Intel code.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/mman.h>
typedef int (*func_t)(void);
int main(int argc, char *argv[])
{
/* push %ebp
xor %eax, %eax ; zero-out %eax
xor %ecx, %ecx ; zero-out %ecx
mov $0x2, %al ; %al = 2
mov $0x2, %cl ; %cl = 2
*/
char *fstart = "\x55\x89\xe5\x31\xc0\x31\xc9\xb0\x02\xb1\x02";
/* mul %ecx */
char *mulecx = "\xf7\xe1";
/* pop %ebp
ret */
char *fend = "\x5d\xc3";
int icnt = 5;
int i;
char *text = mmap(NULL, 4096, PROT_READ|PROT_WRITE|PROT_EXEC
, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
if (argv[1]) {
icnt = atoi(argv[1]);
}
strcpy(text, fstart);
for (i = 0; i < icnt; i++) {
strcat(text, mulecx);
}
strcat(text, fend);
func_t fn = (func_t)text;
printf("%d\n", fn());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment