성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] 그냥 RSS Reader 기능과 약간의 UI 편의성 때문에 사용...
[이종효] 오래된 소프트웨어는 보안 위협이 되기도 합니다. 혹시 어떤 기능...
[정성태] @Keystroke IEEE의 문서를 소개해 주시다니... +_...
[손민수 (Keystroke)] 괜히 듀얼채널 구성할 때 한번에 같은 제품 사라고 하는 것이 아...
[정성태] 전각(Full-width)/반각(Half-width) 기능을 토...
[정성태] Vector에 대한 내용은 없습니다. Vector가 닷넷 BCL...
[orion] 글 읽고 찾아보니 디자인 타임에는 InitializeCompon...
[orion] 연휴 전에 재현 프로젝트 올리자 생각해 놓고 여의치 않아서 못 ...
[정성태] 아래의 글에 정리했으니 참고하세요. C# - Typed D...
[정성태] 간단한 재현 프로젝트라도 있을까요? 저런 식으로 설명만 해...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>windbg - 32비트 프로세스의 메모리 덤프인 경우 !peb 명령어로 나타나지 않는 환경 변수</h1> <p> 간단하게 다음의 코드를 작성하고,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using System; internal class Program { static void Main(string[] args) { <span style='color: blue; font-weight: bold'>Environment.SetEnvironmentVariable("TEST", "1");</span> Console.ReadLine(); } } </pre> <br /> 32비트로 빌드한 다음, 64비트 운영체제에서 실행해 봅니다. 이후 Console.ReadLine에 걸려 있는 프로세스를 "작업 관리자"를 이용해 메모리 덤프를 떠 windbg로 확인하면 다음과 같이 "TEST" 환경 변수가 없습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 0:000> <span style='color: blue; font-weight: bold'>!peb</span> Wow64 PEB32 at d4c000 ...[생략]... Environment: 0000000000000000 Unable to read Environment string. Wow64 PEB at 0000000000d4b000 InheritedAddressSpace: No ...[생략]... DllPath: '< Name not readable >' Environment: 00000000010b1300 ...[생략]... <span style='color: blue; font-weight: bold'>TEMP=C:\Users\testusr\AppData\Local\Temp ThreadedWaitDialogDpiContext=-4</span> ...[생략]... </pre> <br /> 대신 동일한 32비트 프로세스를 <a target='tab' href='https://learn.microsoft.com/en-us/sysinternals/downloads/process-explorer'>process explorer</a>로 메모리 덤프를 뜨면 이렇게 잘 나옵니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 0:000> <span style='color: blue; font-weight: bold'>!peb</span> PEB at 00d4c000 InheritedAddressSpace: No ...[생략]... DllPath: '< Name not readable >' Environment: 0121c730 ...[생략]... TEMP=C:\Users\testusr\AppData\Local\Temp <span style='color: blue; font-weight: bold'>TEST=1</span> ThreadedWaitDialogDpiContext=-4 ...[생략]... </pre> <br /> 결국, 64비트 환경에서 32비트 프로세스에 대해서는 언제나 작업 관리자가 아닌 <a target='tab' href='https://learn.microsoft.com/en-us/sysinternals/downloads/process-explorer'>process explorer</a>를 이용하는 것이 답입니다. ^^<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > windbg - SOS does not support the current target architecture. ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/11262'>https://www.sysnet.pe.kr/2/0/11262</a> WinDbg - Failed to load data access module, 0x80004002 ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/12994'>https://www.sysnet.pe.kr/2/0/12994</a> </pre> <br /> <hr style='width: 50%' /><br /> <br /> 비록 작업 관리자가 뜬 메모리 덤프의 해석을 windbg가 혼동하고는 있지만 해당 덤프에는 정상적으로 데이터가 담겨 있다는 사실에는 변함이 없습니다. 따라서, 약간의 단계를 추가하면 (변경된) 환경 변수를 보는 것이 가능합니다.<br /> <br /> 위의 2개 덤프 기록에서 !peb 명령어의 결과를 보면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // 작업 관리자로 뜬 x86 프로세스의 덤프 0:000> <span style='color: blue; font-weight: bold'>!peb</span> Wow64 PEB32 at <span style='color: blue; font-weight: bold'>d4c000</span> ...[생략]... </pre> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // Process Explorer로 뜬 x86 프로세스의 덤프 0:000> <span style='color: blue; font-weight: bold'>!peb</span> PEB at <span style='color: blue; font-weight: bold'>00d4c000</span> ...[생략]... </pre> <br /> 그나마 "Wow64 PEB32"로 출력된 d4c000 주소가 (변경된 환경 변수의 위치를 담고 있는) PEB의 주소와 같다는 것을 확인할 수 있습니다. PEB 구조체는 다음과 같이 정의되고,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // <a target='tab' href='https://theroadtodelphi.com/2012/05/26/getting-the-environment-variables-of-an-external-x86-process/'>https://theroadtodelphi.com/2012/05/26/getting-the-environment-variables-of-an-external-x86-process/</a> typedef struct _PEB { BYTE Reserved1[2]; BYTE BeingDebugged; BYTE Reserved2[1]; PVOID Reserved3[2]; PPEB_LDR_DATA Ldr; <span style='color: blue; font-weight: bold'>PRTL_USER_PROCESS_PARAMETERS ProcessParameters;</span> BYTE Reserved4[104]; PVOID Reserved5[52]; PPS_POST_PROCESS_INIT_ROUTINE PostProcessInitRoutine; BYTE Reserved6[128]; PVOID Reserved7[1]; ULONG SessionId; } PEB, *PPEB; </pre> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 0:000> <span style='color: blue; font-weight: bold'>dt _PEB32</span> ntdll!_PEB32 +0x000 InheritedAddressSpace : UChar +0x001 ReadImageFileExecOptions : UChar +0x002 BeingDebugged : UChar +0x003 BitField : UChar +0x003 ImageUsesLargePages : Pos 0, 1 Bit +0x003 IsProtectedProcess : Pos 1, 1 Bit +0x003 IsImageDynamicallyRelocated : Pos 2, 1 Bit +0x003 SkipPatchingUser32Forwarders : Pos 3, 1 Bit +0x003 IsPackagedProcess : Pos 4, 1 Bit +0x003 IsAppContainer : Pos 5, 1 Bit +0x003 IsProtectedProcessLight : Pos 6, 1 Bit +0x003 IsLongPathAwareProcess : Pos 7, 1 Bit +0x004 Mutant : Uint4B +0x008 ImageBaseAddress : Uint4B +0x00c Ldr : Uint4B <span style='color: blue; font-weight: bold'>+0x010 ProcessParameters : Uint4B</span> +0x014 SubSystemData : Uint4B // ...[생략]... </pre> <br /> 환경 변수는 "ProcessParameters" 필드가 가리키는 주소에 포함돼 있습니다. 다행히 이 값은 저 구조체를 보고 구하는 것도 가능하지만,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 0:000> <span style='color: blue; font-weight: bold'>dd d4c000 + 0x10 L1</span> 00000000`00d4c010 <span style='color: blue; font-weight: bold'>011b69c8</span> </pre> <br /> !peb 명령어의 결과에도 나오므로 그 값을 그대로 재활용하면 됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 0:000> <span style='color: blue; font-weight: bold'>!peb</span> Wow64 PEB32 at d4c000 InheritedAddressSpace: No ReadImageFileExecOptions: No BeingDebugged: No ImageBaseAddress: 0000000000be0000 NtGlobalFlag: 0 NtGlobalFlag2: 0 Ldr 000000007747cb00 *** _PEB_LDR_DATA type was not found... *** unable to read Ldr table at 000000007747cb00 SubSystemData: 0000000000000000 ProcessHeap: 00000000011b0000 <span style='color: blue; font-weight: bold'>ProcessParameters: 00000000011b69c8</span> ...[생략]... </pre> <br /> 이렇게 구한 ProcessParameters 구조체는 다음과 같은데,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // <a target='tab' href='https://theroadtodelphi.com/2012/05/26/getting-the-environment-variables-of-an-external-x86-process/'>https://theroadtodelphi.com/2012/05/26/getting-the-environment-variables-of-an-external-x86-process/</a> typedef struct _RTL_USER_PROCESS_PARAMETERS { ULONG MaximumLength; ULONG Length; ULONG Flags; ULONG DebugFlags; PVOID ConsoleHandle; ULONG ConsoleFlags; PVOID StandardInput; PVOID StandardOutput; PVOID StandardError; CURDIR CurrentDirectory; UNICODE_STRING DllPath; UNICODE_STRING ImagePathName; UNICODE_STRING CommandLine; PVOID Environment; ...[생략]... } RTL_USER_PROCESS_PARAMETERS, *PRTL_USER_PROCESS_PARAMETERS; </pre> <br /> 아쉽게도 x64 windbg 버전의 dt 명령어로는 32비트 포인터를 사용하지 않으므로 정확한 오프셋을 구할 수는 없으므로, 위의 구조체를 보고 직접 오프셋을 구해야 합니다. 혹은, windbg에서 !wow64exts.sw 명령어로 모드 변경을 한 다음 _RTL_USER_PROCESS_PARAMETERS 구조체를 구하면 다음과 같이 32비트 환경의 오프셋으로 출력됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 0:000> <span style='color: blue; font-weight: bold'>!wow64exts.sw</span> Switched to Guest (WoW) mode 0:000:x86> <span style='color: blue; font-weight: bold'>dt _RTL_USER_PROCESS_PARAMETERS</span> ntdll_77350000!_RTL_USER_PROCESS_PARAMETERS +0x000 MaximumLength : Uint4B +0x004 Length : Uint4B +0x008 Flags : Uint4B +0x00c DebugFlags : Uint4B +0x010 ConsoleHandle : Ptr32 Void +0x014 ConsoleFlags : Uint4B +0x018 StandardInput : Ptr32 Void +0x01c StandardOutput : Ptr32 Void +0x020 StandardError : Ptr32 Void +0x024 CurrentDirectory : _CURDIR +0x030 DllPath : _UNICODE_STRING +0x038 ImagePathName : _UNICODE_STRING +0x040 CommandLine : _UNICODE_STRING <span style='color: blue; font-weight: bold'>+0x048 Environment : Ptr32 Void</span> +0x04c StartingX : Uint4B +0x050 StartingY : Uint4B +0x054 CountX : Uint4B +0x058 CountY : Uint4B +0x05c CountCharsX : Uint4B +0x060 CountCharsY : Uint4B +0x064 FillAttribute : Uint4B +0x068 WindowFlags : Uint4B +0x06c ShowWindowFlags : Uint4B +0x070 WindowTitle : _UNICODE_STRING +0x078 DesktopInfo : _UNICODE_STRING +0x080 ShellInfo : _UNICODE_STRING +0x088 RuntimeData : _UNICODE_STRING +0x090 CurrentDirectores : [32] _RTL_DRIVE_LETTER_CURDIR <span style='color: blue; font-weight: bold'>+0x290 EnvironmentSize : Uint4B</span> +0x294 EnvironmentVersion : Uint4B // ...[생략]... </pre> <br /> 이렇게 0x48, 0x290 오프셋 값을 이용해, 최종적으로 다음과 같이 덤프를 하시면 됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 0:000> <span style='color: blue; font-weight: bold'>dd 011b69c8 + 0x48 L1</span> 00000000`011b6a10 0121c730 0:000> <span style='color: blue; font-weight: bold'>dd 011b69c8 + 0x290 L1</span> 00000000`011b6c58 000023f2 0:000> <span style='color: blue; font-weight: bold'>db 121c730 L000023f2</span> ...[생략: 환경 변수 출력]... </pre> <br /> <hr style='width: 50%' /><br /> <br /> 정리해 보면, 64비트 운영체제에서 실행되는 32비트 프로세스의 경우 PEB가 2개 버전으로 관리된다는 것인데요, 우선 64비트 PEB에는 프로세스 시작 시점의 환경 변수가 반영되고 이후 Wow64 PEB로 복사되고 나서는 프로세스의 코드에 따른 변경 사항이 Wow64 PEB에서 이뤄진다는 것을 알 수 있습니다.<br /> <br /> 그렇다면 위와 같은 결과는, 이제 반대로도 생각할 수 있습니다. 64비트 운영체제에서 실행된 32비트 프로세스의 덤프를 Process Explorer로 뜬 경우, 변경된 환경 변수의 버전이 아닌, 초기 프로세스 실행 시점의 환경 변수도 함께 구할 수 있다는 것입니다.<br /> <br /> 다시 메모리 덤프를 출력한 결과를 볼까요?<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // 작업 관리자로 뜬 x86 프로세스의 덤프 0:000> <span style='color: blue; font-weight: bold'>!peb</span> Wow64 PEB32 at d4c000 ...[생략]... Wow64 PEB at <span style='color: blue; font-weight: bold'>0000000000d4b000</span> InheritedAddressSpace: No ...[생략: 프로세스 실행 시점의 환경 변수를 담고 있는 PEB]... </pre> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // Process Explorer로 뜬 x86 프로세스의 덤프 0:000> <span style='color: blue; font-weight: bold'>!peb</span> PEB at 00d4c000 ...[생략]... </pre> <br /> 아쉽게도, "Process Explorer로 뜬 x86 프로세스의 덤프"에서는 "Wow64 PEB"의 주소인 0x0d4b000 값을 구할 수 없습니다. 단지, 2개의 메모리 주소가 0x1000 (4KB) 차이가 난다는 것에서 운이 좋다면 PEB 주소에서 그만큼을 빼는 것도 가능할 것입니다. 이후의 과정은 64비트 버전의 _PEB, _RTL_USER_PROCESS_PARAMETERS 구조체 오프셋 연산을 통해 초기 환경 변수의 구성을 구할 수 있습니다. 다시 말하지만... 운이 좋다면! ^^)<br /> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
5010
(왼쪽의 숫자를 입력해야 합니다.)