Exemple #1
0
def run_functionality(filename, save_as=None, optimize=False):

    filepath = os.path.join(bin_location, filename)
    if save_as is None:
        save_as = os.path.join('/', 'tmp', 'functionality',
                               os.path.basename(filename))

    intermediate = None
    if optimize:
        intermediate = save_as + ".intermediate"
        try:
            optimize_it(filepath, intermediate, debugging=True)
        except BinaryOptimizationError:
            print "Optimization failed on file %s" % filepath
            raise

        # update filepath
        filepath = intermediate

    p = ReassemblerBackend(filepath, debugging=True)
    r = p.save(save_as)

    if intermediate:
        try:
            os.unlink(intermediate)
        except OSError:
            pass

    if not r:
        print "Compiler says:"
        print p._compiler_stdout
        print p._compiler_stderr

    nose.tools.assert_true(r, 'Reassembler fails on binary %s' % filename)
Exemple #2
0
def run_optimization(filename):
    filepath = os.path.join(bin_location, filename)

    target_filepath = os.path.join('/', 'tmp', 'optimized_binaries',
                                   os.path.basename(filename))
    rr_filepath = target_filepath + ".rr"
    cp_filepath = target_filepath + ".cp"

    # register reallocation first
    b1 = ReassemblerBackend(filepath, debugging=True)
    cp = BinaryOptimization(filepath, b1, {'register_reallocation'})
    #cp = BinaryOptimization(filepath, b1, {'redundant_stack_variable_removal'})
    patches = cp.get_patches()
    b1.apply_patches(patches)
    r = b1.save(rr_filepath)

    if not r:
        print "Compiler says:"
        print b1._compiler_stdout
        print b1._compiler_stderr

    # other optimization techniques
    b2 = ReassemblerBackend(rr_filepath, debugging=True)
    #cp = BinaryOptimization(rr_filepath, b2, {'constant_propagation', 'redundant_stack_variable_removal'})
    cp = BinaryOptimization(rr_filepath, b2, {'constant_propagation'})
    patches = cp.get_patches()
    b2.apply_patches(patches)
    r = b2.save(target_filepath)

    if not r:
        print "Compiler says:"
        print b2._compiler_stdout
        print b2._compiler_stderr

    nose.tools.assert_true(r, 'Optimization fails on binary %s' % filename)
def run_adversarial(filename):
    filepath = os.path.join(bin_location, filename)

    p = ReassemblerBackend(filepath, debugging=True)

    patch = Adversarial(filepath, p)
    patches = patch.get_patches()

    p.apply_patches(patches)

    r = p.save(os.path.join('/', 'tmp', 'adversarial', os.path.basename(filename)))

    if not r:
        print("Compiler says:")
        print(p._compiler_stdout)
        print(p._compiler_stderr)

    nose.tools.assert_true(r, 'Adversarial patching with reassembler fails on binary %s' % filename)
Exemple #4
0
def run_shiftstack(filename):
    filepath = os.path.join(bin_location, filename)

    p = ReassemblerBackend(filepath, debugging=True)

    patch = ShiftStack(filepath, p)
    patches = patch.get_patches()

    p.apply_patches(patches)

    r = p.save(
        os.path.join('/', 'tmp', 'shiftstack', os.path.basename(filename)))

    if not r:
        print "Compiler says:"
        print p._compiler_stdout
        print p._compiler_stderr

    nose.tools.assert_true(
        r,
        'ShiftStack patching with reassembler fails on binary %s' % filename)