parse.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/bin/env python3
  2. import os
  3. import re
  4. import sys
  5. def generate_binary_from_log(logfile):
  6. """
  7. Parses a log file to extract binary data sections and writes them to separate files.
  8. Args:
  9. logfile (str): Path to the log file to process.
  10. Returns:
  11. bool: True if processing was successful, False otherwise.
  12. """
  13. # Check if the log file exists
  14. if not os.path.isfile(logfile):
  15. print(f"{logfile} does not exist. Please check!")
  16. return False
  17. # Define regular expressions for data start/end patterns (consider making them more specific)
  18. datastart_pattern = r"Dump\s+.+?\s+start"
  19. dataend_pattern = r"Dump\s+.+?\s+finish"
  20. # Track processing state (0: idle, 1: collecting hex data, 2: generating binary file)
  21. state = 0
  22. # Initialize variables for storing hex data and generated filename
  23. hexstr = ""
  24. genfilename = ""
  25. # Open the log file in read mode
  26. with open(logfile, "r") as lf:
  27. for line in lf.readlines():
  28. # Remove leading/trailing whitespace
  29. line = line.strip()
  30. # Skip empty lines
  31. if not line:
  32. continue
  33. # Check for data start pattern
  34. if re.search(datastart_pattern, line):
  35. # Reset state for new data section
  36. state = 1
  37. hexstr = ""
  38. genfilename = ""
  39. continue
  40. # Check for data end pattern
  41. if re.search(dataend_pattern, line):
  42. # Reset state for idle
  43. state = 0
  44. continue
  45. # Check for "CREATE" line indicating filename
  46. if line.startswith("CREATE"):
  47. # Extract filename and reset state for collecting hex data
  48. state = 2
  49. genfilename = line.strip("CREATE:").strip()
  50. # Append hex data to hexstr while collecting data
  51. if state == 1:
  52. hexstr += line
  53. # Process extracted hex data and create binary file
  54. if state == 2 and genfilename:
  55. try:
  56. # Attempt to convert hex string to bytes (handle potential conversion errors)
  57. binarydata = bytes.fromhex(hexstr)
  58. print(f"Generating {genfilename}")
  59. # Open the binary file in write-binary mode
  60. with open(genfilename, "wb") as wf:
  61. wf.write(binarydata)
  62. except ValueError:
  63. print(f"Error: Invalid hex data when creating : {genfilename}")
  64. # Reset variables for next data section
  65. hexstr = ""
  66. genfilename = ""
  67. state = 1
  68. return True
  69. # Call in Nuclei Studio IDE Terminal in a Project Directory like this
  70. # NOTE: prof.log is the console log print in qemu console or uart console like Dump xxx start ... Dump xxx finish
  71. # python nuclei_sdk/Components/profiling/parse.py prof.log
  72. if __name__ == "__main__":
  73. if len(sys.argv) > 1:
  74. logfile = sys.argv[1]
  75. print(f"Parsing log file {logfile}")
  76. generate_binary_from_log(logfile)
  77. else:
  78. print(f"Help: {sys.argv[0]} logfile")