Skip to content

Instantly share code, notes, and snippets.

@Ern-st
Created December 23, 2017 21:37
Show Gist options
  • Save Ern-st/05bded95cec7a2eede428379e86c1a7b to your computer and use it in GitHub Desktop.
Save Ern-st/05bded95cec7a2eede428379e86c1a7b to your computer and use it in GitHub Desktop.
nc3ctf2017 - fragfs
#!/usr/bin/env python
import os
f = open("three.dd","rb")
fragfs = bytearray(f.read());
f.close();
def createPath(path):
dir = os.path.dirname(path)
if not os.path.exists(dir):
os.makedirs(dir)
sectorSize = 4096
dataStart = sectorSize * 20
sectors = [bytearray(fragfs[i:i + sectorSize]) for i in xrange(0, len(fragfs), sectorSize)]
headerSeperator = bytearray.fromhex(u"00 ff ff 00 ff 00")
headersPathTable, headersOffsetTable = fragfs[512:dataStart].split(headerSeperator)
headerPathPairSeperator = bytearray.fromhex(u"00 FF FF")
headersPathTable = [(x[0:32], x[33:]) for x in headersPathTable.split(headerPathPairSeperator)]
headersOffsetPairSeperator = bytearray.fromhex(u"00 FF")
headersOffsetSeperator = bytearray.fromhex(u"00")
headersOffsetTable = [(x.split(headersOffsetSeperator)) for x in headersOffsetTable.split(headersOffsetPairSeperator)]
allFiles = dict()
for fileID, filePATH in headersPathTable:
allFiles[fileID.decode("utf-8")] = {"filePath": filePATH.decode("utf-8")}
for file in headersOffsetTable:
if file[0].decode("utf-8") != u"":
allFiles[file[0].decode("utf-8")]["offsets"] = [int(x) for x in file[1].decode("utf-8").split(" ")]
for key in allFiles:
path = allFiles[key]["filePath"]
frame = bytearray()
for offset in allFiles[key]["offsets"]:
sector = sectors[offset]
frame.extend(sector)
createPath(path)
file = open(path, "wb")
file.write(frame)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment