Linux sys_call_table变动检测
catalogue
. 引言
. 内核ko timer定时器,检测sys_call_table adress变动
. 通过/dev/kmem获取IDT adress
. 比较原始的系统调用地址和当前内核态中的系统调用地址发现是否有sys_call_table hook行为
0. 引言
内核rookit通常以系统调用为攻击目标,主要出于两个原因
. 在内核态劫持系统调用能以较小的代价控制整个系统,不必修太多东西
. 应用层大多数函数是一个或多个系统调用不同形式的封装,更改系统调用意味着其上层所有的函数都会被欺骗
当前的系统调用地址保存在系统调用表中,位于操作系统为内核保留的内存空间(虚拟地址最高1GB),系统调用入口地址的存放顺序同/usr/include/asm/unistd.h中的排列顺序,按系统调用号递增9
Relevant Link:
http://www.blackhat.com/presentations/bh-europe-09/Lineberry/BlackHat-Europe-2009-Lineberry-code-injection-via-dev-mem-slides.pdf
1. 内核ko timer定时器,检测sys_call_table adress变动
. The module does a copy of the Syscall Table to save all syscalls pointers
. After this first step, the module uses the kernel timer to check every X secondes the diff between the Syscall Table and the copy.
. If a diff is found, the module creates a workqueue to execute the python script and restore the good syscall pointer.
The python script is executed with root creds and the syscall number which is hooked, is passed as the first argument of script (sys.argv[1]).
0x1: hook_detection.c
/*
** Copyright (C) 2013 - Jonathan Salwan - http://twitter.com/JonathanSalwan
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
**
** For more information about this module,
** see : http://shell-storm.org/blog/Simple-Hook-detection-Linux-module/
**
*/ #include <asm/uaccess.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/syscalls.h>
#include <linux/timer.h>
#include <linux/workqueue.h> #define PATH_PYTHON "/usr/bin/python2.7"
#define PATH_SCRIPT "/opt/scripts/hook_detected.py" #define TIME_SLEEP 30000 /* in msec */ static struct timer_list timer_s;
static struct workqueue_struct *wq;
static unsigned int syscall_table_size;
static unsigned long *addr_syscall_table;
static unsigned long *dump_syscall_table; static int exec_python_script(unsigned int sys_num)
{
char s_num[];
char *argv[] = {PATH_PYTHON, PATH_SCRIPT, s_num, NULL};
static char *envp[] = {"HOME=/", "TERM=linux", "PATH=/sbin:/bin:/usr/sbin:/usr/bin", NULL};
struct subprocess_info *sub_info; sprintf(s_num, "%d", sys_num);
sub_info = call_usermodehelper_setup(argv[], argv, envp, GFP_ATOMIC);
if (sub_info == NULL)
return -ENOMEM;
call_usermodehelper_exec(sub_info, UMH_WAIT_PROC);
return ;
} static unsigned long *get_syscalls_table(void)
{
unsigned long *start; /* hack :/ */
for (start = (unsigned long *)0xc0000000; start < (unsigned long *)0xffffffff; start++)
if (start[__NR_close] == (unsigned long)sys_close){
return start;
}
return NULL;
} static unsigned int get_size_syscalls_table(void)
{
unsigned int size = ; while (addr_syscall_table[size++]);
return size * sizeof(unsigned long *);
} static void check_diff_handler(struct work_struct *w)
{
unsigned int sys_num = ; while (addr_syscall_table[sys_num]){
if (addr_syscall_table[sys_num] != dump_syscall_table[sys_num]){
printk(KERN_INFO "hook_detection: Hook detected ! (syscall %d)\n", sys_num);
write_cr0(read_cr0() & (~0x10000));
addr_syscall_table[sys_num] = dump_syscall_table[sys_num];
write_cr0(read_cr0() | 0x10000);
exec_python_script(sys_num);
printk(KERN_INFO "hook_detection: syscall %d is restored\n", sys_num);
}
sys_num++;
}
}
static DECLARE_DELAYED_WORK(check_diff, check_diff_handler); static void timer_handler(unsigned long data)
{
unsigned long onesec; onesec = msecs_to_jiffies();
queue_delayed_work(wq, &check_diff, onesec);
if (mod_timer(&timer_s, jiffies + msecs_to_jiffies(TIME_SLEEP)))
printk(KERN_INFO "hook_detection: Failed to set timer\n");
} static int __init hook_detection_init(void)
{
addr_syscall_table = get_syscalls_table();
if (!addr_syscall_table){
printk(KERN_INFO "hook_detection: Failed - Address of syscalls table not found\n");
return -ECANCELED;
} syscall_table_size = get_size_syscalls_table();
dump_syscall_table = kmalloc(syscall_table_size, GFP_KERNEL);
if (!dump_syscall_table){
printk(KERN_INFO "hook_detection: Failed - Not enough memory\n");
return -ENOMEM;
}
memcpy(dump_syscall_table, addr_syscall_table, syscall_table_size); wq = create_singlethread_workqueue("hook_detection_wq"); setup_timer(&timer_s, timer_handler, );
if (mod_timer(&timer_s, jiffies + msecs_to_jiffies(TIME_SLEEP))){
printk(KERN_INFO "hook_detection: Failed to set timer\n");
return -ECANCELED;
} printk(KERN_INFO "hook_detection: Init OK\n");
return ;
} static void __exit hook_detection_exit(void)
{
if (wq)
destroy_workqueue(wq);
kfree(dump_syscall_table);
del_timer(&timer_s);
printk(KERN_INFO "hook_detection: Exit\n");
} module_init(hook_detection_init);
module_exit(hook_detection_exit); MODULE_AUTHOR("Jonathan Salwan");
MODULE_DESCRIPTION("Hook Detection");
MODULE_LICENSE("GPL");
Relevant Link:
http://shell-storm.org/blog/Simple-Hook-detection-Linux-module/hook_detection.c
http://shell-storm.org/blog/Simple-Hook-detection-Linux-module/
2. 通过/dev/kmem获取IDT adress
核心逻辑是通过idt获取80中断的地址,获取了sys_call_table之后,进行一次拷贝,之后就可以进行diff对比
#include < stdio.h >
#include < sys/types.h >
#include < fcntl.h >
#include < stdlib.h > int kfd; 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; int readkmem (unsigned char *mem,
unsigned off,
int bytes)
{
if (lseek64 (kfd, (unsigned long long) off,
SEEK_SET) != off)
{
return -;
} if (read (kfd, mem, bytes) != bytes)
{
return -;
} } int main (void)
{
unsigned long sct_off;
unsigned long sct;
unsigned char *p, code[];
int i; /* request IDT and fill struct */ asm ("sidt %0":"=m" (idtr)); if ((kfd = open ("/dev/kmem", O_RDONLY)) == -)
{
perror("open");
exit(-);
} if (readkmem ((unsigned char *)&idt,
idtr.base + * 0x80, sizeof (idt)) == -)
{
printf("Failed to read from /dev/kmem\n");
exit(-);
} sct_off = (idt.off2 < < ) | idt.off1; if (readkmem (code, sct_off, 0x100) == -)
{
printf("Failed to read from /dev/kmem\n");
exit(-);
} /* find the code sequence that calls SCT */ sct = ;
for (i = ; i < ; i++)
{
if (code[i] == 0xff && code[i+] == 0x14 &&
code[i+] == 0x85)
sct = code[i+] + (code[i+] < < ) +
(code[i+] < < ) + (code[i+] < < );
}
if (sct)
printf ("sys_call_table: 0x%x\n", sct);
close (kfd);
}
Relevant Link:
http://www.rootkitanalytics.com/kernelland/IDT-dev-kmem-method.php
http://www.phpweblog.net/GaRY/archive/2007/06/17/get_sys_call_table_address.html
3. 比较原始的系统调用地址和当前内核态中的系统调用地址发现是否有sys_call_table hook行为
原始的系统调用地址在内核编译阶段被指定,不会更改,通过比较原始的系统调用地址和当前内核态中的系统调用地址我们就可以发现系统调用有没有被更改。原始的系统调用地址在编译阶段被写入两个文件
. System.map: 该文件包含所有的符号地址,系统调用也包含在内
. vmlinux-2.4.x: 系统初始化时首先被读入内存的内核映像文件
vmlinux-2.4.x文件通常以压缩的格式存放在/boot目录下,所以在比较之前必须解压这个文件,另一个问题是: 我们的比较的前提是假设system.map及vmlinuz image都没有被入侵者更改,所以更安全的做法是在系统干净时已经创建这两个文件的可信任的拷贝,并创建文件的md5 hash
在大多数被装载内核后门情况中,内核在系统初始化之后才被更改,更改发生在加载了rootkit的module或者被植入直接读写/dev/kmem的on-the-fly kernel patch之后。而通常情况下rootkit并不更改vmlinuz和system.map这两个文件,所以打印这两个文件中的符号地址就可以知道系统原始的系统调用地址,系统当前运行中的系统调用地址(可能被更改)可以同过/proc下的kcore文件得到,比较两者就知道结果
0x1: 获取原始内核系统调用函数地址
/boot/System.map-3.10.-.el7.x86_64
0x2: 获取当前内核系统调用地址
cat /proc/kallsyms
0x3: 对比区别
从里面枚举sys_call_table的function point地址
/boot/System.map-3.10.-.el7.x86_64 和
cat /proc/kallsyms | grep sys_fork
进行diff对比 cat System.map-4.4.--generic | grep sys_fork
这里遇到一个问题,ubuntu对sys_socket相关的系统调用会进行内核地址重定位,因此需要对检测结果进行一个误报过滤,看是否所有的函数的gap都相同,如果相同,则说明是系统自己的function address relocation行为
def hex2dec(string_num):
return str(int(string_num.upper(), )) def check_rookit(diff_func_table):
last_func_address_gap =
cur_func_address_gap =
for item in diff_func_table:
cur_func_address_gap = abs(int(hex2dec(item['cur_funcaddress'])) - int(hex2dec(item['ori_funcaddress'])))
if last_func_address_gap != and cur_func_address_gap != last_func_address_gap:
return True
else:
last_func_address_gap = cur_func_address_gap
return False
Relevant Link:
http://www.xfocus.net/articles/200411/754.html
http://www.magicsite.cn/blog/Linux-Unix/Linux/Linux41576.html
http://blog.csdn.net/tommy_wxie/article/details/8039695
Copyright (c) 2017 LittleHann All rights reserved
Linux sys_call_table变动检测的更多相关文章
- Linux IO时事检测工具iostat
Linux IO时事检测工具iostat iostat命令用于检测linux系统io设备的负载情况,运行iostat将显示自上次运行该命令以后的统计信息.用户可以通过指定统计的次数和时间来获得所需的统 ...
- 安全运维之:Linux后门入侵检测工具的使用
安全运维之:Linux后门入侵检测工具的使用 https://blog.csdn.net/exitgogo/article/details/39547113
- linux编写脚本检测本机链接指定IP段是否畅通
linux编写脚本检测本机链接指定IP段是否畅通,通过ping命令检测指定IP,检测命令执行结果,若为0表示畅通,若为1表示不通,以此判断网络是否畅通,但是指定机器禁用ping命令除外.代码如下: # ...
- Linux中系统检测工具top命令
Linux中系统检测工具top命令 本文转自:https://www.cnblogs.com/zhoug2020/p/6336453.html 首先介绍top中一些字段的含义: VIRT:virtua ...
- Linux内核死锁检测机制【转】
转自:http://www.oenhan.com/kernel-deadlock-check 死锁就是多个进程(线程)因为等待别的进程已占有的自己所需要的资源而陷入阻塞的一种状态,死锁状态一旦形成,进 ...
- Linux硬盘性能检测
对于现在的计算机来讲,整个计算机的性能主要受磁盘IO速度的影响,内存.CPU包括主板总线的速度已经很快了. 基础检测方法 1.dd命令 dd命令功能很简单,就是从一个源读取数据以bit级的形式写到一个 ...
- [LINUX]警告:检测到时钟错误。您的创建可能是不完整的。
[LINUX]警告:检测到时钟错误.您的创建可能是不完整的. 原因: 如果上一次编译时为20071001,你把系统时间改成20070901后再编译就会报这样的错误. 解决: 把时间 ...
- Linux主机入侵检测
检查系统信息.用户账号信息 ● 操作系统信息 cat /proc/version 用户信息 用户信息文件 /etc/passwd root:x:0:0:root:/root:/bin/bash 用户名 ...
- Linux后门入侵检测
蛋疼啊,服务器被入侵成肉鸡了,发出大量SYN请求到某个网站!(瞬间有种被OOXX(强)(奸)的赶脚) 泪奔ING... 源起: Linux服务器日常检查,#ps aux 发现大量httpd进程,和往常 ...
随机推荐
- Node.js机制及原理理解初步【转】
一.node.js优缺点 node.js是单线程. 好处就是 1)简单 2)高性能,避免了频繁的线程切换开销 3)占用资源小,因为是单线程,在大负荷情况下,对内存占用仍然很低 3)线程安全,没有加锁. ...
- Git秘钥生成以及Gitlab配置
安装Git:详见http://www.cnblogs.com/xiuxingzhe/p/9300905.html 开通gitlab(开通需要咨询所在公司的gitlab管理员)账号后,本地Git仓库和g ...
- bzoj 1103 : [POI2007]大都市meg (树链剖分+线段树)
Description 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Mary也开始骑着摩托车传递邮件了.不过,她经常回忆起以前在乡间漫步的情景.昔日,乡下有依次编号为1..n ...
- 将xml文件由格式化变为压缩字符串
标签:去除xml文件的空格 有些时候解析xml文件,要求读取的字符串必须是压缩后的xml文件,不能有多余的空格.考虑到在<>标签内包含空格和大于号的情况,写了以下的转换方式. 传入的是压缩 ...
- nagios 配置 check_traffic 流量监控模块(被监控端)
安装软件包yum -y install net-snmp*chkconfig nrpe onchkconfig snmpd on 使用SCP命令拷贝 check_traffic.sh 到 / usr/ ...
- 数论细节梳理&模板
初阶 扩展欧拉 \(k\ge\varphi(m)\)时,\(b^k\equiv b^{k\%\varphi(m)+\varphi(m)}(\bmod m\)) 扩展CRT 推式子合并同余方程. htt ...
- 自学华为IoT物联网_03 公共事业物联网常见问题及解决方案
点击返回自学华为IoT物流网 自学华为IoT物联网_03 公共事业物联网常见问题及解决方案 本文从以下六项公共事业,看看物联网的解决方案: 停车问题 路灯管理问题 消防栓管理问题 井盖管理问题 水表管 ...
- ViewHolder模式的简洁写法
大家通常怎么写ViewHolder呢? ViewHolder holder = null; if(convertView == null){ convertView = mInflater.infla ...
- SNMP扫描
------------------------------------------------------------------grub引导密码:[root@localhost ~]# jm4pt ...
- NOIp2018 复习笔记
其实也没什么用啦,只是来占个坑 OI知识 3367 [模板]并查集 就这么做啊 没什么其他的 就是可以做tarjan LCA和Kruskal的操作 //关键函数 int getfa(int t) { ...