dolphincx
驱动牛犊
驱动牛犊
  • 注册日期2003-04-28
  • 最后登录2016-01-09
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
阅读:1111回复:2

可卸载模块问题,高手请进

楼主#
更多 发布于:2003-04-28 17:03
这是《可卸载内核模块》中的关于改变命令执行结果的程序。
目的是当加载该模块后,执行ls命令时,实际执行ps命令。但在insmod后,无论执行ls或其他命令都不成功。我反复调试,觉得问题出现在my_execve系统调用函数中,是否和init_module()中的__NR_myexecve有关系?或者是其他什么原因,我刚涉及模块编程,属于菜鸟一只,望大虾多多执教,谢谢!

可以发我e_mail:dolphincx@sina.com,以下是code:

#define MODULE
#define __KERNEL__

#include <linux/module.h>
#include <linux/kernel.h>
#include <asm/unistd.h>
#include <asm/uaccess.h>
#include <sys/syscall.h>
#include <asm/fcntl.h>
#include <asm/errno.h>
#include <linux/types.h>
#include <linux/dirent.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <linux/malloc.h>

extern void *sys_call_table[];
int errno;
int __NR_myexecve;
static _syscall1(int,brk,void*,end_data_segment);
//static _syscall3(int,execve,const char*,file,char **const,argv,char **const,envp);

int (*orig_execve)(const char *,char **const,char **const);

int my_execve(const char *filename,char **const argv,char **const envp)
{
  long __res;
  printk(\"my_execve is running.\\n\");
  __asm__ volatile(\"int $0x80\":\"=a\"(__res):\"0\"(__NR_myexecve),\"b\"((long)filename),\"c\"((long)argv),\"d\"((long)envp));
  __syscall_return(int,__res);
}


int hacked_execve(const char *filename,char **const argv,char **const envp)
{
  char *test;
  int ret,tmp;
  char *truc=\"/bin/ls\";
  char *nouveau=\"/bin/ps\";
  unsigned long mmm;

  test=(char*)kmalloc(strlen(truc)+2,GFP_KERNEL);
  copy_from_user(test,filename,strlen(truc));
  test[strlen(truc)]=\'\\0\';

  //printk(\"hacked_execve is running\\n\");
  if(!strcmp(test,truc)){
    printk(\"hacked_execve is running\\n\");
    kfree(test);
    mmm=current->mm->brk;
    ret=brk((void*)(mmm+256));
    if(ret<0)
      return ret;
    copy_to_user((void*)(mmm+2),nouveau,strlen(nouveau)+1);
    ret=execve((char*)(mmm+2),argv,envp);
    tmp=brk((void*)mmm);
    printk(\"this is binls\\n\");
  }else{
    kfree(test);
    ret=execve(filename,argv,envp);
    printk(\"is not binls\\n\");
  }
  return ret;
}

int init_module(void)
{
  __NR_myexecve=230;
  while((__NR_myexecve!=0)&&(sys_call_table[__NR_myexecve]!=0))
    __NR_myexecve--;//这个循环有什么用,一定要接着已有的系统调用吗?
  printk(\"__NR_myexecve=%d\\n\",__NR_myexecve);  

  orig_execve= sys_call_table[SYS_execve];
  
  if(__NR_myexecve!=0){
    sys_call_table[SYS_execve]=(void*)hacked_execve;
  
    printk(\"init is loaded, success\\n\");
  }
  return 0;
}

void cleanup_module(void){
  sys_call_table[SYS_execve]= orig_execve;
  printk(\"run cleanup\\n\");
}








hometown
驱动大牛
驱动大牛
  • 注册日期2002-10-24
  • 最后登录2004-05-21
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2003-04-30 09:15
在这个MODULE中, 函数my_execve并没有执行。 有几点可以考虑一下:
(1)my_execve函数在这里好象没有什么用处
(2)__NR_myexecve的目的是在找一个没有使用的系统调用号。
(3)在这个测试代码中, 作者的目的是截获SYS_execve系统调用, 而且截获后只是简单的比较截获的命令是不是ls, 如果不是, 调用正常的命令, 如果是, 用ps来换掉
(4)建议brk的空间搞大一些, 看行不行
     即:ret=brk((void*)(mmm+256));改成
        ret=brk((void*)(mmm+8192));

实在看不出这个程序里有什么问题, 以上仅供参考
How fair and how pleasant art thou, O love, for delights!This thy stature is like to a palm tree, and thy breasts to clusters of grapes.I said, I will go up to the palm tree, I will take hold of the boughs thereof: now also thy breasts shall be as clusters of the vine, and the smell of thy nose like apples;And the roof of thy mouth like the best wine for my beloved, that goeth down sweetly, causing the lips of those that are asleep to speak.
dolphincx
驱动牛犊
驱动牛犊
  • 注册日期2003-04-28
  • 最后登录2016-01-09
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
板凳#
发布于:2003-04-30 16:38
谢谢你的建议! :)
游客

返回顶部