Pymetasploit3 How to stay in loop when certain inputs are met

34 Views Asked by At

I'm experimenting with a Python Library called PyMetasploit3 and trying to simplify some tasks.

Let's say i am running screenshare on a box and i would like to run a command at the same time without it continuing the loop and go to the next session. How will i do that? Currently the behaviour is that i have a few else/elif statements and whenever i want to run a command and print the output back it just continues the loop to the next session and won't run the desired outcome..

here's a code snippet of where it happens:

    elif inp == "screenidle":
        idleses = []
        rprint("\n[bold italic]n = next session, s = stop[/bold italic]\n")
        
        for i,v in enumerate(idle_list):
            try:
                sesid = v[0]
                name = v[1]
                desktop = name.split("\\")[0]
            except IndexError:
                with open("checked.txt", "w") as f:
                    for x in checked:
                        json.dump(x, f)
                        f.write("\n")

                rprint("End of sessions reached.")
                break

            idleses.append(sesid)
            
            if not desktop in checked:
                
                if job == None:
                    screenshare(int(sesid), name)
                    checked.append(desktop)
                else:
                    client.jobs.stop(job['job_id'])
                    job = None
                    continue

                    next = input("hack@theplanet:~$ ")

                    if next == "n":
                        if job == None:
                            rprint("There are currently no jobs.")
                            currentsession = False
                            continue
                        else:
                            client.jobs.stop(job['job_id'])
                            job = None
                            currentsession = False
                            continue

                    elif next == "s" or next == "exit":
                        break

                    elif next == "play fart": # Will not execute and loop will continue to next session
                        shell = client.sessions.session(sesid)
                        shell.write("play fart.wav")
                        print(shell.read())

                    
                    else:
                        rprint("[bold red]Unknown command.[/bold red]")
                        #next = input("n = next session, s = stop,\nhack@theplanet:~$ ")

            elif desktop in checked:
                rprint(f":computer: {sesid} - [strike red] {name}[/strike red]")
        

        with open("checked.txt", "w") as f:
            for x in checked:
                json.dump(x, f)
                f.write("\n")

It won't run the "if next == "play fart" it will just continue the loop because no conditions are met I would like it to stay on the same loop/session so i can run commands or do other fun things whilst screensharing.

Any solutions will be greatly appreciated!

Also once finished i will release the script on github! No illegal activities are being performed just learning on the way with Python and Metasploit!

0

There are 0 best solutions below