Ghidra is a software reverse engineering (SRE) framework
Merge remote-tracking branch 'origin/GP-7036_ryanmkurtz_PR-9176_XOR-RAX_pointer-to-raw-data'
Ryan Kurtz5291c4822f7a
diff --cc Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/SectionHeader.javaindex 6bb3f95780,6ac132a83a..74a311c189--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/SectionHeader.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/SectionHeader.java@@@ -413,19 -413,26 +413,28 @@@ public class SectionHeader implements S}/**-- * Returns the file offset where the data-- * for the section begins. For executables,- * this value must be a multiple of the file- * this value *should* be a multiple of the file-- * alignment given in the PE header.- * Note: While this value *should* be a- * multiple of the file alignment, Windows will- * round down to the nearest multiple of 0x200- * regardless of the file alignment.- * Also note that for values below 0x200 but above 0x0- * Windows will round down to 0 but still load the section- * into memory instead of not loading anything as it would- * if it were 0 from the start.- *- * @return the file offset where the data for the section begins++ * {@return the file offset where the data for the section begins}+ * <p>- * If a section is uninitialized, this value will be 0.- *- * @return the file offset where the data for the section begins++ * For executables, this value *should* be a multiple of the file alignment given in the PE++ * header.++ * <p>++ * Note: While this value *should* be a multiple of the file alignment, Windows will round down++ * to the nearest multiple of 0x200 regardless of the file alignment. Also note that for values++ * below 0x200 but above 0x0 Windows will round down to 0 but still load the section into memory++ * instead of not loading anything as it would if it were 0 from the start.*/public int getPointerToRawData() {- if (pointerToRawData < 0x200) {- return 0;- }+ return (pointerToRawData / 0x200) * 0x200;+ }+++ /**++ * {@return the file offset where the data for the section begins, as specified in the file.++ * This value does not take alignment into account, and may not represent the true start of++ * the data in the file}++ *++ * @see #getPointerToRawData()++ */+ public int getRawPointerToRawData() {return pointerToRawData;}diff --cc Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/PeLoader.javaindex ef417dd5b9,6095b71181..41c4de5352--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/PeLoader.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/PeLoader.java@@@ -673,69 -676,68 +673,70 @@@ public class PeLoader extends AbstractPx = ((sections[i].getCharacteristics() &SectionFlags.IMAGE_SCN_MEM_EXECUTE.getMask()) != 0x0);- int rawDataSize = sections[i].getSizeOfRawData();- int rawDataPtr = sections[i].getPointerToRawData();+ int rawSize = sections[i].getSizeOfRawData();+ int rawPtr = sections[i].getPointerToRawData();+ long fileBytesSize = fileBytes.getSize();virtualSize = sections[i].getVirtualSize();- int unChangedRawDataPtr = sections[i].getRawPointerToRawData();++ int unchangedRawPtr = sections[i].getRawPointerToRawData();MemoryBlock block = null;- if (rawDataSize != 0 && unChangedRawDataPtr != 0) { // Check PointerToRawData before it was rounded down- int dataSize =- ((rawDataSize > virtualSize && virtualSize > 0) || rawDataSize < 0)- ? virtualSize- : rawDataSize;- if (ntHeader.checkRVA(dataSize) ||- (0 < dataSize && dataSize < pe.getFileLength())) {- if (!ntHeader.checkRVA(dataSize)) {- Msg.warn(this, "OptionalHeader.SizeOfImage < size of " +- sections[i].getName() + " section");- }++ if (virtualSize == 0) {+ Msg.error(this, "Section '%s' has size zero".formatted(sectionName));+ }++ // Malware can deliberately set invalid or abnormally large values of SizeOfRawData+ // to break static analysis. If we suspect that is happening, default to the+ // VirtualSize. This may result in padding bytes being replaced by garbage bytes,+ // but it will allow the binary to be imported and analyzed.+ if (rawSize < 0) {+ log.appendMsg("WARNING: Section '%s' has negative SizeOfRawData (0x%x)"+ .formatted(sectionName, rawSize));+ log.appendMsg("Using '%s' VirtualSize (0x%x) in its place"+ .formatted(sectionName, virtualSize));+ rawSize = virtualSize;+ }++ // If PointerToRawData and SizeOfRawData are set, make an initialized block from- // the FileBytes- if (rawSize != 0 && rawPtr != 0) {++ // the FileBytes (check PointerToRawData before it was rounded down)++ if (rawSize != 0 && unchangedRawPtr != 0) {+ int n = rawSize > virtualSize && virtualSize > 0 ? virtualSize : rawSize;+ if (rawPtr + n > fileBytesSize) {+ int orig = n;+ n = (int) (fileBytesSize - rawPtr);+ log.appendMsg(+ "WARNING: Section '%s' (0x%x) extends beyond end of file...truncating to 0x%x"+ .formatted(sectionName, orig, n));+ }+ if (n > 0) {block = MemoryBlockUtils.createInitializedBlock(prog, false, sectionName,- address, fileBytes, rawDataPtr, dataSize, "", "", r, w, x, log);+ address, fileBytes, rawPtr, n, "", "", r, w, x, log);sectionToAddress.put(sections[i], address);+ virtualSize -= n;+ address = address.add(n);}- if (rawDataSize == virtualSize) {- continue;- }- else if (rawDataSize > virtualSize) {- // virtual size fully initialized- continue;- }- // remainder of virtual size is uninitialized- if (rawDataSize < 0) {- Msg.error(this,- "Section[" + i + "] has invalid size " +- Integer.toHexString(rawDataSize) + " (" +- Integer.toHexString(virtualSize) + ")");- break;- }- virtualSize -= rawDataSize;- address = address.add(rawDataSize);}- if (virtualSize == 0) {- Msg.error(this, "Section[" + i + "] has size zero");- }- else {- int dataSize = (virtualSize > 0 || rawDataSize < 0) ? virtualSize : 0;- if (dataSize > 0) {- if (block != null) {- MemoryBlock paddingBlock = MemoryBlockUtils.createInitializedBlock(prog,- false, sectionName, address, dataSize, "", "", r, w, x, log);- if (paddingBlock != null) {- try {- prog.getMemory().join(block, paddingBlock);- }- catch (Exception e) {- log.appendMsg(e.getMessage());- }+ // If there is any virtual size left, map in the padding bytes+ if (virtualSize > 0) {+ if (block != null) {+ // Put the padding bytes in the same block we created above+ MemoryBlock paddingBlock = MemoryBlockUtils.createInitializedBlock(prog,+ false, sectionName, address, virtualSize, "", "", r, w, x, log);+ if (paddingBlock != null) {+ try {+ prog.getMemory().join(block, paddingBlock);+ }+ catch (Exception e) {+ log.appendMsg(e.getMessage());}}- else {- MemoryBlockUtils.createUninitializedBlock(prog, false, sectionName,- address, dataSize, "", "", r, w, x, log);- sectionToAddress.putIfAbsent(sections[i], address);- }+ }+ else {+ // We didn't create an initialized block above. Put all the padding in a+ // new uninitialized block.+ MemoryBlockUtils.createUninitializedBlock(prog, false, sectionName,+ address, virtualSize, "", "", r, w, x, log);+ sectionToAddress.putIfAbsent(sections[i], address);}}