extract_fuzzer_dictionary.sh 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env bash
  2. # usage: script.sh target-dictionary-filename
  3. # This script searches for interesting strings in the source code and converts
  4. # them into a standard fuzzer dictionary file.
  5. #
  6. # Note that this script is phased out in favor of the more sophisticated
  7. # extract_fuzzer_dictionary.py program
  8. # TODO known issues: the end result has some duplicates in it
  9. TARGET_DIR=../tests
  10. OUTPUT_FILE=${1:-fuzzer_crypto_tests_strings_dictionary1.txt}
  11. multiline_string_search() {
  12. # TODO the `find` regex behavior is Linux-specific
  13. find $TARGET_DIR -type f -regextype posix-extended -regex '.*\.(c|h|py|json|java|js)' | xargs cat | perl -p0e 's/"\s*\n\s*\"//smg'
  14. }
  15. # ensure empty file
  16. echo -n "" > $OUTPUT_FILE
  17. # strip multiline strings and extract them
  18. # exclude some hex strings, but allow hex strings with mixed capitalization (Ethereum, rskip60)
  19. multiline_string_search | grep -P -o "\"[\w ]+\"" | grep -v -P "\"(([0-9a-f][0-9a-f])+|([0-9A-F][0-9A-F])+)\"" | sort | uniq | while read -r line ; do
  20. echo "$line" >> $OUTPUT_FILE
  21. done
  22. # extract individual BIP39 and SLIP39 words
  23. # TODO are those actually valuable as fuzzer dictionary input?
  24. # grep -r -P -o -h "\"\w+\"" ../slip39_wordlist.h ../bip39_english.h | sort | uniq >> fuzzer_crypto_tests_strings_dictionary1.txt
  25. # extract and convert binary input data from the unit tests
  26. # find each file, cat it, concatenate multiline strings, look for hex strings in quotes
  27. # note that this returns multiple megabyte of result strings due to the large amount
  28. # of test cases in the wycheproof project subfolder
  29. multiline_string_search | grep -P -o "\"([0-9a-fA-F][0-9a-fA-F])+\"" | grep -P -o "([0-9a-fA-F][0-9a-fA-F])+" | sort | uniq | while read -r line ; do
  30. # turn ascii hex strings AA into \xaa for the fuzzer format and add quotes
  31. # extra backslash escape due to the bash nesting
  32. escaped_hex=`echo $line | sed -e 's/../\\\\x&/g'`
  33. echo "\"$escaped_hex\"" >> $OUTPUT_FILE
  34. done
  35. # search and reassemble BIP39 test seeds that span multiple lines
  36. # find each file, cat it, concatenate multiline strings, look for BIP39 seed combinations with reasonable length
  37. multiline_string_search | grep -Po "(\w{3,10} ){11,23}(\w{3,10})" | sort | uniq | while read -r line ; do
  38. echo "\"$line\"" >> $OUTPUT_FILE
  39. done