Java Code Examples for ghidra.app.util.bin.ByteProvider#getInputStream()

The following examples show how to use ghidra.app.util.bin.ByteProvider#getInputStream() . 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: DefLoader.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private List<DefExportLine> parseExports(ByteProvider provider) throws IOException {
	List<DefExportLine> list = new ArrayList<>();
	try (InputStream inputStream = provider.getInputStream(0)) {
		boolean hasExports = false;
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
		String line = null;
		while ((line = reader.readLine()) != null) {
			if (line.startsWith(";") || line.isEmpty()) {// comment
				continue;
			}
			else if (line.startsWith("LIBRARY")) {
				// why skip libraries?  Who knows?  If you do, please update this comment
			}
			else if (line.startsWith("EXPORTS")) {
				hasExports = true;
			}
			else if (hasExports) {
				DefExportLine exp = new DefExportLine(line);
				list.add(exp);
			}
		}
	}
	return list;
}
 
Example 2
Source File: DexLoader.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void load(ByteProvider provider, LoadSpec loadSpec, List<Option> options,
		Program program, TaskMonitor monitor, MessageLog log) throws IOException {

	monitor.setMessage( "DEX Loader: creating dex memory" );
	try {
		Address start = program.getAddressFactory().getDefaultAddressSpace().getAddress( 0x0 );
		long length = provider.length();

		try (InputStream inputStream = provider.getInputStream(0)) {
			program.getMemory().createInitializedBlock(".dex", start, inputStream, length,
				monitor, false);
		}

		BinaryReader reader = new BinaryReader( provider, true );
		DexHeader header = new DexHeader( reader );

		monitor.setMessage( "DEX Loader: creating method byte code" );

		createMethodLookupMemoryBlock( program, monitor );
		createMethodByteCodeBlock( program, length, monitor);

		for ( ClassDefItem item : header.getClassDefs( ) ) {
			monitor.checkCanceled( );

			ClassDataItem classDataItem = item.getClassDataItem( );
			if ( classDataItem == null ) {
				continue;
			}

			createMethods( program, header, item, classDataItem.getDirectMethods( ), monitor, log );
			createMethods( program, header, item, classDataItem.getVirtualMethods( ), monitor, log );
		}
	}
	catch ( Exception e) {
		log.appendException( e );
	}
}
 
Example 3
Source File: MapLoader.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Parses exported symbol information from the MAP file represented by the given provider.
 * 
 * @param provider The bytes representing a MAP file
 * @param log An optional log to write to (could be null)
 * @return A {@link List} of {@link MapExport}s representing exported symbol information
 * @throws IOException If there was a problem parsing
 */
private List<MapExport> parseExports(ByteProvider provider, MessageLog log) throws IOException {
	ArrayList<MapExport> list = new ArrayList<>();
	try (BufferedReader reader =
		new BufferedReader(new InputStreamReader(provider.getInputStream(0)))) {
		boolean hasExports = false;

		String line;
		int lineNumber = 0;
		while ((line = reader.readLine()) != null) {
			lineNumber++;
			line = line.trim();
			if (line.startsWith(";")) {// comment
				continue;
			}
			if (hasExports) {
				if (!line.isEmpty()) {
					try {
						list.add(MapExport.parse(line, lineNumber));
					}
					catch (ParseException e) {
						if (log != null) {
							log.appendMsg(e.getMessage());
						}
					}
				}
				else if (!list.isEmpty()) {
					break;
				}
			}
			else if (line.indexOf("Publics by Value") != -1) {
				hasExports = true;
			}
		}
	}
	return list;
}
 
Example 4
Source File: GdtLoader.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private static boolean isGDTFile(ByteProvider provider) {
	if (!provider.getName().toLowerCase().endsWith(FileDataTypeManager.SUFFIX)) {
		return false;
	}
	boolean isGDT = false;
	try (InputStream inputStream = provider.getInputStream(0)) {
		isGDT = ItemSerializer.isPackedFile(inputStream);
	}
	catch (IOException e) {
		// ignore
	}
	return isGDT;
}
 
Example 5
Source File: GzfLoader.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private static boolean isGzfFile(ByteProvider provider) {
	if (!provider.getName().toLowerCase().endsWith(".gzf")) {
		return false;
	}
	boolean isGZF = false;
	try (InputStream inputStream = provider.getInputStream(0)) {
		isGZF = ItemSerializer.isPackedFile(inputStream);
	}
	catch (IOException e) {
		// ignore
	}
	return isGZF;
}
 
