Microsoft MVP성태의 닷넷 이야기
Linux: 92. WSL 2 - 커널 이미지로부터 커널 함수 역어셈블 [링크 복사], [링크+제목 복사],
조회: 5690
글쓴 사람
정성태 (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

비밀번호

댓글 작성자
 




... [106]  107  108  109  110  111  112  113  114  115  116  117  118  119  120  ...
NoWriterDateCnt.TitleFile(s)
11274정성태8/22/201719378.NET Framework: 674. Thread 타입의 Suspend/Resume/Join 사용 관련 예외 처리
11273정성태8/22/201721633오류 유형: 415. 윈도우 업데이트 에러 Error 0x80070643
11272정성태8/21/201724794VS.NET IDE: 120. 비주얼 스튜디오 2017 버전 15.3.1 - C# 7.1 공개 [2]
11271정성태8/19/201719241VS.NET IDE: 119. Visual Studio 2017에서 .NET Core 2.0 프로젝트 환경 구성하는 방법
11270정성태8/17/201730716.NET Framework: 673. C#에서 enum을 boxing 없이 int로 변환하기 [2]
11269정성태8/17/201721487디버깅 기술: 93. windbg - 풀 덤프에서 .NET 스레드의 상태를 알아내는 방법
11268정성태8/14/201721059디버깅 기술: 92. windbg - C# Monitor Lock을 획득하고 있는 스레드 찾는 방법
11267정성태8/10/201725117.NET Framework: 672. 모노 개발 환경
11266정성태8/10/201724932.NET Framework: 671. C# 6.0 이상의 소스 코드를 Visual Studio 설치 없이 명령행에서 컴파일하는 방법
11265정성태8/10/201753171기타: 66. 도서: 시작하세요! C# 7.1 프로그래밍: 기본 문법부터 실전 예제까지 [11]
11264정성태8/9/201724092오류 유형: 414. UWP app을 signtool.exe로 서명 시 0x8007000b 오류 발생
11263정성태8/9/201719574오류 유형: 413. The C# project "..." is targeting ".NETFramework, Version=v4.0", which is not installed on this machine. [3]
11262정성태8/5/201718249오류 유형: 412. windbg - SOS does not support the current target architecture. [3]
11261정성태8/4/201720834디버깅 기술: 91. windbg - 풀 덤프 파일로부터 강력한 이름의 어셈블리 추출 후 사용하는 방법
11260정성태8/3/201718938.NET Framework: 670. C# - 실행 파일로부터 공개키를 추출하는 방법
11259정성태8/2/201718174.NET Framework: 669. 지연 서명된 어셈블리를 sn.exe -Vr 등록 없이 사용하는 방법
11258정성태8/1/201718996.NET Framework: 668. 지연 서명된 DLL과 서명된 DLL의 차이점파일 다운로드1
11257정성태7/31/201719166.NET Framework: 667. bypassTrustedAppStrongNames 옵션 설명파일 다운로드1
11256정성태7/25/201720657디버깅 기술: 90. windbg의 lm 명령으로 보이지 않는 .NET 4.0 ClassLibrary를 명시적으로 로드하는 방법 [1]
11255정성태7/18/201723210디버깅 기술: 89. Win32 Debug CRT Heap Internals의 0xBAADF00D 표시 재현 [1]파일 다운로드3
11254정성태7/17/201719582개발 환경 구성: 322. "Visual Studio Emulator for Android" 에뮬레이터를 "Android Studio"와 함께 쓰는 방법
11253정성태7/17/201719933Math: 21. "Coding the Matrix" 문제 2.5.1 풀이 [1]파일 다운로드1
11252정성태7/13/201718454오류 유형: 411. RTVS 또는 PTVS 실행 시 Could not load type 'Microsoft.VisualStudio.InteractiveWindow.Shell.IVsInteractiveWindowFactory2'
11251정성태7/13/201717139디버깅 기술: 88. windbg 분석 - webengine4.dll의 MgdExplicitFlush에서 발생한 System.AccessViolationException의 crash 문제 (2)
11250정성태7/13/201720698디버깅 기술: 87. windbg 분석 - webengine4.dll의 MgdExplicitFlush에서 발생한 System.AccessViolationException의 crash 문제 [1]
11249정성태7/12/201718532오류 유형: 410. LoadLibrary("[...].dll") failed - The specified procedure could not be found.
... [106]  107  108  109  110  111  112  113  114  115  116  117  118  119  120  ...