Skip to content

Instantly share code, notes, and snippets.

@barneygale
Last active February 16, 2025 12:04
Show Gist options
  • Save barneygale/8ff070659178135b10b5e202a1ecaa3f to your computer and use it in GitHub Desktop.
Save barneygale/8ff070659178135b10b5e202a1ecaa3f to your computer and use it in GitHub Desktop.

Revisions

  1. barneygale revised this gist Apr 13, 2016. 1 changed file with 24 additions and 19 deletions.
    43 changes: 24 additions & 19 deletions sudo.py
    Original file line number Diff line number Diff line change
    @@ -1,26 +1,31 @@
    import sys, marshal, textwrap, functools, subprocess
    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):
    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])
    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
  2. barneygale revised this gist Apr 13, 2016. 1 changed file with 1 addition and 23 deletions.
    24 changes: 1 addition & 23 deletions sudo.py
    Original 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()
    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)
    print whoami()
  3. barneygale revised this gist Apr 13, 2016. 1 changed file with 30 additions and 4 deletions.
    34 changes: 30 additions & 4 deletions sudo.py
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,35 @@
    import sys, marshal, textwrap, subprocess
    import sys, marshal, textwrap, functools, subprocess

    def sudo(fn):
    return marshal.loads(
    subprocess.Popen([
    "sudo",
    @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("""
  4. barneygale created this gist Apr 12, 2016.
    28 changes: 28 additions & 0 deletions sudo.py
    Original 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)