import_table.inc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright (C) 2012 Zeex
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  4. // this software and associated documentation files (the "Software"), to deal in
  5. // the Software without restriction, including without limitation the rights to
  6. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  7. // of the Software, and to permit persons to whom the Software is furnished to do
  8. // so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in all
  11. // copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. // SOFTWARE.
  20. #include <a_samp>
  21. #include "../phys_memory"
  22. // Helpful resources:
  23. //
  24. // o Peering Inside the PE: A Tour of the Win32 Portable Executable File Format
  25. //
  26. // http://msdn.microsoft.com/en-us/library/ms809762.aspx
  27. //
  28. // o Understanding the Import Address Table
  29. //
  30. // http://sandsprite.com/CodeStuff/Understanding_imports.html
  31. //
  32. // o Microsoft PE and COFF Specification
  33. //
  34. // http://msdn.microsoft.com/en-us/windows/hardware/gg463119.aspx
  35. static stock const DefaultImageBase = 0x00400000;
  36. static stock const SizeOfFileHeader = 0x14;
  37. static stock const SizeOfOptionalHeader = 0xE0;
  38. static stock const SizeOfImportDirectory = 0x14;
  39. stock GetImportPointer(const name[]) {
  40. new DosHeader = DefaultImageBase;
  41. new NtHeaders = DosHeader + ReadDword(DosHeader, 0x3C);
  42. new FileHeader = NtHeaders + 0x04;
  43. new OptionalHeader = FileHeader + SizeOfFileHeader;
  44. new ImageBase = ReadDword(OptionalHeader, 0x1C);
  45. new ImportTableRva = ReadDword(OptionalHeader, 0x68);
  46. new ImportDirectories = ImageBase + ImportTableRva;
  47. for (new i = 0; ; i++) {
  48. new ImportDirectory = ImportDirectories + i * SizeOfImportDirectory;
  49. new Name = ReadDword(ImportDirectory, 0x0C);
  50. if (Name == 0)
  51. break;
  52. new ImportLookupTable = ImageBase + ReadDword(ImportDirectory, 0x00);
  53. new ImportAddressTable = ImageBase + ReadDword(ImportDirectory, 0x10);
  54. for (new j = 0 ; ; j++) {
  55. new NameOrdinal = ReadDword(ImportLookupTable, j * 4);
  56. new bool:NameOrdinalFlag = (NameOrdinal & 0x80000000) != 0;
  57. if (NameOrdinalFlag)
  58. continue;
  59. new ImportByName = NameOrdinal & ~0x80000000;
  60. if (ImportByName == 0)
  61. break;
  62. new iname[256];
  63. ReadString(ImageBase, ImportByName + 2, iname);
  64. if (strcmp(iname, name) == 0) {
  65. return ImportAddressTable + j * 4;
  66. }
  67. }
  68. }
  69. return 0;
  70. }
  71. // Finds a function in the Import Table and returns its address or 0 if found nothing.
  72. stock GetImportAddress(const name[]) {
  73. new ImportPointer = GetImportPointer(name);
  74. if (ImportPointer != 0) {
  75. return ReadDword(ImportPointer, 0);
  76. }
  77. return 0;
  78. }
  79. static stock ToCharString(s[], size = sizeof(s)) {
  80. for (new i = 0; i < size; i++) {
  81. s[i] = swapchars(s[i]);
  82. }
  83. }
  84. static stock ReadDword(base, offset = 0) {
  85. return ReadPhysMemoryCell(base + offset);
  86. }
  87. static stock ReadString(base, offset = 0, dest[], size = sizeof(dest)) {
  88. ReadPhysMemory(base + offset, dest, size);
  89. ToCharString(dest, size);
  90. strunpack(dest, dest, size);
  91. }