Exemplo n.º 1
0
 def compile_test(self, infile):
     sf.sflog.info("Compiling test program %s" % infile)
     if infile.endswith(".c"):
         outfile = infile[:-2]
     else:
         outfile = infile + ".tst"
     cmd = "%s %s -o %s %s" % (self.CC, self.TEST_CFLAGS, outfile, infile)
     sf.sflog.debug("      ------[Test compilation command]------\n%s" %
                    cmd)
     sf.sflog.debug("      --------------------------------------")
     f = os.popen(cmd)
     out = f.read()
     status = f.close()
     if status:
         if status & 0xff:
             sf.sflog.error(
                 "compiler killed by signal %i while compiling test program"
                 % (status & 0xff))
         elif status:
             sf.sflog.error(
                 "compiler reported error %i while compiling test program" %
                 (status >> 8))
         raise sf.BuildError(out)
     if out:
         sf.sflog.warning(out)
     return outfile
Exemplo n.º 2
0
    def disassemble(self, file):
        sf.sflog.info("Disassembling file %s" % file)
        cmd = "%s %s -m %s -b binary -D %s" % (self.OBJDUMP, self.DISFLAGS,
                                               self.CPU, file)
        sf.sflog.debug("      ------[Disassembling command]------\n%s" % cmd)
        sf.sflog.debug("      -----------------------------------")
        fin, fout, ferr = os.popen3(cmd)
        out = fout.read()
        err = ferr.read()

        if err:
            sf.sflog.error(
                "objdump reported error while working on test program")
            raise sf.BuildError(err)
        return out
Exemplo n.º 3
0
    def extract_shcode(self, infile):
        sf.sflog.info("Extracting shellcode from %s" % infile)
        cmd = "%s -j .text -s -z  %s" % (self.OBJDUMP, infile)
        sf.sflog.debug("      ------[Extraction command]------\n%s" % cmd)
        sf.sflog.debug("      --------------------------------")
        print "Objdump " + cmd
        f = os.popen(cmd)
        inp = f.readlines()
        status = f.close()
        if status:
            if status & 0xff:
                sf.sflog.error(
                    "objdump killed by signal %i while extracting from %s" %
                    (status & 0xff, infile))
            elif status:
                sf.sflog.error(
                    "objdump reported error %i while extracting from %s" %
                    (status >> 8, infile))
            raise sf.BuildError("")
#        if out:
#            sf.sflog.warning(out)

# Extract machine code
        dump = re.compile("^ [0-9a-f]{4}")
        out = []
        for l in inp:
            if dump.match(l):
                out += l[:42].split()[1:]
        out = "".join(out)
        shcode = ""
        for i in range(len(out) / 2):
            shcode += chr(int(out[2 * i:2 * i + 2], 16))

        if shcode:
            return shcode
        raise sf.BuildError("No code in .text section of %s !?" % infile)
Exemplo n.º 4
0
 def assemble(self, infile):
     sf.sflog.info("Assembling %s" % infile)
     outfile = infile + ".o"
     cmd = "%s %s %s -o %s %s" % (self.CC, self.LDFLAGS, self.ASSEMBLEFLAGS,
                                  outfile, infile)
     sf.sflog.debug("       ------[Assembling command]------\n%s" % cmd)
     sf.sflog.debug("      ---------------------------------")
     print "Assemble: " + cmd
     f = os.popen(cmd)
     out = f.read()
     status = f.close()
     if status:
         if status & 0xff:
             sf.sflog.error(
                 "compiler killed by signal %i while assembling %s" %
                 (status & 0xff, infile))
         else:
             sf.sflog.error(
                 "compiler reported error %i while assembling %s" %
                 (status >> 8, infile))
         raise sf.BuildError(out)
     if out:
         sf.sflog.warning(out)
     return outfile
Exemplo n.º 5
0
    def compile(self, infile):
        sf.sflog.info("Compiling %s" % infile)
        outfile = infile + ".S"
        cmd = "%s %s %s -o %s %s 2>&1" % (self.CC, self.CFLAGS, self.INCLUDES,
                                          outfile, infile)
        sf.sflog.debug("       ------[Compilation command]------\n%s" % cmd)
        sf.sflog.debug("       ---------------------------------")
        print "Compile " + cmd
        f = os.popen(cmd)
        out = f.read()
        status = f.close()

        if status:
            if status & 0xff:
                sf.sflog.error("compiler got signal %i while compiling %s" %
                               (status & 0xff, infile))
            else:
                sf.sflog.error(
                    "compiler reported error %i while compiling %s" %
                    (status >> 8, infile))
            raise sf.BuildError(out)
        if out:
            sf.sflog.warning(out)
        return outfile