Java Code Examples for ghidra.program.model.mem.Memory#getMinAddress()

The following examples show how to use ghidra.program.model.mem.Memory#getMinAddress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SearchInfo.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Generate an address set which only includes initialized memory
 * 
 * @param program the program
 * @param startAddress starting point for search or null to start from the top of memory
 * @param selection addresses to be searched or null to search all memory
 * @return searchable address set
 */
protected AddressSetView getSearchableAddressSet(Program program, Address startAddress,
		ProgramSelection selection) {

	if (startAddress == null) {
		return new AddressSet();		// special case if we are at the first address going backwards
		// or the last address going forwards
	}

	Memory memory = program.getMemory();
	AddressSetView set = includeNonLoadedBlocks ? memory.getAllInitializedAddressSet()
			: memory.getLoadedAndInitializedAddressSet();
	if (searchSelection && selection != null && !selection.isEmpty()) {
		set = set.intersect(selection);
	}
	Address start = forwardSearch ? startAddress : memory.getMinAddress();
	Address end = forwardSearch ? memory.getMaxAddress() : startAddress;
	if (start.compareTo(end) > 0) {
		return new AddressSet();
	}
	AddressSet addressSet = program.getAddressFactory().getAddressSet(start, end);
	return set.intersect(addressSet);
}
 
Example 2
Source File: EmbeddedFinderScript.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
  public void run() throws Exception {
byte[] MAGIC_DOS_HEADER = new byte[] { 0x4d, 0x5a };				// M Z
byte[] MAGIC_NT_HEADER  = new byte[] { 0x50, 0x45, 0x00, 0x00 };	// P E 0x00 0x00

List<Address> allFound = new ArrayList<Address>();

Memory memory = currentProgram.getMemory();
Address baseAddr = memory.getMinAddress();
Address currAddr = baseAddr;

while (currAddr != null) {
	// The purpose of breaking each check into small segments (where they could be combined)
	// is to make way for future file type support, keep code clean, and to encourage readability.
	boolean DOSExists = false;
	boolean NTExists = false;
	boolean DOSAgreesWithNT = false;

	Address DOS = memory.findBytes(currAddr, MAGIC_DOS_HEADER, null, true, getMonitor());
	if (DOS != null) {
		// IMAGE_DOS_HEADER is 128 bytes in length, so let's check if that much memory is available
		if (memory.contains(DOS.add(128)))
			DOSExists = true;
	}

	Address NT = memory.findBytes(DOS, MAGIC_NT_HEADER, null, true, getMonitor());
	if (NT != null) {
		// IMAGE_NT_HEADERS32 is 80 bytes in length, so let's check if that much memory is available
		if (memory.contains(NT.add(80)))
			NTExists = true;
	}

	if (DOSExists && NTExists) {
		// It would be better to import the proper structs rather than hard coding offsets.
		// However I'm unsure of what the best way of doing this would be. It's possible to include WINNT.h
		// but this requires the non-development environment to have access to it which makes things
		// less flexible and renders it brittle for future embedded target-type searches.
		// IMAGE_DOS_HEADER + 0x3c is the IMAGE_NT_HEADERS32 offset
		long impliedOffset = memory.getShort(DOS.add(0x3c));
		long actualOffset = NT.getAddressableWordOffset() - DOS.getAddressableWordOffset();
		if (impliedOffset == actualOffset)
			DOSAgreesWithNT = true;
	}

	if (DOSAgreesWithNT) {
		byte[] MAGIC_NT_HEADER_TEST = new byte[4];	// [TODO] Get this to dynamically pull correct size, not hardcoded
		memory.getBytes(NT, MAGIC_NT_HEADER_TEST);

		if (Arrays.equals(MAGIC_NT_HEADER, MAGIC_NT_HEADER_TEST)) {
			if (DOS != baseAddr)
				allFound.add(DOS);		// We only care about targets that are not also the parent file
		}
	}

	if (DOS != null)
		currAddr = DOS.add(1);	// Ensure next search doesn't overlap with current target
	else
		currAddr = null;
}

// Present user with target discovery(s)

if (allFound.isEmpty())
	println("No embedded targets identified");
else {
	println("Embedded targets identified");
	for (Address found : allFound)
		println("\t" + found.toString());
}
  }