Skip to content

Instantly share code, notes, and snippets.

@sector7-nl
Last active November 8, 2024 10:51
Show Gist options
  • Select an option

  • Save sector7-nl/a926318736cdf525b9b72ac5995ec75d to your computer and use it in GitHub Desktop.

Select an option

Save sector7-nl/a926318736cdf525b9b72ac5995ec75d to your computer and use it in GitHub Desktop.
from pwn import *
import sys
if len(sys.argv) != 4:
print(f"Usage: {sys.argv[0]} <serial port> <old file> <new file>")
sys.exit(1)
serial = tubes.serialtube.serialtube(port=sys.argv[1], baudrate=115200)
serial.write(b"\n")
print(serial.recvuntil(b">>> "))
serial.write(b"\x01")
print(serial.recvuntil(b">"))
serial.write(b"""card = pyb.MMCard()
card.power(True)
while True:
offset = input().replace('\\r', '').replace('\\n', '')
old = input().replace('\\r', '').replace('\\n', '')
newdata = input().replace('\\r', '').replace('\\n', '')
offset = int(offset)
old = bytes.fromhex(old)
newdata = bytes.fromhex(newdata)
existing = card.read(offset)
if existing != newdata:
print("Block is not a match!")
else:
print("OK")
\x04""")
serial.recvuntil(b'OK')
with open(sys.argv[2], "rb") as fold:
with open(sys.argv[3], "rb") as fnew:
while True:
location = fold.tell()
block_old = fold.read(512)
block_new = fnew.read(512)
if len(block_old) == 0:
break
assert(len(block_old) == 512)
assert(len(block_old) == len(block_new))
if block_old != block_new:
print(f"Sending block at block number {location // 512}...")
block_old_hex = block_old.hex()
block_new_hex = block_new.hex()
assert(len(block_old_hex) == len(block_new_hex))
assert(len(block_old_hex) == 1024)
serial.write(f"{location // 512}\n".encode())
# ignore the echo
serial.readline()
serial.write(f"{block_old.hex()}\n".encode())
# ignore the echo
serial.readline()
serial.write(f"{block_new.hex()}\n".encode())
# ignore the echo
serial.readline()
response = serial.readline().strip()
if response != b"OK":
print(f"Failed: {response}")
if b"Old is not a match" in response:
print(f"{location // 512} block is not a match, but continuing")
else:
print(serial.clean())
break
from pwn import *
import sys
if len(sys.argv) != 4:
print(f"Usage: {sys.argv[0]} <serial port> <old file> <new file>")
sys.exit(1)
serial = tubes.serialtube.serialtube(port=sys.argv[1], baudrate=115200)
serial.write(b"\n")
print(serial.recvuntil(b">>> "))
serial.write(b"\x01")
print(serial.recvuntil(b">"))
serial.write(b"""card = pyb.MMCard()
card.power(True)
while True:
offset = input().replace('\\r', '').replace('\\n', '')
old = input().replace('\\r', '').replace('\\n', '')
newdata = input().replace('\\r', '').replace('\\n', '')
offset = int(offset)
old = bytes.fromhex(old)
newdata = bytes.fromhex(newdata)
existing = None
for i in range(10):
try:
existing = card.read(offset)
break
except:
pass
if existing is None:
print("Read old failed")
continue
if existing == old or existing == bytes.fromhex("00" * 512):
card.write(offset, newdata)
overwritten = None
for i in range(10):
try:
overwritten = card.read(offset)
break
except:
pass
if overwritten == newdata:
print("OK")
else:
print("Write failed")
else:
print("Old is not a match")
\x04""")
serial.recvuntil(b'OK')
with open(sys.argv[2], "rb") as fold:
with open(sys.argv[3], "rb") as fnew:
while True:
location = fold.tell()
block_old = fold.read(512)
block_new = fnew.read(512)
if len(block_old) == 0:
break
assert(len(block_old) == 512)
assert(len(block_old) == len(block_new))
if block_old != block_new:
print(f"Sending block at block number {location // 512}...")
block_old_hex = block_old.hex()
block_new_hex = block_new.hex()
assert(len(block_old_hex) == len(block_new_hex))
assert(len(block_old_hex) == 1024)
serial.write(f"{location // 512}\n".encode())
# ignore the echo
serial.readline()
serial.write(f"{block_old.hex()}\n".encode())
# ignore the echo
serial.readline()
serial.write(f"{block_new.hex()}\n".encode())
# ignore the echo
serial.readline()
response = serial.readline().strip()
if response != b"OK":
print(f"Failed: {response}")
if b"Old is not a match" in response:
print(f"{location // 512} block is not a match, but continuing")
else:
print(serial.clean())
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment