📄 fipstest_8cpp-source.html
字号:
<a name="l00277"></a>00277 <span class="preprocessor"></span> HMODULE h;<a name="l00278"></a>00278 {<a name="l00279"></a>00279 <span class="keywordtype">char</span> moduleFilenameBuf[MAX_PATH] = <span class="stringliteral">""</span>;<a name="l00280"></a>00280 <span class="keywordflow">if</span> (moduleFilename == NULL)<a name="l00281"></a>00281 {<a name="l00282"></a>00282 <span class="preprocessor">#if (defined(_MSC_VER) && _MSC_VER >= 1400) // ifstream doesn't support wide filename on other compilers</span><a name="l00283"></a>00283 <span class="preprocessor"></span> <span class="keywordtype">wchar_t</span> wideModuleFilename[MAX_PATH];<a name="l00284"></a>00284 <span class="keywordflow">if</span> (GetModuleFileNameW(s_hModule, wideModuleFilename, MAX_PATH) > 0)<a name="l00285"></a>00285 {<a name="l00286"></a>00286 moduleStream.open(wideModuleFilename, std::ios::in | std::ios::binary);<a name="l00287"></a>00287 h = GetModuleHandleW(wideModuleFilename);<a name="l00288"></a>00288 }<a name="l00289"></a>00289 <span class="keywordflow">else</span><a name="l00290"></a>00290 <span class="preprocessor">#endif</span><a name="l00291"></a>00291 <span class="preprocessor"></span> {<a name="l00292"></a>00292 GetModuleFileNameA(s_hModule, moduleFilenameBuf, MAX_PATH);<a name="l00293"></a>00293 moduleFilename = moduleFilenameBuf;<a name="l00294"></a>00294 }<a name="l00295"></a>00295 }<a name="l00296"></a>00296 <span class="preprocessor">#endif</span><a name="l00297"></a>00297 <span class="preprocessor"></span> <span class="keywordflow">if</span> (moduleFilename != NULL)<a name="l00298"></a>00298 {<a name="l00299"></a>00299 moduleStream.open(moduleFilename, std::ios::in | std::ios::binary);<a name="l00300"></a>00300 <span class="preprocessor">#ifdef CRYPTOPP_WIN32_AVAILABLE</span><a name="l00301"></a>00301 <span class="preprocessor"></span> h = GetModuleHandleA(moduleFilename);<a name="l00302"></a>00302 moduleFilename = NULL;<a name="l00303"></a>00303 }<a name="l00304"></a>00304 <span class="preprocessor">#endif</span><a name="l00305"></a>00305 <span class="preprocessor"></span> }<a name="l00306"></a>00306 <a name="l00307"></a>00307 <span class="keywordflow">if</span> (!moduleStream)<a name="l00308"></a>00308 {<a name="l00309"></a>00309 <span class="preprocessor">#ifdef CRYPTOPP_WIN32_AVAILABLE</span><a name="l00310"></a>00310 <span class="preprocessor"></span> OutputDebugString(<span class="stringliteral">"Crypto++ DLL integrity check failed. Cannot open file for reading."</span>);<a name="l00311"></a>00311 <span class="preprocessor">#endif</span><a name="l00312"></a>00312 <span class="preprocessor"></span> <span class="keywordflow">return</span> <span class="keyword">false</span>;<a name="l00313"></a>00313 }<a name="l00314"></a>00314 <a class="code" href="class_file_store.html" title="file-based implementation of Store interface">FileStore</a> file(moduleStream);<a name="l00315"></a>00315 <a name="l00316"></a>00316 <span class="preprocessor">#ifdef CRYPTOPP_WIN32_AVAILABLE</span><a name="l00317"></a>00317 <span class="preprocessor"></span> <span class="comment">// try to hash from memory first</span><a name="l00318"></a>00318 <span class="keyword">const</span> byte *memBase = (<span class="keyword">const</span> byte *)h;<a name="l00319"></a>00319 <span class="keyword">const</span> IMAGE_DOS_HEADER *ph = (IMAGE_DOS_HEADER *)memBase;<a name="l00320"></a>00320 <span class="keyword">const</span> IMAGE_NT_HEADERS *phnt = (IMAGE_NT_HEADERS *)(memBase + ph->e_lfanew);<a name="l00321"></a>00321 <span class="keyword">const</span> IMAGE_SECTION_HEADER *phs = IMAGE_FIRST_SECTION(phnt);<a name="l00322"></a>00322 DWORD nSections = phnt->FileHeader.NumberOfSections;<a name="l00323"></a>00323 <span class="keywordtype">size_t</span> currentFilePos = 0;<a name="l00324"></a>00324 <a name="l00325"></a>00325 <span class="keywordtype">size_t</span> checksumPos = (byte *)&phnt->OptionalHeader.CheckSum - memBase;<a name="l00326"></a>00326 <span class="keywordtype">size_t</span> checksumSize = <span class="keyword">sizeof</span>(phnt->OptionalHeader.CheckSum);<a name="l00327"></a>00327 <span class="keywordtype">size_t</span> certificateTableDirectoryPos = (byte *)&phnt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY] - memBase;<a name="l00328"></a>00328 <span class="keywordtype">size_t</span> certificateTableDirectorySize = <span class="keyword">sizeof</span>(phnt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY]);<a name="l00329"></a>00329 <span class="keywordtype">size_t</span> certificateTablePos = phnt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY].VirtualAddress;<a name="l00330"></a>00330 <span class="keywordtype">size_t</span> certificateTableSize = phnt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY].Size;<a name="l00331"></a>00331 <a name="l00332"></a>00332 verifier.AddRangeToSkip(0, checksumPos, checksumSize);<a name="l00333"></a>00333 verifier.AddRangeToSkip(0, certificateTableDirectoryPos, certificateTableDirectorySize);<a name="l00334"></a>00334 verifier.AddRangeToSkip(0, certificateTablePos, certificateTableSize);<a name="l00335"></a>00335 <a name="l00336"></a>00336 <span class="keywordflow">while</span> (nSections--)<a name="l00337"></a>00337 {<a name="l00338"></a>00338 <span class="keywordflow">switch</span> (phs->Characteristics)<a name="l00339"></a>00339 {<a name="l00340"></a>00340 <span class="keywordflow">default</span>:<a name="l00341"></a>00341 <span class="keywordflow">break</span>;<a name="l00342"></a>00342 <span class="keywordflow">case</span> IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ:<a name="l00343"></a>00343 <span class="keywordflow">case</span> IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ:<a name="l00344"></a>00344 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> sectionSize = STDMIN(phs->SizeOfRawData, phs->Misc.VirtualSize);<a name="l00345"></a>00345 <span class="keyword">const</span> byte *sectionMemStart = memBase + phs->VirtualAddress;<a name="l00346"></a>00346 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> sectionFileStart = phs->PointerToRawData;<a name="l00347"></a>00347 <span class="keywordtype">size_t</span> subSectionStart = 0, nextSubSectionStart;<a name="l00348"></a>00348 <a name="l00349"></a>00349 <span class="keywordflow">do</span><a name="l00350"></a>00350 {<a name="l00351"></a>00351 <span class="keyword">const</span> byte *subSectionMemStart = sectionMemStart + subSectionStart;<a name="l00352"></a>00352 <span class="keywordtype">size_t</span> subSectionFileStart = sectionFileStart + subSectionStart;<a name="l00353"></a>00353 <span class="keywordtype">size_t</span> subSectionSize = sectionSize - subSectionStart;<a name="l00354"></a>00354 nextSubSectionStart = 0;<a name="l00355"></a>00355 <a name="l00356"></a>00356 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> entriesToReadFromDisk[] = {IMAGE_DIRECTORY_ENTRY_IMPORT, IMAGE_DIRECTORY_ENTRY_IAT};<a name="l00357"></a>00357 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i=0; i<<span class="keyword">sizeof</span>(entriesToReadFromDisk)/<span class="keyword">sizeof</span>(entriesToReadFromDisk[0]); i++)<a name="l00358"></a>00358 {<a name="l00359"></a>00359 <span class="keyword">const</span> IMAGE_DATA_DIRECTORY &entry = phnt->OptionalHeader.DataDirectory[entriesToReadFromDisk[i]];<a name="l00360"></a>00360 <span class="keyword">const</span> byte *entryMemStart = memBase + entry.VirtualAddress;<a name="l00361"></a>00361 if (subSectionMemStart <= entryMemStart && entryMemStart < subSectionMemStart + subSectionSize)<a name="l00362"></a>00362 {<a name="l00363"></a>00363 subSectionSize = entryMemStart - subSectionMemStart;<a name="l00364"></a>00364 nextSubSectionStart = entryMemStart - sectionMemStart + entry.Size;<a name="l00365"></a>00365 }<a name="l00366"></a>00366 }<a name="l00367"></a>00367 <a name="l00368"></a>00368 <span class="preprocessor">#if defined(_MSC_VER) && _MSC_VER >= 1400</span><a name="l00369"></a>00369 <span class="preprocessor"></span> <span class="comment">// first byte of _CRT_DEBUGGER_HOOK gets modified in memory by the debugger invisibly, so read it from file</span><a name="l00370"></a>00370 <span class="keywordflow">if</span> (IsDebuggerPresent())<a name="l00371"></a>00371 {<a name="l00372"></a>00372 <span class="keywordflow">if</span> (subSectionMemStart <= (byte *)&_CRT_DEBUGGER_HOOK && (byte *)&_CRT_DEBUGGER_HOOK < subSectionMemStart + subSectionSize)<a name="l00373"></a>00373 {<a name="l00374"></a>00374 subSectionSize = (byte *)&_CRT_DEBUGGER_HOOK - subSectionMemStart;<a name="l00375"></a>00375 nextSubSectionStart = (byte *)&_CRT_DEBUGGER_HOOK - sectionMemStart + 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -