|
| 1 | +import re |
| 2 | +import sys |
| 3 | +import os |
| 4 | +from binaryninja import sharedcache, load |
| 5 | + |
| 6 | + |
| 7 | +# This is some apple map file thingy, you will know if you have one because you will have a .map file. |
| 8 | +def parse_map_file(map_file_path): |
| 9 | + mappings = [] |
| 10 | + libraries = [] |
| 11 | + |
| 12 | + with open(map_file_path, 'r') as file: |
| 13 | + lines = file.readlines() |
| 14 | + |
| 15 | + mapping_pattern = re.compile( |
| 16 | + r"mapping\s+(?P<type>[A-Z]+)\s+(?P<size>[\dMKB]+)\s+0x(?P<start>[a-fA-F0-9]+)\s+->\s+0x(?P<end>[a-fA-F0-9]+)" |
| 17 | + ) |
| 18 | + library_pattern = re.compile( |
| 19 | + r"^(?P<library>.+)$" |
| 20 | + ) |
| 21 | + section_pattern = re.compile( |
| 22 | + r"\s+(?P<section>[^\s]+)\s+0x(?P<start>[a-fA-F0-9]+)\s+->\s+0x[a-fA-F0-9]+" |
| 23 | + ) |
| 24 | + |
| 25 | + current_library = None |
| 26 | + |
| 27 | + for line in lines: |
| 28 | + # Check for a region mapping line |
| 29 | + mapping_match = mapping_pattern.match(line) |
| 30 | + if mapping_match: |
| 31 | + mappings.append({ |
| 32 | + "type": mapping_match.group("type"), |
| 33 | + "size": mapping_match.group("size"), |
| 34 | + "start": int(mapping_match.group("start"), 16) |
| 35 | + }) |
| 36 | + continue |
| 37 | + |
| 38 | + # Check for a section line within a library |
| 39 | + section_match = section_pattern.match(line) |
| 40 | + if section_match and current_library is not None: |
| 41 | + current_library["sections"].append({ |
| 42 | + "section": section_match.group("section"), |
| 43 | + "start": int(section_match.group("start"), 16) |
| 44 | + }) |
| 45 | + continue |
| 46 | + |
| 47 | + # Check for a library name line |
| 48 | + library_match = library_pattern.match(line) |
| 49 | + if library_match: |
| 50 | + current_library = { |
| 51 | + "name": library_match.group("library"), |
| 52 | + "sections": [] |
| 53 | + } |
| 54 | + libraries.append(current_library) |
| 55 | + continue |
| 56 | + |
| 57 | + return mappings, libraries |
| 58 | + |
| 59 | + |
| 60 | +def main(): |
| 61 | + if len(sys.argv) < 2: |
| 62 | + print("Please provide a shared cache binary path to validate. There must be an adjacent .map file.") |
| 63 | + sys.exit(1) |
| 64 | + |
| 65 | + binary_path = sys.argv[1] |
| 66 | + bv = load(binary_path) |
| 67 | + assert bv is not None, f"Failed to create BinaryView for {str(binary_path)}" |
| 68 | + controller = sharedcache.SharedCacheController(bv) |
| 69 | + assert controller.is_valid |
| 70 | + |
| 71 | + map_file_path = bv.file.filename + ".map" |
| 72 | + if not os.path.exists(map_file_path): |
| 73 | + print(f"Error: Map file does not exist at path: {map_file_path}") |
| 74 | + sys.exit(1) |
| 75 | + |
| 76 | + mappings, map_images = parse_map_file(map_file_path) |
| 77 | + |
| 78 | + # Validate images and sections |
| 79 | + for map_image in map_images: |
| 80 | + image = controller.get_image_with_name(map_image["name"]) |
| 81 | + if not image: |
| 82 | + raise ValueError(f"Image not found: {map_image['name']}") |
| 83 | + print(f"Checking image... {image.name}") |
| 84 | + |
| 85 | + for section in map_image["sections"]: |
| 86 | + if not section["start"] in image.region_starts: |
| 87 | + raise ValueError( |
| 88 | + f"Section not found in image '{image.name}': {section['start']} -> {image.region_starts}") |
| 89 | + |
| 90 | + print("Validation successful!") |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + main() |
0 commit comments