Advertisement
opexxx

unhex.awk

Jul 12th, 2014
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.49 KB | None | 0 0
  1. ########################################################
  2. # AWK script to convert hex encoded ASCII strings back #
  3. # in to the printable ASCII characters that they       #
  4. # represent.                                           #
  5. # Non-printable characters are converted to a '?'.     #
  6. #                                                      #
  7. # unhex.awk v2013.04.01                                #
  8. # http://malwaremusings.com/scripts/unhex-awk          #
  9. #                                                      #
  10. # usage: awk -f unhex.awk <inputfilename>              #
  11. #   where inputfilename is a text file containing hex  #
  12. #   encoded (0x...) strings.                           #
  13. ########################################################
  14.  
  15.  
  16. /0x/ {
  17.   while (s = match($0,"0[Xx][0-9A-Fa-f]+",matcharray) > 0) {
  18.     chrstr = "";
  19.     hexdigits = "0123456789abcdef";
  20.     word = tolower(matcharray[0]);
  21.     value = 0;
  22.     for (nibble = 3;nibble <= length(word);nibble++) {
  23.       char = substr(word,nibble,1);
  24.       idx = index(hexdigits,char) - 1;
  25.       value += idx * ((nibble % 2) == 1?16:1);
  26.       if (idx == -1) printf("WARNING: Invalid hex digit %c\n",char) >"/dev/stderr";
  27.       if (nibble % 2 == 0) {
  28.         if ((value >= 0x20) && (value < 0x7f)) {
  29.           chrstr = chrstr sprintf("%c",value);
  30.         } else {
  31.           chrstr = chrstr "?";
  32.         }
  33.         value = 0;
  34.       }
  35.     }
  36.     $0 = substr($0,1,RSTART - 1) "0x\"" chrstr "\"" substr($0,RSTART + RLENGTH);
  37.   }
  38.   print;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement