Microsoft MVP성태의 닷넷 이야기
Linux: 92. WSL 2 - 커널 이미지로부터 커널 함수 역어셈블 [링크 복사], [링크+제목 복사],
조회: 5533
글쓴 사람
정성태 (seongtaejeong at gmail.com)
홈페이지
첨부 파일
 
(연관된 글이 1개 있습니다.)
(시리즈 글이 2개 있습니다.)
Linux: 92. WSL 2 - 커널 이미지로부터 커널 함수 역어셈블
; https://www.sysnet.pe.kr/2/0/13780

Linux: 93. Ubuntu 22.04 - 커널 이미지로부터 커널 함수 역어셈블
; https://www.sysnet.pe.kr/2/0/13782




WSL 2 - 커널 이미지로부터 커널 함수 역어셈블

현시점(2024-10-22) WSL의 리눅스 커널 버전은 5.15.153.1이 최신입니다.

$ uname -r
5.15.153.1-microsoft-standard-WSL2

그리고 이것의 빌드 이미지는 윈도우 디렉터리에 저장돼 있는데요,

"C:\Windows\System32\lxss\tools\kernel"
/mnt/c/Windows/System32/lxss/tools/kernel

아래의 글을 보면,

How to extract and disassemble a Linux kernel image (vmlinuz)
; https://blog.packagecloud.io/how-to-extract-and-disassmble-a-linux-kernel-image-vmlinuz/

커널 이미지를 이렇게 추출할 수 있다고 나옵니다.

$ wget -O extract-vmlinux https://raw.githubusercontent.com/torvalds/linux/master/scripts/extract-vmlinux

$ chmod +x extract-vmlinux

$ sudo ./extract-vmlinux /boot/vmlinuz > vmlinux

$ objdump -D vmlinux | less

따라서 WSL 환경에서는 /boot/vmlinuz 대신 윈도우 디렉터리의 kernel 파일을 대상으로 추출할 수 있습니다.

$ sudo ./extract-vmlinux /mnt/c/Windows/System32/lxss/tools/kernel > vmlinux

하지만, 저렇게 커널 이미지만 추출할 수 있을 뿐, 정작 System.map 파일이 없어 특정 함수의 주소를 찾을 수는 없습니다. (제가 방법을 모르는 것일 수도 있지만) WSL의 경우 System.map 파일을 구하려면 커널 소스코드를 직접 빌드해야 하는데요, 기왕 빌드해야 한다면 최신 커널로 업데이트하면 좋겠지만, 아쉽게도 6.6.x 버전은 Docker Desktop이 지원되지 않아 현재 버전(5.15.153.1)을 다운로드해야 합니다.

linux-msft-wsl-5.15.153.1
; https://github.com/microsoft/WSL2-Linux-Kernel/releases/tag/linux-msft-wsl-5.15.153.1

빌드 방법은 지난 글에 설명한 것을 그대로 따릅니다.

WSL 2 - Mariner VM 커널 이미지 업데이트 방법
; https://www.sysnet.pe.kr/2/0/13779

약간의 버전 차이만 있을 뿐이니, 아래에 간략하게 명령어만을 나열해 놓겠습니다.

$ git clone https://github.com/microsoft/WSL2-Linux-Kernel.git --depth=1 -b linux-msft-wsl-5.15.y

$ sudo apt update && sudo apt upgrade -y
$ sudo apt update && sudo apt install build-essential flex bison libssl-dev libelf-dev bc python3 -y

// Ubuntu 20.04의 경우
$ sudo apt install dwarves -y

// Ubuntu 24.04의 경우
$ sudo apt install pahole -y

$ cd WSL2-Linux-Kernel

$ make -j$(nproc) KCONFIG_CONFIG=Microsoft/config-wsl
...[생략]...
  LD      vmlinux
  BTFIDS  vmlinux
  SORTTAB vmlinux
  SYSMAP  System.map
...[생략]...
  LD      arch/x86/boot/compressed/vmlinux
  ZOFFSET arch/x86/boot/zoffset.h
  OBJCOPY arch/x86/boot/vmlinux.bin
  AS      arch/x86/boot/header.o
  LD      arch/x86/boot/setup.elf
  OBJCOPY arch/x86/boot/setup.bin
  BUILD   arch/x86/boot/bzImage
