I have some python code below I managed to cobble together to achieve what I needed, but being quite new to python I believe there should be a much more elegant solution than the one I have here.
Essentially I need to take an arbitrary string (MAGICSTRING) and convert each of it's characters into their hex value, then pad a 0x00 to the start and end of the resultant hex string, and finally put the hex value of the length of the entire padded string on the end. I then need these escaped so it can be passed to a socket connection later on.
In the example, MAGICSTRING is 1234, and the expected result of 'value' is '\x00\x31\x32\x33\x34\x00\x06'
See below example.
MAGICSTRING = '1234'
i=0
hexpass = ''
while i < len(MAGICSTRING):
hexpass = hexpass + hex(ord(MAGICSTRING[i]))[2:]
i += 1
strlen = len(MAGICSTRING) + 2
sendstr = '00'+ hexpass + '00' + '0' + hex(strlen)[2:]
value = sendstr.decode('hex')
Appreciate any improvements that can be made to the above.
Aucun commentaire:
Enregistrer un commentaire