├── .gitignore ├── LICENSE ├── Module.manifest ├── Readme.md ├── bin └── .gitignore ├── build.gradle ├── data └── README.txt ├── extension.properties ├── ghidra_scripts └── README.txt ├── lib └── README.txt ├── os ├── linux_x86_64 │ └── README.txt ├── mac_x86_64 │ └── README.txt └── win_x86_64 │ └── README.txt └── src └── main ├── help └── help │ ├── TOC_Source.xml │ └── topics │ └── macossupport │ └── help.html ├── java └── macossupport │ ├── MacOSSupportAnalyzer.java │ └── MacOSSupportLoader.java └── resources └── images └── README.txt /.gitignore: -------------------------------------------------------------------------------- 1 | /dist/ 2 | .gradle/ 3 | .classpath 4 | .settings/ 5 | .project 6 | .DS_Store 7 | .vscode/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /Module.manifest: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nmggithub/macOSSupport/5a69bdb04418d2b20ba9aa15feaa728f31b99811/Module.manifest -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # Archived. 2 | 3 | I have archived this repository in favor of working on the [GhidraApple](https://github.com/ReverseApple/GhidraApple) extension. I may revive this at some point, but right now I have already ported over my Universal Binary importing code to that plugin (and they already have a (better) handler for `objc_msgSend` stubs). 4 | 5 | # macOS Support (Ghidra Extension) 6 | 7 | This is a Ghidra extension that I wrote to better support macOS security research. I use it for my own purposes, but I thought I'd also put it out here in case anyone else finds it useful. 8 | 9 | ## What does the extension do? 10 | 11 | Currently the extension has two parts: 12 | 13 | 1. A Loader extension that gives loaded Mach-O binaries more friendly names in the project files (and in the Code Editor). Without it, the contents of universal Mach-O binaries will appear in the project directory named only after the architecture and CPU information. This extension will prefix those names with the parent binary name. The program name itself (used to name things such as the Data Type archive in CodeBrowser, among other things), is also similarly changed. **_Note that this is automatic, and not configurable._** 14 | 2. An analysis pass that renames functions that only call `objc_msgSend` after the selector used (in addition to a user-supplied prefix). Many of the ARM programs inside a Mach-O universal binary include functions which only call `objc_msgSend`, essentially "stubbing out" these calls. To make navigating through the code in CodeBrowser easier, this extension includes an analyzer called "Rename objc_msgSend stubs" which will rename these functions after the selector used, prefixed with a configurable prefix (by default the prefix is `objc_msgSend$`). ***Note that this method uses a very strict fingerprint-matching function and may not catch all of the "stubs".*** 15 | 16 | ## Why does the extension do these things? 17 | 18 | I wrote this extension to handle two of the things I find myself doing often in Ghidra when working on macOS binaries. This extension helps me work faster and focus more time on reverse engineering instead of renaming things. 19 | 20 | ## Will there be consistent updates? 21 | 22 | Probably not. This repository is really just a dumping ground for my extension and any updates will only be included as I see fit for my uses. 23 | 24 | ## Can I use this extension? 25 | 26 | Of course! Feel free to use this extension however you want. To install it: 27 | 28 | 1. Download a ZIP (*not* the Source code ZIP, but the other one) from [the Releases section](https://github.com/nmggithub/macOSSupport/releases) 29 | 2. Open Ghidra 30 | 3. Go to File -> Install Extensions 31 | 4. Click the green + (plus) icon 32 | 5. Navigate to the ZIP file and select it 33 | 6. Click OK 34 | 7. Close and reopen Ghidra as prompted 35 | 36 | 37 | ## Can I contribute? 38 | 39 | Sure! Just note that this really requires the use of the [Eclipse IDE](https://eclipseide.org). I'm not really going to provide much support on how to use Eclipse for Ghidra extension development, other than to point you to official resources. 40 | 41 | Developing Ghidra extensions on Eclipse requires GhidraDev, and instructions on how to install GhidraDev into Eclipse are located *within* your installation of Ghidra (at lease on v11.2) at: 42 | 43 | ``` 44 | {Ghidra-Installation-Folder}/Extensions/Eclipse/GhidraDev/GhidraDev_README.html#ManualInstall 45 | ``` 46 | 47 | ### Before Running 48 | 49 | Ghidra only supports loading built modules from `bin/main` in the project root. Unfortunately, it seems Eclipse defaults to `bin/default`, which will cause this extension to not be loaded by Ghidra when using the Run Configurations provided GhidraDev. To fix this, simply: 50 | 51 | 1. Right-Click the project in Eclipse to open the context menu. 52 | 2. Hover over the "Build Path" item to open the sub-menu and select "Configure Build Path". 53 | 3. If not already selected, select the Source tab and change the "Default output folder" to: `{project-name}/bin/main`. 54 | 55 | I unfortunately have not found an easy way to distribute this with the project, so (for now) this step has to be done manually. 56 | -------------------------------------------------------------------------------- /bin/.gitignore: -------------------------------------------------------------------------------- 1 | /default/ 2 | /main/ 3 | /scripts/ 4 | /test/ 5 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | // Builds a Ghidra Extension for a given Ghidra installation. 17 | // 18 | // An absolute path to the Ghidra installation directory must be supplied either by setting the 19 | // GHIDRA_INSTALL_DIR environment variable or Gradle project property: 20 | // 21 | // > export GHIDRA_INSTALL_DIR= 22 | // > gradle 23 | // 24 | // or 25 | // 26 | // > gradle -PGHIDRA_INSTALL_DIR= 27 | // 28 | // Gradle should be invoked from the directory of the project to build. Please see the 29 | // application.gradle.version property in /Ghidra/application.properties 30 | // for the correction version of Gradle to use for the Ghidra installation you specify. 31 | 32 | //----------------------START "DO NOT MODIFY" SECTION------------------------------ 33 | def ghidraInstallDir 34 | 35 | if (System.env.GHIDRA_INSTALL_DIR) { 36 | ghidraInstallDir = System.env.GHIDRA_INSTALL_DIR 37 | } 38 | else if (project.hasProperty("GHIDRA_INSTALL_DIR")) { 39 | ghidraInstallDir = project.getProperty("GHIDRA_INSTALL_DIR") 40 | } 41 | else { 42 | ghidraInstallDir = "" 43 | } 44 | 45 | task distributeExtension { 46 | group "Ghidra" 47 | 48 | apply from: new File(ghidraInstallDir).getCanonicalPath() + "/support/buildExtension.gradle" 49 | dependsOn ':buildExtension' 50 | } 51 | //----------------------END "DO NOT MODIFY" SECTION------------------------------- 52 | 53 | repositories { 54 | // Declare dependency repositories here. This is not needed if dependencies are manually 55 | // dropped into the lib/ directory. 56 | // See https://docs.gradle.org/current/userguide/declaring_repositories.html for more info. 57 | // Ex: mavenCentral() 58 | } 59 | 60 | dependencies { 61 | // Any external dependencies added here will automatically be copied to the lib/ directory when 62 | // this extension is built. 63 | } 64 | 65 | // Exclude additional files from the built extension 66 | // Ex: buildExtension.exclude '.idea/**' 67 | -------------------------------------------------------------------------------- /data/README.txt: -------------------------------------------------------------------------------- 1 | The "data" directory is intended to hold data files that will be used by this module and will 2 | not end up in the .jar file, but will be present in the zip or tar file. Typically, data 3 | files are placed here rather than in the resources directory if the user may need to edit them. 4 | 5 | An optional data/languages directory can exist for the purpose of containing various Sleigh language 6 | specification files and importer opinion files. 7 | 8 | The data/buildLanguage.xml is used for building the contents of the data/languages directory. 9 | 10 | The skel language definition has been commented-out within the skel.ldefs file so that the 11 | skeleton language does not show-up within Ghidra. 12 | 13 | See the Sleigh language documentation (docs/languages/index.html) for details Sleigh language 14 | specification syntax. 15 | -------------------------------------------------------------------------------- /extension.properties: -------------------------------------------------------------------------------- 1 | name=@extname@ 2 | description=A set of optimizations to help with reverse-engineering macOS binaries. 3 | author=Noah Gregory 4 | createdOn= 5 | version=@extversion@ 6 | -------------------------------------------------------------------------------- /ghidra_scripts/README.txt: -------------------------------------------------------------------------------- 1 | Java source directory to hold module-specific Ghidra scripts. 2 | -------------------------------------------------------------------------------- /lib/README.txt: -------------------------------------------------------------------------------- 1 | The "lib" directory is intended to hold Jar files which this module is dependent upon. Jar files 2 | may be placed in this directory manually, or automatically by maven via the dependencies block 3 | of this module's build.gradle file. -------------------------------------------------------------------------------- /os/linux_x86_64/README.txt: -------------------------------------------------------------------------------- 1 | The "os/linux_x86_64" directory is intended to hold Linux native binaries 2 | which this module is dependent upon. This directory may be eliminated for a specific 3 | module if native binaries are not provided for the corresponding platform. 4 | -------------------------------------------------------------------------------- /os/mac_x86_64/README.txt: -------------------------------------------------------------------------------- 1 | The "os/mac_x86_64" directory is intended to hold macOS (OS X) native binaries 2 | which this module is dependent upon. This directory may be eliminated for a specific 3 | module if native binaries are not provided for the corresponding platform. 4 | -------------------------------------------------------------------------------- /os/win_x86_64/README.txt: -------------------------------------------------------------------------------- 1 | The "os/win_x86_64" directory is intended to hold MS Windows native binaries (.exe) 2 | which this module is dependent upon. This directory may be eliminated for a specific 3 | module if native binaries are not provided for the corresponding platform. 4 | -------------------------------------------------------------------------------- /src/main/help/help/TOC_Source.xml: -------------------------------------------------------------------------------- 1 | 2 | 49 | 50 | 51 | 52 | 57 | 58 | -------------------------------------------------------------------------------- /src/main/help/help/topics/macossupport/help.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | 9 | 10 | 11 | 12 | macOS Support 13 | 14 | 15 | 16 | 17 |

Help for macOS Support

18 | 19 |

This is a simple extension I wrote to support me in my macOS security research. Maybe it will help you too!

20 | 21 | 22 | -------------------------------------------------------------------------------- /src/main/java/macossupport/MacOSSupportAnalyzer.java: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package macossupport; 17 | 18 | import java.util.Arrays; 19 | import java.util.ArrayList; 20 | 21 | import ghidra.app.services.AbstractAnalyzer; 22 | import ghidra.app.services.AnalyzerType; 23 | import ghidra.app.util.importer.MessageLog; 24 | import ghidra.framework.options.OptionType; 25 | import ghidra.framework.options.Options; 26 | import ghidra.program.model.address.Address; 27 | import ghidra.program.model.address.AddressSetView; 28 | import ghidra.program.model.address.AddressSpace; 29 | import ghidra.program.model.lang.Register; 30 | import ghidra.program.model.listing.CodeUnit; 31 | import ghidra.program.model.listing.Function; 32 | import ghidra.program.model.listing.FunctionManager; 33 | import ghidra.program.model.listing.Instruction; 34 | import ghidra.program.model.listing.Listing; 35 | import ghidra.program.model.listing.Program; 36 | import ghidra.program.model.mem.Memory; 37 | import ghidra.program.model.mem.MemoryAccessException; 38 | import ghidra.program.model.scalar.Scalar; 39 | import ghidra.program.model.symbol.Reference; 40 | import ghidra.program.model.symbol.SourceType; 41 | import ghidra.program.model.symbol.Symbol; 42 | import ghidra.program.model.symbol.SymbolIterator; 43 | import ghidra.program.model.symbol.SymbolTable; 44 | import ghidra.util.exception.CancelledException; 45 | import ghidra.util.exception.DuplicateNameException; 46 | import ghidra.util.exception.InvalidInputException; 47 | import ghidra.util.task.TaskMonitor; 48 | 49 | public class MacOSSupportAnalyzer extends AbstractAnalyzer { 50 | 51 | private static final String ANALYZER_NAME = "Rename objc_msgSend stubs"; 52 | 53 | private static final String FUNCTION_PREFIX_OPTION_NAME = "Function prefix"; 54 | private static final String DEFAULT_FUNCTION_PREFIX = "objc_msgSend$"; 55 | private static final String FUNCTION_PREFIX_DESCRIPTION = "The prefix to append to the selector name before renaming the function after it."; 56 | 57 | private static final String ONLY_RENAME_DEFAULT_NAMED_FUNCTIONS_OPTION_NAME = "Only rename default-named (FUN_*) functions"; 58 | private static final boolean DEFAULT_ONLY_RENAME_DEFAULT_NAMED_FUNCTIONS = true; 59 | private static final String ONLY_RENAME_DEFAULT_NAMED_FUNCTIONS_DESCRIPTION = "Only rename functions that are named with the default prefix (FUN_*)"; 60 | 61 | public MacOSSupportAnalyzer() { 62 | super( 63 | ANALYZER_NAME, 64 | "This will rename any functions that only call `objc_msgSend` after the selector.", 65 | AnalyzerType.FUNCTION_ANALYZER); 66 | this.setSupportsOneTimeAnalysis(true); 67 | } 68 | 69 | @Override 70 | public boolean getDefaultEnablement(Program program) { 71 | return false; 72 | } 73 | 74 | @Override 75 | public boolean canAnalyze(Program program) { 76 | return program.getLanguage().getProcessor().toString().equals("AARCH64") && 77 | program.getExecutableFormat().equals("Mac OS X Mach-O") && 78 | program.getLanguage().isBigEndian() == false; 79 | } 80 | 81 | @Override 82 | public void registerOptions(Options options, Program program) { 83 | options.registerOption(FUNCTION_PREFIX_OPTION_NAME, OptionType.STRING_TYPE, DEFAULT_FUNCTION_PREFIX, null, 84 | FUNCTION_PREFIX_DESCRIPTION); 85 | options.registerOption(ONLY_RENAME_DEFAULT_NAMED_FUNCTIONS_OPTION_NAME, OptionType.BOOLEAN_TYPE, 86 | DEFAULT_ONLY_RENAME_DEFAULT_NAMED_FUNCTIONS, null, 87 | ONLY_RENAME_DEFAULT_NAMED_FUNCTIONS_DESCRIPTION); 88 | } 89 | 90 | private Address rawPointerAddressToActualAddress(long pointerAddress, AddressSpace addressSpace, Memory memory) 91 | throws MemoryAccessException { 92 | Address paramPointerAddress = addressSpace.getAddress(pointerAddress); 93 | byte[] paramPointer = new byte[8]; 94 | memory.getBytes(paramPointerAddress, paramPointer, 0, 8); 95 | long rawActualAddress = 0; 96 | for (int i = 0; i < paramPointer.length; i++) 97 | rawActualAddress |= (((long) paramPointer[i] & 0xFF) << (8 * i)); 98 | Address actualAddress = addressSpace.getAddress(rawActualAddress); 99 | return actualAddress; 100 | } 101 | 102 | // In many ARM64 binaries on macOS, each `objc_msgSend` call is stubbed out into 103 | // its own function. This function attempts to identify these functions and 104 | // return the selector string that is passed to `objc_msgSend`. 105 | private String objcMsgSendSelector( 106 | Function function, ArrayList
objcMsgSendAddresses, 107 | boolean onlyRenameDefaultNamedFunctions) { 108 | long selectorRawPointerAddress = 0; 109 | long objcMsgSendRawPointerAddress = 0; 110 | if (function.getName().startsWith("FUN_") != true && onlyRenameDefaultNamedFunctions) 111 | return null; 112 | if (function.getParameterCount() != 0) 113 | return null; 114 | Listing listing = function.getProgram().getListing(); 115 | Address entryPoint = function.getEntryPoint(); 116 | CodeUnit entryPointCodeUnit = listing.getCodeUnitAt(entryPoint); 117 | if (!(entryPointCodeUnit instanceof Instruction)) 118 | return null; 119 | Instruction instruction = (Instruction) entryPointCodeUnit; 120 | int instructionIndex = 1; 121 | 122 | // In some cases, the function uses one less instruction. We'll call this the 123 | // "simpler version" and modify the detection logic accordingly. 124 | boolean isSimplerVersion = false; 125 | while (instructionIndex <= 6) { 126 | if (instruction == null) 127 | return null; 128 | String mnemonic = instruction.getMnemonicString(); 129 | Object[] inputObjects = instruction.getInputObjects(); 130 | Object[] resultObjects = instruction.getResultObjects(); 131 | switch (instructionIndex) { 132 | // The first instruction loads a constant into x1. 133 | case 1: { 134 | if (!mnemonic.equals("adrp")) 135 | return null; 136 | 137 | if (inputObjects.length != 1) 138 | return null; 139 | if (!(inputObjects[0] instanceof Scalar)) 140 | return null; 141 | if (((Scalar) inputObjects[0]).isSigned() == true) 142 | return null; 143 | selectorRawPointerAddress = ((Scalar) inputObjects[0]).getValue(); 144 | 145 | if (resultObjects.length != 1) 146 | return null; 147 | if (!(resultObjects[0] instanceof Register)) 148 | return null; 149 | if (!((Register) resultObjects[0]).getName().equals("x1")) 150 | return null; 151 | break; 152 | } 153 | 154 | // The second instruction loads the value at the address [x1 + offset] into x1, 155 | // which loads the pointer to the selector string into x1. 156 | case 2: { 157 | if (!mnemonic.equals("ldr")) 158 | return null; 159 | 160 | Register register = (Register) Arrays.stream(inputObjects) 161 | .filter(obj -> obj instanceof Register) 162 | .findFirst() 163 | .orElse(null); 164 | if (register == null || !register.getName().equals("x1")) 165 | return null; 166 | 167 | Scalar scalar = (Scalar) Arrays.stream(inputObjects) 168 | .filter(obj -> obj instanceof Scalar) 169 | .findFirst() 170 | .orElse(null); 171 | 172 | long scalarValue = 0; 173 | 174 | if (scalar != null) { 175 | scalarValue = scalar.getValue(); 176 | } 177 | 178 | selectorRawPointerAddress = selectorRawPointerAddress + scalarValue; 179 | 180 | if (resultObjects.length != 1) 181 | return null; 182 | if (!(resultObjects[0] instanceof Register)) 183 | return null; 184 | if (!((Register) resultObjects[0]).getName().equals("x1")) 185 | return null; 186 | break; 187 | } 188 | 189 | // The third instruction loads a constant into x17 or x16. 190 | case 3: { 191 | if (!mnemonic.equals("adrp")) 192 | return null; 193 | 194 | if (inputObjects.length != 1) 195 | return null; 196 | if (!(inputObjects[0] instanceof Scalar)) 197 | return null; 198 | if (((Scalar) inputObjects[0]).isSigned() == true) 199 | return null; 200 | objcMsgSendRawPointerAddress = ((Scalar) inputObjects[0]).getValue(); 201 | 202 | if (resultObjects.length != 1) 203 | return null; 204 | if (!(resultObjects[0] instanceof Register)) 205 | return null; 206 | String resultRegisterName = ((Register) resultObjects[0]).getName(); 207 | if (!resultRegisterName.equals("x17") && !resultRegisterName.equals("x16")) 208 | return null; 209 | if (resultRegisterName.equals("x16")) { 210 | isSimplerVersion = true; 211 | break; 212 | } 213 | break; 214 | } 215 | 216 | // The fourth instruction adds a constant to x17, which loads a 217 | // pointer to the `objc_msgSend` function into x17. 218 | // 219 | // In simpler functions, this instruction is skipped, and the addition is done 220 | // in the `ldr` instruction. 221 | case 4: { 222 | if (isSimplerVersion) { 223 | break; 224 | } 225 | if (!mnemonic.equals("add")) 226 | return null; 227 | // For some reason, Ghidra seems to include a second scalar with a value of 0 228 | // in the input objects. To cover our bases, we'll sum all the scalar values. 229 | long scalarSum = Arrays.stream(inputObjects) 230 | .filter(obj -> obj instanceof Scalar) 231 | .map(obj -> (Scalar) obj) 232 | .filter(scalar -> !scalar.isSigned()) 233 | .mapToLong(Scalar::getValue) 234 | .sum(); 235 | objcMsgSendRawPointerAddress = objcMsgSendRawPointerAddress + scalarSum; 236 | 237 | Register inputRegister = (Register) Arrays.stream(inputObjects) 238 | .filter(obj -> obj instanceof Register) 239 | .findFirst() 240 | .orElse(null); 241 | if (inputRegister == null || !inputRegister.getName().equals("x17")) 242 | return null; 243 | 244 | // Ghidra includes many temporary registers in the result objects, but we only 245 | // want the actual result register. We'll filter out the temporary register. 246 | Register resultRegister = (Register) Arrays.stream(resultObjects) 247 | .filter(obj -> obj instanceof Register) 248 | .filter(register -> !((Register) register).getName().startsWith("tmp")) 249 | .findFirst() 250 | .orElse(null); 251 | if (resultRegister == null || !resultRegister.getName().equals("x17")) 252 | return null; 253 | 254 | break; 255 | } 256 | 257 | // The fifth instruction loads the value at the address [x17] into x16, 258 | // which loads the address of the `objc_msgSend` function into x16. 259 | // 260 | // In simpler functions, this is the fourth instruction and it operates only on 261 | // x16, performing the addition (skipped above) and load in one step. 262 | case 5: { 263 | if (!mnemonic.equals("ldr")) 264 | return null; 265 | if (isSimplerVersion) { 266 | // The simpler version combines the addition and load into one instruction. 267 | Register inputRegister = (Register) Arrays.stream(inputObjects) 268 | .filter(obj -> obj instanceof Register) 269 | .findFirst() 270 | .orElse(null); 271 | if (!(inputRegister.getName().equals("x16"))) 272 | return null; 273 | long scalarSum = Arrays.stream(inputObjects) 274 | .filter(obj -> obj instanceof Scalar) 275 | .map(obj -> (Scalar) obj) 276 | .filter(scalar -> !scalar.isSigned()) 277 | .mapToLong(Scalar::getValue) 278 | .sum(); 279 | objcMsgSendRawPointerAddress = objcMsgSendRawPointerAddress + scalarSum; 280 | } else { 281 | if (inputObjects.length != 1) 282 | return null; 283 | if (!(inputObjects[0] instanceof Register)) 284 | return null; 285 | if (!(((Register) inputObjects[0]).getName().equals("x17"))) 286 | return null; 287 | } 288 | 289 | if (resultObjects.length != 1) 290 | return null; 291 | if (!(resultObjects[0] instanceof Register)) 292 | return null; 293 | if (!((Register) resultObjects[0]).getName().equals("x16")) 294 | return null; 295 | break; 296 | } 297 | 298 | // The sixth instruction branches to the `objc_msgSend` function, 299 | // using the address in x16. The selector string in x1 is used as 300 | // the second argument to the `objc_msgSend` call. 301 | // 302 | // In simpler functions, this is the fifth instruction, but it is otherwise 303 | // identical (see above for differences). 304 | case 6: { 305 | // While we could be more specific here, as it appears that the actual 306 | // implementations only use specific branch instructions, we'll try to 307 | // capture the many branch instructions in ARM64. 308 | // TODO: Is "starts with 'br'" too broad? 309 | if (!mnemonic.startsWith("br")) 310 | return null; 311 | 312 | if (inputObjects.length != 1) 313 | return null; 314 | if (!(inputObjects[0] instanceof Register)) 315 | return null; 316 | if (!(((Register) inputObjects[0]).getName().equals("x16"))) 317 | return null; 318 | break; 319 | } 320 | default: 321 | return null; 322 | } 323 | 324 | // In cases where the function is using the simpler version, the instruction 325 | // index and the index of the actual instruction we're evaluating may be out 326 | // of sync. While this isn't ideal, it lets us reuse the same logic for both 327 | // the regular and simpler versions. 328 | 329 | boolean isSkippingInstruction4 = isSimplerVersion && instructionIndex == 4; 330 | if (!isSkippingInstruction4) { 331 | // If we're skipping instruction 4, we don't want to get the next instruction as 332 | // we haven't actually evaluated the current instruction. 333 | instruction = instruction.getNext(); 334 | } 335 | instructionIndex++; // We always increment the instruction index, because that's what the case 336 | // statement is based on. TODO: Maybe handle this better? 337 | } 338 | try { 339 | Program program = function.getProgram(); 340 | AddressSpace theSpace = function.getEntryPoint().getAddressSpace(); 341 | Memory memory = program.getMemory(); 342 | Address objcMsgSendActualAddress = rawPointerAddressToActualAddress( 343 | objcMsgSendRawPointerAddress, theSpace, memory); 344 | 345 | // Perform a final sanity check to ensure that x16 is actually pointing to 346 | // `objc_msgSend`, and not some other function. 347 | boolean isPointingToObjcMsgSend = false; 348 | for (Address objcMsgSendAddress : objcMsgSendAddresses) { 349 | if (objcMsgSendAddress.equals(objcMsgSendActualAddress)) 350 | isPointingToObjcMsgSend = true; 351 | } 352 | if (!isPointingToObjcMsgSend) 353 | return null; 354 | 355 | // Parse the selector string from the pointer in x1. 356 | Address selectorActualAddress = rawPointerAddressToActualAddress(selectorRawPointerAddress, theSpace, 357 | memory); 358 | StringBuilder selectorStringBuilder = new StringBuilder(); 359 | byte b; 360 | while (true) { 361 | b = memory.getByte(selectorActualAddress); 362 | if (b == 0) 363 | break; 364 | selectorStringBuilder.append((char) b); 365 | selectorActualAddress = selectorActualAddress.add(1); 366 | } 367 | return selectorStringBuilder.toString(); 368 | } catch (MemoryAccessException e) { 369 | return null; 370 | } 371 | } 372 | 373 | @Override 374 | public boolean added(Program program, AddressSetView set, TaskMonitor monitor, MessageLog log) 375 | throws CancelledException { 376 | 377 | String functionPrefix = program 378 | .getOptions("Analyzers") 379 | .getOptions(ANALYZER_NAME) 380 | .getString(FUNCTION_PREFIX_OPTION_NAME, DEFAULT_FUNCTION_PREFIX); 381 | 382 | boolean onlyRenameDefaultNamedFunctions = program 383 | .getOptions("Analyzers") 384 | .getOptions(ANALYZER_NAME) 385 | .getBoolean(ONLY_RENAME_DEFAULT_NAMED_FUNCTIONS_OPTION_NAME, 386 | DEFAULT_ONLY_RENAME_DEFAULT_NAMED_FUNCTIONS); 387 | 388 | SymbolTable symbolTable = program.getSymbolTable(); 389 | SymbolIterator symbols = symbolTable.getExternalSymbols("_objc_msgSend"); 390 | ArrayList
objcMsgSendAddresses = new ArrayList
(); 391 | for (Symbol symbol : symbols) { 392 | for (Reference ref : symbol.getReferences()) { 393 | objcMsgSendAddresses.add(ref.getFromAddress()); 394 | } 395 | } 396 | 397 | FunctionManager functionManager = program.getFunctionManager(); 398 | for (Function function : functionManager.getFunctionsNoStubs(true)) { 399 | try { 400 | String selectorString = objcMsgSendSelector( 401 | function, objcMsgSendAddresses, 402 | onlyRenameDefaultNamedFunctions); 403 | if (selectorString == null) 404 | continue; 405 | if (selectorString.length() != 0) 406 | function.setName(functionPrefix + selectorString, SourceType.ANALYSIS); 407 | } catch (DuplicateNameException e) { 408 | continue; 409 | } catch (InvalidInputException e) { 410 | continue; 411 | } 412 | } 413 | return true; 414 | } 415 | } 416 | -------------------------------------------------------------------------------- /src/main/java/macossupport/MacOSSupportLoader.java: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package macossupport; 17 | 18 | import java.io.IOException; 19 | import java.util.*; 20 | 21 | import ghidra.app.util.Option; 22 | import ghidra.app.util.bin.ByteProvider; 23 | import ghidra.app.util.importer.MessageLog; 24 | import ghidra.app.util.opinion.Loaded; 25 | import ghidra.framework.model.DomainFolder; 26 | import ghidra.framework.model.Project; 27 | import ghidra.framework.model.ProjectData; 28 | import ghidra.program.model.listing.Program; 29 | import ghidra.util.exception.CancelledException; 30 | import ghidra.util.task.TaskMonitor; 31 | import ghidra.app.util.opinion.MachoLoader; 32 | 33 | public class MacOSSupportLoader extends MachoLoader { 34 | 35 | public String getPreferredFileName(ByteProvider provider) { 36 | String original = super.getPreferredFileName(provider); 37 | String prettyString = provider.getFSRL().toPrettyFullpathString(); 38 | String[] parentAndChild = prettyString.split("\\|"); 39 | String parent = parentAndChild[0]; 40 | if (parent == null) return original; 41 | String[] pathComponents = parent.split("\\/"); 42 | String lastPathComponent = pathComponents[pathComponents.length - 1]; 43 | return lastPathComponent+ "-" + original; 44 | } 45 | 46 | @Override 47 | protected void postLoadProgramFixups(List> loadedPrograms, Project project, 48 | List