Example 6
Source File: MotorolaHexLoader.java    From ghidra with Apache License 2.0 5 votes vote down vote up
static boolean isPossibleHexFile(ByteProvider provider) {
	try (BoundedBufferedReader reader =
		new BoundedBufferedReader(new InputStreamReader(provider.getInputStream(0)))) {
		String line = reader.readLine();
		while (line.matches("^\\s*$")) {
			line = reader.readLine();
		}
		return line.matches("^[S:][0-9a-fA-F]+$");
	}
	catch (Exception e) {
		return false;
	}
}
 
Example 7
Source File: ElfProgramBuilder.java    From ghidra with Apache License 2.0 4 votes vote down vote up
protected void load(TaskMonitor monitor) throws IOException, CancelledException {

		monitor.setMessage("Completing ELF header parsing...");
		monitor.setCancelEnabled(false);
		elf.parse();
		monitor.setCancelEnabled(true);

		int id = program.startTransaction("Load ELF program");
		boolean success = false;
		try {

			addProgramProperties(monitor);

			setImageBase();
			program.setExecutableFormat(ElfLoader.ELF_NAME);

			// resolve segment/sections and create program memory blocks
			ByteProvider byteProvider = elf.getReader().getByteProvider();
			try (InputStream fileIn = byteProvider.getInputStream(0)) {
				fileBytes = program.getMemory().createFileBytes(byteProvider.getName(), 0,
					byteProvider.length(), fileIn, monitor);
			}

			// process headers and define "section" within memory elfProgramBuilder
			processProgramHeaders(monitor);
			processSectionHeaders(monitor);

			resolve(monitor);

			if (elf.e_shnum() == 0) {
				// create/expand segments to their fullsize if not sections are defined
				expandProgramHeaderBlocks(monitor);
			}

			if (memory.isEmpty()) {
				// TODO: Does this really happen?
				success = true;
				return;
			}

			if (elf.e_shnum() != 0) {
				// discard tiny alignment/filler segment fragments when
				// section headers are present
				pruneDiscardableBlocks();
			}

			markupElfHeader(monitor);
			markupProgramHeaders(monitor);
			markupSectionHeaders(monitor);
			markupDynamicTable(monitor);
			markupInterpreter(monitor);

			processStringTables(monitor);

			processSymbolTables(monitor);

			elf.getLoadAdapter().processElf(this, monitor);

			processRelocations(monitor);
			processEntryPoints(monitor);
			processImports(monitor);

			monitor.setMessage("Processing PLT/GOT ...");
			elf.getLoadAdapter().processGotPlt(this, monitor);

			markupHashTable(monitor);
			markupGnuHashTable(monitor);

			processGNU(monitor);
			processGNU_readOnly(monitor);

			success = true;
		}
		finally {
			program.endTransaction(id, success);
		}
	}
 
Example 8
Source File: AbstractProgramLoader.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private String computeBinaryMD5(ByteProvider provider) throws IOException {
	try (InputStream in = provider.getInputStream(0)) {
		return MD5Utilities.getMD5Hash(in);
	}
}
 
Example 9
Source File: AbstractProgramLoader.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private String computeBinarySHA256(ByteProvider provider) throws IOException {
	try (InputStream in = provider.getInputStream(0)) {
		return HashUtilities.getHash(HashUtilities.SHA256_ALGORITHM, in);
	}
}
 
Example 10
Source File: MemoryBlockUtils.java    From ghidra with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a new {@link FileBytes} object using a portion of the bytes from a {@link ByteProvider}
 * @param program the program in which to create a new FileBytes object
 * @param provider the ByteProvider from which to get the bytes.
 * @param offset the offset into the ByteProvider from which to start loading bytes.
 * @param length the number of bytes to load
 * @param monitor the monitor for canceling this potentially long running operation.
 * @return the newly created FileBytes object.
 * @throws IOException if an IOException occurred.
 * @throws CancelledException if the user cancelled the operation
 */
public static FileBytes createFileBytes(Program program, ByteProvider provider, long offset,
		long length, TaskMonitor monitor) throws IOException, CancelledException {
	Memory memory = program.getMemory();
	try (InputStream fis = provider.getInputStream(offset)) {
		return memory.createFileBytes(provider.getName(), offset, length, fis, monitor);
	}
}