Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.30 KB | None | 0 0
  1. # Function to quickly rotate ASCII characters by 90 places
  2. # emulates charXor in a lazy way but is infinitely faster
  3. charRotate() {
  4.   # shellcheck disable=SC2020
  5.   tr ' !"#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~' 'z{|}~ !"#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxy'
  6. }
  7.  
  8. # xor function derived from http://zurlinux.com/?p=712
  9. charXor() {
  10.   while read -r line; do
  11.     # Start by removing whitespace to reduce character bias
  12.     # pipe to 'od' which converts each character to one octal per line
  13.     for i in $(printf "%s" "${line}" | tr -d " " | od -A n -t o1 -w1 -v); do
  14.       # for each octal, run an xor and then print it
  15.       # shellcheck disable=SC2059
  16.       printf \\"$(printf '%03o' "$(( i ^ 90 ))" )"
  17.    done
  18.  done
  19. }
  20.  
  21. # Redacted stuff
  22.  
  23.  # Check we have the required binaries
  24.  ErrCount=0
  25.  for Prog in od tr tail head; do
  26.    if ! command -v "${Prog}" >/dev/null 2>&1; then
  27.      printf "%s\n" "[ERROR]: '${Prog}' is required but was not found."
  28.      ErrCount=$(( ErrCount + 1 ))
  29.    fi
  30.  done
  31.  
  32.  # If any packages are missing, then ErrCount's going
  33.  # to be greater than 0, in which case, exit.
  34.  if [ "${ErrCount}" -gt 0 ]; then
  35.    exit 1
  36.  fi
  37.  
  38.  # Let's try and find a hashing method, we use this sparingly for now
  39.  for cryptopt in sha256sum sha1sum shasum md5sum digest csum cksum; do
  40.    if command -v "${cryptopt}" > /dev/null 2>&1; then
  41.      crypt="${cryptopt}"
  42.      if [ "${crypt}" = digest ]; then
  43.        crypt="digest -v -a md5"
  44.      fi
  45.      break
  46.    else
  47.      printf "%s\n" "[ERROR]: no hash method is available"
  48.      exit 1
  49.    fi
  50.  done
  51.  
  52.  # We need some "entropy", so let's first check for an existing entropy file from
  53.  # a previous invocation.  If found, we want to rotate its characters
  54.  if [ -f /tmp/entropy ]; then
  55.    charRotate < /tmp/entropy > /tmp/entropy2
  56.    mv /tmp/entropy2 /tmp/entropy
  57.  
  58.  # Otherwise, let's generate some "entropy" by simply running through this list of commands
  59.  # And trying to remove as much whitespace as possible, then do a fast 'xor'
  60.  # Some of these may fail, so we redirect stderr into the stream anyway.  Throw it all at the wall, see what sticks.
  61.  else
  62.    {
  63.      ps aux
  64.      printf "%s" "$$"
  65.      top -n 1
  66.      date '+%H%M%S'
  67.      df -k
  68.      printf "%s" "$$"
  69.      netstat -a
  70.      date '+%H%M%S'
  71.      vmstat -i
  72.      printf "%s" "$$"
  73.      vmstat -s
  74.      date '+%H%M%S'
  75.      iostat
  76.      printf "%s" "$$"
  77.      pstat -afipSsT
  78.    } 2>&1 | tr -d " " | tr -d "\n" | charRotate > /tmp/entropy
  79.  fi
  80.  
  81. # Redacted stuff
  82.  
  83.    # Now we have to churn the entropy file.  We remove the leading 32 Bytes
  84.    # of the entropy file, and temporarily store the result
  85.    tail -c +33 /tmp/entropy | tr -d "\n" > /tmp/entropy2
  86.  
  87.    {
  88.      # Seed some more data into the temporary entropy file, here we hash the PID
  89.      printf "%s" "$$" | charXor | tr -d " " | tr -d "\n" | "${crypt}"
  90.      # Rotate the entropy, get the first 64 chars, xor it to the bottom of the temp file
  91.      printf "%s" "$(fold -b -w 64 /tmp/entropy | head -n 1 | charXor)"
  92.      date '+%H%M%S' | charXor | tr -d " " | tr -d "\n"
  93.    } >> /tmp/entropy2
  94.  
  95.    # Move the temporary file into place
  96.    mv /tmp/entropy2 /tmp/entropy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement