Advertisement
Guest User

Convert AD objectGUID bytes to readable string

a guest
Sep 19th, 2014
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. def objectGUIDAsString(bytes):
  2.         """
  3.         Returns an AD objectGUID as a human-readable string.  Takes into consideration big-endian formatting as follows:
  4.         <reversed first four bytes>-<reversed second two bytes>-<reversed third two bytes>-<normal fourth two bytes>-<normal last 6 bytes>
  5.         """
  6.        
  7.         ## python-ldap reads AD objectGUIDs incorrectly. Unpack each byte from the objectGUID as an integer, then translate back to hex and order correctly
  8.         result =""
  9.         byteIntVals=struct.unpack('!BBBBBBBBBBBBBBBB',bytes)
  10.         for byteInt in byteIntVals:
  11.             result = result + hex(byteInt).replace("0x",",")
  12.         resultByteList=result[1:].split(",")
  13.        
  14.         #Prepend zero to single-digit values in byte list.
  15.         for strByte in resultByteList:
  16.             if len(strByte) == 1:
  17.                 resultByteList[resultByteList.index(strByte)] = "0" + strByte
  18.                
  19.         return resultByteList[3] + resultByteList[2] + resultByteList[1] + resultByteList[0] + "-" \
  20.                + resultByteList[5] + resultByteList[4] + "-" \
  21.                + resultByteList[7] + resultByteList[6] + "-" \
  22.                + resultByteList[8] + resultByteList[9] + "-" \
  23.                + resultByteList[10] + resultByteList[11] + resultByteList[12] + resultByteList[13] + resultByteList[14] + resultByteList[15]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement