|
| 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 | +// This script looks for offcut instruction(s) in the current selection or location and |
| 17 | +// automatically fixes "safe" offcuts. This script is suitable for correcting polyglot instruction |
| 18 | +// executable size optimizations, LOCK prefix issues, and offcut code used for code obfuscation. |
| 19 | +// |
| 20 | +// Offcuts are determined to be safe if they don't have additional conflicting offcuts in the same |
| 21 | +// base instruction. |
| 22 | +// The new instruction length override will be set by assuming there actually is an instruction at |
| 23 | +// the safe offcut reference. If a failure to flow this instruction occurs the script will emit |
| 24 | +// a warning about the exception and continue processing. |
| 25 | +// A check is done for pseudo-disassembly viability before setting the instruction or flowing |
| 26 | +// the code so these exceptions shouldn't be reached. |
| 27 | +// |
| 28 | +// When fixups are applied any existing Error level bookmarks for the Bad Instruction will be |
| 29 | +// removed and replaced with info that an offcut was fixed. These can be interpreted that |
| 30 | +// assumptions were made about the context flowed locally to the fixed instruction that should be |
| 31 | +// taken as fact cautiously since the binary is already confirmed to be well behaved, that is |
| 32 | +// strictly flowed. |
| 33 | +// |
| 34 | +//@category Analysis |
| 35 | + |
| 36 | +import ghidra.app.script.GhidraScript; |
| 37 | +import ghidra.app.script.ScriptMessage; |
| 38 | +import ghidra.app.util.PseudoDisassembler; |
| 39 | +import ghidra.app.util.PseudoInstruction; |
| 40 | +import ghidra.program.disassemble.Disassembler; |
| 41 | +import ghidra.program.model.address.Address; |
| 42 | +import ghidra.program.model.address.AddressSet; |
| 43 | +import ghidra.program.model.lang.*; |
| 44 | +import ghidra.program.model.listing.*; |
| 45 | +import ghidra.program.model.symbol.*; |
| 46 | +import ghidra.program.model.util.CodeUnitInsertionException; |
| 47 | +import ghidra.util.Msg; |
| 48 | + |
| 49 | +public class FixOffcutInstructionScript extends GhidraScript { |
| 50 | + |
| 51 | + public final String INFO_BOOKMARK_CATEGORY = "Offcut Instruction"; |
| 52 | + public final String INFO_BOOKMARK_COMMENT = "Fixed offcut instruction"; |
| 53 | + public final int MAX_OFFCUT_DISTANCE = 64; |
| 54 | + private Listing currentListing; |
| 55 | + private BookmarkManager currentBookmarkManager; |
| 56 | + private ReferenceManager currentReferenceManager; |
| 57 | + private int alignment; |
| 58 | + |
| 59 | + @Override |
| 60 | + protected void run() throws Exception { |
| 61 | + currentListing = currentProgram.getListing(); |
| 62 | + currentBookmarkManager = currentProgram.getBookmarkManager(); |
| 63 | + currentReferenceManager = currentProgram.getReferenceManager(); |
| 64 | + alignment = currentProgram.getLanguage().getInstructionAlignment(); |
| 65 | + // run in strict mode if a selection |
| 66 | + final boolean doExtraValidation = currentSelection != null; |
| 67 | + |
| 68 | + // restrict processing to the current selection |
| 69 | + final AddressSet restrictedSet = |
| 70 | + (currentSelection != null) ? (new AddressSet(currentSelection)) |
| 71 | + : (new AddressSet(currentLocation.getAddress())); |
| 72 | + |
| 73 | + final InstructionIterator instrIter = currentListing.getInstructions(restrictedSet, true); |
| 74 | + |
| 75 | + while (instrIter.hasNext() && !monitor.isCancelled()) { |
| 76 | + final Instruction curInstr = instrIter.next(); |
| 77 | + |
| 78 | + if (curInstr.isLengthOverridden()) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + final Address offcutAddress = getQualifiedOffcutAddress(curInstr, doExtraValidation); |
| 83 | + if (offcutAddress == null) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + // This script is only useful for static offcut instruction fixing. Dynamic offcuts |
| 88 | + // will raise an exception that will be logged here. |
| 89 | + try { |
| 90 | + fixOffcutInstruction(curInstr, offcutAddress); |
| 91 | + } |
| 92 | + catch (Exception e) { |
| 93 | + Msg.error(this, new ScriptMessage("Failed to fix offuct instruction at " + |
| 94 | + curInstr.getAddressString(false, true)), e); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private Address getQualifiedOffcutAddress(final Instruction instr, |
| 100 | + final boolean doExtraValidation) { |
| 101 | + // short-circuit too small instructions |
| 102 | + if (instr.getLength() < 2) { |
| 103 | + return null; |
| 104 | + } |
| 105 | + final Address instrAddr = instr.getAddress(); |
| 106 | + final AddressSet instrBody = |
| 107 | + new AddressSet(instr.getMinAddress().add(1), instr.getMaxAddress()); |
| 108 | + Address offcutAddress = null; |
| 109 | + for (final Address address : currentReferenceManager |
| 110 | + .getReferenceDestinationIterator(instrBody, true)) { |
| 111 | + if ((address.getOffset() % alignment) != 0) { |
| 112 | + continue; |
| 113 | + } |
| 114 | + for (final Reference reference : currentReferenceManager.getReferencesTo(address)) { |
| 115 | + final RefType refType = reference.getReferenceType(); |
| 116 | + if (doExtraValidation && Math.abs( |
| 117 | + instrAddr.subtract(reference.getFromAddress())) > MAX_OFFCUT_DISTANCE) { |
| 118 | + continue; |
| 119 | + } |
| 120 | + if (refType.isJump() && refType.hasFallthrough()) { |
| 121 | + if (offcutAddress == null) { |
| 122 | + offcutAddress = address; |
| 123 | + } |
| 124 | + } |
| 125 | + else { |
| 126 | + continue; |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + return offcutAddress; |
| 131 | + } |
| 132 | + |
| 133 | + private void fixOffcutInstruction(Instruction instr, Address offcutAddress) |
| 134 | + throws CodeUnitInsertionException { |
| 135 | + if (!canDisassembleAt(instr, offcutAddress)) { |
| 136 | + Msg.warn(this, |
| 137 | + new ScriptMessage("\t> Offcut construction would not be valid. Skipping...")); |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + instr.setLengthOverride((int) offcutAddress.subtract(instr.getMinAddress())); |
| 142 | + |
| 143 | + // Once the override is complete there will be code to disassemble. |
| 144 | + disassemble(offcutAddress); |
| 145 | + |
| 146 | + // Usually there will be a bookmark complaining about how there is a well formed instruction |
| 147 | + // already at this location which this change has obsoleted |
| 148 | + fixBookmark(offcutAddress); |
| 149 | + } |
| 150 | + |
| 151 | + private void fixBookmark(Address at) { |
| 152 | + final Bookmark bookmark = currentBookmarkManager.getBookmark(at, BookmarkType.ERROR, |
| 153 | + Disassembler.ERROR_BOOKMARK_CATEGORY); |
| 154 | + if (bookmark != null) { |
| 155 | + currentBookmarkManager.removeBookmark(bookmark); |
| 156 | + |
| 157 | + // inform the user this instruction was fixed. even though the disassembly appears |
| 158 | + // fixed the fact remains that there are two potentially conflicting context flows |
| 159 | + // happening at this instruction and it was assumed that the exposed instruction holds |
| 160 | + // flow attention for execution here due to the direct references. |
| 161 | + // team opted for a simple remark rather repeat this fact about context since |
| 162 | + // this script being applied implies the user understands the potential for conflicts |
| 163 | + currentBookmarkManager.setBookmark(at, BookmarkType.INFO, INFO_BOOKMARK_CATEGORY, |
| 164 | + INFO_BOOKMARK_COMMENT); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + protected boolean canDisassembleAt(Instruction instr, Address at) { |
| 169 | + try { |
| 170 | + // only the instruction prototype is needed to determine if an instruction can exist |
| 171 | + // in the offcut location |
| 172 | + final PseudoDisassembler pdis = new PseudoDisassembler(currentProgram); |
| 173 | + final PseudoInstruction testInstr = pdis.disassemble(at); |
| 174 | + return (testInstr != null && testInstr.getMaxAddress().equals(instr.getMaxAddress())); |
| 175 | + } |
| 176 | + catch (InsufficientBytesException | UnknownInstructionException |
| 177 | + | UnknownContextException e) { |
| 178 | + Msg.error(this, |
| 179 | + "Could not disassemble instruction at " + at + " (" + e.getMessage() + ")", e); |
| 180 | + return false; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | +} |
0 commit comments