성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
[정성태] "props[i].GetValue(props[i])" 코드에서 ...
[정성태] 저렇게 조각 코드 말고, 실제로 재현이 되는 예제 프로젝트를 압...
[정성태] Modules 창(Ctrl+Shift+U)을 띄워서, 해당 Op...
글쓰기
제목
이름
암호
전자우편
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 - "*** WARNING: Unable to verify checksum for" 경고 없애는 방법</h1> <p> .NET EXE/DLL을 windbg에서 디버깅하는 경우 pdb 정보가 요구되는 명령어의 출력에 꼭 다음과 같은 경고가 나옵니다.<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'>!clrstack</span> OS Thread Id: 0x2bc0 (0) Child SP IP Call Site 000000a3674fed20 00007ffec2dd04c1 <span style='color: blue; font-weight: bold'>*** WARNING: Unable to verify checksum for</span> ConsoleApp1.exe ConsoleApp1.Program.Main(System.String[]) [E:\ConsoleApp1\Program.cs @ 15] 000000a3674ff0a0 00007fff223c6793 [GCFrame: 000000a3674ff0a0] </pre> <br /> 물론 symbol 로딩 명령어에도 나옵니다.<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&gt; <span style='color: blue; font-weight: bold'>.reload -f</span> .<span style='color: blue; font-weight: bold'>*** WARNING: Unable to verify checksum for</span> ConsoleApp1.exe .......................... Loading unloaded module list </pre> <br /> 이게 나오는 원인은 PE header의 checksum 데이터가 없기 때문입니다. 이는 windbg에서 !dh 명령어로 확인할 수 있습니다.<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'>!dh ConsoleApp1</span> File Type: EXECUTABLE IMAGE FILE HEADER VALUES 14C machine (i386) 3 number of sections 59DF6494 time date stamp Thu Oct 12 05:48:20 2017 0 file pointer to symbol table 0 number of symbols F0 size of optional header 22 characteristics Executable App can handle >2gb addresses OPTIONAL HEADER VALUES 20B magic # 48.00 linker version C00 size of code 800 size of initialized data 0 size of uninitialized data 2B36 address of entry point 2000 base of code ----- new ----- 0000000000400000 image base 2000 section alignment 200 file alignment 3 subsystem (Windows CUI) 4.00 operating system version 0.00 image version 6.00 subsystem version 8000 size of image 200 size of headers <span style='color: blue; font-weight: bold'>0 checksum</span> 0000000000100000 size of stack reserve 0000000000001000 size of stack commit 0000000000100000 size of heap reserve ...[이하 생략]... </pre> <br /> 보는 바와 같이 "0 checksum"으로 값이 0입니다.<br /> <br /> .NET의 경우 checksum 데이터를 Debug/Release 버전에 상관없이 생성하지 않으므로 저 경고는 언제나 발생할 수밖에 없습니다. 대신 위안이 되는 것은, 경고 외에는 windbg의 닷넷 어셈블리 분석에 아무런 영향도 미치지 않기 때문에 무시하면 그만이라는 점입니다. (심지어 !clrstack 명령어는 두 번째 실행부터는 해당 경고를 출력하지 않습니다. 정말... 이 경고는 아주 경미하게 거슬리는 정도입니다.)<br /> <br /> <hr style='width: 50%' /><br /> <br /> 혹시나 결벽증이 있는 분을 위해... 더 말씀드리면, checksum 데이터가 다행히 계산 방법이 공개되어 있어 원한다면 설정해 버리면 됩니다. 이에 대해서는 다음의 글에 자세히 나옵니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > An Analysis of the Windows PE Checksum Algorithm ; <a target='tab' href='https://www.codeproject.com/Articles/19326/An-Analysis-of-the-Windows-PE-Checksum-Algorithm'>https://www.codeproject.com/Articles/19326/An-Analysis-of-the-Windows-PE-Checksum-Algorithm</a> </pre> <br /> 게다가 위의 글에 보면 PEChecksum.exe라는 도구를 공개해 직접 계산된 값을 설정할 수 있습니다. 그렇다면 C#으로도 한번 만들어 볼까요? ^^ 다행히 코드도 다음의 링크에서 쉽게 구했습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > portable-executable-library/pe_lib/pe_checksum.cpp ; <a target='tab' href='https://github.com/mrexodia/portable-executable-library/blob/master/pe_lib/pe_checksum.cpp#L43'>https://github.com/mrexodia/portable-executable-library/blob/master/pe_lib/pe_checksum.cpp#L43</a> </pre> <br /> 이와 함께 <a target='tab' href='http://www.sysnet.pe.kr/2/0/11237'>지난번</a>에 다뤘던 Workshell.PE 라이브러리를 NuGet으로부터 받아,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Install-Package Workshell.PE -Version 1.7.0 </pre> <br /> 다음과 같이 간단하게 만들어 줄 수 있습니다.<br /> <br /> <pre style='height: 400px; margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // <a target='tab' href='https://github.com/stjeong/SetPEChecksum'>https://github.com/stjeong/SetPEChecksum</a> using System; using System.IO; using Workshell.PE; namespace SetPEChecksum { class Program { // <a target='tab' href='https://github.com/mrexodia/portable-executable-library/blob/master/pe_lib/pe_checksum.cpp#L43'>https://github.com/mrexodia/portable-executable-library/blob/master/pe_lib/pe_checksum.cpp#L43</a> static int Main(string[] args) { if (args.Length != 1 && args.Length != 2) { Console.WriteLine("[options] file_path"); Console.WriteLine("/s - calc & set checksum"); return 1; } bool setNewChecksum = false; string filePath = null; if (args[0] == "/s" || args[0] == "-s") { setNewChecksum = true; filePath = args[1]; } else { filePath = args[0]; } uint currentCheckSum = 0; uint newCheckSum = 0; uint checkSumPos = 0; using (Workshell.PE.ExecutableImage pe = Workshell.PE.ExecutableImage.FromFile(filePath)) { currentCheckSum = pe.NTHeaders.OptionalHeader.CheckSum; Console.WriteLine($"Current Checksum: {currentCheckSum}(0x{currentCheckSum.ToString("x")})"); newCheckSum = CalcChecksum(pe, out checkSumPos); Console.WriteLine($"New Checksum: {newCheckSum}(0x{newCheckSum.ToString("x")})"); } if (setNewChecksum == true && (currentCheckSum != newCheckSum)) { byte[] contents = File.ReadAllBytes(filePath); byte[] newCheckSumBuffer = BitConverter.GetBytes(newCheckSum); Array.Copy(newCheckSumBuffer, 0, contents, checkSumPos, 4); try { File.WriteAllBytes(filePath, contents); } catch (System.IO.IOException) { string newFilePath = filePath + ".new"; Console.WriteLine($"New file({newFilePath}) created because it is being used by another process."); File.WriteAllBytes(newFilePath, contents); } } return 0; } private static uint CalcChecksum(ExecutableImage pe, out uint checkSumPos) { const uint checksum_pos_in_optional_headers = 64; checkSumPos = (uint)pe.NTHeaders.OptionalHeader.Location.FileOffset + checksum_pos_in_optional_headers; uint fileSize = (uint)pe.GetBytes().Length; MemoryStream ms = new MemoryStream(pe.GetBytes()); byte[] bytes4 = new byte[4]; int pos = 0; ulong calcSum = 0; ulong top = (ulong)0xFFFFFFFF + 1; while (ms.Read(bytes4, pos, 4) == 4) { uint dw = BitConverter.ToUInt32(bytes4, 0); if (ms.Position == checkSumPos + 4) { continue; } calcSum = (calcSum & 0xFFFFFFFF) + dw + (calcSum >> 32); if (calcSum > top) { calcSum = (calcSum & 0xFFFFFFFF) + (calcSum >> 32); } } calcSum = (calcSum & 0xffff) + (calcSum >> 16); calcSum = (calcSum) + (calcSum >> 16); calcSum = calcSum & 0xffff; calcSum += (uint)fileSize; return (uint)calcSum; } } } </pre> <br /> 가장 쉬운 사용법은, 빌드한 SetPEChecksum을 여러분들의 프로젝트 설정의 "Build Events" / "Post-build event command line"에 다음과 같이 등록하는 것입니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > ...[path]...\SetPEChecksum.exe /s "$(TargetPath)" </pre> <br /> 그럼 빌드할 때마다 출력 창에 다음과 같은 메시지를 볼 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 2> Current Checksum: 0(0x0) 2> New Checksum: 19088(0x4a90) </pre> <br /> <hr style='width: 50%' /><br /> <br /> 이렇게 checksum 파일이 설정된 모듈을 windbg에서 사용하게 되면, 다음과 같이 아주 깔끔한 출력 결과를 볼 수 있습니다.<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'>!clrstack</span> OS Thread Id: 0x4370 (0) Child SP IP Call Site 0000003e4f0fea30 00007ffec2dc04c1 SetPEChecksum.Program.Main(System.String[]) [E:\ConsoleApp1\Program.cs @ 15] 0000003e4f0fedb0 00007fff223c6793 [GCFrame: 0000003e4f0fedb0] </pre> <br /> (<a target='tab' href='https://github.com/stjeong/SetPEChecksum'>이 글의 전체 코드는 github에 실려 있습니다.</a>)<br /> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
1629
(왼쪽의 숫자를 입력해야 합니다.)