Skip to content

Instantly share code, notes, and snippets.

@s0me0ne-unkn0wn
Created August 14, 2023 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s0me0ne-unkn0wn/867613e55bce2db2f12c90c53ae904e5 to your computer and use it in GitHub Desktop.
Save s0me0ne-unkn0wn/867613e55bce2db2f12c90c53ae904e5 to your computer and use it in GitHub Desktop.
#define _GNU_SOURCE
#include <unistd.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <fcntl.h>
int main(int argc, char **argv) {
int fd;
if((fd = open("/bin/ls", O_RDONLY)) == -1) exit(1);
off_t len = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
unsigned char *buf = malloc(len);
read(fd, buf, len);
close(fd);
int memfd;
if((memfd = memfd_create("memls", MFD_CLOEXEC)) == -1) exit(2);
write(memfd, buf, len);
char *args[] = {"ls", "/", NULL};
char *env[] = {NULL};
fexecve(memfd, args, env);
exit(3);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment