Python3 - seek and write an exe address - python

I have a file (original.exe) which I edit using OllyDbg but I want to learn how to do this with Python.
In OllyDbg I go to Address:
0045B82C
Then I would change:
JNZ SHORT 0045B89F
To:
JE SHORT 0045B90F
So in python using readtest.py:
offset = 0x0045B82C
with open('original.exe', 'r+b') as victim:
victim.seek(offset)
line = victim.readline()
print(line)
I get:
b'Wf93\x8b\xe9\x89t$\x10u\x05\xe8\x83\'\x00\x00f\x81{\x02\xd3\x07sP\xa1\xe4\xf2\x9c\x00\x8b\x15\xa8#\xae\x00\x8d\x0c\xc0\xc1\xe1\x04\x03\xc8\x8d\x84J\x80\x00\x00\x00\x8b\x8cJ\x80\x00\x00\x00;\xcet+\x8b\t\x89\r\xa45\xa8\x00\x890\xa1\xe4\xf2\x9c\x00\x8d\x14\xc0\xc1\xe2\x04\x03\xd0\xa1\xa8#\xae\x00\x8d\x0cPQ\xe8A\xfb\xcd\xff\x83\xc4\x04\x89pij\xffh\xd3\x07\x00\x00j\x06j\x01\x8dL$,\xe8\xc7\xe6\xce\xff\x8b\x08\x8bP\x04\x89L$\x14f\x8bD$\x16f;C\x02\x89T$\x18\x0f\x85\xa4\x00\x00\x00f;\x0b\x0f\x85\x9b\x00\x00\x00\xa1\xa45\xa8\x00\x83\xf8\xff\x0f\x84\x8d\x00\x00\x00\x8b\x15\xb0#\xae\x00\x8d\x0c\x80\xc1\xe1\x03+\xc8\xa1\xe4\xf2\x9c\x00\x8d\x0cJ\x8d\x14\xc0\xc1\xe2\x04\x03\xd0\xa1\xa8#\xae\x00\x89\x8cP\x80\x00\x00\x00\xa1\xe4\xf2\x9c\x00\x8b\x15\xa8#\xae\x00\x8d\x0c\xc0\xc1\xe1\x04\x03\xc8\x8d4JV\xe8\xae\xfa\xcd\xff\x8b\x8e\x80\x00\x00\x00\x83\xc4\x04\x89Hi\xa1\xa45\xa8\x00\xb9\x90_\x01\x00\x8d\x14\x80\xc1\xe2\x03+\xd0\xa1\xb0#\xae\x00\x89LP<\xa1\xa45\xa8\x00\x8d\x14\x80\xc1\xe2\x03+\xd0\xa1\xb0#\xae\x00\x89LP#\xc7\x05\xa45\xa8\x00\xff\xff\xff\xff\x0f\xbf\x03\x99\xb9\x1e\x00\x00\x00\xf7\xf9\x85\xd2\x0f\x85\xb2\x00\x00\x00\x8b}\x00\x85\xff\x0f\x84\xa7\x00\x00\x00\x8bE\x043\xf6\x85\xc0\x0f\x8e\x9a\x00\x00\x00\x8b\x13\x8bC\x04\x89T$\x14f\x8bT$\x16\x89D$\x183\xc0\x8d\x0c#\x8dD\x8f\x04f\x8bL\x8f\x06f;\xd1w\x15r\'f\x8b\\$\x143\xc9f;\x18\x0f\x9f\xc1\x8b\xc1\x85\xc0t\x14\x8bD$\x10\x8bM\x04#F\x89D$\x10\x0f\xbf\xc6;\xc1|\xc6\x8bD$\x10f\x85\xc0tC\x0f\xbf\xd0\x8bE\x04;\xd0}\x1d\x8d\x0cR+\xc2\xc1\xe1\x02H\x8d\x04#\x8d4\x87\x8b\xc1\xc1\xe9\x02\xf3\xa5\x8b\xc8\x83\xe1\x03\xf3\xa4\x8bE\x04\x8bM\x00+\xc2h\x80\xe3\x85\x00j\x0cPQ\x89E\x04\xe8\x08\x99\x0e\x00\x83\xc4\x10_^][\x83\xc4\x14\xc2\x04\x00\x90\x90\x90\x90\x90\x90\x90\x90\x81\xec\x04\x02\x00\x00SUVWh(\xf9\x98\x00h\xe05\xa8\x00\x8b\xf9h\xa0}\x98\x00\xe8\x80\x03\x0b\x00\x83\xc4\x08P\xe8\x97\xa0\x0e\x00\x8b\xf0\x83\xc4\x08\x85\xf6\x0f\x84"\x03\x00\x00V\x8do\x08j\x01j\x04U\xe8z\xa1\x0e\x00\x83\xc4\x10\x83\xf8\x01tv\x8dD$\x14\x8d\x8c$\x14\x01\x00\x00PQj\x00j\x00h\xb05\xa8\x00\xe8X\x92\x0e\x00\x83\xc4\x14\x8dT$\x14\x8d\x84$\x14\x01\x00\x00h\xf2\x00\x00\x00RP\xe8o\x16\x0b\x00Ph\xe8p\x98\x00h\xd0$\xae\x00\xe8\xce\x91\x0e\x00j\x00h\xd0$\xae\x00h\xe0p\x98\x00\xe8\xae\xc7\xd8\xffV\xc7\x054z\xb6\x00\x00\x00\x00\x00\xe8\x17\x9f\x0e\x00\x83\xc4(3\xc0_^][\x81\xc4\x04\x02\x00\x00\xc3V\x8d_\x04j\x01j\x04S\xe8\xee\xa0\x0e\x00\x83\xc4\x10\x83\xf8\x01t5\x8d\x8c$\x14\x01\x00\x00\x8dT$\x14QRj\x00j\x00h\xb05\xa8\x00\xe8\xcc\x91\x0e\x00\x83\xc4\x14\x8d\x84$\x14\x01\x00\x00\x8dL$\x14h\xf9\x00\x00\x00PQ\xe9o\xff\xff\xff\x8bE\x003\xed;\xc5tG\x8d\x14#\xc1\xe2\x02R\xe8\xe4\x92\x0e\x00\x83\xc4\x04;\xc5\x89\x07u2\x8d\x84$\x14\x01\x00\x00\x8dL$\x14PQUUh\xb05\xa8\x00\xe8{\x91\x0e\x00\x83\xc4\x14\x8d\x94$\x14\x01\x00\x00\x8dD$\x14h\x05\x01\x00\x00R\xe9\xc6\x01\x00\x00\x8b\x03;\xc5tH\x8b\x0fVPj\x0cQ\xe8O\xa0\x0e\x00\x8b\x0b\x83\xc4\x10;\xc1t3\x8d\x94$\x14\x01\x00\x00\x8dD$\x14RPUUh\xb05\xa8\x00\xe8.\x91\x0e\x00\x83\xc4\x14\x8d\x8c$\x14\x01\x00\x00\x8dT$\x14h\x13\x01\x00\x00QR\xe9y\x01\x00\x00V\x8do\x14j\x01j\x04U\xe8\x05\xa0\x0e\x00\x83\xc4\x10\x83\xf8\x01t4\x8d\x84$\x14\x01\x00\x00\x8dL$\x14PQj\x00j\x00h\xb05\xa8\x00\xe8\xe3\x90\x0e\x00\x83\xc4\x14\x8d\x94$\x14\x01\x00\x00\x8dD$\x14h\x1d\x01\x00\x00R\xe9\x86\xfe\xff\xffV\x8d_\x10j\x01j\x04S\xe8\xbb\x9f\x0e\x00\x83\xc4\x10\x83\xf8\x01t5\x8d\x8c$\x14\x01\x00\x00\x8dT$\x14QRj\x00j\x00h\xb05\xa8\x00\xe8\x99\x90\x0e\x00\x83\xc4\x14\x8d\x84$\x14\x01\x00\x00\x8dL$\x14h$\x01\x00\x00PQ\xe9<\xfe\xff\xffVj\x01\x8dW\x18j\x01R\xe8p\x9f\x0e\x00\x83\xc4\x10\x83\xf8\x01t4\x8d\x84$\x14\x01\x00\x00\x8dL$\x14PQj\x00j\x00h\xb05\xa8\x00\xe8N\x90\x0e\x00\x83\xc4\x14\x8d\x94$\x14\x01\x00\x00\x8dD$\x14h+\x01\x00\x00R\xe9\xf1\xfd\xff\xff\x8bE\x003\xed;\xc5tC\xc1\xe0\x04P\xe8j\x91\x0e\x00\x83\xc4\x04;\xc5\x89G\x0cu0\x8d\x8c$\x14\x01\x00\x00\x8dT$\x14QRUUh\xb05\xa8\x00\xe8\x00\x90\x0e\x00\x83\xc4\x14\x8d\x84$\x14\x01\x00\x00\x8dL$\x14h7\x01\x00\x00PQ\xebN\x8b\x03;\xc5\x0f\x84\xc9\x00\x00\x00\x8bW\x0cVPj\x10R\xe8\xd1\x9e\x0e\x00\x8b\x0b\x83\xc4\x10;\xc1to\x8d\x84$\x14\x01\x00\x00\x8dL$\x14PQUUh\xb05\xa8\x00\xe8\xb0\x8f\x0e\x00\x83\xc4\x14\x8d\x94$\x14\x01\x00\x00\x8dD$\x14hE\x01\x00\x00RP\xe8\xc7\x13\x0b\x00Ph\xe8p\x98\x00h\xd0$\xae\x00\xe8&\x8f\x0e\x00Uh\xd0$\xae\x00h\xe0p\x98\x00\xe8\x07\xc5\xd8\xffV\x89-4z\xb6\x00\xe8t\x9c\x0e\x00\x83\xc4(_^]3\xc0[\x81\xc4\x04\x02\x00\x00\xc3;\xcd\x89l$\x10~<3\xd2\x8bO\x0c\x8bD\x11\x0c\x8dL\x11\x0c\x83\xf8\xfft\x16\x8d,\x80\xc1\xe5\x03+\xe8\xa1\xb0#\xae\x00\x8d\x04h3\xed\x89\x01\xeb\x02\x89)\x8bD$\x10\x8b\x0b#\x83\xc2\x10;\xc1\x89D$\x10|\xc6\xa1\x10q\xdd\x00\x993\xc2+\xc2=)\xe0\x03\x00\x0f\x8c\x80\x00\x00\x00Vj\x01j\x04h\xa45\xa8\x00\xe8\xf1\x9d\x0e\x00\x83\xc4\x10\x83\xf8\x01ti\x8d\x8c$\x14\x01\x00\x00\x8dT$\x14QRUUh\xb05\xa8\x00\xe8\xd1\x8e\x0e\x00\x83\xc4\x14\x8d\x84$\x14\x01\x00\x00\x8dL$\x14h\\\x01\x00\x00PQ\xe8\xe8\x12\x0b\x00Ph\xe8p\x98\x00h\xd0$\xae\x00\xe8G\x8e\x0e\x00Uh\xd0$\xae\x00h\xe0p\x98\x00\xe8(\xc4\xd8\xff\x83\xc4$\x89-4z\xb6\x003\xc0_^][\x81\xc4\x04\x02\x00\x00\xc3V\xe8\x85\x9b\x0e\x00\x83\xc4\x04\xb8\x01\x00\x00\x00_^][\x81\xc4\x04\x02\x00\x00\xc3\x90\x90\x90\x90\x90\x90\x90\x90\x90\x81\xec\x04\x02\x00\x00SUVWh(\x7f\x9a\x00h\xe05\xa8\x00\x8b\xf1h\xa0}\x98\x00\xe8 \xff\n'
I then apply the changes using OllyDbg and re run readtest.py
b'Wf93\x8b\xe9\x89t$\x10u\x05\xe8\x83\'\x00\x00f\x81{\x02\xd3\x07sP\xa1\xe4\xf2\x9c\x00\x8b\x15\xa8#\xae\x00\x8d\x0c\xc0\xc1\xe1\x04\x03\xc8\x8d\x84J\x80\x00\x00\x00\x8b\x8cJ\x80\x00\x00\x00;\xcet+\x8b\t\x89\r\xa45\xa8\x00\x890\xa1\xe4\xf2\x9c\x00\x8d\x14\xc0\xc1\xe2\x04\x03\xd0\xa1\xa8#\xae\x00\x8d\x0cPQ\xe8A\xfb\xcd\xff\x83\xc4\x04\x89pij\xffh\xd3\x07\x00\x00j\x06j\x01\x8dL$,\xe8\xc7\xe6\xce\xff\x8b\x08\x8bP\x04\x89L$\x14f\x8bD$\x16f;C\x02\x89T$\x18\x0f\x85\xa4\x00\x00\x00f;\x0b\x0f\x85\x9b\x00\x00\x00\xa1\xa45\xa8\x00\x83\xf8\xff\x0f\x84\x8d\x00\x00\x00\x8b\x15\xb0#\xae\x00\x8d\x0c\x80\xc1\xe1\x03+\xc8\xa1\xe4\xf2\x9c\x00\x8d\x0cJ\x8d\x14\xc0\xc1\xe2\x04\x03\xd0\xa1\xa8#\xae\x00\x89\x8cP\x80\x00\x00\x00\xa1\xe4\xf2\x9c\x00\x8b\x15\xa8#\xae\x00\x8d\x0c\xc0\xc1\xe1\x04\x03\xc8\x8d4JV\xe8\xae\xfa\xcd\xff\x8b\x8e\x80\x00\x00\x00\x83\xc4\x04\x89Hi\xa1\xa45\xa8\x00\xb9\x90_\x01\x00\x8d\x14\x80\xc1\xe2\x03+\xd0\xa1\xb0#\xae\x00\x89LP<\xa1\xa45\xa8\x00\x8d\x14\x80\xc1\xe2\x03+\xd0\xa1\xb0#\xae\x00\x89LP#\xc7\x05\xa45\xa8\x00\xff\xff\xff\xff\x0f\xbf\x03\x99\xb9\x1e\x00\x00\x00\xf7\xf9\x85\xd2\x0f\x85\xb2\x00\x00\x00\x8b}\x00\x85\xff\x0f\x84\xa7\x00\x00\x00\x8bE\x043\xf6\x85\xc0\x0f\x8e\x9a\x00\x00\x00\x8b\x13\x8bC\x04\x89T$\x14f\x8bT$\x16\x89D$\x183\xc0\x8d\x0c#\x8dD\x8f\x04f\x8bL\x8f\x06f;\xd1w\x15r\'f\x8b\\$\x143\xc9f;\x18\x0f\x9f\xc1\x8b\xc1\x85\xc0t\x14\x8bD$\x10\x8bM\x04#F\x89D$\x10\x0f\xbf\xc6;\xc1|\xc6\x8bD$\x10f\x85\xc0tC\x0f\xbf\xd0\x8bE\x04;\xd0}\x1d\x8d\x0cR+\xc2\xc1\xe1\x02H\x8d\x04#\x8d4\x87\x8b\xc1\xc1\xe9\x02\xf3\xa5\x8b\xc8\x83\xe1\x03\xf3\xa4\x8bE\x04\x8bM\x00+\xc2h\x80\xe3\x85\x00j\x0cPQ\x89E\x04\xe8\x08\x99\x0e\x00\x83\xc4\x10_^][\x83\xc4\x14\xc2\x04\x00\x90\x90\x90\x90\x90\x90\x90\x90\x81\xec\x04\x02\x00\x00SUVWh(\xf9\x98\x00h\xe05\xa8\x00\x8b\xf9h\xa0}\x98\x00\xe8\x80\x03\x0b\x00\x83\xc4\x08P\xe8\x97\xa0\x0e\x00\x8b\xf0\x83\xc4\x08\x85\xf6\x0f\x84"\x03\x00\x00V\x8do\x08j\x01j\x04U\xe8z\xa1\x0e\x00\x83\xc4\x10\x83\xf8\x01tv\x8dD$\x14\x8d\x8c$\x14\x01\x00\x00PQj\x00j\x00h\xb05\xa8\x00\xe8X\x92\x0e\x00\x83\xc4\x14\x8dT$\x14\x8d\x84$\x14\x01\x00\x00h\xf2\x00\x00\x00RP\xe8o\x16\x0b\x00Ph\xe8p\x98\x00h\xd0$\xae\x00\xe8\xce\x91\x0e\x00j\x00h\xd0$\xae\x00h\xe0p\x98\x00\xe8\xae\xc7\xd8\xffV\xc7\x054z\xb6\x00\x00\x00\x00\x00\xe8\x17\x9f\x0e\x00\x83\xc4(3\xc0_^][\x81\xc4\x04\x02\x00\x00\xc3V\x8d_\x04j\x01j\x04S\xe8\xee\xa0\x0e\x00\x83\xc4\x10\x83\xf8\x01t5\x8d\x8c$\x14\x01\x00\x00\x8dT$\x14QRj\x00j\x00h\xb05\xa8\x00\xe8\xcc\x91\x0e\x00\x83\xc4\x14\x8d\x84$\x14\x01\x00\x00\x8dL$\x14h\xf9\x00\x00\x00PQ\xe9o\xff\xff\xff\x8bE\x003\xed;\xc5tG\x8d\x14#\xc1\xe2\x02R\xe8\xe4\x92\x0e\x00\x83\xc4\x04;\xc5\x89\x07u2\x8d\x84$\x14\x01\x00\x00\x8dL$\x14PQUUh\xb05\xa8\x00\xe8{\x91\x0e\x00\x83\xc4\x14\x8d\x94$\x14\x01\x00\x00\x8dD$\x14h\x05\x01\x00\x00R\xe9\xc6\x01\x00\x00\x8b\x03;\xc5tH\x8b\x0fVPj\x0cQ\xe8O\xa0\x0e\x00\x8b\x0b\x83\xc4\x10;\xc1t3\x8d\x94$\x14\x01\x00\x00\x8dD$\x14RPUUh\xb05\xa8\x00\xe8.\x91\x0e\x00\x83\xc4\x14\x8d\x8c$\x14\x01\x00\x00\x8dT$\x14h\x13\x01\x00\x00QR\xe9y\x01\x00\x00V\x8do\x14j\x01j\x04U\xe8\x05\xa0\x0e\x00\x83\xc4\x10\x83\xf8\x01t4\x8d\x84$\x14\x01\x00\x00\x8dL$\x14PQj\x00j\x00h\xb05\xa8\x00\xe8\xe3\x90\x0e\x00\x83\xc4\x14\x8d\x94$\x14\x01\x00\x00\x8dD$\x14h\x1d\x01\x00\x00R\xe9\x86\xfe\xff\xffV\x8d_\x10j\x01j\x04S\xe8\xbb\x9f\x0e\x00\x83\xc4\x10\x83\xf8\x01t5\x8d\x8c$\x14\x01\x00\x00\x8dT$\x14QRj\x00j\x00h\xb05\xa8\x00\xe8\x99\x90\x0e\x00\x83\xc4\x14\x8d\x84$\x14\x01\x00\x00\x8dL$\x14h$\x01\x00\x00PQ\xe9<\xfe\xff\xffVj\x01\x8dW\x18j\x01R\xe8p\x9f\x0e\x00\x83\xc4\x10\x83\xf8\x01t4\x8d\x84$\x14\x01\x00\x00\x8dL$\x14PQj\x00j\x00h\xb05\xa8\x00\xe8N\x90\x0e\x00\x83\xc4\x14\x8d\x94$\x14\x01\x00\x00\x8dD$\x14h+\x01\x00\x00R\xe9\xf1\xfd\xff\xff\x8bE\x003\xed;\xc5tC\xc1\xe0\x04P\xe8j\x91\x0e\x00\x83\xc4\x04;\xc5\x89G\x0cu0\x8d\x8c$\x14\x01\x00\x00\x8dT$\x14QRUUh\xb05\xa8\x00\xe8\x00\x90\x0e\x00\x83\xc4\x14\x8d\x84$\x14\x01\x00\x00\x8dL$\x14h7\x01\x00\x00PQ\xebN\x8b\x03;\xc5\x0f\x84\xc9\x00\x00\x00\x8bW\x0cVPj\x10R\xe8\xd1\x9e\x0e\x00\x8b\x0b\x83\xc4\x10;\xc1to\x8d\x84$\x14\x01\x00\x00\x8dL$\x14PQUUh\xb05\xa8\x00\xe8\xb0\x8f\x0e\x00\x83\xc4\x14\x8d\x94$\x14\x01\x00\x00\x8dD$\x14hE\x01\x00\x00RP\xe8\xc7\x13\x0b\x00Ph\xe8p\x98\x00h\xd0$\xae\x00\xe8&\x8f\x0e\x00Uh\xd0$\xae\x00h\xe0p\x98\x00\xe8\x07\xc5\xd8\xffV\x89-4z\xb6\x00\xe8t\x9c\x0e\x00\x83\xc4(_^]3\xc0[\x81\xc4\x04\x02\x00\x00\xc3;\xcd\x89l$\x10~<3\xd2\x8bO\x0c\x8bD\x11\x0c\x8dL\x11\x0c\x83\xf8\xfft\x16\x8d,\x80\xc1\xe5\x03+\xe8\xa1\xb0#\xae\x00\x8d\x04h3\xed\x89\x01\xeb\x02\x89)\x8bD$\x10\x8b\x0b#\x83\xc2\x10;\xc1\x89D$\x10|\xc6\xa1\x10q\xdd\x00\x993\xc2+\xc2=)\xe0\x03\x00\x0f\x8c\x80\x00\x00\x00Vj\x01j\x04h\xa45\xa8\x00\xe8\xf1\x9d\x0e\x00\x83\xc4\x10\x83\xf8\x01ti\x8d\x8c$\x14\x01\x00\x00\x8dT$\x14QRUUh\xb05\xa8\x00\xe8\xd1\x8e\x0e\x00\x83\xc4\x14\x8d\x84$\x14\x01\x00\x00\x8dL$\x14h\\\x01\x00\x00PQ\xe8\xe8\x12\x0b\x00Ph\xe8p\x98\x00h\xd0$\xae\x00\xe8G\x8e\x0e\x00Uh\xd0$\xae\x00h\xe0p\x98\x00\xe8(\xc4\xd8\xff\x83\xc4$\x89-4z\xb6\x003\xc0_^][\x81\xc4\x04\x02\x00\x00\xc3V\xe8\x85\x9b\x0e\x00\x83\xc4\x04\xb8\x01\x00\x00\x00_^][\x81\xc4\x04\x02\x00\x00\xc3\x90\x90\x90\x90\x90\x90\x90\x90\x90\x81\xec\x04\x02\x00\x00SUVWh(\x7f\x9a\x00h\xe05\xa8\x00\x8b\xf1h\xa0}\x98\x00\xe8 \xff\n'
They are both the same.
What am I doing wrong? Why can I not see a change in the readline()?
#Ethan Furman
offset1 = 0x472077
offset2 = 0x472078
offset3 = 0x472079
offset4 = 0x47208a
offset5 = 0x47208b
offset6 = 0x47208c
newvalue1 = b'\xe9'
newvalue2 = b'\xe9'
newvalue3 = b'\x00'
newvalue4 = b'\x00'
newvalue5 = b'\x00'
newvalue6 = b'\x90'
#originals
#offset old new
#0x472077 0xf 0xe9
#0x472078 0x85 0xe9
#0x472079 0xe8 0x0
#0x47208a 0x0 0x0
#0x47208b 0x0 0x0
#0x47208c 0x0 0x90
with open('test.exe', 'r+b') as victim:
victim.seek(offset1)
victim.write(newvalue1)
victim.seek(offset2)
victim.write(newvalue2)
victim.seek(offset3)
victim.write(newvalue3)
victim.seek(offset4)
victim.write(newvalue4)
victim.seek(offset5)
victim.write(newvalue5)
victim.seek(offset6)
victim.write(newvalue6)

The hard part is not changing the file, it's know what the codes are:
NOP = ???
OFFSET = 0x0045b82c
with open('some.exe', 'r+b') as victim:
victim.seek(OFFSET)
victim.write(NOP * 2)

Related

2xRaspberry Pi nRF24L01 not connecting

I am new to Stackoverflow. I have searched for answer, but didn't find anything.
I have two Raspberry Pi 2B+, each with nRF24l01 connected. I found few libraries to make this connect, only one give any results, but not connections. This one: Github BLavery
I write script to send and to recv:
send.py:
import RPi.GPIO as GPIO
from lib_nrf24 import NRF24
import time
import spidev
GPIO.setmode(GPIO.BCM)
pipes = [[0xe7, 0xe7, 0xe7, 0xe7, 0xe7], [0xc2, 0xc2, 0xc2, 0xc2, 0xc2]]
radio = NRF24(GPIO, spidev.SpiDev())
radio.begin(0, 17)
radio.setPayloadSize(32)
radio.setChannel(0x60)
radio.setDataRate(NRF24.BR_2MBPS)
radio.setPALevel(NRF24.PA_MIN)
radio.setAutoAck(True)
radio.enableDynamicPayloads()
radio.enableAckPayload()
radio.openWritingPipe(pipes[1])
radio.printDetails()
while True:
message = list("Hello World")
radio.write(message)
print("We sent the message of {}".format(message))
# Check if it returned a ackPL
if radio.isAckPayloadAvailable():
returnedPL = []
radio.read(returnedPL, radio.getDynamicPayloadSize())
print("Our returned payload was {}".format(returnedPL))
else:
print("No payload received")
time.sleep(1)
recv.py:
import RPi.GPIO as GPIO
from lib_nrf24 import NRF24
import time
import spidev
GPIO.setmode(GPIO.BCM)
pipes = [[0xe7, 0xe7, 0xe7, 0xe7, 0xe7], [0xc2, 0xc2, 0xc2, 0xc2, 0xc2]]
radio = NRF24(GPIO, spidev.SpiDev())
radio.begin(0, 17)
radio.setPayloadSize(32)
radio.setChannel(0x60)
radio.setDataRate(NRF24.BR_2MBPS)
radio.setPAlevel(NRF24.PA_MIN)
radio.setAutoAck(True)
radio.enableDynamicPayloads()
radio.enableAckPayload()
radio.openReadingPipe(1, pipes[1])
radio.printDetails()
radio.startListening()
while True:
ackPL = [1]
while not radio.available (0):
time.sleep(1/100)
receivedMessage = []
radio.read(receivedMessage, radio.getDynamicPayloadSize())
print("Received: {}".format(receivedMessage))
print("Translating the receivedMessage into unicode characters...")
string = ""
for n in receivedMessage:
# Decode into standard i=unicode set
if (n >=32 and n <= 126):
string += chr(n)
print(string)
radio.writeAckPayload(1, ackPL, len(ackPL))
print("Loaded payload reply of {}".format(ackPL))
Everything seems to be alright, below are code returned by both scripts:
send:
STATUS = 0x03 RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=1 TX_FULL=1
RX_ADDR_P0-1 =
0xf8f8f8f8f8 0xf8f8f8f8f8
RX_ADDR_P2-5 =
0xf8
0xf9
0xf9
0xf9
TX_ADDR =
0xf8f8f8f8f8
RX_PW_P0-6 =
0x0c
0x00
0x00
0x00
0x00
0x00
EN_AA =
0x0f
EN_RXADDR =
0x00
RF_CH =
0x1c
RF_SETUP =
0x00
CONFIG =
0x03
DYNPD/FEATURE =
0x03
0x01
Data Rate = 1MBPS
Model = nRF24L01
CRC Length = Disabled
PA Power = PA_MIN
We sent the message of ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
No payload received
recv.py:
STATUS = 0x03 RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=1 TX_FULL=1
RX_ADDR_P0-1 =
0xf8f8f8f8f8 0xf8f8f8f8f8
RX_ADDR_P2-5 =
0xf8
0xf9
0xf9
0xf9
TX_ADDR =
0xf8f8f8f8f8
RX_PW_P0-6 =
0x0c
0x0c
0x00
0x00
0x00
0x00
EN_AA =
0x0f
EN_RXADDR =
0x00
RF_CH =
0x1c
RF_SETUP =
0x00
CONFIG =
0x03
DYNPD/FEATURE =
0x03
0x01
Data Rate = 1MBPS
Model = nRF24L01
CRC Length = Disabled
PA Power = PA_MIN
Received: []
Translating the receivedMessage into unicode characters...
Loaded payload reply of [1]
I don't really understand why it won't connect one to other,
Both have the same wiring:
nRF24L01-Raspberry Pi (Pin#)
GND - GND (6)
VCC - 3,3V (1)
CE - GPIO17 (11)
CSN - GPIO08(24)
SCK - GPIO11 (23)
MOSI - GPIO10 (19)
MISO - GPIO25 (22)
IRQ - unconnected
I need to send information from one RPi to second to control engine via PWM.
Can i ask for help

python mmap skipping every second byte when writing

I have a strange problem with using mmap in python when writing to memory (/dev/mem).
To be clear, reading is done in the same manner and it works OK.
But when it comes to writing, it seems that every second byte is unwritable.
ex.
when I read i get
addr 0x200 val 0x1234
but when I try to write
addr 0x200 val 0x4321
what really is written is
addr 0x200 val 0x0021
When I try to write byte by byte, the same happens.
ex.
write:
addr 0x200 0x43
addr 0x201 0x21
I get
addr 0x200 0x00
addr 0x201 0x21
Code:
class Pydump:
def __init__(self, addr, length = 1, word_size = 4, filename = '/dev/mem'):
if addr < 0 or length < 0: raise ValueError('Address or length invalid')
self._verbose = verbose
self.word_size = word_size
self.mask = ~(self.word_size - 1)
self.base_addr = addr & ~(mmap.PAGESIZE - 1)
self.addr_offset = addr - self.base_addr
self.word_length = length
self.no_of_bytes = self.word_length * self.word_size
# align length to full words
end_addr = addr + self.no_of_bytes
if end_addr % self.mask:
end_addr = (end_addr + self.word_size) & self.mask
self.map_length = end_addr - self.base_addr
self.fname = filename
self.fd = os.open(self.fname, os.O_RDWR | os.O_SYNC)
self.mem = mmap.mmap(self.fd, self.map_length, mmap.MAP_SHARED,
mmap.PROT_READ | mmap.PROT_WRITE,
offset=self.base_addr)
def read(self):
mem = self.mem
virt_base_addr = self.addr_offset & self.mask
mem.seek(virt_base_addr)
data = []
for i in range(self.no_of_bytes):
data.append(struct.unpack('B', mem.read_byte())[0])
abs_addr = self.base_addr + virt_base_addr
return PydumpBuffer(abs_addr, data, self.word_size)
def write(self, data):
mem = self.mem
virt_base_addr = self.addr_offset & self.mask
mem.seek(virt_base_addr)
if self.word_size == 1:
mem.write(struct.pack('B', data))
elif self.word_size == 2:
mem.write(struct.pack('H', data))
elif self.word_size == 4:
mem.write(struct.pack('I', data))
else:
raise ValueError('Invalid word size')
def write_bytes(self, bytes):
if len(bytes) != self.no_of_bytes: raise ValueError('Wrong number of bytes given')
mem = self.mem
virt_base_addr = self.addr_offset & self.mask
mem.seek(virt_base_addr)
for byte in bytes:
mem.write_byte(byte)
Example run (I prepared the memory with other memdump tool [bin] to be 0xEEEEEEEE):
>>> from pydump import Pydump as memdump
>>> memdump(0x18007C20, 1, 4).read()
0xEEEEEEEE
>>> memdump(0x18007C20, 1, 4).write(0x12345678)
>>> memdump(0x18007C20, 1, 4).read()
0x00340078
>>> memdump(0x18007C20, 1, 4).write(0x87654321)
>>> memdump(0x18007C20, 1, 4).read()
0x00650021
Example no 2 (I could not write even 2 bytes at 'first' byte place):
>>> memdump(0x18007C20, 1, 2).write(0xABCD)
>>> memdump(0x18007C20, 1, 4).read()
0x00650021
>>> memdump(0x18007C21, 1, 1).write(0xCD)
>>> memdump(0x18007C20, 1, 4).read()
0x00650021
>>> memdump(0x18007C22, 1, 1).write(0xCD)
>>> memdump(0x18007C20, 1, 4).read()
0x00CDCD00
Any thoughts on what could be the problem ?
I know what is wrong now.
The answer is that it's fault as well of python as my specific hardware/driver.
I looked through the implementation of mmap and it uses memcpy, which as we know copies byte by byte. In C implemention this did not happen, when we needed to write 4 bytes, 4 bytes were written.
So here comes the limitation of my hardware/driver which is that every register must be written in full (I did not know this at the time of stating the question), thus when writing byte by byte I will get weird behaviour.
Some registers are 16bit and some are 32bit.
When I was writing 0xFFFF to 16bit register I ended up having 0x00FF. So memcpy was copying 0xFF twice. My driver got write(0xFF) then write(0xFF), so what I was actually doing (because of the python) was two writes to register of byte 0xFF :)
Probably the same happened with 32bit registers, although it looked different (but also the driver may behave different).
For it to work I would have to change the implementation of python mmap :(

AttributeError: Encryption instance has no attribute '__getitem__'

I am tring to create a AES like crytography code and I am having errors that I need help with.
File "C:\Users\work\Desktop\try.py", line 156, in byte_construct
if (pos & array_8[i]):
AttributeError: Encryption instance has no attribute '__getitem__'
I keep getting the error above.Can somebody give me a solution. Following is my source
def rotate_byte_left(byte):
value = 0x00
value = byte & 0x80
byte = byte << 1
byte = byte & 0xFF
if value == 0x80:
byte = byte | 0x01
return byte
def rotate_byte_right(byte):
value = 0x00
value = byte & 0x01
byte = byte >> 1
byte = byte & 0xFF
if value == 0x01:
byte = byte | 0x80
return byte
def byte_construct(array_8,bit_pos):
byte = 0x00
for p in bit_pos:
pos = (0x01 << p)
for i in range(0,8): Specifically the error is poiting here.
if (pos & array_8[i]):
byte = byte & (0x01 << bit_pos)
return byte
def addRoundKey(self, state, roundKey):
"""Adds (XORs) the round key to the state."""
for i in range(0, len(state)):
state[i] ^= roundKey[i]
return state
def ecb(self, plaintext, key):
start = time.time()
pre_round1 = self.convert_hex_to_list(plaintext ^ key)
substitution_result = self.subBytes(pre_round1, False)
permutaion_result = self.byte_construct(substitution_result)
if __name__ == "__main__":
encrypt = Encryption()
encrypt.ecb(0x0000000000000000, 0x00FF00FF00FF00FF)
print encrypt.convert_list_to_hex([255,0,255])
The function byte_construct() is not a instance method of the class Encryption, but you are calling it like it is
permutation_result = self.byte_construct(substitution_result)
What this means is that now byte_construct would use self as its first argument (array-8 in this case), and substitution_result as its second argument (bit_pos). Since self is an Encryption object, which I'm guessing is not meant to be indexed (i.e. you didn't define self.__getitem__() for it), you've got the above error.

Fragmented TCP message in Python

I have an application which read live SIP Packets and decode information in real time.
when packet is small UDP/TCP is able to get the information, but when packet is large, it arrives in different segments:
The following is an extract from Wireshark:
3 Reassembled TCP Segments (3331 bytes): #1(1448), #3(1448), #5(435)
Frame: 1, payload: 0-1447 (1448 bytes)
Frame: 3, payload: 1448-2895 (1448 bytes)
Frame: 5, payload: 2896-3330 (435 bytes)
Segment count: 3
Reassembled TCP length: 3331
My application believes there is a new SIP Packet for each fragment and fails to decode info.
How can I do this? I need to read the packet, assemble all sip message if fragmented and pass the info to my control module. This is my current code:
s = socket.socket( socket.AF_PACKET , socket.SOCK_RAW , socket.ntohs(0x0003))
while (True):
packet = s.recvfrom(65565)
#packet string from tuple
packet = packet[0]
#parse ethernet header
eth_length = 14
eth_header = packet[:eth_length]
eth = unpack('!6s6sH' , eth_header)
eth_protocol = socket.ntohs(eth[2])
if eth_protocol == 8 :
#Parse IP header
#take first 20 characters for the ip header
ip_header = packet[eth_length:20+eth_length]
#now unpack them :)
iph = unpack('!BBHHHBBH4s4s' , ip_header)
version_ihl = iph[0]
version = version_ihl >> 4
ihl = version_ihl & 0xF
iph_length = ihl * 4
ttl = iph[5]
protocol = iph[6]
s_addr = socket.inet_ntoa(iph[8]);
d_addr = socket.inet_ntoa(iph[9]);
#TCP protocol
if protocol == 6 :
t = iph_length + eth_length
tcp_header = packet[t:t+20]
#now unpack them :)
tcph = unpack('!HHLLBBHHH' , tcp_header)
source_port = tcph[0]
dest_port = tcph[1]
sequence = tcph[2]
acknowledgement = tcph[3]
doff_reserved = tcph[4]
tcph_length = doff_reserved >> 4
if dest_port == sipLocatorConfig.SIP_PORT:
print
logging.info("------------------------------------------------------SIP Packet detected------------------------------------------------------")
h_size = eth_length + iph_length + tcph_length * 4
data_size = len(packet) - h_size
#get data from the packet
data = packet[h_size:]
ipInfo = {}
ipInfo['protocol'] = protocol
ipInfo['s_addr'] = str(s_addr)
ipInfo['source_port'] = source_port
ipInfo['d_addr'] = str(d_addr)
ipInfo['dest_port'] = dest_port
processSipPacket(data,ipInfo)
I believe this is what I wrote bufsock for:
http://stromberg.dnsalias.org/~strombrg/bufsock.html
It allows you to say "give me all the data until the next null" or "give me the next 64 bytes" and similar things. It deals intelligently with fragmented and aggregated packets.
Unlike many such tools, it does not require that you have bufsock at both the producer and the consumer - you can use it fine on one end and not the other. It is a little bit like stdio for sockets, in python.
It works on CPython 2.x, CPython 3.x, Pypy, Pypy3 (which is still beta at this time) and Jython.

Python HyBi10 websocket server

I've struggled the past 2 hours with the new Websocket version. I've managed to get the handshake and receiving these new frames, but I'm having problems sending them now.
I'm encoding my text like this:
def encode_hybi(buf, opcode, base64=False):
""" Encode a HyBi style WebSocket frame.
Optional opcode:
0x0 - continuation
0x1 - text frame (base64 encode buf)
0x2 - binary frame (use raw buf)
0x8 - connection close
0x9 - ping
0xA - pong
"""
if base64:
buf = b64encode(buf)
b1 = 0x80 | (opcode & 0x0f) # FIN + opcode
payload_len = len(buf)
if payload_len <= 125:
header = struct.pack('>BB', b1, payload_len)
elif payload_len > 125 and payload_len < 65536:
header = struct.pack('>BBH', b1, 126, payload_len)
elif payload_len >= 65536:
header = struct.pack('>BBQ', b1, 127, payload_len)
#print("Encoded: %s" % repr(header + buf))
#return header + buf, len(header), 0
return header+buf
But I don't know in what form I have to pour it to send it over the socket.
By the way: isn't there some easy python websocket module somewhere? My code has now seen 3 websocket versions and it's an utter mess.

Categories

Resources