ssongk
ssongk
ssongk
전체 방문자
오늘
어제

공지사항

  • resources
  • 분류 전체보기 (626)
    • CTF (24)
    • background (79)
      • fuzzing (5)
      • linux (29)
      • linux kernel (15)
      • windows (2)
      • web assembly (1)
      • embedded (0)
      • web (13)
      • crypto (9)
      • mobile (1)
      • AI (1)
      • etc.. (3)
    • write-up(pwn) (171)
      • dreamhack (102)
      • pwn.college (4)
      • pwnable.xyz (51)
      • pwnable.tw (3)
      • pwnable.kr (5)
      • G04T (6)
    • write-up(rev) (32)
      • dreamhack (24)
      • reversing.kr (8)
    • write-up(web) (195)
      • dreamhack (63)
      • LOS (40)
      • webhacking.kr (69)
      • websec.fr (3)
      • wargame.kr (6)
      • webgoat (1)
      • G04T (7)
      • suninatas (6)
    • write-up(crypto) (19)
      • dreamhack (16)
      • G04T (1)
      • suninatas (2)
    • write-up(forensic) (53)
      • dreamhack (5)
      • ctf-d (47)
      • suninatas (1)
    • write-up(misc) (13)
      • dreamhack (12)
      • suninatas (1)
    • development (31)
      • Linux (14)
      • Java (13)
      • Python (1)
      • C (2)
      • TroubleShooting (1)
    • 자격증 (8)
    • 이산수학 (1)
    • 정보보안 (0)
hELLO · Designed By 정상우.
ssongk

ssongk

Linux Kernel Debugging (with QEMU)
background/linux kernel

Linux Kernel Debugging (with QEMU)

2024. 4. 2. 23:31

이 글 읽어보고 따라 실습해보면서 정리하는 글

https://jeongzero.oopy.io/73084e52-54fa-43e2-986b-072ee2a4f80d

 

리눅스 커널 디버깅하기

1. 환경

jeongzero.oopy.io

 


 

기본 환경 세팅


ubuntu 18.04에서 진행한다.

사용하는 리눅스 커널의 버전은 4.7이다.

sudo apt-get install qemu qemu-system
git clone https://kernel.googlesource.com/pub/scm/linux/kernel/git/torvalds/linux
cd linux
git checkout v4.7

 

필요한 패키지 설치

sudo apt-get install build-essential libncurses5 libncurses5-dev bin86 kernel-package libssl-dev bison flex libelf-dev

 

 

커널 이미지 빌드


 kgdb를 위한 설정

(linux 디렉터리로 들어가서 진행)

make defconfig
make menuconfig

 

메뉴컴피그에선 다음 항목들을 체크한다.

(y 누르면 체크 됨)

kernel hacking → Compile-time checks and compiler options → Compil the kernel with debug info
kernel hacking → KGDB: kernel debugger

 

커널 빌드

make

 

오류 발생 시 gcc 버전을 확인해서 7이면 6으로 낮춰준다.

https://gist.github.com/Nannigalaxy/1e473cac77f9f7c8dc4dc3784cc0c2ef

 

Ubuntu 18.04 downgrade the gcc version to version 5.5

Ubuntu 18.04 downgrade the gcc version to version 5.5 - downgrade_gcc_version.md

gist.github.com

 

빌드에 성공하면 커널이미지 파일이 arch/x86/boot 하위에 bzimage라는 이름으로 생긴다.

 

 

rootfs 만들기


busybox로 rootfs 제작

wget https://busybox.net/downloads/busybox-1.31.0.tar.bz2
tar -xvf busybox-1.31.0.tar.bz2
cd busybox-1.31.0
make defconfig
make menuconfig

 

setting → Build static binary

 

busybox 빌드

make busybox
mkdir _install
make CONFIG_PREFIX=./_install install

 

_install 디렉터리에 내용물이 들어가면 잘 된 것이다.

 

 

qemu 실행


부트 스크립트를 제작해야 한다.

(bzImage, rootfs 경로를 잘 확인하자)

qemu-system-x86_64 \
-m 256M \
-kernel ./arch/x86/boot/bzImage \ <- bzImage path
-initrd ../for_busybox/busybox-1.31.0/_install/rootfs.img.gz \ <- rootfs path
-append "root=/dev/ram rdinit=/bin/sh kgdboc=ttyS0,115200 kgdbwait" \
-serial pty

 

이제 부트 스크립트를 실행시키면 qumu가 등장한다.

qemu를 보면 remote gdb를 기다리고 있다고 뜬다.

호스트를 살펴보면 /dev/pts/1로 붙으라고 나와 있다.

 

 

디버깅


linux 폴더로 이동하면 심볼 파일이 vmlinux라는 이름으로 존재한다.

(Compile the kernel with debug info 옵션을 설정했기 때문)

 

따라서 커널에 붙으려면 linux 디렉터리로 이동한 뒤

gdb로 /dev/pts/1에 붙으면 된다.

gdb vmlinux
target remote /dev/pts/1

 

붙은 뒤 테스트로 sys_sync함수에 bp를 건다.

b sys_sync
c

 

커널에 붙길 기다리고 있다

 

이 상태에서 브포를 걸고 c로 진행해주면 부팅이 진행된다.

브포를 걸고 실행시키면 부팅된다.

 

ctf 문제들 따라해보면서 좀 더 연습해봐야 할 것 같다.

(그리고 pwndbg는 도저히 설치가 안 된다..)

'background > linux kernel' 카테고리의 다른 글

Kernel Address Space Layout Randomization (KASLR)  (0) 2024.04.03
Linux Kernel Debugging (with VMware)  (1) 2024.04.03
[Lazenca][Development of Kernel Module] 04.Creating a kernel module to privilege escalation  (0) 2024.03.29
[Lazenca][Development of Kernel Module] 03.ioctl(Input/Output control)  (0) 2024.03.28
[Lazenca][Development of Kernel Module] 02.Character Device Drivers  (0) 2024.03.28
    'background/linux kernel' 카테고리의 다른 글
    • Kernel Address Space Layout Randomization (KASLR)
    • Linux Kernel Debugging (with VMware)
    • [Lazenca][Development of Kernel Module] 04.Creating a kernel module to privilege escalation
    • [Lazenca][Development of Kernel Module] 03.ioctl(Input/Output control)
    ssongk
    ssongk
    벌레 사냥꾼이 되고 싶어요

    티스토리툴바