import archr
import os
import plumber


def test_leak_detection(leak_input):
    """
    Test that our write what where exploit can leak, and works in the presence of a shadowstack
    """
    path = os.path.dirname(os.path.abspath(__file__)) + "/example_1_nonet.bin"

    argv_sensitive_idx = 1
    argv_sensitive_target = plumber.ArgvSensitiveTarget(argv_sensitive_idx)
    sensitive = [argv_sensitive_target]

    with archr.targets.LocalTarget([path, 'secret', 'password'], target_os='linux', target_arch='x86_64') as target:
        p = plumber.Plumber(target, leak_input, sensitive) # initialize the plumber
        p.run() # Go!

    if p.exploitable():
        p.pov()


if __name__ == "__main__":
    import sys
    import logging
    logging.getLogger('rex').setLevel('DEBUG')
    logging.getLogger("angr.state_plugins.preconstrainer").setLevel("DEBUG")
    logging.getLogger("angr.simos").setLevel("DEBUG")
    logging.getLogger("angr.exploration_techniques.tracer").setLevel("DEBUG")
    logging.getLogger("angr.exploration_techniques.crash_monitor").setLevel("DEBUG")

    leak_input = b"A"*84
    test_leak_detection(leak_input)

