linux 网络劫持编程,Linux下实现劫持系统调用的总结(上)--代码及实现

Linux内核版本2.6中已经不再导出系统调用符号表了。因此,如果想实现劫持系统调用,就得想办法找到系统调用表的地址。网上应该可以搜到相关的实现。我这里找到了albcamus兄的精华文章,并在内核版本2.6.18.3上实践了其中的代码。这里总结一下。

本文欢迎自由转载,但请标明出处和本文链接,并保持本文的完整性。

Dec 2, 2009

一、代码及实现

(一) 劫持open系统调用的代码

内核态实现劫持系统调用的代码如下,来自参考链接1,即albcamus兄提供的代码。我这里屏蔽了一些代码,仅实现了劫持open系统调用。

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

MODULE_DESCRIPTION("Intercept the system

call table in Linux");

MODULE_AUTHOR("alert7 (alert7@xfocus.org)

\n\t\talbcamus ");

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, limit at 0x%X\n", (unsigned

int)idtr.base,(unsigned short)idtr.limit);

/*

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("call open()\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,

&regs);

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);

(二) 编译及实践

Makefile如下:

obj-m:=hack_open.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*

*.symvers

编译之后,加载模块,然后查看日志信息

Sep 24 19:06:49

localhost kernel: intercept: function:get_sys_call_table-L220: sys_call_table

at 0xc11f14e0, call

dispatch at 0xcebeceaa

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系统调用执行次数之频繁。

--未完待续

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/557793.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

分析Spring容器启动流程 Spring初始化

分析Spring容器启动流程 Spring初始化 每当启动Web容器时&#xff08;例如Tomcat&#xff09;&#xff0c;会读取Web应用中的web.xml文件。以下这段代码就是启动Spring容器的关键代码。 ContextLoaderListener 类继承了ContextLoader&#xff0c;实现 了ServletContextListen…

IllegalStateException: No WebApplicationContext found: no Co

严重: Servlet.service() for servlet Persistent Faces Servlet threw exception java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered? at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingF…

linux 喂狗时间,狗狗正确喂食时间表,喂狗最佳时间指南

未满3月龄的狗狗&#xff0c;每天的早、晚餐分别在7&#xff5e;8点与19&#xff5e;20点喂食&#xff0c;期间每隔3&#xff5e;4小时再喂一次。未满6月龄的狗狗&#xff0c;7&#xff5e;8点喂早餐&#xff0c;12&#xff5e;13点喂午餐&#xff0c;19&#xff5e;20点喂晚餐…

帮帮忙—ssm框架中,简单自定义标签SimpleTagSupport如何注入spirng中的bean

权限太多&#xff0c;想用简单自定义标签来控制&#xff0c;但遇到一个头疼的问题&#xff0c;不能用autowird自动注入spring管理的bean&#xff0c;让人恼火&#xff1b; 经过周折&#xff0c;终于解决问题&#xff0c;与大家一起分享&#xff0c;可能不是最好的方法&#xf…

linux连接svn上代码,代码管理平台介绍、安装svn、客户端上使用svn(linux)、客户端上使用svn(windows)...

代码管理平台介绍代码管理平台介绍--svn版本控制&#xff0c;记录若干文件内容变化&#xff0c;以便未来查阅特定版本修订状况.好比某一个业务&#xff0c;须要不断更新&#xff0c;好比产品经理这周提交了产品新的需求&#xff0c;改动了一些代码&#xff0c;咱们把新的代码上…

linux 文件读取 监控,linux 文件系统的监控

完整性检查是HIDS的重要组成部分之一&#xff0c;linux下做完整性检查的思路有3个1、哈希对比2、签名校验3、inotify方法有2个&#xff1a;A、定期检测&#xff0c;例如通过cron或程序内置计时器B、实时检测&#xff0c;inotify1、2一般和A&#xff0c;3一般和B。用inotify做完…

<load-on-startup>1</load-on-startup>的作用

1)load-on-startup元素标记容器是否在启动的时候就加载这个servlet(实例化并调用其init()方法)。 2)它的值必须是一个整数&#xff0c;表示servlet应该被载入的顺序 3)当值为0或者大于0时&#xff0c;表示容器在应用启动时就加载并初始化这个servlet&#xff1b; 4)当值小于0或…

Spring初始化:org.springframework.we...ContextLoaderListener的作用

Spring初始化&#xff1a;org.springframework.web.context.ContextLoaderListener的作用 在web.xml种这样配置 <listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>ContextLoaderList…

mx linux安装方法,MX Linux是什么_MX Linux镜像下载-华为云

简介MX Linux是基于Debian稳定分支的面向桌面的Linux发行&#xff0c;它是antiX及早先的MEPIS Linux社区合作的产物。使用说明MX Linux的仓库地址为&#xff1a;https://repo.huaweicloud.com/mxlinux/MX Linux的镜像地址为&#xff1a;https://repo.huaweicloud.com/mxlinux-c…

Java中Map里put方法的返回值

在java中,Map里的put方法,如果key值不存在,则返回值是null,但是key值如果存在,则会返回原先被替换掉的value值.(当然,map中的key和value都允许是null). Map map new HashMap();Object obj null;obj map.put(null, null);System.out.println(obj); //nullobj map.put(null, …

Linux统一编程接口,restful接口设计规范总结

一、重要概念&#xff1a;REST,即Representational State Transfer的缩写。我对这个词组的翻译是"表现层状态转化"。Resource(资源) &#xff1a;对象的单个实例。 例如&#xff0c;一只动物。它可以是一段文本、一张图片、一首歌曲、一种服务&#xff0c;总之就是一…

重写hashcode和equals方法

一。前言 我们都知道&#xff0c;要比较两个对象是否相等时需要调用对象的equals()方法&#xff0c;即判断对象引用所指向的对象地址是否相等&#xff0c;对象地址相等时&#xff0c;那么与对象相关的对象句柄、对象头、对象实例数据、对象类型数据等也是完全一致的&#xff0…

linux 安装log4j,Log4j 安装

Log4j教程 - Log4j安装Log4j API包是根据Apache软件许可证分发的。最新的log4j版本&#xff0c;包括全源代码&#xff0c;类文件和文档可以在http://logging.apache.org/log4j/找到。我们可以从上面的链接下载apache-log4j-x.x.x.tar.gz或zip文件。支持库我们可以使用log4j将信…

input输入框只读的几种方式

input输入框只读的几种方式 **readonly&#xff1a;**只针对input(text / password)和textarea有效&#xff1b;如果设为true&#xff0c;用户只是不能编辑对应的文本&#xff0c;但是仍然可以聚焦焦点&#xff0c;并且在提交表单的时候&#xff0c;该输入项会作为form的一项提…

linux sql 语句菜鸟,Linux安装mysql

要在centos上安装mysql&#xff0c;这些知识还不是很了解&#xff0c;找了一些资料分享一下1、下载mysql-5.5.3-m3.tar.gz&#xff0c;并且解压.tar -xzvf mysql-5.1.36.tar.gz2、添加mysql组&#xff0c;新建mysql用户groupadd mysqluseradd -g mysql mysql3、进入目录cd mysq…

@Autowired注入为null的几种情况

1.在应用的Filter或Listener中使用了Autowired &#xff0c; 原因&#xff1a;因为Filter和Listener加载顺序优先于spring容器初始化实例&#xff0c;所以使用Autowired肯定为null了~~ 解决&#xff1a;用ApplicationContext根据bean名称&#xff08;注意名称为实现类而不是接…

约瑟夫环c语言程序完整版,C语言:约瑟夫环问题(源代码)

本帖最后由 geige 于 2015-7-26 00:48 编辑#include#includestruct stu //构建一个新的数据类型{int num;struct stu *next;};struct stu *createlist(int n); //函数声明int main(){struct stu *head,*p,*temp; //定义头指针&#xff0c;以及中间指针temp&#xff0c;用来删…

js中遇到的一个错误Uncaught SyntaxError: missing )after argument list

报的错误如下&#xff1a; 代码&#xff1a; 解决&#xff1a; 加上引号即可。

c语言第六次实验报告,第一年C语言实验报告6列

成都理工大学计算机工程学院“编程基础”实验报告1. 实验目的(1)精通一维数组. 二维数组的定义&#xff0c;初始化以及输入和输出方法(2)掌握字符数组和字符串函数的使用(3)掌握与数组有关的常用算法2. 实验内容(1)在给定的字符串中找到指定的字符&#xff1b;要求:①通过直接初…

静态页面被拦截解决办法

在配置中央调度器的时候&#xff0c;url-pattern设置为*.do&#xff0c;以后的请求中写什么.do&#xff0c;不会出现静态页面被拦截的情况。 当url-pattern设置为/时&#xff0c;出现了静态页面被拦截 解决&#xff1a; 1.使用defaultServlet&#xff0c;在web.xml中的Dispatch…