Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
3.8 KiB

  1. #include "stdafx.h"
  2. #include <io.h>
  3. #include "vchk.h"
  4. #include "ilimpchk.h"
  5. #include "pefile.h"
  6. #define NAMES_LIST_BUFSIZE (1024)
  7. #define TMP_BUFFERS (1024)
  8. char ImportSectionNames [NAMES_LIST_BUFSIZE];
  9. // char AllowedImportDLLsNames [NAMES_LIST_BUFSIZE];
  10. // extern AllowedAndIllegals allowx;
  11. Names Modules = { NULL, 0 };
  12. LPSTR FoundSectionName = NULL;
  13. LPVOID FileData = NULL;
  14. BOOL
  15. InitIllegalImportsSearch (LPCSTR FileName, LPCSTR SectionNames/*, AllowedAndIllegals& allowx*/)
  16. /*
  17. SectionNames - possible names of import sections, despite .idata
  18. AllowedImportDLLs - DLLs, which are allowed to be among importers
  19. */
  20. {
  21. int ofs = 0;
  22. int fno = 0;
  23. size_t flen = 0;
  24. FILE* fp = fopen (FileName,"rb");
  25. if (!fp)
  26. return FALSE;
  27. fno = _fileno( fp );
  28. flen = (size_t)_filelength (fno);
  29. if (flen<=0)
  30. return FALSE;
  31. if (FileData)
  32. free (FileData);
  33. FileData = malloc (flen);
  34. if (!FileData)
  35. return FALSE;
  36. if (fread (FileData, 1, flen, fp) != flen)
  37. return FALSE;
  38. fclose (fp);
  39. ofs = 0;
  40. if (SectionNames) {
  41. LPCSTR SecName;
  42. for (SecName = SectionNames; *SecName; SecName++, ofs++) {
  43. strcpy (ImportSectionNames+ofs, SecName);
  44. ofs += strlen (SecName);
  45. SecName += strlen (SecName);
  46. }
  47. *(ImportSectionNames+ofs) = 0;
  48. }
  49. else {
  50. ImportSectionNames[0] = 0;
  51. ImportSectionNames[1] = 0;
  52. }
  53. ofs=0;
  54. /*
  55. if (AllowedImportDLLs) {
  56. LPCSTR DllName;
  57. for (DllName = AllowedImportDLLs; *DllName; DllName++, ofs++) {
  58. strcpy (AllowedImportDLLsNames+ofs, DllName);
  59. ofs += strlen (DllName);
  60. DllName += strlen (DllName);
  61. }
  62. *(AllowedImportDLLsNames+ofs) = 0;
  63. }
  64. else {
  65. AllowedImportDLLsNames[0] = 0;
  66. AllowedImportDLLsNames[1] = 0;
  67. }
  68. */
  69. Modules.Ptr = NULL;
  70. Modules.Num = 0;
  71. return TRUE;
  72. }
  73. LPSTR GetNextName(LPSTR NamePtr)
  74. {
  75. if (!NamePtr || !*NamePtr)
  76. return NULL;
  77. NamePtr += (strlen (NamePtr) + 1);
  78. if (*NamePtr)
  79. return NamePtr;
  80. return NULL;
  81. }
  82. /*
  83. BOOL
  84. IsAllowedModuleName (LPCSTR ModuleName)
  85. {
  86. LPCSTR AllowedName;
  87. for (AllowedName = AllowedImportDLLsNames;
  88. *AllowedName;
  89. AllowedName += (strlen(AllowedName)+1)) {
  90. if (!strcmp (AllowedName, ModuleName))
  91. return TRUE;
  92. }
  93. return FALSE;
  94. }
  95. */
  96. void
  97. FreeName (Names name)
  98. {
  99. HeapFree (GetProcessHeap(), 0, name.Ptr);
  100. }
  101. Names
  102. CheckSectionsForImports (void)
  103. /*
  104. Returns buffer with the names of import sections.
  105. This memory is been freed during FinilizeIllegalImportsSearch,
  106. one need not free it manually.
  107. */
  108. {
  109. char* SectionName;
  110. Modules.Num = GetImportModuleNames (FileData, ".idata", &Modules.Ptr);
  111. if (Modules.Num <= 0) {
  112. for (SectionName = ImportSectionNames; *SectionName; SectionName++) {
  113. Modules.Num = GetImportModuleNames (FileData, SectionName, &Modules.Ptr);
  114. if (Modules.Num > 0) {
  115. FoundSectionName = SectionName;
  116. break;
  117. }
  118. SectionName += strlen (SectionName);
  119. }
  120. }
  121. return Modules;
  122. }
  123. Names GetImportsList (LPCSTR ModuleName)
  124. /*
  125. Returns buffer with the names of import functions for ModuleName.
  126. This memory is been freed during FinilizeIllegalImportsSearch,
  127. one need not free it manually.
  128. */
  129. {
  130. Names Imports = {NULL, 0};
  131. Imports.Num = GetImportFunctionNamesByModule (FileData,
  132. FoundSectionName,
  133. (char*)ModuleName,
  134. &Imports.Ptr);
  135. return Imports;
  136. }
  137. void
  138. FinilizeIllegalImportsSearch (void)
  139. /*
  140. Frees temporarily allocated memory.
  141. */
  142. {
  143. }