33
Windows SystemFile Format 2009.10.20 ㈜ 안철수연구소 ASEC (AhnLab Security Emergency response Center) Anti-Virus Researcher, CISSP 장 영 준 주임 연구원

2. windows system과 file format

Embed Size (px)

DESCRIPTION

2009년 10월 서울여대 강연 자료

Citation preview

Page 1: 2. windows system과 file format

Windows System과 File Format

2009.10.20

㈜ 안철수연구소

ASEC (AhnLab Security Emergency response Center)

Anti-Virus Researcher, CISSP

장 영 준 주임 연구원

Page 2: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.

목 차

1. Windows System 구조

2. Memory, Process, Thread 및 Registry 구조

3. Application Programming Interface

4. Windows Executables와 구조

Page 3: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.

1. Windows System 구조

Page 4: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.4

Ⅰ. 악성코드 개요1. Windows System 구조

1) Windows의 역사

- Windows 운영 체제16비트 운영체제, Windows 95, 98과 ME

- Windows NT 운영 체제32비트 운영체제, 현재 가장 일반적으로 사용하는 윈도우 운영 체제Windows NT 4.0, Windows 2000, Windows XP, Windows Visit과 Windows 7

Page 5: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.

1. Windows System 구조

2) Windows NT 아키텍처 특징

1) 순수한 32비트 아키텍처16비트 어플리케이션 사용 가능한 32비트 운영체제

2) 가상 메모리 지원완전한 가상 메모리 모델을 이용

3) 이식 가능성NT는 C와 C++로 제작되어 다른 프로세서 플랫폼에서 동작 가능

4) 멀티 스레드 지원NT는 완전한 선점형 운영체제이자 멀티 스레드 운영체제

5) 멀티 프로세스 지원다중 CPU를 지원함으로 대규모 데이터 센터 서버에 사용 가능

6) 보안성시스템의 모든 객체는 ACL(Access Control List)에 의해 사용자가 조작 가능

7) 호환성NT 이전 버전의 16비트와 DOS 어플리케이션에 대한 호환성 제공

5

Page 6: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.

1. Windows System 구조

3) Windows 아키텍처

6

Page 7: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.7

1. Windows System 구조

4) Windows 아키텍처 - User Mode

1) System Support Processes – Logon 프로세스와 세션 관리자와 같은 프로세스

2) Service Processes – 작업 스케줄러와 스풀러 서비스 윈도우 서비스들을 호스팅

3) User Application – POSIX, Win32, OS/2 응용 프로그램

4) Environment Subsystems – 운영체제 환경 또는 사용자와 프로그래머에 나타난개별 구성에 대한 지원 부분

Page 8: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.8

Ⅰ. 악성코드 개요1. Windows System 구조

4) Windows 아키텍처 - Kernel Mode

1) Windows Execute – 메모리, 프로세스와 스레드 관리, 보안, I/O, 네트워킹과프로세스 간의 통신과 같은 기본 운영체제 서비스들을 포함

2) Windows Kernel – 스레드 스케줄링, 인터럽터, 예외 디스패칭, 멀티 프로세스동기화와 같은 저 수준 운영체제 함수들로 구성

3) Device Drivers – 사용자 I/O 함수 호출을 특정한 하드웨어 장치 I/O 요청으로 변환

4) Hardware Abstraction Layer(HAL) – 커널, 장치 드라이버와 Windows Execute를분리하는 코드 계층

5) Windowing과 Graphics – 사용자 인터페이스와 그래픽 인터페이스 함수들을 구현

Page 9: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.

1. Windows System 구조

5) I/O System

1) I/O System – 시스템에서 동작하는 디바이스 드라이버를 관리하고 어플리케이션과 디바이스 드라이버간의 통신을 담당하는 Kernel 컴포넌트들로 구성

• I/O Monitoring – 디바이스 계층에 특별한 필터링 코드를 삽입해서 데이터의 흐름을 관찰, 이를 통해 네트워크 인터페이스, 하이 레벨 네트워크 프로토콜, 파일 시스템이나 물리적 저장장치 같은 다양한 종류의 I/O 요소들의 관찰 가능

