From 159eb86f979716b2e0c6d819b1ca598441c9ddf9 Mon Sep 17 00:00:00 2001 From: dusoleil Date: Tue, 7 Sep 2021 02:34:54 -0400 Subject: Clean up exception handling in main.py The handling from the daemon mode code will also work in the process and pipes cases. Putting it in a common location removes the need for the outer try/except. It is also easier to read/maintain in general. Signed-off-by: dusoleil --- tools/sploit/sploit/main.py | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/tools/sploit/sploit/main.py b/tools/sploit/sploit/main.py index 6404786..f97e04e 100644 --- a/tools/sploit/sploit/main.py +++ b/tools/sploit/sploit/main.py @@ -14,18 +14,15 @@ def main(): help='target program to exploit') args = parser.parse_args() - try: - if(len(args.target)>0): - if(args.daemon): - print("Target Given. Ignoring Daemon Flag...") - target(args.script, args.target) + if(len(args.target)>0): + if(args.daemon): + print("Target Given. Ignoring Daemon Flag...") + target(args.script, args.target) + else: + if(args.daemon): + daemon(args.script) else: - if(args.daemon): - daemon(args.script) - else: - pipe(args.script) - except KeyboardInterrupt: - pass + pipe(args.script) def daemon(script): print("Running in Pipe Daemon Mode...") @@ -35,12 +32,7 @@ def daemon(script): p = Pipes(tmpdir) except KeyboardInterrupt: break - try: - runscript(script, Comm(p)); - except KeyboardInterrupt: - pass - except: - traceback.print_exc() + runscript(script, Comm(p)); del p def pipe(script): @@ -52,8 +44,15 @@ def target(script, target): runscript(script, Comm(Process(target))); def runscript(script, comm): - print("Running Script...") - code = compile(open(script).read(), script, 'exec') - exec(code, {'io': comm}) - print("Script Finished!") - comm.readall() + try: + print("Running Script...") + code = compile(open(script).read(), script, 'exec') + exec(code, {'io': comm}) + print("Script Finished!") + comm.readall() + return + except KeyboardInterrupt: + pass + except: + traceback.print_exc() + print("Script Ended Early!") -- cgit v1.2.3 From c43b676086f26edb1ea989b255e0eaf356c8ad5a Mon Sep 17 00:00:00 2001 From: dusoleil Date: Tue, 7 Sep 2021 02:36:54 -0400 Subject: Manually run garbage collection after exec Apparently python won't run garbage collection on stuff owned by the exec context if you define a function in the exec. This can lead to random leaks, but it is most impactful in daemon mode. If the globals dictionary given to exec isn't cleaned up, there will be a random reference to comm that still exists. This holds a reference to the Pipes object which prevents it from getting cleaned up before we try to make a new one. Making a new one needs the fifos to have been cleaned up, so it relies on the fact that the old one was supposed to be cleaned up. The most straightforward and non-intrusive way I could think to fix this was to just manually run the garbage collector after exec. This is able to find the leaked references and clean it all up. Signed-off-by: dusoleil --- tools/sploit/sploit/main.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/sploit/sploit/main.py b/tools/sploit/sploit/main.py index f97e04e..77ef49e 100644 --- a/tools/sploit/sploit/main.py +++ b/tools/sploit/sploit/main.py @@ -1,6 +1,7 @@ import argparse import tempfile import traceback +import gc from sploit.comm import * @@ -55,4 +56,6 @@ def runscript(script, comm): pass except: traceback.print_exc() + finally: + gc.collect() print("Script Ended Early!") -- cgit v1.2.3