├── images ├── 1.png └── 2.png ├── LICENSE ├── plugin.json ├── README.md └── __init__.py /images/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/extremecoders-re/bnpy/HEAD/images/1.png -------------------------------------------------------------------------------- /images/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/extremecoders-re/bnpy/HEAD/images/2.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /plugin.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugin": { 3 | "name": "bnpy", 4 | "type": ["architecture"], 5 | "api": "python2", 6 | "description": "Python 2.7 architecture plugin.", 7 | "longdescription": "This is a Python 2.7 architecture plugin for disassembling raw python bytecode.", 8 | "license": { 9 | "name": "MIT", 10 | "text": "Copyright (c) 2017 extremecoders\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE." 11 | }, 12 | 13 | "version": "1.0 alpha", 14 | "author": "extremecoders", 15 | "minimumBinaryNinjaVersion": { 16 | "dev": "1.0.dev-576", 17 | "release": "9999" 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # bnpy 2 | 3 | A binary ninja architecture plugin to disassemble raw python bytecode. 4 | 5 | ## Installation 6 | 7 | Clone the repo to the Binary ninja plugins directory. 8 | 9 | ```sh 10 | $ git clone https://github.com/extremecoders-re/bnpy.git 11 | ``` 12 | 13 | ## Usage 14 | 15 | Lets say we have a file example.pyc. The first step is to extract raw instruction stream to a file 16 | 17 | ```python 18 | import marshal 19 | 20 | # Open the pyc file 21 | pyc_file = open('example.pyc', 'rb') 22 | 23 | # Skip magic and timestamp 24 | pyc_file.seek(8) 25 | 26 | # Unmarshal 27 | code_object = marshal.load(pyc_file) 28 | code_string = code_object.co_code 29 | 30 | # Dump to file 31 | open('py_code.bin', 'wb').write(code_string) 32 | ``` 33 | 34 | The produced file then be loaded in Binary Ninja. Press `P` to select the architecure as Python. 35 | 36 | This way you can disassemble the root code object. A pyc file however may contain multiple nested code objects embedded in `co_consts`. To dump all of them you can use the following recursive approach. 37 | 38 | ```python 39 | import marshal 40 | import types 41 | 42 | file_num = 0 43 | 44 | def parse(code_object): 45 | global file_num 46 | open('py_code_' + str(file_num) + '.bin', 'wb').write(code_object.co_code) 47 | file_num += 1 48 | 49 | for const in code_object: 50 | if instanceof(const, types.CodeType): 51 | parse(code_object) 52 | 53 | # Open the pyc file 54 | pyc_file = open('example.pyc', 'rb') 55 | 56 | # Skip magic and timestamp 57 | pyc_file.seek(8) 58 | 59 | # Unmarshal 60 | code_object = marshal.load(pyc_file) 61 | parse(code_object) 62 | ``` 63 | 64 | ## TODO 65 | 66 | - Implement a full fledged BinaryView to support disassembling of pyc files. Currently, this is difficult to do due to limitations in the Binary Ninja API (See issues [#133][1], [#551][2], [#728][3]) 67 | - IL of bytecode instructions 68 | - Inline `co_consts`, `co_names` within the disassembly rather than referring them by indices 69 | 70 | [1]: https://github.com/Vector35/binaryninja-api/issues/133 71 | [2]: https://github.com/Vector35/binaryninja-api/issues/551 72 | [3]: https://github.com/Vector35/binaryninja-api/issues/728 73 | 74 | ## Screenshot 75 | 76 | 77 | ### Graph view 78 | 79 | ![](images/1.png) 80 | 81 | ### Linear view 82 | 83 | ![](images/2.png) 84 | 85 | 86 | ## License 87 | 88 | The MIT License -------------------------------------------------------------------------------- /__init__.py: -------------------------------------------------------------------------------- 1 | from binaryninja import * 2 | 3 | import dis 4 | import struct 5 | 6 | 7 | class Python(Architecture): 8 | name = 'Python' 9 | max_instr_length = 3 10 | 11 | # SP register is required, even if we are not going to use it 12 | regs = {'sp': RegisterInfo('sp', 2)} 13 | stack_pointer = 'sp' 14 | 15 | 16 | def perform_get_instruction_info(self, data, addr): 17 | opkode = struct.unpack('= dis.HAVE_ARGUMENT: 69 | arg = struct.unpack('