Kernel: arch/x86/boot/bzImage is ready  (#1)

$ sudo make modules_install headers_install

$ cp arch/x86/boot/bzImage /mnt/c/temp

// c:\temp에 복사한 파일을 윈도우 탐색기에서 다시 "c:\" 루트 경로에 "bzImage-wsl2-5.15.153.1" 파일명으로 복사

마지막으로, 저 이미지 파일을 %USERPROFILE%\.wslconfig 파일에 다음과 같이 반영한 다음,

c:\temp> type %USERPROFILE%\.wslconfig
[wsl2]
kernel=C:\\bzImage-wsl2-5.15.153.1

wsl을 재시작합니다.

// 관리자 권한으로 실행
c:\temp> wsl --shutdown

확인을 위해 아무 Linux 배포본을 하나 실행한 다음 uname 명령을 내리면,

$ uname -r
5.15.153.1-microsoft-standard-WSL2+

기존 uname 결과에서 끝에 "+"만 붙은 것을 확인할 수 있습니다. 다행히, 이 버전으로는 Docker Desktop이 잘 동작합니다.




자, 그럼 방금 전의 빌드로 인해 System.map 파일을 구할 수 있으니 특정 함수가 정의된 주소를 찾을 수 있습니다.

// make 빌드를 실행했던 경로에 System.map 파일 위치

$ cat ./System.map | grep do_sys_open
ffffffff8131a0c0 t do_sys_openat2
ffffffff8131bea0 T do_sys_open

또한, 커널 이미지도 vmlinux 파일로 이미 풀려 있으므로 굳이 extract-vmlinux로 추출할 필요 없이 해당 함수의 역어셈블 코드를 알아낼 수 있습니다.

$ objdump -S --start-address=0xffffffff8131bea0 ./vmlinux | less
./vmlinux:     file format elf64-x86-64

Disassembly of section .text:

ffffffff8131bea0 <do_sys_open>:
        putname(tmp);
        return fd;
}

long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
{
ffffffff8131bea0:       e8 bb b6 db ff          callq  ffffffff810d7560 <__fentry__>
ffffffff8131bea5:       48 83 ec 20             sub    $0x20,%rsp
ffffffff8131bea9:       65 48 8b 04 25 28 00    mov    %gs:0x28,%rax
ffffffff8131beb0:       00 00
ffffffff8131beb2:       48 89 44 24 18          mov    %rax,0x18(%rsp)
ffffffff8131beb7:       31 c0                   xor    %eax,%eax
        if (how.flags & O_PATH)
ffffffff8131beb9:       f7 c2 00 00 20 00       test   $0x200000,%edx
ffffffff8131bebf:       89 d0                   mov    %edx,%eax
ffffffff8131bec1:       74 3a                   je     ffffffff8131befd <do_sys_open+0x5d>
                how.flags &= O_PATH_FLAGS;
ffffffff8131bec3:       25 00 00 2b 00          and    $0x2b0000,%eax
        if (how.flags & O_PATH)
ffffffff8131bec8:       31 c9                   xor    %ecx,%ecx
        struct open_how how = build_open_how(flags, mode);
        return do_sys_openat2(dfd, filename, &how);
ffffffff8131beca:       48 89 e2                mov    %rsp,%rdx
        return how;
ffffffff8131becd:       48 89 04 24             mov    %rax,(%rsp)
ffffffff8131bed1:       48 89 4c 24 08          mov    %rcx,0x8(%rsp)
ffffffff8131bed6:       48 c7 44 24 10 00 00    movq   $0x0,0x10(%rsp)
ffffffff8131bedd:       00 00
        return do_sys_openat2(dfd, filename, &how);
ffffffff8131bedf:       e8 dc e1 ff ff          callq  ffffffff8131a0c0 <do_sys_openat2>
}
...[생략]...




이 외에도 GDB를 이용한 방법이 있습니다.

$ gdb ~/temp/WSL2-Linux-Kernel/vmlinux
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.2) 9.2
...[생략]...

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /home/testusr/temp/WSL2-Linux-Kernel/vmlinux...
(gdb) 

위와 같이 vmlinux 이미지 파일을 gdb로 시작하면 그에 대한 symbol 정보도 함께 자동으로 읽어냅니다. 이후, gdb 명령어를 이용해 특정 함수의 코드를 역어셈블할 수 있습니다.

(gdb) disass do_sys_open
Dump of assembler code for function do_sys_open:
   0xffffffff8131bea0 <+0>:     callq  0xffffffff810d7560 <__fentry__>
   0xffffffff8131bea5 <+5>:     sub    $0x20,%rsp
   0xffffffff8131bea9 <+9>:     mov    %gs:0x28,%rax
   0xffffffff8131beb2 <+18>:    mov    %rax,0x18(%rsp)
   0xffffffff8131beb7 <+23>:    xor    %eax,%eax
   0xffffffff8131beb9 <+25>:    test   $0x200000,%edx
   0xffffffff8131bebf <+31>:    mov    %edx,%eax
   0xffffffff8131bec1 <+33>:    je     0xffffffff8131befd <do_sys_open+93>
   0xffffffff8131bec3 <+35>:    and    $0x2b0000,%eax
   0xffffffff8131bec8 <+40>:    xor    %ecx,%ecx
   0xffffffff8131beca <+42>:    mov    %rsp,%rdx
   0xffffffff8131becd <+45>:    mov    %rax,(%rsp)
   0xffffffff8131bed1 <+49>:    mov    %rcx,0x8(%rsp)
   0xffffffff8131bed6 <+54>:    movq   $0x0,0x10(%rsp)
   0xffffffff8131bedf <+63>:    callq  0xffffffff8131a0c0 <do_sys_openat2>
   0xffffffff8131bee4 <+68>:    mov    0x18(%rsp),%rsi
   0xffffffff8131bee9 <+73>:    xor    %gs:0x28,%rsi
   0xffffffff8131bef2 <+82>:    jne    0xffffffff8131bf12 <do_sys_open+114>
   0xffffffff8131bef4 <+84>:    add    $0x20,%rsp
   0xffffffff8131bef8 <+88>:    jmpq   0xffffffff822054c0 <__x86_return_thunk>
   0xffffffff8131befd <+93>:    and    $0x7fffc3,%eax
   0xffffffff8131bf02 <+98>:    and    $0x400040,%edx
   0xffffffff8131bf08 <+104>:   je     0xffffffff8131bf17 <do_sys_open+119>
   0xffffffff8131bf0a <+106>:   and    $0xfff,%ecx
   0xffffffff8131bf10 <+112>:   jmp    0xffffffff8131beca <do_sys_open+42>
   0xffffffff8131bf12 <+114>:   callq  0xffffffff81ee9120 <__stack_chk_fail>
   0xffffffff8131bf17 <+119>:   xor    %ecx,%ecx
   0xffffffff8131bf19 <+121>:   jmp    0xffffffff8131beca <do_sys_open+42>
End of assembler dump.)




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 10/22/2024]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




1  2  3  4  5  6  7  [8]  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13743정성태9/26/20246401닷넷: 2298. C# - Console 프로젝트에서의 await 대상으로 Main 스레드 활용하는 방법 [1]
13742정성태9/26/20246687닷넷: 2297. C# - ssh-keygen으로 생성한 ecdsa 유형의 Public Key 파일 해석 [1]파일 다운로드1
13741정성태9/25/20245867디버깅 기술: 202. windbg - ASP.NET MVC Web Application (.NET Framework) 응용 프로그램의 덤프 분석 시 요령
13740정성태9/24/20245728기타: 86. RSA 공개키 등의 modulus 값에 0x00 선행 바이트가 있는 이유(ASN.1 인코딩)
13739정성태9/24/20245876닷넷: 2297. C# - ssh-keygen으로 생성한 Public Key 파일 해석과 fingerprint 값(md5, sha256) 생성 [1]파일 다운로드1
13738정성태9/22/20245599C/C++: 174. C/C++ - 윈도우 운영체제에서의 file descriptor, FILE*파일 다운로드1
13737정성태9/21/20245962개발 환경 구성: 727. Visual C++ - 리눅스 프로젝트를 위한 빌드 서버의 msbuild 구성
13736정성태9/20/20245962오류 유형: 923. Visual Studio Code - Could not establish connection to "...": Port forwarding is disabled.
13735정성태9/20/20246036개발 환경 구성: 726. ARM 플랫폼용 Visual C++ 리눅스 프로젝트 빌드
13734정성태9/19/20245744개발 환경 구성: 725. ssh를 이용한 원격 docker 서비스 사용
13733정성태9/19/20246074VS.NET IDE: 194. Visual Studio - Cross Platform / "Authentication Type: Private Key"로 접속하는 방법
13732정성태9/17/20246111개발 환경 구성: 724. ARM + docker 환경에서 .NET 8 설치
13731정성태9/15/20246695개발 환경 구성: 723. C# / Visual C++ - Control Flow Guard (CFG) 활성화 [1]파일 다운로드2
13730정성태9/10/20246362오류 유형: 922. docker - RULE_APPEND failed (No such file or directory): rule in chain DOCKER
13729정성태9/9/20247105C/C++: 173. Windows / C++ - AllocConsole로 할당한 콘솔과 CRT 함수 연동 [1]파일 다운로드1
13728정성태9/7/20246917C/C++: 172. Windows - C 런타임에서 STARTUPINFO의 cbReserved2, lpReserved2 멤버를 사용하는 이유파일 다운로드1
13727정성태9/6/20247455개발 환경 구성: 722. ARM 플랫폼 빌드를 위한 미니 PC(?) - Khadas VIM4 [1]
13726정성태9/5/20247375C/C++: 171. C/C++ - 윈도우 운영체제에서의 file descriptor와 HANDLE파일 다운로드1
13725정성태9/4/20246127디버깅 기술: 201. WinDbg - sos threads 명령어 실행 시 "Failed to request ThreadStore"
13724정성태9/3/20247983닷넷: 2296. Win32/C# - 자식 프로세스로 HANDLE 상속파일 다운로드1
13723정성태9/2/20248249C/C++: 170. Windows - STARTUPINFO의 cbReserved2, lpReserved2 멤버 사용자 정의파일 다운로드2
13722정성태9/2/20245984C/C++: 169. C/C++ - CRT(C Runtime) 함수에 의존성이 없는 프로젝트 생성
13721정성태8/30/20246019C/C++: 168. Visual C++ CRT(C Runtime DLL: msvcr...dll)에 대한 의존성 제거 - 두 번째 이야기
13720정성태8/29/20246174VS.NET IDE: 193. C# - Visual Studio의 자식 프로세스 디버깅
13719정성태8/28/20246331Linux: 79. C++ - pthread_mutexattr_destroy가 없다면 메모리 누수가 발생할까요?
13718정성태8/27/20247401오류 유형: 921. Visual C++ - error C1083: Cannot open include file: 'float.h': No such file or directory [2]
1  2  3  4  5  6  7  [8]  9  10  11  12  13  14  15  ...