Ejemplo n.º 1
0
def GetUnloadSymbolCommand(EnclaveFile, Base):
    text = readelf.ReadElf(EnclaveFile)
    if text == None:
        return -1
    SegsFile = StringIO(text)

    try:
        FileList = SegsFile.readlines()
        # Parse the readelf output file to extract the section names and
        # their offsets and add the Proj base address.
        for line in FileList:
            list = line.split()
            if (len(list) > 0):
                SegOffset = -1
                # The readelf will put a space after the open bracket for single
                # digit section numbers.  This causes the line.split to create
                # an extra element in the array for these lines.
                if (re.match('\[\s*[0-9]+\]', list[0])):
                    SegOffset = 0
                if (re.match('\s*[0-9]+\]', list[1])):
                    SegOffset = 1

                if (SegOffset != -1):
                    if (list[SegOffset + 1][0] == '.'):
                        # If it is the .text section, get the .text start address and plus enclave start address
                        if (list[SegOffset + 1].find(".text") != -1):
                            return "remove-symbol-file -a " + str(
                                int(list[SegOffset + 3], 16) + int(Base, 10))

    except:
        print("Error parsing enclave file.  Check format of file.")
        return -1
Ejemplo n.º 2
0
def GetLoadSymbolCommand(EnclaveFile, Base):
    text = readelf.ReadElf(EnclaveFile)
    if text == None:
        return -1
    SegsFile = StringIO(text)

    try:
        FileList = SegsFile.readlines()
        n = 4
        m = 100
        Out = [[[] for ni in range(n)] for mi in range(m)]
        i = 0
        # Parse the readelf output file to extract the section names and
        # their offsets and add the Proj base address.
        textSectionFound = False
        for line in FileList:
            list = line.split()
            if (len(list) > 0):
                SegOffset = -1
                # The readelf will put a space after the open bracket for single
                # digit section numbers.  This causes the line.split to create
                # an extra element in the array for these lines.
                if (re.match('\[\s*[0-9]+\]', list[0])):
                    SegOffset = 0
                if (re.match('\s*[0-9]+\]', list[1])):
                    SegOffset = 1

                if (SegOffset != -1):
                    if (list[SegOffset + 1][0] == '.'):
                        if (list[SegOffset + 1].find(".text") != -1):
                            textSectionFound = True
                            Out[i][0] = list[SegOffset + 1]
                            Out[i][1] = str(
                                int(list[SegOffset + 3], 16) + int(Base, 10))
                            i = i + 1
                        elif (list[SegOffset + 1] == ".tdata"):
                            continue
                        #print "%#08x" % (int(list[SegOffset+3], 16))
                        elif (int(list[SegOffset + 3], 16) != 0):
                            Out[i][0] = list[SegOffset + 1]
                            Out[i][1] = str(
                                int(list[SegOffset + 3], 16) + int(Base, 10))
                            i = i + 1
        if (textSectionFound == True):
            # Write the LLDB 'target modules add' command with all the arguments to the setup LLDB command file.
            lldbcmd = "target modules add " + EnclaveFile
            lldbcmd += "\n"
            # Write the LLDB 'target modules load' command with all the arguments to the setup LLDB command file.
            lldbcmd += "target modules load --file " + EnclaveFile
            for j in range(i):
                lldbcmd += " " + Out[j][0] + " " + '%(Location)#08x' % {
                    'Location': int(Out[j][1])
                }
            return lldbcmd
        else:
            return -1
    except:
        print("Error parsing enclave file.  Check format of file.")
        return -1
Ejemplo n.º 3
0
def GetLoadSymbolCommand(EnclaveFile, Base):
    text = readelf.ReadElf(EnclaveFile)
    if text == None:
        return -1
    SegsFile = StringIO(text)

    try:
        FileList = SegsFile.readlines()
        n = 4
        m = 100
        Out = [[[] for ni in range(n)] for mi in range(m)]
        i = 0
        Out[99][2] = '0'
        # Parse the readelf output file to extract the section names and
        # their offsets and add the Proj base address.
        for line in FileList:
            list = line.split()
            if (len(list) > 1):
                SegOffset = -1
                # The readelf will put a space after the open bracket for single
                # digit section numbers.  This causes the line.split to create
                # an extra element in the array for these lines.
                if (re.match('\[\s*[0-9]+\]', list[0])):
                    SegOffset = 0
                if (re.match('\s*[0-9]+\]', list[1])):
                    SegOffset = 1

                if (SegOffset != -1):
                    if (list[SegOffset + 1][0] == '.'):
                        # If it is the .text section, put it in a special place in the array
                        # because the 'add-symbol-file' command treats it differently.
                        #print "%#08x" % (int(list[SegOffset+3], 16))
                        if (list[SegOffset + 1] == ".text"):
                            Out[99][0] = "-s"
                            Out[99][1] = list[SegOffset + 1]
                            Out[99][2] = str(
                                int(list[SegOffset + 3], 16) + int(Base, 10))
                            Out[99][3] = " "
                        elif (int(list[SegOffset + 3], 16) != 0):
                            Out[i][0] = "-s"
                            Out[i][1] = list[SegOffset + 1]
                            Out[i][2] = str(
                                int(list[SegOffset + 3], 16) + int(Base, 10))
                            Out[i][3] = " "
                            i = i + 1
        if ('0' != Out[99][2]):
            # The last section must not have the '\' line continuation character.
            Out[i - 1][3] = ''

            # Write the GDB 'add-symbol-file' command with all the arguments to the setup GDB command file.
            # Note: The mandatory argument for the 'add-symbol-file' command is the .text section without a
            # '-s .SectionName'.  All other sections need the '-s .SectionName'.
            gdbcmd = "add-symbol-file '" + EnclaveFile + "' " + '%(Location)#08x' % {
                'Location': int(Out[99][2])
            } + " "
            for j in range(i):
                gdbcmd += Out[j][0] + " " + Out[j][
                    1] + " " + '%(Location)#08x' % {
                        'Location': int(Out[j][2])
                    } + " " + Out[j][3]
        else:
            return -1

        return gdbcmd

    except:
        print("Error parsing enclave file.  Check format of file.")
        return -1