Skip to content

Ghidra is a software reverse engineering (SRE) framework

GT-674: Creating proper external references for PE Delay Load Imports

Ryan Kurtza7f06d7bcc45
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/DelayImportDataDirectory.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/DelayImportDataDirectory.java
index 94b7346c3d..24c8a3d42a 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/DelayImportDataDirectory.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/DelayImportDataDirectory.java
@@ -226,10 +226,6 @@ public class DelayImportDataDirectory extends DataDirectory {
List<ThunkData> thunks,
TaskMonitor monitor,
MessageLog log) {
-
- DataType dt = ntHeader.getOptionalHeader().is64bit()
- ? (DataType)QWORD
- : (DataType)DWORD;
long thunkPtr = va(ptr, isBinary);
if (!descriptor.isUsingRVA()) {
@@ -240,6 +236,15 @@ public class DelayImportDataDirectory extends DataDirectory {
if (monitor.isCancelled()) {
return;
}
+ DataType dt;
+ if (thunk.getAddressOfData() == 0) {
+ dt = ntHeader.getOptionalHeader().is64bit() ? QWORD : DWORD;
+ }
+ else {
+ dt = ntHeader.getOptionalHeader().is64bit() ? Pointer64DataType.dataType
+ : Pointer32DataType.dataType;
+ }
+
Address thunkAddress = space.getAddress(thunkPtr);
PeUtils.createData(program, thunkAddress, dt, log);
setEolComment(program, thunkAddress, thunk.getStructName());
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/DelayImportDescriptor.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/DelayImportDescriptor.java
index 79417e1633..66c2172ed4 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/DelayImportDescriptor.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/DelayImportDescriptor.java
@@ -22,6 +22,7 @@ import ghidra.app.util.bin.BinaryReader;
import ghidra.app.util.bin.StructConverter;
import ghidra.app.util.bin.format.FactoryBundledWithBinaryReader;
import ghidra.program.model.data.*;
+import ghidra.program.model.symbol.SymbolUtilities;
import ghidra.util.Conv;
import ghidra.util.Msg;
import ghidra.util.exception.DuplicateNameException;
@@ -64,7 +65,7 @@ public class DelayImportDescriptor implements StructConverter {
private List<ThunkData> thunksBoundIAT = new ArrayList<ThunkData>();
private List<ThunkData> thunksUnloadIAT = new ArrayList<ThunkData>();
- private List<DelayImportInfo> delayImportInfoList = new ArrayList<DelayImportInfo>();
+ private List<ImportInfo> delayImportInfoList = new ArrayList<ImportInfo>();
private Map<ThunkData, ImportByName> importByNameMap = new HashMap<ThunkData, ImportByName>();
private boolean isValid;
@@ -121,6 +122,8 @@ public class DelayImportDescriptor implements StructConverter {
}
long thunkPtr = 0;
+ int offset = 0;
+
if (isUsingRVA()) {
thunkPtr = ntHeader.rvaToPointer(ptr);
}
@@ -146,9 +149,9 @@ public class DelayImportDescriptor implements StructConverter {
continue;
}
+ String funcName;
if (thunk.isOrdinal()) {
- long ordinal = thunk.getOrdinal();
- delayImportInfoList.add(new DelayImportInfo(ordinal));
+ funcName = SymbolUtilities.ORDINAL_PREFIX + thunk.getOrdinal();
}
else {
long ibnPtr = 0;
@@ -164,11 +167,12 @@ public class DelayImportDescriptor implements StructConverter {
}
ImportByName ibn = ImportByName.createImportByName(reader, (int) ibnPtr);
importByNameMap.put(thunk, ibn);
- int ordinal = ibn.getHint();
- String name = ibn.getName();
- delayImportInfoList.add(new DelayImportInfo(ordinal, name));
+ funcName = ibn.getName();
thunk.setImportByName(ibn);
}
+
+ delayImportInfoList.add(new ImportInfo(offset, "", dllName, funcName, false));
+ offset += thunk.getStructSize();
}
return thunkList;
@@ -292,8 +296,8 @@ public class DelayImportDescriptor implements StructConverter {
return new HashMap<ThunkData, ImportByName>(importByNameMap);
}
- public List<DelayImportInfo> getImportList() {
- return new ArrayList<DelayImportInfo>(delayImportInfoList);
+ public List<ImportInfo> getImportList() {
+ return new ArrayList<ImportInfo>(delayImportInfoList);
}
public List<ThunkData> getThunksIAT() {
@@ -312,6 +316,7 @@ public class DelayImportDescriptor implements StructConverter {
return new ArrayList<ThunkData>(thunksUnloadIAT);
}
+ @Override
public DataType toDataType() throws DuplicateNameException, IOException {
DataType ibo32 = new ImageBaseOffset32DataType();
StructureDataType struct = new StructureDataType(NAME, 0);
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/DelayImportInfo.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/DelayImportInfo.java
deleted file mode 100644
index 8d8cd00e3e..0000000000
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/DelayImportInfo.java
+++ /dev/null
@@ -1,65 +0,0 @@
-/* ###
- * IP: GHIDRA
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package ghidra.app.util.bin.format.pe;
-
-/**
- * A class to hold the information extracted from a
- * delay import descriptor.
- *
- * NOTE:
- * This class is simply a storage class created for
- * parsing the PE header data structures.
- * It does not map back to a PE data data structure.
- *
- *
- */
-public class DelayImportInfo {
- private long ordinal;
- private String name;
-
- DelayImportInfo(long ordinal) {
- this.ordinal = ordinal;
- }
-
- DelayImportInfo(int ordinal, String name) {
- this.ordinal = ordinal;
- this.name = name;
- }
-
- /**
- * Returns the ordinal number of the imported DLL.
- * @return the ordinal number of the imported DLL
- */
- public long getOrdinal() {
- return ordinal;
- }
-
- /**
- * Returns the name of the imported DLL.
- * @return the name of the imported DLL
- */
- public String getName() {
- return name;
- }
-
- /**
- * Returns true if the import is 'by name'.
- * @return true if the import is 'by name'
- */
- public boolean hasName() {
- return name != null;
- }
-}
diff --git 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
index 5852acb5e1..f7f1141135 100644
--- 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
@@ -35,6 +35,7 @@ import ghidra.app.util.importer.MessageLog;
import ghidra.app.util.importer.MessageLogContinuesFactory;
import ghidra.framework.model.DomainObject;
import ghidra.framework.options.Options;
+import ghidra.program.database.function.OverlappingFunctionException;
import ghidra.program.database.mem.FileBytes;
import ghidra.program.model.address.*;
import ghidra.program.model.data.*;
@@ -141,6 +142,7 @@ public class PeLoader extends AbstractPeDebugLoader {
processExports(optionalHeader, program, monitor, log);
processImports(optionalHeader, program, monitor, log);
+ processDelayImports(optionalHeader, program, monitor, log);
processRelocations(optionalHeader, program, monitor, log);
processDebug(optionalHeader, fileHeader, sectionToAddress, program, monitor);
processProperties(optionalHeader, program, monitor);
@@ -446,6 +448,84 @@ public class PeLoader extends AbstractPeDebugLoader {
}
}
+ private void processDelayImports(OptionalHeader optionalHeader, Program program,
+ TaskMonitor monitor, MessageLog log) {
+
+ if (monitor.isCancelled()) {
+ return;
+ }
+ monitor.setMessage("[" + program.getName() + "]: processing delay imports...");
+
+ DataDirectory[] dataDirectories = optionalHeader.getDataDirectories();
+ if (dataDirectories.length <= OptionalHeader.IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT) {
+ return;
+ }
+
+ DelayImportDataDirectory didd =
+ (DelayImportDataDirectory) dataDirectories[OptionalHeader.IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT];
+ if (didd == null) {
+ return;
+ }
+
+ log.appendMsg("Delay imports detected...");
+
+ AddressSpace space = program.getAddressFactory().getDefaultAddressSpace();
+ Listing listing = program.getListing();
+ ReferenceManager refManager = program.getReferenceManager();
+ FunctionManager funcManager = program.getFunctionManager();
+
+ DelayImportDescriptor[] descriptors = didd.getDelayImportDescriptors();
+ for (DelayImportDescriptor descriptor : descriptors) {
+ if (monitor.isCancelled()) {
+ return;
+ }
+
+ // Get address of the first entry in the import address table
+ Address iatBaseAddr = space.getAddress(descriptor.isUsingRVA()
+ ? descriptor.getAddressOfIAT() + optionalHeader.getImageBase()
+ : descriptor.getAddressOfIAT());
+
+ for (ImportInfo importInfo : descriptor.getImportList()) {
+
+ // Get the offset from the import list. -1 is the default (no offset)
+ long offset = importInfo.getAddress();
+ if (offset < 0) {
+ break;
+ }
+
+ // Get address of current position in the import address table
+ Address iatAddr = iatBaseAddr.add(offset);
+ Data iatData = listing.getDataAt(iatAddr);
+ if (iatData == null || !(iatData.getValue() instanceof Address)) {
+ continue;
+ }
+
+ // Create external reference
+ try {
+ refManager.addExternalReference(iatAddr, importInfo.getDLL(),
+ importInfo.getName(), null, SourceType.IMPORTED, 0, RefType.DATA);
+ }
+ catch (DuplicateNameException | InvalidInputException e) {
+ log.appendMsg(
+ "Failed to create Delay Load external function at: " + iatAddr);
+ }
+
+ // Create delay load proxy function
+ Address proxyFuncAddr = (Address) iatData.getValue();
+ if (funcManager.getFunctionAt(proxyFuncAddr) == null) {
+ try {
+ funcManager.createFunction("DelayLoad_" + importInfo.getName(),
+ proxyFuncAddr, new AddressSet(proxyFuncAddr), SourceType.IMPORTED);
+ }
+ catch (InvalidInputException | OverlappingFunctionException e) {
+ log.appendMsg(
+ "Failed to create Delay Load proxy function at: " + proxyFuncAddr);
+ }
+ }
+ }
+ }
+ }
+
/**
* Mark this location as code in the CodeMap.
* The analyzers will pick this up and disassemble the code.
@@ -515,7 +595,6 @@ public class PeLoader extends AbstractPeDebugLoader {
AddressFactory af = program.getAddressFactory();
AddressSpace space = af.getDefaultAddressSpace();
SymbolTable symTable = program.getSymbolTable();
- Memory memory = program.getMemory();
Listing listing = program.getListing();
ReferenceManager refManager = program.getReferenceManager();
@@ -593,7 +672,7 @@ public class PeLoader extends AbstractPeDebugLoader {
private Map<SectionHeader, Address> processMemoryBlocks(PortableExecutable pe, Program prog,
FileBytes fileBytes, TaskMonitor monitor, MessageLog log)
- throws AddressOverflowException, IOException {
+ throws AddressOverflowException {
AddressFactory af = prog.getAddressFactory();
AddressSpace space = af.getDefaultAddressSpace();