text
stringlengths
0
1.99k
|| \ | /
vv
+------------+ /---------\
| |---------GPIO 14------->| |
| | \ LED /
| QCA956x | | | |
| |---------GPIO 19------------->| |
| |-----------GND------------------->|
+------------+ | | |
After this test, I created a python script that connects to a UART serial
adapter via the `serial` module and accepts commands to read or write 4
bytes of data to memory via a TCP socket. Using this approach is expected
to be slow, but if this works then I can integrate all of it in C via
libftdi or libusb and eliminate the need of using a socket and python.
Make it work first, then make it fast later.
Code: NOTE: The code below is only part of the final MITM python script.
```
def read_from_phy_memory(ser, address):
print(f'[*] Reading from Address: 0x{address:08X}...')
ser.write(bytes(f"md 0x{address:08X}1\n","UTF-8"))
out = get_response(ser) offset = out.rfind(bytes(f'{address:08x}:
',"UTF-8"))
value = int(b'0x' + out[offset + len(bytes(f'{address:08x}: ',
"UTF-8")):offset + len(bytes(f'{address:08x}:',"UTF-8")) + 8], 16)
logging.debug(f"READ: 0x{address:08X}:0x{value:08X}")
return value
def write_to_phy_memory(ser, address, value):
print(f'[*] Writing to Address: 0x{address:08X} with value \
0x{value:08X}...')
logging.debug(f"WRITE: 0x{address:08X}: 0x{value:08X}")
ser.write(bytes(f"mw 0x{address:04X} 0x{value:04X}\n","UTF-8"))
def listen_and_respond(ser, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
s.bind(("0.0.0.0", port))
except socket.error as msg:
print('[-] Bind failed. Error Code : ' + str(msg[0]) + \
' Message ' + msg[1])
return False
s.listen(1)
print (f'[*] Socket now listening on port {port}')
while True:
conn, addr = s.accept()
msg = conn.recv(1024)
while len(msg) > 0:
if msg == b'exit\n':
conn.send(bytes('[*] Byeeeeee\n', "UTF-8"))
conn.close()
break
if msg == b'shutdown\n':
conn.send(bytes('[*] The server is going down down\n',
"UTF-8"))
conn.close()
s.shutdown(socket.SHUT_WR)
s.close()
return
elif msg[0] == ord('r'):
# Read Bytes
addr = int(msg[1:].strip(b'\n'), 16)
out = read_from_phy_memory(ser, addr)
conn.send(bytes(hex(out), "UTF-8"))
elif msg[0] == ord('w'):
# Write Bytes
address = msg[1:].split(b" ")[0]
value = msg[1:].split(b" ")[1].strip(b'\n')
write_to_phy_memory(ser, int(address, 16), int(value, 16))
conn.send(b'1')
msg = conn.recv(1024)
```
--[ 4 - Using Qemu to record MMIO transactions
To help speed up development, I copied the `mipssim.c` board within the
`qemu/hw/mips` directory and used it as a skeleton. From there I reviewed
the documentation for Qemu to learn the memory APIs. All that was needed
to make a MMIO region is to first call `memory_region_init_io()` with the
MemoryRegion *pointer (comes from g_new(MemoryRegion,1)), a struct that
contains the `.read.`, `.write.`, callbacks populated (struct
MemoryRegionOps), the name of the region for Qemu to use (e.g. "DDR"), and
then the size of the region. An Object can be supplied to the second
argument which is passed to the callbacks, but that isn't needed at this
stage. However, it'll be needed when implementing the logic for the virtual
component. Lastly a call to `memory_region_add_subregion()` needs to be
called for the subregion to be applied to the main memory space (return
value of get_system_memory()).
The following code Qemu snippet demonstrates registering the subregion
for the GPIO registers:
```
[...]
/* MMIO Callbacks for GPIO */
// READ
static uint64_t gpio_mmio_read(void *opaque, hwaddr addr, unsigned size)