autobinscope.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env python
  2. # This Source Code Form is subject to the terms of the Mozilla Public
  3. # License, v. 2.0. If a copy of the MPL was not distributed with this
  4. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  5. # run Microsoft's Binscope tool (http://www.microsoft.com/download/en/details.aspx?id=11910)
  6. # against a fresh Windows build. output a 'binscope.log' file with full details
  7. # of the run and appropriate strings to integrate with the buildbots
  8. # from the docs : "The error code returned when running under the command line is equal
  9. # to the number of failures the tool reported plus the number of errors. BinScope will return
  10. # 0 only if there are no errors or failures."
  11. # the symbol dir should point to the symbol dir hierarchy created
  12. # via running make buildsymbols in a windows build's objdir
  13. import sys
  14. import subprocess
  15. import os
  16. BINSCOPE_OUTPUT_LOGFILE = r".\binscope_xml_output.log"
  17. # usage
  18. if len(sys.argv) < 3:
  19. print """usage : autobinscope.by path_to_binary path_to_symbols [log_file_path]"
  20. log_file_path is optional, log will be written to .\binscope_xml_output.log by default"""
  21. sys.exit(0)
  22. binary_path = sys.argv[1]
  23. symbol_path = sys.argv[2]
  24. if len(sys.argv) == 4:
  25. log_file_path = sys.argv[3]
  26. else:
  27. log_file_path = BINSCOPE_OUTPUT_LOGFILE
  28. # execute binscope against the binary, using the BINSCOPE environment
  29. # variable as the path to binscope.exe
  30. try:
  31. binscope_path = os.environ['BINSCOPE']
  32. except KeyError:
  33. print "BINSCOPE environment variable is not set, can't check DEP/ASLR etc. status."
  34. sys.exit(0)
  35. try:
  36. proc = subprocess.Popen([binscope_path, "/target", binary_path,
  37. "/output", log_file_path, "/sympath", symbol_path,
  38. "/c", "ATLVersionCheck", "/c", "ATLVulnCheck", "/c", "SharedSectionCheck", "/c", "APTCACheck", "/c", "NXCheck",
  39. "/c", "GSCheck", "/c", "GSFriendlyInitCheck",
  40. "/c", "CompilerVersionCheck", "/c", "SafeSEHCheck", "/c", "SNCheck",
  41. "/c", "DBCheck"], stdout=subprocess.PIPE)
  42. except WindowsError, (errno, strerror):
  43. if errno != 2 and errno != 3:
  44. print "Unexpected error ! \nError " + str(errno) + " : " + strerror + "\nExiting !\n"
  45. sys.exit(0)
  46. else:
  47. print "Could not locate binscope at location : %s\n" % binscope_path
  48. print "Binscope wasn't installed or the BINSCOPE env variable wasn't set correctly, skipping this check and exiting..."
  49. sys.exit(0)
  50. proc.wait()
  51. output = proc.communicate()[0]
  52. # is this a PASS or a FAIL ?
  53. if proc.returncode != 0:
  54. print "Error count: %d" % proc.returncode
  55. print "TEST-UNEXPECTED-FAIL | autobinscope.py | %s is missing a needed Windows protection, such as /GS or ASLR" % binary_path
  56. logfile = open(log_file_path, "r")
  57. for line in logfile:
  58. print(line),
  59. else:
  60. print "TEST-PASS | autobinscope.py | %s succeeded" % binary_path