Skip to content

Instantly share code, notes, and snippets.

@mtnmts
Last active June 28, 2016 20:41
Show Gist options
  • Save mtnmts/3df8abde9734f8380c37 to your computer and use it in GitHub Desktop.
Save mtnmts/3df8abde9734f8380c37 to your computer and use it in GitHub Desktop.
Parses intel's microcode dat file into the actual inc's
# Author : Matan M. Mates
# Purpose: Parse Intel's microcode files into binaries
import re
import os
MICROCODE_FILE = "./microcode.dat"
LICENSE_MARKER = '/---'
OUTPUT_DIR = '.' + os.sep + "parsed_files"
COMMENT_CLOSE_MARKER_RE, COMMENT_START_MARKER_RE = "\*/", "/\*"
COMMENT_CLOSE_MARKER, COMMENT_START_MARKER = "*/", "/*"
def clean_data(raw_data):
# Clear License
data = raw_data[raw_data.index(LICENSE_MARKER)+len(LICENSE_MARKER):]
# Clear date, first comment is the date
return data[re.search(COMMENT_CLOSE_MARKER_RE, data).end():]
def binarize_file_data(data):
""" File data is in comma seperated yucky hex..., fix it """
return data.replace('0x','').replace(',','').decode('hex')
def parse_data(data):
"""
Return list of (filename,bindata)
"""
stop = False
# Spaces and newlines are for losers
data = data.replace('\n','').replace(' ','').replace('\t','')
files = []
while COMMENT_START_MARKER in data:
file_name = data[data.index(COMMENT_START_MARKER) + len(COMMENT_START_MARKER):data.index(COMMENT_CLOSE_MARKER)]
data = data[data.index(COMMENT_CLOSE_MARKER) + len(COMMENT_CLOSE_MARKER):]
# Handle the last file with try
try:
file_data = data[:data.index(COMMENT_START_MARKER)]
data = data[data.index(COMMENT_START_MARKER):]
except:
file_data = data
pass
files.append((file_name, binarize_file_data(file_data)))
return files
def main():
data = open(MICROCODE_FILE, 'rb').read()
actual_data = clean_data(data)
files = parse_data(actual_data)
print "[*] There are {filec} files".format(filec=len(files))
print "[*] Attempting to craete output directory: " + OUTPUT_DIR
try:
os.stat(OUTPUT_DIR)
print "[!] Directory exists, Removing it."
os.rmdir(OUTPUT_DIR)
except:
pass
os.mkdir(OUTPUT_DIR)
print "[*] Saving Files"
for f in files:
open(OUTPUT_DIR + os.sep + f[0], 'wb').write(f[1])
print "[*] Finished"
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment