sercomm-payload.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env python3
  2. import argparse
  3. import hashlib
  4. import os
  5. def create_output(args):
  6. in_st = os.stat(args.input_file)
  7. in_size = in_st.st_size
  8. in_f = open(args.input_file, 'r+b')
  9. in_bytes = in_f.read(in_size)
  10. in_f.close()
  11. if (args.pid_file):
  12. pid_st = os.stat(args.pid_file)
  13. pid_size = pid_st.st_size
  14. pid_f = open(args.pid_file, 'r+b')
  15. pid_bytes = pid_f.read(pid_size)
  16. pid_f.close()
  17. else:
  18. pid_bytes = bytes.fromhex(args.pid)
  19. sha256 = hashlib.sha256()
  20. sha256.update(in_bytes)
  21. out_f = open(args.output_file, 'w+b')
  22. out_f.write(pid_bytes)
  23. out_f.write(sha256.digest())
  24. out_f.write(in_bytes)
  25. out_f.close()
  26. def main():
  27. global args
  28. parser = argparse.ArgumentParser(description='')
  29. parser.add_argument('--input-file',
  30. dest='input_file',
  31. action='store',
  32. type=str,
  33. help='Input file')
  34. parser.add_argument('--output-file',
  35. dest='output_file',
  36. action='store',
  37. type=str,
  38. help='Output file')
  39. parser.add_argument('--pid-file',
  40. dest='pid_file',
  41. action='store',
  42. type=str,
  43. help='Sercomm PID file')
  44. parser.add_argument('--pid',
  45. dest='pid',
  46. action='store',
  47. type=str,
  48. help='Sercomm PID')
  49. args = parser.parse_args()
  50. if ((not args.input_file) or
  51. (not args.output_file) or
  52. (not args.pid_file and not args.pid)):
  53. parser.print_help()
  54. create_output(args)
  55. main()