9

Page 10: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.

1. Windows System 구조

5) Win32 Subsystem

1) Win32 Subsystem – Windows User Interface의 모든 부분을 담당하는Component

Win32 Subsystem은 GDI(Graphics Device Interface)라고 하는 로우 레벨 그래픽엔진과 윈도우 창, 메뉴, 사용자 입력 처리와 같은 하이 레벨 GUI를 처리하는 User 컴포넌트로 구성

Win32 Subsysem은 커널 레벨의 Win32k.sys에 의해 구현되고 유저 레벨의User32.dll과 Gdi32.dll에 의해 관리

유저 모드 DLL과 커널 컴포넌트 사이의 통신은 시스템 콜 방식을 이용

10

Page 11: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.

1. Windows System 구조

6) 주요 Windows System Files (1)

1) Ntosknl.exe – Windows Execute와 Windows Kernel

2) Hal.dll – 하드웨어 추상화 계층

3) Win32k.sys – Windows Subsystem의 Kernel Mode 부분

4) Ntdll.dll – 내부 지원 함수들과 Windows Execute 함수들에대한 Service Dispatch Stub

5) Kernel32.dll, Advapi32.dll, User32.dll, Gdi32.dll – 핵심 Widonws Subsystem DLL

11

Page 12: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.

1. Windows System 구조

7) 주요 Windows System Files (2)

1) Smss.exe – Session Manager로 부팅 시 최초 실행되는 프로세스

2) Csrss.exe – Windows Subsystem 프로세스

3) Winlogon.exe – logon을 조정하는 프로세스

4) Services.exe – Windows Services를 조정하는 프로세스

5) Svchost.exe – 공유 서비스를 제공하는 프로세스

6) Lsass.exe – Local Security Authentication 프로세스

7) Userinit.exe – 사용자 세션을 초기화 하는 프로세스

12

Page 13: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.

2. Memory, Process, Thread 및 Registry 구조

Page 14: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.

2. Memory, Process, Thread 및 Registry 구조

1) Memory 관리

14

1) Virtual Memory – 운영체제의 기본적인 개념

소프트웨어가 물리 메모리에 직접 접근해서 사용하는 대신에 프로세스가 운영체제와 조합해서 소프트웨어와 물리 메모리 사이에 가상의 계층을 만드는 것

1) Paging – 사용되지 않고 있는 메모리 영역이 하드 드라이브에 일시적으로 저장 되는 과정

2) Page Fault – 페이지 파일에 페이지 내용이 저장 되어 있을 때 프로그램이 해당 페이지에 접근하려고 할 때 발생

Page 15: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.

2. Memory, Process, Thread 및 Registry 구조

2) Kernel Memory와 User Memory

1) Kernel Memory2GB 공간으로 예약

커널 자체의 코드와 디바이스 드라이버와 같은 커널 컴포넌트들의 코드도 포함

2) User Memory2GB 공간으로 할당된 어플리케이션 메모리 영역

15

Page 16: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.

2. Memory, Process, Thread 및 Registry 구조

3) Process와 Thread

1) Process – 프로그램에 의해 생성되는 메모리 주소 공간

모든 프로그램은 자신만의 고유한 주소 공간에서 실행되며 시스템은 프로세스 메모리 주소 공간에 모듈을 로드하며 최소한 하나 이상의 스레드를 생성

1) Thread – 가장 기본적인 코드 실행 유닛이며 매 순간마다 CPU는 항상 스레드를 실행 시키고 있음

16

Page 17: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.

2. Memory, Process, Thread 및 Registry 구조

4) Process 초기화 및 생성 과정

1) 프로세스가 CreateProcess Win32 API 호출하면 API는 프로세스 객체와 새로운 프로세스를 위한 메모리 주소 공간 할당

2) CreateProcess API는 Ntdll.dll과 프로그램 바이너리 파일을 새로 생성된 주소 공간에 매핑

3) CreatProcess API는 프로세스의 첫 번째 스레드를 생성하고 그 스레드를 위한 스택 공간을 할당

4) Ntdll.dll의 LdrpInitialize 함수가 실행 됨으로 프로세스의 첫 번째 스레드가 다시 실행

5) LdrpInitialize는 첫 번째 실행 바이너리의 임포트 테이블을 반복적으로 분석 후 실행에필요한 모든 바이너리를 메모리에 매핑

6) LdrpRunInitializeRoutines는 Ntdll.dll 내부 루틴으로 주소 공간에 현재 로드된 모든 정적링크 DLL들을 초기화 작업 수행

7) LdrpInitialize는 스레드의 실제적인 초기화 루틴인 Kernel32.dll의 BaseProcessStart함수 호출, 프로세스의 초기화 작업 완료후 바이너리의 WinMain 엔트리 포인트를 호출

17

Page 18: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.

2. Memory, Process, Thread 및 Registry 구조

5) Registry 관리

1) Registry운영체제와 설치된 모든 프로그램들의 설정과 구성 데이터를 보관

2) Registry는 Hive, Key 그리고 value로 구성

3) Hives 역시 파일C:\windows\system32\config 폴더에 보관

18

Page 19: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.

2. Memory, Process, Thread 및 Registry 구조

6) 주요 Registry 부분

1) HKEY_LOCAL_MACHINE시스템과 관련된 주요 정보들이 기록

2) HKEY_USERS모든 사용자 계정과 관련된 사용자 정보 기록

3) HKEY_CURRENT_USER일반적인 사용자 특정 정보 기록, HKEY_USERS와 연결

4) HKEY_CLASSES_ROOT파일과 관련된 정보들이 기록, HKLM\Software\Classes와 연결

5) HKEY_PERFORMANCE_DATA시스템 성능 관련 정보들이 기록

6) HKEY_CURRENT_CONFIG하드웨어 관련 정보들이 기록HKLM\System\CurrentControlSet\HardwareProfiles\Current와 연결

19

Page 20: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.

3. Application Programming Interface

Page 21: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.

3. Application Programming Interface

1) Win32 API (Application Programming Interface)

1) Win32 APIWin32 API는 상당히 많은 함수들의 모음으로 윈도우 어플리케이션을 개발하기 위한 공식적인 로우 레벨 프로그래밍 인터페이스

MFC는 윈도우와 상호작용하기 위해 사용되는 C++ 객체 계층이며 내부적으로Win32 API를 사용

핵심적인 Win32 API는 약 2000개(자세한 사항은 MSDN을 참고)

모든 API들은 Kernel, User, GDI 로 3가지분류 가능

21

Page 22: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.

3. Application Programming Interface

2) 주요 Win32 API (Application Programming Interface)

1) Kernel APIKernel32.dll 모듈 안에 구현되어 있으며 파일 I/O, 메모리 관리, 객체 관리, 프로세스와 스레드 관리 등을 제공

다양한 서비스를 구현하기 위해 Ntdll.dll의 Native API를 호출

2) GDI APIGdi32.dll 모듈 안에 구현되어 있으며 로우 레벨 그래픽 서비스를 제공

GDI API는 커널의 Win32k.sys 모듈 안에 구현 되어 있으며 기능을 수행하기 위해서는 Win32k.sys에 시스템 콜을 시도

GDI는 그래픽을 그리기 위해서 디바이스 컨텍스트, 브러시, 펜 등과 같은 GDI 객체를 사용

3) USER APIUser32.dll 모듈 안에 구현되어 있으며 윈도우 창 관리, 메뉴, 대화상자, 사용자 인터페이스 관련 객체를 처리

22

Page 23: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.

3. Application Programming Interface

3) Native API (Application Programming Interface)와 System Call 메커니즘

1) Native APINative API는 메모리 관리자, I/O 시스템, 객체 관리자, 프로세스와 스레드 등에 집적 접근 할 수 있는 인터페이스를 제공하며 윈도우 커널에 가장 직접적인 인터페이스

Native API는 Ntdll.dll과 Ntoskrnl.exe에서 익스포트된 함수들의 집합

