본문 바로가기

Debugging

(24)
CPU Registers and Instructions - Registers Assembly Language에서 Instruction에 사용되는 Register와 관련한 내용을 다뤄보겠습니다. Instruction은 CISC와 RISC 유형이 있는데요. Intel x86_x64 Processor Architecture는 개별 Instruction의 길이가 서로 다르기 때문에, 이러한 Processor Architecture는 CISC(Complex Instruction Set Computer)라고 합니다. 반대로 개별 Instruction의 길이가 동일한 Processor Architecture는 RISC(Reduced Instruction Set Computer)라고 하며, RISC는 ARM 또는 SPARC Processor에서 사용됩니다. 아래는 우리가 흔히 접하는 x86_6..
How to collect performance log using performance monitor Windows OS 내에서 성능 자료를 수집할 때 Built-in 도구인 perfmon을 사용할 수 있습니다. 아래 수집 가이드를 이용하시어 문제가 발생하고 있는 시점에 수집 후 Performance Metric들을 살펴볼 수 있습니다. 1) Collector 생성 ## -max 800은 Circular 모드로 수집되며 최대 800MB까지 파일 사이즈 ## -si 00:00:10은 10초 마다 Sampling 하는 옵션 Logman.exe create counter PerfLog-Short -o "c:\perflogs\PerfLog-Short.blg" -f bincirc -v mmddhhmm -max 800 -c "\LogicalDisk(*)\*" "\Memory\*" "\PhysicalDisk(*)\*..
64bit Stack Walking 64bit 프로그램에서 대부분의 함수들은 대개 Stack에 RBP Register의 값을 Push 하지 않습니다. 이는 Compile 시에 -fomit-frame-pointer flag 때문에 발생합니다. 이런 경우, 32bit 프로그램에 비해서 Call Stack을 수동으로 쫓아가는 것이 쉽지는 않습니다. 64bit 프로그램에서 Return Address를 확인하기 위해 Stack에 있는 Data와 Instruction Pointer를 이용할 수 있습니다. 다음과 같은 Call Stack 예제를 통해서 하나씩 확인해보겠습니다. ## 아래 Call Stack의 9번 Frame 부터 보면, Symbol 이 없기 때문에 정상적으로 함수명이 표현되지 않는 것을 알 수 있습니다. (gdb) bt ... #7 0..
ELF Format #3 - Section Header 3. Section header ELF file에는 여러 Section이 위치하고, 각 Section 관련 정보는 Section header에 보관됩니다. 또한, 각 Section은 고유의 이름을 가질 수 있고 주요 Section으로는 .text와 .data를 들 수 있습니다. .text section은 프로그램의 instruction을 보관하고 있고, .data section은 프로그램 시작 전 초기화되는 값들을 보관하고 있습니다. 아래는 실행 파일과 오브젝트 파일의 Section header 정보 예제입니다. 예제1) 실행 파일 hwjung@jhaewon-z01:~$ readelf -S helloworld There are 31 section headers, starting at offset 0x39..
ELF Format #2 - Program Header 2. Program header Program header는 ELF header 바로 다음에 위치한 Program header table에 있습니다. 각 Program header는 관련된 Section 정보를 가지고 있습니다. 프로그램을 시작할 때, OS Loader는 Program header에 있는 정보를 기반으로 Segment들을 Virtual Address Space에 위치시킵니다. Segment는 Read Only, Read/Write, Executable 속성으로 분리됩니다. hwjung@jhaewon-z01:~$ readelf -l helloworld Elf file type is DYN (Shared object file) Entry point 0x1060 There are 13 prog..
ELF Format #1 - ELF Header ELF Format(Executable and Linkable Format)은 각 영역(TEXT, DATA, BSS등)을 정의하여 프로그램이 실행 시에 이 영역들을 메모리에 올리기 위해 사용됩니다. ELF Format의 File은 여러 segments와 sections로 구성되며, 단일 segment는 여러 section으로 구성될 수 있습니다. ELF Format에는 ELF header, Program header, Section header가 존재하며 이를 통해 각 영역들을 확인할 수 있습니다. 아래에서는 위에서 언급한 각 header에 대해서 보다 상세하게 알아보도록 하겠습니다. 1. ELF header ELF Format File의 맨 처음에 위치하며, Program header와 Section ..
Pending되어 있는 NBL 중 Network Monitor를 통한 Packet 확인 ndiskd extension을 이용하여, pending되어 있는 NBL 중 특정 NBL을 Network Monitor로 확인해 볼 수 있습니다. 0: kd> !ndiskd.nblpool ffffe0011d27e000 -find ((@$extin.Flags)&0x108)==0x100) MATCHING NBLs NBL Active? Matched ffffe0014c905030 Allocated ffffe0014d86fd20 Allocated ffffe0014d86bd20 Allocated ffffe0014d8b4d20 Allocated ffffe0014d867d20 Allocated ffffe0014d8af030 Allocated ffffe0014d8b1030 Allocated ffffe0014d8b303..
!ndiskd.pendingnbls Pending되어 있는 NBL을 확인하기 위해서는 ndiskd extension을 사용할 수 있습니다. 0: kd> !ndiskd.pendingnbls This command requires NBL tracking to be enabled on the debugee target machine. (By default, client operating systems have level 2, and servers have level 0). To enable, set this REG_DWORD value to a nonzero value on the target machine and reboot the target machine: HKLM\SYSTEM\CurrentControlSet\Services\NDIS\Pa..
!mex.us 원하는 모듈명이나 함수명을 이용하여 전체 Thread의 Call Stack을 조사하고자 할 때, "!mex.us" 를 이용할 수 있습니다. 0: kd> !mex.us -help !UniqueStacks (!us) - Like the built-in !uniqstacks except it associates thread IDs with the stack traces Usage: !UniqueStacks [-a] [-c] [-cpu] [-crash] [-d ] [-f ] [-3] [-i] [-k] [-s ] [-l] [-m] [-max ] [-nw] [-w] [-p ] [-state ] [-wr ] [-q] [-r] [-t ] [-e ] [] -a : (Kernel Only) Scans all process..
2. Testing a Sample Driver 1. Sample Driver 작성 Visual Studio를 실행(Visual Studio를 관리자 권한으로 실행해야 Cryptographic Exception 이 발생하지 않음)하고, Empty WDM Driver 프로젝트 생성 프로젝트 > Driver Files 폴더에 있는 sample.inf 파일 제거 프로젝트 선택 -> 속성 -> C/C++ -> Code Generation -> Sepctre Mitigation을 Disabled로 변경하고 다시 Build Sample Code #include NTSTATUS DriverEntry(PDRIVER_OBJECT pDrvObj, PUNICODE_STRING pRegistryPath) { pDrvObj = pDrvObj; pRegistryPath = p..