본문 바로가기
Programming/System Programming

VirtualProtectEx메모리 속성변경 size

by Winduck 2018. 5. 24.
반응형

특정 메모리의 몇 바이트만 메모리 속성변경하고 에러 핸들링을 싶었는데 이부분이 변경되지 않아 삽질이 시작했습니다....그러면서 알게된 내용들은 정리해보겠습니다

 

먼저, VirtualProtectEx API에 대해서 알아본다.



BOOL WINAPI VirtualProtectEx(
  _In_  HANDLE hProcess,
  _In_  LPVOID lpAddress,
  _In_  SIZE_T dwSize,
  _In_  DWORD  flNewProtect,
  _Out_ PDWORD lpflOldProtect
);
 


- hProcess : 메모리 속성 변경 프로세스

- lpAddress : 변경 메모리

- dwSize : 변경메모리 size

flNewProtect : 변경할 메모리 속성

lpflOldProtect : 기존 메모리 속성


 dwSize이부분에서 저의 삽질은 시작되었습니다...ㅠㅠ size라고 쓰여있길래 당연히 BYTE단위인 줄알고 개인 프로젝트를 진행했다.

그런데 내가 의도한 부분과 다른 곳에서 계속 예외가 발생했다.

그래서 메모리 속성을 보았다. 그런데 해당 메모리의 속성자체가 바뀌어 있었다.


MSDN에 의하면 dwSize파라미터에대해 다음과같이 설명한다.


The size of the region whose access protection attributes are to be changed, in bytes. The region of affected pages includes all pages containing one or more bytes in the range from the lpAddress parameter to (lpAddress+dwSize). This means that a 2-byte range straddling a page boundary causes the protection attributes of both pages to be changed.                                              

                                                                                                                                                                   by MSDN 


간단히 말하면 ldAddress+dwSize(byte단위)해당 범위를 포함한 페이지의 속성을 변경한다는 말이다.

예를들어) VirtualProtectEx(hProc,0x401000,0x20,PAGE_NOACCESS,lpflOldProtect)라면 메모리 속성 변경 적용범위는 0x401020가 아니라 0x402000까지이다.


개인프로젝트 진행하며 알게된 사실에 대해 작성했습니다. 이부분때문에 거의 하루는 삽질한듯하네요... 고정관념이라는게 무섭네요... 잊지않도록해야겟습니다.

반응형

댓글