Advertisement
Guest User

Untitled

a guest
Nov 29th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #
  4. # Author: Calum Hunter
  5. # Date: 28/11/2016
  6. # Version: 1.0
  7. # Purpose: To generate a Mac UID from the objectGUID attribute
  8. # (GeneratedUID) in AD.
  9. # This uses the same method that the Apple
  10. # AD Plugin uses
  11. #
  12.  
  13. ## Start by loading up our ldap query variables
  14. SVC_ACCOUNT_NAME="Username"
  15. SVC_ACCOUNT_PASS="Password"
  16. DOMAIN="my.domain"
  17. LDAP_SERVER="dc.my.domain:389"
  18. SEARCH_BASE="OU=Users,DC=My,DC=Domain"
  19.  
  20. DECODE_BASE64(){
  21. # This function takes the encoded output from ldapsearch and decodes it
  22. # It then needs to be "hex-dumped" in order to get it into regular text
  23. # So that we can work with it
  24. OBJECT_ID="$1"
  25. BASE64_DECODED=$(echo $OBJECT_ID | base64 -D)
  26. G=($(echo ${BASE64_DECODED} | hexdump -e '1/1 " %02X"'))
  27. OBJECTGUID="${G[3]}${G[2]}${G[1]}${G[0]}-${G[5]}${G[4]}-${G[7]}${G[6]}-${G[8]}${G[9]}-${G[10]}${G[11]}${G[12]}${G[13]}${G[14]}${G[15]}"
  28. }
  29.  
  30. # Search LDAP for our user account
  31. RESULT=$(ldapsearch -LLL -H ldap://$LDAP_SERVER -E pr=1000/noprompt -o ldif-wrap=no -x -D ${SVC_ACCOUNT_NAME}@$DOMAIN -w ${SVC_ACCOUNT_PASS} -b "${SEARCH_BASE}" \
  32. -s sub -a always "(objectClass=user)" "sAMAccountName" "objectGUID")
  33.  
  34. i=1
  35. s=1
  36. declare -a RESULT_ARRAY
  37. while IFS= read -r line; do
  38. # If we find an empty line, then we increase the counter (i),
  39. # set the flag (s) to one, and skip to the next line
  40. [[ $line == "" ]] && ((i++)) && s=1 && continue
  41.  
  42. # If the flag (s) is zero, then we are not in a new line of the block
  43. # so we set the value of the array to be the previous value concatenated
  44. # with the current line
  45. [[ $s == 0 ]] && RESULT_ARRAY[$i]="${RESULT_ARRAY[$i]}
  46. $line" || {
  47. # Otherwise we are in the first line of the block, so we set the value
  48. # of the array to the current line, and then we reset the flag (s) to zero
  49. RESULT_ARRAY[$i]="$line"
  50. s=0;
  51. }
  52. done <<< "$RESULT"
  53.  
  54. for USER in "${RESULT_ARRAY[@]}"; do
  55. USER_DN=$(echo "$USER" | grep "dn:")
  56. USER_GUID_BASE64=$(echo "$USER" | awk -F "::" '/objectGUID/ {print $2}')
  57. # Get our GeneratedUID from LDAPSEARCH by decoding and hex dumping it
  58. DECODE_BASE64 "$USER_GUID_BASE64"
  59. # Now lets get the first 32 bits of our GUID
  60. GUID_32=$(echo "$OBJECTGUID" | awk -F "-" '{print $1}')
  61. # Now convert this to decimal
  62. GUID_32_DEC=$(echo "ibase=16; $GUID_32" | bc)
  63. if [ $GUID_32_DEC -gt 2147483647 ]; then
  64. # Get the first character of our 32bit GUID
  65. FIRST_CHAR=${GUID_32:0:1}
  66. # Use the below table to replace the first character with number it represents. ie: A=2
  67. case $FIRST_CHAR in
  68. A)
  69. NUMBER=2 ;;
  70. B)
  71. NUMBER=3 ;;
  72. C)
  73. NUMBER=4 ;;
  74. D)
  75. NUMBER=5 ;;
  76. E)
  77. NUMBER=6 ;;
  78. F)
  79. NUMBER=7 ;;
  80. 9)
  81. NUMBER=1 ;;
  82. 8)
  83. NUMBER=0 ;;
  84. *)
  85. esac
  86. # Now lets replace the first character with our new number
  87. A=$(echo $GUID_32 | cut -c2-)
  88. NEW_32_GUID="${NUMBER}${A}"
  89. GUID_32_DEC=$(echo "ibase=16; $NEW_32_GUID" | bc)
  90. fi
  91. # Echo our output
  92. USERNAME=$(echo $USER_DN | awk -F "dn:" '{print $2}')
  93. echo "$USERNAME,$GUID_32_DEC"
  94. echo "$USERNAME,$GUID_32_DEC" >> users_with_UID.csv
  95. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement