[fw]拦截系统调用
今天在ubuntu中玩了下“拦截系统调用”,记录下自己对整个实现的理解。
原理
在linux kernel中,系统调用都放在一个叫做“sys_call_table”的分配表里面,在进入一个系统调用的最后一步,会调用与eax中包含的系统调用号对应的特定服务例程:
- call *sys_call_table(,%eax,4)
因为分派表中的每个表项占4个字节,因此首先把系统调用号乘以4,再加上sys_call_table分配表的起始地址,然后从从这个地址单元获取指向服务例程的指针,内核就找到了要调用的服务例程。我们只要修改对应的分配表项,即可实现系统调用的拦截。
获取sys_call_table的地址
网上介绍了很多种方法得到sys_call_table的地址,我使用了相对简单的一种方法——从内核导出的符号表中获取。
图中,十六进制数c15b3000即为sys_call_table的地址。同时,我们也得到了一个重要的信息,该符号对应的内存区域是只读的!
清除写保护
因为sys_call_table分配表的内存属性为只读,因此,我们要先清除对应地址的写保护。暂时使用了两种方法实现该目的:
第一种方法,修改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 16 bit of CR0, a.k.a WP bit */
- 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)
- );
- }
第二种方法,设置虚拟地址对应页表项的读写属性:
- /* make the page writable */
- int make_rw(unsigned long address)
- {
- unsigned int level;
- pte_t *pte = lookup_address(address, &level);
- if (pte->pte & ~_PAGE_RW)
- pte->pte |= _PAGE_RW;
- return 0;
- }
- /* make the page write protected */
- int make_ro(unsigned long address)
- {
- unsigned int level;
- pte_t *pte = lookup_address(address, &level);
- pte->pte &= ~_PAGE_RW;
- return 0;
- }
附:完整代码
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/init.h>
- #include <linux/sched.h>
- #include <linux/init.h>
- #include <linux/fs.h>
- #include <linux/file.h>
- #include <linux/fs_struct.h>
- #include <linux/fdtable.h>
- #include <linux/string.h>
- #include <linux/mm.h>
- #include <linux/syscalls.h>
- #include <linux/list.h>
- #include <linux/jiffies.h>
- #include <linux/cdev.h>
- #include <asm/unistd.h>
- #include <asm/uaccess.h>
- #include <linux/path.h>
- #include <linux/time.h>
- #include <linux/stat.h>
- #include <net/sock.h>
- #include <net/inet_sock.h>
- #include <asm/cpufeature.h>
- /* grep sys_call_table /boot/System.map-`uname -r` */
- unsigned long **sys_call_table = (unsigned long **)0xc15b3000;
- unsigned long *orig_mkdir = NULL;
- /* make the page writable */
- int make_rw(unsigned long address)
- {
- unsigned int level;
- pte_t *pte = lookup_address(address, &level);
- if (pte->pte & ~_PAGE_RW)
- pte->pte |= _PAGE_RW;
- return 0;
- }
- /* make the page write protected */
- int make_ro(unsigned long address)
- {
- unsigned int level;
- pte_t *pte = lookup_address(address, &level);
- pte->pte &= ~_PAGE_RW;
- return 0;
- }
- asmlinkage long hacked_mkdir(const char __user *pathname, int mode)
- {
- printk("mkdir pathname: %s\n", pathname);
- printk(KERN_ALERT "mkdir do nothing!\n");
- return 0; /*everything is ok, but he new systemcall does nothing*/
- }
- static int syscall_init_module(void)
- {
- printk(KERN_ALERT "sys_call_table: 0x%lx\n", sys_call_table);
- orig_mkdir = (unsigned long *)(sys_call_table[__NR_mkdir]);
- printk(KERN_ALERT "orig_mkdir: 0x%lx\n", orig_mkdir);
- make_rw((unsigned long)sys_call_table);
- sys_call_table[__NR_mkdir] = (unsigned long *)hacked_mkdir;
- make_ro((unsigned long)sys_call_table);
- return 0;
- }
- static void syscall_cleanup_module(void)
- {
- printk(KERN_ALERT "Module syscall unloaded.\n");
- make_rw((unsigned long)sys_call_table);
- sys_call_table[__NR_mkdir] = (unsigned long *)orig_mkdir; /*set mkdir syscall to the origal one*/
- make_ro((unsigned long)sys_call_table);
- }
- module_init(syscall_init_module);
- module_exit(syscall_cleanup_module);
- MODULE_LICENSE("GPL");
- MODULE_DESCRIPTION("hack syscall");
参考:
1、《深入理解linux内核(第三版)》
2、Hijack Linux System Calls: Part III. System Call Table
[fw]拦截系统调用的更多相关文章
- xenomai内核解析之双核系统调用(一)
版权声明:本文为本文为博主原创文章,转载请注明出处.如有错误,欢迎指正.博客地址:https://www.cnblogs.com/wsg1100/ 目录 xenomai 内核系统调用 一.32位Lin ...
- Hook android系统调用研究(一)
本文的博客链接:http://blog.csdn.net/qq1084283172/article/details/55657300 一.Android内核源码的编译环境 系统环境:Ubuntu 14 ...
- hook Android系统调用的乐趣和好处
翻译:myswsun 0x00 前言 Android的内核是逆向工程师的好伙伴.虽然常规的Android应用被限制和沙盒化,逆向工程师可以按自己希望自定义和改变操作系统和内核中行为.这给了你不可多得的 ...
- Linux Rootkit Sample && Rootkit Defenser Analysis
目录 . 引言 . LRK5 Rootkit . knark Rootkit . Suckit(super user control kit) . adore-ng . WNPS . Sample R ...
- linux ptrace I
这几天通过<游戏安全--手游安全技术入门这本书>了解到linux系统中ptrace()这个函数可以实现外挂功能,于是在ubuntu 16.04 x86_64系统上对这个函数进行了学习. 参 ...
- [译] 玩转ptrace (一)
[本文翻译自这里: http://www.linuxjournal.com/article/6100?page=0,0,作者:Pradeep Padaia] 你是否曾经想过怎样才能拦截系统调用?你是否 ...
- 玩转ptrace (一)
转自http://www.cnblogs.com/catch/p/3476280.html [本文翻译自这里: http://www.linuxjournal.com/article/6100?pag ...
- python-Flask模版注入攻击SSTI(python沙盒逃逸)
一篇以python Flask 模版渲染为例子的SSTI注入教学~ 0x01 Flask使用和渲染 这里简化了flask使用和渲染的教程 只把在安全中我们需要关注的部分写出来 来一段最简单的FLASK ...
- linux ptrace I【转】
转自:https://www.cnblogs.com/mmmmar/p/6040325.html 这几天通过<游戏安全——手游安全技术入门这本书>了解到linux系统中ptrace()这个 ...
随机推荐
- LA 3263 That Nice Euler Circuit(欧拉定理)
That Nice Euler Circuit Little Joey invented a scrabble machine that he called Euler, after the grea ...
- win7提示不是正版桌面变黑
1.以管理员身份运行cmd.exe 2.在该界面>后面输入SLMGR -REARM,大家注意下有个空格键 然后点击确定,重启电脑就OK了.
- Typescript + TSLint + webpack 搭建 Typescript 的开发环境
(1)初始化项目 新建一个文件夹“client-side”,作为项目根目录,进入这个文件夹: 我们先使用 npm 初始化这个项目: 这时我们看到了在根目录下已经创建了一个 package.json 文 ...
- bzoj3123 [Sdoi2013]森林 树上主席树+启发式合并
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3123 题解 如果是静态的查询操作,那么就是直接树上主席树的板子. 但是我们现在有了一个连接两棵 ...
- jmeter性能工具 之监控cpu,内存等信息(四)
1.jmeter 本身不支持直接监控 cpu,内存等信息,需要去官网下载控件 JMeterPlugins-Standard-1.4.0.zip 解压好将其中\lib\ext\JMeterPlug ...
- TCP TIME_WAIT和CLOSE_WAIT
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11484451.html 使用如下指令查看当前Server的TCP状态 netstat -n | awk ...
- 【leetcode】877. Stone Game
题目如下: Alex and Lee play a game with piles of stones. There are an even number of piles arranged in ...
- java通配符写法
有时候我们会遇到这样的需求,需要把一个报文里的某些参数项通过通配符的形式配置成我们需要的结果值插入回报文中. String filetext = "<cn>#用户身份ID(主账号 ...
- php怎么启动exe文件
PHP作为一种服务器端的脚本语言,象编写简单,或者是复杂的动态网页这样的任务,它完全能够胜任.但事情不总是如此,有时为了实现某个功能,必须借助于操作系统的外部程序(或者称之为命令),这样可以做到事半功 ...
- 大白话vue——slot的作用与使用
这篇内容本来是不打算放在首页上的,因为内容实在是比较简单,但是在查找slot的使用讲解时发现相关的讲解比较少,要么像官方文档一样简单讲解(看过任然一脸懵逼),也许是自己理解能力比较差...所以在此讲述 ...