Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. import argparse
  2.  
  3.  
  4. def alignmentUp(size, alignment):
  5. if size % alignment != 0:
  6. size += alignment
  7. return size
  8.  
  9.  
  10. def rva2Offset(sections, alignment, rva):
  11. for section in sections:
  12. begin = section['virtualAddress']
  13. end = begin + alignmentUp(section['virtualSize'], alignment)
  14. if begin < rva < end:
  15. return rva - begin + section['pointerToRawData']
  16.  
  17. return 0
  18.  
  19. def va2Offset(sections, imageBase, alignment, va):
  20. return rva2Offset(sections, alignment, va - imageBase)
  21.  
  22.  
  23.  
  24.  
  25. def main():
  26. p = argparse.ArgumentParser(description='PE format parser')
  27. p.add_argument('filename')
  28.  
  29. args = p.parse_args()
  30.  
  31. with open(args.filename, 'rb') as f:
  32.  
  33. assert f.read(2) == b'MZ'
  34. f.seek(60)
  35. lfanew = int.from_bytes(f.read(4), byteorder='little')
  36. f.seek(lfanew)
  37.  
  38. # IMAGE_NT_HEADERS
  39. assert f.read(4) == b'PE\0\0'
  40.  
  41. #skip IMAGE_FILE_HEADER
  42. f.seek(f.tell() + 20)
  43.  
  44. f.seek(f.tell() + 2)
  45. numberOfSections = int.from_bytes(f.read(2), byteorder='little')
  46. f.seek(f.tell() - 4)
  47.  
  48.  
  49.  
  50. #IMAGE_OPTIONAL_HEADER32
  51. assert f.read(2) == b'\x0b\x01'
  52. f.seek(f.tell() - 2)
  53. f.seek(f.tell() + 0x1c)
  54. imageBase = int.from_bytes(f.read(4), byteorder='little')
  55. sectionAlignment = int.from_bytes(f.read(4), byteorder='little')
  56. f.seek(f.tell() - 0x24)
  57. f.seek(f.tell() + 96)
  58. exportAddress = int.from_bytes(f.read(4), byteorder='little')
  59. exportSize = int.from_bytes(f.read(4), byteorder='little')
  60. print("virtual address {}".format(hex(exportAddress)))
  61. print("size {}".format(hex(exportSize)))
  62. print("numberOfSections {}".format(numberOfSections))
  63. print("imageBase {}".format(hex(imageBase)))
  64. print("sectionAlignment {}".format(sectionAlignment))
  65. f.seek(f.tell() - 8)
  66. f.seek(f.tell() + 8*16)
  67.  
  68.  
  69. sections = []
  70.  
  71. for idx in range(0, numberOfSections):
  72. name = f.read(8).decode()
  73. virtualSize = int.from_bytes(f.read(4), byteorder='little')
  74. virtualAddress = int.from_bytes(f.read(4), byteorder='little')
  75. sizeOfRawData = int.from_bytes(f.read(4), byteorder='little')
  76. pointerToRawData = int.from_bytes(f.read(4), byteorder='little')
  77. pointerToRelocations = int.from_bytes(f.read(4), byteorder='little')
  78. pointerToLinenumbers = int.from_bytes(f.read(4), byteorder='little')
  79. numberOfRelocations = int.from_bytes(f.read(2), byteorder='little')
  80. numberOfLinenumbers = int.from_bytes(f.read(2), byteorder='little')
  81. characteristics = int.from_bytes(f.read(4), byteorder='little')
  82. sections.append({
  83. 'name': name,
  84. 'virtualSize': virtualSize,
  85. 'virtualAddress': virtualAddress,
  86. 'sizeOfRawData': sizeOfRawData,
  87. 'pointerToRawData': pointerToRawData,
  88. 'pointerToRelocations': pointerToRelocations,
  89. 'pointerToLinenumbers': pointerToLinenumbers,
  90. 'numberOfRelocations': numberOfRelocations,
  91. 'numberOfLinenumbers': numberOfLinenumbers,
  92. 'characteristics': characteristics
  93. })
  94.  
  95. print("sections: {}".format(sections))
  96.  
  97.  
  98. f.seek(rva2Offset(sections, sectionAlignment, exportAddress))
  99.  
  100. f.seek(f.tell() + 0x14)
  101. numberOfFunctions = int.from_bytes(f.read(4), byteorder='little')
  102. numberOfName = int.from_bytes(f.read(4), byteorder='little')
  103. addressOfFunctions = int.from_bytes(f.read(4), byteorder='little')
  104. addressOfNames = int.from_bytes(f.read(4), byteorder='little')
  105. addressOfNameOrdinals = int.from_bytes(f.read(4), byteorder='little')
  106. print("numberOfFunctions {}".format(numberOfFunctions))
  107. print("numberOfName {}".format(numberOfName))
  108. print("addressOfFunctions {}".format(hex(addressOfFunctions)))
  109. print("addressOfNames {}".format(hex(addressOfNames)))
  110. print("addressOfNameOrdinals {}".format(hex(addressOfNameOrdinals)))
  111.  
  112. def readString():
  113.  
  114. str = ''
  115. ch = f.read(1)
  116. while ch != b'\x00':
  117. str += ch.decode()
  118. ch = f.read(1)
  119.  
  120. return str
  121.  
  122. for i in range(0, numberOfFunctions):
  123. f.seek(rva2Offset(sections, sectionAlignment, addressOfFunctions) + 0x4 * i)
  124. functionAddress = f.read(4).hex()
  125. f.seek(rva2Offset(sections, sectionAlignment, addressOfNameOrdinals) + 0x2 * i)
  126. functionOrdinal = f.read(2).hex()
  127. f.seek(rva2Offset(sections, sectionAlignment, addressOfNames) + 0x4 * i)
  128. functionNameAddress = int.from_bytes(f.read(4), byteorder='little')
  129. f.seek(rva2Offset(sections, sectionAlignment, functionNameAddress))
  130. functionName = readString()
  131.  
  132. print("0x{} 0x{} {}".format(functionAddress, functionOrdinal, functionName))
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149. if __name__ == '__main__':
  150. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement