# VirtualAlloc

VirtualAlloc은 현재 프로세스 내에서 메모리를 할당하는 함수이며, 이 함수에 의해 할당된 메모리는 자동으로 0으로 초기화됩니다.

이 함수는 주로 인젝션 시 페이로드를 저장할 공간 확보에 사용됩니다.

{% hint style="info" %}
다른 프로세스의 주소 공간에 메모리를 할당하려면 [VirtualAllocEx](/defense-evasion/windows-api/virtualallocex.md)함수를 사용합니다.
{% endhint %}

```cpp
LPVOID VirtualAlloc(
  [in, optional] LPVOID lpAddress,
  [in]           SIZE_T dwSize,
  [in]           DWORD  flAllocationType,
  [in]           DWORD  flProtect
);
```

<table><thead><tr><th width="186">인자</th><th width="227">설명</th><th>보편적인 값</th></tr></thead><tbody><tr><td>lpAddress</td><td>할당할 주소</td><td>NULL(OS가 자동 선택)</td></tr><tr><td>dwSize</td><td>할당 크기</td><td></td></tr><tr><td>flAllocationType</td><td>할당 타입</td><td><code>MEM_COMMIT | MEM_RESERVE</code></td></tr><tr><td>flProtect</td><td>메모리 보호 속성</td><td><code>PAGE_EXECUTE_READWRITE</code></td></tr></tbody></table>

## Example

```cpp
#include <windows.h>
#include <iostream>

int main() {
    const SIZE_T size = 0x1000;

    // 1) VirtualAlloc: 예약+커밋 + RW 권한
    unsigned char* buffer = (unsigned char*)VirtualAlloc(NULL, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);

    if (!buffer) {
        std::cout << "VirtualAlloc failed. GLE=" << GetLastError() << "\n";
        return 1;
    }

    std::cout << "[+] VirtualAlloc succeeded\n";
    std::cout << "    Address: " << (void*)buffer << "\n";

    // 2) 쓰기/읽기 검증
    buffer[0] = 0x41;   // 'A'
    buffer[1] = 0x42;   // 'B'
    std::cout << "[+] Write/Read OK\n";
    std::cout << "    buffer[0]=0x" << std::hex << (int)buffer[0]
        << " buffer[1]=0x" << (int)buffer[1] << std::dec << "\n";

    // 3) VirtualQuery로 할당 상태/보호 확인
    MEMORY_BASIC_INFORMATION mbi{};
    if (VirtualQuery(buffer, &mbi, sizeof(mbi)) == 0) {
        std::cout << "VirtualQuery failed. GLE=" << GetLastError() << std::endl;
        return 1;
    }

    std::cout << "[+] VirtualQuery\n";
    std::cout << "    State      : 0x" << std::hex << mbi.State << std::dec << std::endl;

    // 4) VirtualFree로 해제
    if (!VirtualFree(buffer, 0, MEM_RELEASE)) {
        std::cout << "VirtualFree failed. GLE=" << GetLastError() << std::endl;;
        return 1;
    }
    std::cout << "[+] VirtualFree(MEM_RELEASE) succeeded" << std::endl;

    // 5) 해제 후 VirtualQuery로 상태 확인 (보통 MEM_FREE로 보임)
    MEMORY_BASIC_INFORMATION mbi2{};
    VirtualQuery(buffer, &mbi2, sizeof(mbi2));
    std::cout << "[+] After free VirtualQuery" << std::endl;
    std::cout << "    State: 0x" << std::hex << mbi2.State << std::dec
        << (mbi2.State == MEM_FREE ? " (MEM_FREE)" : "") << std::endl;

    return 0;
}
```

## References

{% embed url="<https://learn.microsoft.com/ko-kr/windows/win32/api/memoryapi/nf-memoryapi-virtualalloc>" %}


---

# 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/defense-evasion/windows-api/virtualalloc.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.
