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

The following examples show how to use ghidra.program.model.mem.Memory#setByte() . 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: ReferencesPluginScreenShots.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testOffsetRefsExample() throws MemoryAccessException { // gif
	removeFlowArrows();
	goToListing(0x0400280);
	int id = program.startTransaction("Test");
	Memory memory = program.getMemory();
	memory.setByte(addr(0x400284), (byte) 0x14);
	memory.setByte(addr(0x400288), (byte) 0x18);
	memory.setByte(addr(0x40028c), (byte) 0x1c);
	memory.setByte(addr(0x400290), (byte) 0x20);
	program.endTransaction(id, true);
	makeSelection(0x400284, 0x400293);

	performAction("Create Offset References", "OffsetTablePlugin", false);
	runSwing(() -> {
		OffsetTableDialog dialog = (OffsetTableDialog) getDialog();
		dialog.setBaseAddress(addr(0x4f5000));
	});
	pressOkOnDialog();
	captureIsolatedProvider(CodeViewerProvider.class, 800, 600);
}
 
Example 2
Source File: SymbolManagerTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void createInstruction(Address addr) throws Exception {
	int tx = program.startTransaction("test");
	try {
		Memory memory = program.getMemory();
		memory.setByte(addr, (byte) 0xd9);
		memory.setByte(addr, (byte) 0x32);
		AddressSet set = new AddressSet(addr, addr.add(1));
		DisassembleCommand cmd = new DisassembleCommand(set, set);
		cmd.applyTo(program);
	}
	finally {
		program.endTransaction(tx, true);
	}
}
 
Example 3
Source File: XorMemoryScript.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void run() throws Exception {
	
	// default to the current memory block
	Memory memory = currentProgram.getMemory();
	MemoryBlock block = memory.getBlock(currentAddress);
	AddressSetView set = new AddressSet(block.getStart(),block.getEnd());
	
	if (currentSelection != null && !currentSelection.isEmpty()) {
		set = currentSelection;
	}
	
	byte[] xorValues = askBytes("XorValue", "Values to xor with selected memory:");
	
	int valueLength = xorValues.length;
	int xorIndex = 0;
	
	AddressIterator aIter = set.getAddresses(true);
	
	while (aIter.hasNext() && !monitor.isCancelled()) {
		Address addr = aIter.next();
		monitor.setMessage(addr.toString());
		byte xorValue = xorValues[xorIndex];
		byte b = memory.getByte(addr);
		b = (byte) (b ^ xorValue);
		memory.setByte(addr, b);
		xorIndex += 1;
		xorIndex = xorIndex % valueLength;
	}
}
 
Example 4
Source File: eBPF_ElfRelocationHandler.java    From eBPF-for-Ghidra with MIT License 4 votes vote down vote up
@Override
public void relocate(ElfRelocationContext elfRelocationContext, ElfRelocation relocation,
		Address relocationAddress) throws MemoryAccessException, NotFoundException {
	
	ElfHeader elf = elfRelocationContext.getElfHeader();
	if (elf.e_machine() != ElfConstants.EM_BPF) {
		return;
	}

	Program program = elfRelocationContext.getProgram();
	Memory memory = program.getMemory();

	int type = relocation.getType();	
	int symbolIndex = relocation.getSymbolIndex();				
	long value;
	boolean appliedSymbol = true;

	if (type == 1) {			
		try {					
			int SymbolIndex= relocation.getSymbolIndex();
			ElfSymbol Symbol = elfRelocationContext.getSymbol(SymbolIndex);
			String map = Symbol.getNameAsString();				
				
			SymbolTable table = program.getSymbolTable();
			Address mapAddr = table.getSymbol(map).getAddress();
			String sec_name = elfRelocationContext.relocationTable.getSectionToBeRelocated().getNameAsString();
			if (sec_name.toString().contains("debug")) {
				return;
			}
				
			value = mapAddr.getAddressableWordOffset();		
			Byte dst = memory.getByte(relocationAddress.add(0x1));
			memory.setLong(relocationAddress.add(0x4), value);			
			memory.setByte(relocationAddress.add(0x1), (byte) (dst + 0x10));				
			}
			catch(NullPointerException e) {}
	}		

	if (appliedSymbol && symbolIndex == 0) {
		markAsWarning(program, relocationAddress, Long.toString(type),
			"applied relocation with symbol-index of 0", elfRelocationContext.getLog());
	}

}
 
Example 5
Source File: FunctionComparisonScreenShots.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testFunctionComparisonWindow() {

	positionListingTop(0x004118f0);
	int txId1 = sourceProgram.startTransaction("Modify Program1");
	int txId2 = destinationProgram.startTransaction("Modify Program2");
	try {
		sourceProgram.getDomainFile().setName("FirstProgram");
		destinationProgram.getDomainFile().setName("SecondProgram");
		sourceProgram.setName("FirstProgram");
		destinationProgram.setName("SecondProgram");

		Listing sourceListing = sourceProgram.getListing();
		Listing destListing = destinationProgram.getListing();
		Memory sourceMemory = sourceProgram.getMemory();

		Function f1 = getFunction(sourceProgram, addr(0x004118f0));
		f1.setName("FunctionA", SourceType.USER_DEFINED);
		sourceListing.setComment(addr(0x004118f0), CodeUnit.PLATE_COMMENT, null);
		sourceListing.clearCodeUnits(addr(0x004119b1), addr(0x004119b4), false);
		sourceMemory.setByte(addr(0x004119b2), (byte) 0x55);
		sourceMemory.setByte(addr(0x004119b4), (byte) 0x52);
		disassemble(sourceProgram, 0x004119b1, 4, false);

		Function f2 = getFunction(destinationProgram, addr(0x004118c0));
		f2.setName("FunctionB", SourceType.USER_DEFINED);
		destListing.setComment(addr(0x004118c0), CodeUnit.PLATE_COMMENT, null);

		FunctionComparisonProviderManager providerMgr =
			getInstanceFieldByClassType(FunctionComparisonProviderManager.class, plugin);
		FunctionComparisonProvider functionComparisonProvider =
			providerMgr.compareFunctions(f1, f2);
		FunctionComparisonPanel functionComparisonPanel =
			functionComparisonProvider.getComponent();
		runSwing(() -> {
			functionComparisonPanel.setCurrentTabbedComponent("Listing View");
			ListingCodeComparisonPanel dualListing =
				(ListingCodeComparisonPanel) functionComparisonPanel.getDisplayedPanel();
			ListingPanel leftPanel = dualListing.getLeftPanel();
			leftPanel.goTo(addr(0x004119aa));
		});
		waitForSwing();
		captureIsolatedProvider(FunctionComparisonProvider.class, 1200, 550);
	}
	catch (DuplicateNameException | InvalidInputException | MemoryAccessException
			| InvalidNameException | IOException e) {
		e.printStackTrace();
	}
	finally {
		destinationProgram.endTransaction(txId2, false);
		sourceProgram.endTransaction(txId1, false);
	}
}