Last active
February 16, 2025 12:04
-
-
Save barneygale/8ff070659178135b10b5e202a1ecaa3f to your computer and use it in GitHub Desktop.
Revisions
-
barneygale revised this gist
Apr 13, 2016 . 1 changed file with 24 additions and 19 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,26 +1,31 @@ import sys, marshal, functools, subprocess child_script = """ import marshal, sys, types; fn, args, kwargs = marshal.load(sys.stdin) marshal.dump( types.FunctionType(fn, globals())(*args, **kwargs), sys.stdout) """ def sudo(fn): @functools.wraps(fn) def inner(*args, **kwargs): proc_args = [ "sudo", sys.executable, "-c", child_script] proc = subprocess.Popen( proc_args, stdin=subprocess.PIPE, stdout=subprocess.PIPE) send_data = marshal.dumps(( fn.func_code, args, kwargs)) recv_data = proc.communicate(send_data)[0] return marshal.loads(recv_data) return inner @sudo -
barneygale revised this gist
Apr 13, 2016 . 1 changed file with 1 addition and 23 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -29,26 +29,4 @@ def whoami(): return "I am: %d" % os.getuid() if __name__ == '__main__': print whoami() -
barneygale revised this gist
Apr 13, 2016 . 1 changed file with 30 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,35 @@ import sys, marshal, textwrap, functools, subprocess def sudo(fn): @functools.wraps(fn) def inner(*args, **kwargs): return marshal.loads( subprocess.Popen([ "sudo", sys.executable, "-c", textwrap.dedent(""" import marshal, sys, types; fn, args, kwargs = marshal.load(sys.stdin) marshal.dump( types.FunctionType(fn, globals())(*args, **kwargs), sys.stdout) """)], stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate( marshal.dumps(( fn.func_code, args, kwargs)))[0]) return inner @sudo def whoami(): import os return "I am: %d" % os.getuid() if __name__ == '__main__': print whoami() sys.executable, "-c", textwrap.dedent(""" -
barneygale created this gist
Apr 12, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ import sys, marshal, textwrap, subprocess def sudo(fn): return marshal.loads( subprocess.Popen([ "sudo", sys.executable, "-c", textwrap.dedent(""" import marshal, sys, types; marshal.dump( types.FunctionType( marshal.load(sys.stdin), globals())(), sys.stdout) """)], stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate( marshal.dumps( fn.func_code))[0]) def whoami(): import os return os.getuid() if __name__ == '__main__': print whoami() print sudo(whoami)