Rating:

we were given a hex code of avr and we were asked to "change the data store in DDRB of AVR to 0x30 and rewrite it", so...

first, I converted the given hex code to a binary file with this: `echo -ne '\x0e\x00\x00\x00\x00\xe2\x04\xb9\x03\xb9\x1f\xef\x1a\x95\xf1\xf7\xfb\xcf\x28' > code.bin`

and then opened it with radare2 as an avr arch and in write mode to be able to modify it: `r2 -w -a avr ./code.bin`

the disassembly of it is as follow:
```
[0x00000000]> pd 12
0x00000000 0e00
0x00000002 0000 nop
0x00000004 00e2 ldi r16, 0x20
0x00000006 04b9 out 0x04, r16
┌─> 0x00000008 03b9 out 0x03, r16
╎ 0x0000000a 1fef ser r17
┌──> 0x0000000c 1a95 dec r17
└──< 0x0000000e f1f7 brne 0xc
└─< 0x00000010 fbcf rjmp 0x8
0x00000012 28ff rjmp 0x8
0x00000014 ffff rjmp 0x8
0x00000016 ffff rjmp 0x8
```

the code is very straightforward... we need to change `ldi r16, 0x20` to `ldi r16, 0x30`, so I did that by radare2 command `wa ldi r16, 0x30 @ 4` and it's done.

but wait! the last byte of the hex code is checksum... the checksum is claculated by sum of all bytes trimmed to one byte (can be done by `checksum &= 0xff` in python) and then 2's complemented (which is like `checksum = (~(checksum) + 1) & 0xff` in python).

after the editing the code by radare2, I used this in python to calculate the checksum and make a hex string out of it:
```
with open('code.bin', 'rb') as f:
code = f.read()[:-1] # trim the last check sum
checksum = (~(sum(code) & 0xff) + 1) & 0xff
hexcode = ''.join([hex(b)[2:].rjust(2, '0') for b in code])
hexcode += hex(checksum)[2:].rjust(2, '0')

print(f'flag: {hexcode}')
```

and the result: `flag: 0e00000000e304b903b91fef1a95f1f7fbcf27`