모든 Native API는 명칭이 Nt와 Zw로 구분하며 Nt 버전 API는 해당 API를 실질적으로 구현한 함수이며 Zw 버전은 시스템 콜 메커니즘 수행 함수

2) System Call 메커니즘시스템 콜은 유저 모드에서 커널 모드 함수를 호출할 필요가 있을 때 발생

윈도우 2000 이전 버전에서는 INT 2E를 이용해 커널을 호출윈도우 2000 이후 버전에서는 Sysenter를 이용해 커널을 호출

23

Page 24: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.

4. Windows Executables와 구조

Page 25: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.

4. Windows Executables와 구조

1) PE (Portable Executable) Format

1) PE 파일Win32 기반의 윈도우 운영체제에서 실행 가능한 파일 형식PE 형식은 유닉스의 COFF(Common Object File Format)에서 유래MZ 는 PE를 만든 Mark Zbilowski의 이니셜

2) PE 파일 종류EXE – DOS 파일에서 32비트 윈도우 파일에 이르는 실행 실행 파일DLL – 동적 링크 라이브러리 (Dynamic-link library) 파일SYS – 커널 모드에 로드 되는 디바이스 드라이버 파일

25

Page 26: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.

4. Windows Executables와 구조

2) PE (Portable Executable) Format 파일 생성

컴파일 링크

심볼과 바이너리 코드

바이너리 코드

가독성이 있는 소스코드

26

Page 27: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.

3) PE (Portable Executable) Format 구조

4. Windows Executables와 구조

PE와 관련된 구조체

PE Header 뒤에 오는 구조체의 배열

.text – 실행 가능한 코드 영역

.data – 초기화된 전역 변수 영역

.rdata – 읽기 전용 데이터 섹션

.idata – Import 함수 정보 영역

.edata – Export 함수 정보 영역

.rsrc – 리소스 데이터 영역

27

Page 28: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.

4. Windows Executables와 구조

4) PE (Portable Executable) Header시작점

로드주소

28

Page 29: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.

5) Import Table과 Export Table을 이용한 모듈 상호간의 동적 링크

4. Windows Executables와 구조

- Import와 Export는 실행 이미지의 동적링크를 가능하게 해주는 메커니즘

- Import Table에는 실행 이미지가 사용하는 각 모듈 목록과 그 모듈에서 사용하는함수 목록 정보가 포함

- Export Table에는 해당 모듈이 익스포트하는 모든 함수의 이름과 RVA를 포함

29

Page 30: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.

4. Windows Executables와 구조

6) Microsoft Office File Format

1) MS Office File FormatMicrosoft Compound Document File Format이라고 부름

Compound Document 파일들은 실제 File Systems와 비슷하게 동작계층화된 Storages 내에서 체계화 되어 독립적인 Data Streams을 포함

- 참고 동영상Bruce Dang & Nick Finco, Microsoft Trustworthy Computing BlueHat - Understanding Office Binary Files

30

Page 31: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.

4. Windows Executables와 구조

7) Adobe Acrobat Reader File Format

Header

Body

XREF

Trailer

1) Header - PDF 파일 포맷 시그니처와 버전 정보 기록

2) Body – 사용자에 의해 기록된 문서 내용

3) XREF – 상호 참조 테이블로 오브젝트들에 대한 파일Offset 저장

4) Trailer - PDF 메타데이터 추출을 위한 정보 저장

31

Page 32: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.32

* Reference

1) MICROSOFT WINDOWS INTERNALS (제4판)

2) WINDOWS VIA C/C++

3) WINDOWS 시스템 실행파일의 구조와 원리

4) 리버싱: 리버스 엔지니어링 비밀을 파헤치다

5) APPLICATION HACKING

Page 33: 2. windows system과 file format

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.33

Copyright (C) AhnLab, Inc. 1988-2009. All rights reserved.

AhnLab, the AhnLab logo, and V3 are trademarks or registered trademarks of AhnLab, Inc.,in Korea and certain other countries. All other trademarks mentioned in this document are the property of their respective owners.

AhnLabThe Joy of Care-Free Your Internet World