# Windows Handle

핸들은 Windows에서 리소스를 식별하고 접근하기 위해 사용하는 임의의 정수 값으로, 사용자가 실제 커널 리소스에 직접 접근하지 못하도록 제어하고 보호하는 역할입니다.

핸들의 값은 중복하여 사용될 수 있으나, 특정 프로세스에 종속된 값이기 때문에 번호가 같다 하더라도 다른 프로세스에서는 유효하지 않습니다.

OS에서는 각 객체의 유형, 메모리 주소, 핸들 번호 등을 매핑하여 관리하는 핸들 테이블이 존재하기 때문에 사용자가 핸들을 통해 객체에 접근하면 OS는 테이블을 확인하여 해당 핸들과 프로세스에 맞는 메모리 주소에 가서 명령을 실행합니다.

<figure><img src="/files/so8lFLo7LKURw8XqmA3H" alt=""><figcaption></figcaption></figure>

사용자가 메모장을 열어서 읽는 과정을 다이어그램으로 나타내면 다음과 같습니다.

```
[프로그램]
   |
   | ① OpenProcess(PROCESS_VM_READ, pid)
   |
   V
[Kernel]
   |
   | → EPROCESS(notepad) 찾음
   | → Handle Table 엔트리 생성
   |   - Object = EPROCESS(notepad)
   |   - GrantedAccess = VM_READ
   V
[HANDLE hRead 반환]
```

```
[프로그램]
   |
   | ② ReadProcessMemory(hRead, addr, buf)
   |
   V
[Kernel : NtReadVirtualMemory]
   |
   | → hRead 검증
   | → Handle Table 엔트리 조회
   | → PROCESS_VM_READ 권한 확인
   | → 통과
   | → 대상 EPROCESS 주소 공간 접근
   | → 메모리 복사
   V
[프로그램 buf에 데이터 전달]

```

대표적으로 많이 사용되는 핸들은 다음과 같습니다.

<table><thead><tr><th width="175">handle type</th><th width="107">data type</th><th width="178">repressents</th><th>used with</th></tr></thead><tbody><tr><td>Windows Handle</td><td>HWND</td><td>애플리케이션 생성</td><td>FindWindow(),<br>GetForegroundWindow(),<br>ShowWindow()</td></tr><tr><td>Process Handle</td><td>HANDLE</td><td>프로세스 실행</td><td>OpenProcess(),<br>GetCurrentProcess(),<br>TerminateProcess()</td></tr><tr><td>Thread Handle</td><td>HANDLE</td><td>프로세스 내 쓰레드</td><td>OpenThread(),<br>CreateThread(),<br>SuspendThread()</td></tr><tr><td>File Handle</td><td>HANDLE</td><td>파일 실행</td><td>CreateFile(),<br>ReadFile()<br>WriteFile()</td></tr><tr><td>Registry Handle</td><td>HKEY</td><td>레지스트리 키</td><td>RegOpenKeyEx(),<br>RegQueryValueEx(),<br>RegSetValueEx()</td></tr><tr><td>Event Handle</td><td>HANDLE</td><td>이벤트 동기화</td><td>CreateEvent(),<br>SetEvent(),<br>ResetEvent()</td></tr><tr><td>Mutex Handle</td><td>HANDLE</td><td>상호 배제 대상</td><td>CreateMutex(),<br>ReleaseMutex()</td></tr></tbody></table>

## References

{% embed url="<https://wonjayk.tistory.com/270>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.pentestwiki.com/etc/system-internals/windows-handle.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
