Replies: 7 comments
-
I think I will iterate through the address set and extract the bytes one by one with the getByte method as a workaround for now before this is updated. I also found that this method 'public byte getByte([Address] addr) throws [MemoryAccessException]' returns an int instead of byte as in the documentation https://ghidra.re/ghidra_docs/api/ghidra/program/database/mem/MemoryBlockDB.html#getByte(ghidra.program.model.address.Address) The MemoryByteIterator(currentProgram().getMemory(), address_set).next() also returns an int instead of byte. |
Beta Was this translation helpful? Give feedback.
-
Are you accessing the Ghidra API through the Jython interface or Python 3 via Ghidrathon? |
Beta Was this translation helpful? Give feedback.
-
Python 3 via Ghidrathon |
Beta Was this translation helpful? Give feedback.
-
So, the jarray solution only applies to Jython, i.e. using the default Ghidra scripting interface. Just to be clear, "bytearray" failed as well? I.e. you tried that before resorting to the solution in #858, yes? |
Beta Was this translation helpful? Give feedback.
-
Yes, all options mentioned in the post fails including bytes_read = bytearray(bb.size). |
Beta Was this translation helpful? Give feedback.
-
OK, borrowing from the Ghidrathon documentation, I tried the following successfully: currentAddress = currentSelection().getMinAddress() "code" correctly receives the byte values from my selection. |
Beta Was this translation helpful? Give feedback.
-
The following seems to be functional under Ghidrathon for me for any method that writes into a buffer passed as a parameter - the ByteBuffer satisfies jep and the underlying ghidra method, and the map prevents python from choking on java's signed bytes:
|
Beta Was this translation helpful? Give feedback.
-
currentProgram.getMemory().getBytes(address_to_read, bytes_read) does not populate my byte array. The byte array remains the same before and after calling the method.
I found #858, it appears I have to use jarray. However, when trying to import jarray, it returns "import jarray
ModuleNotFoundError: No module named 'jarray'". I cannot find the jarray package to install.
import jarray
bytes = jarray.array(bytes_read, "b")
My attempts without using jarray
My code attempts to read bb.size bytes from a specified address into the bytes_read array, but bytes_read remains the same before and after calling getBytes method.
address_to_read = bb.getMinAddress()
Initialize a byte array to store the read bytes
bytes_read = bytearray(bb.size)
print("bytes_read before: ", bytes_read)
currentProgram = getCurrentProgram()
Attempt to read bytes from the specified address into the byte array
num_bytes_read = currentProgram.getMemory().getBytes(address_to_read, bytes_read)
print("bytes_read after: ", bytes_read)
print("num_bytes_read: ", num_bytes_read)
Example Result of one of the many basic blocks
bytes_read before: bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
bytes_read after: bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
num_bytes_read: 12
Expected behavior
currentProgram.getMemory().getBytes(address_to_read, bytes_read) is expected to populate bytes_read
I've tried the following to initialize bytes_read:
bytes_read = array.array('b', bytes_read)
bytes_read = array.array("b", bytes(bytes_read))
bytes_read = array.array('b', repr(bytes(bytes_read))[2:-1])
bytes_read = array.array('b', bytes_read.decode('utf-8'))
bytes_read = array.array('b', bytes_read.decode())
bytes_read = list()*bb.size
num_bytes_read = currentProgram.getMemory().getBytes(address_to_read, bytes_read, 0, bb.size)
None of them populate bytes_read.
Environment (please complete the following information):
I run the script on many different basic blocks in different functions of a program. bytes_read remains the same throughout. Any guidance is highly appreciated.
Beta Was this translation helpful? Give feedback.
All reactions