Created
January 26, 2012 23:08
-
-
Save sweenzor/1685717 to your computer and use it in GitHub Desktop.
Revisions
-
sweenzor revised this gist
Jan 26, 2012 . 1 changed file with 11 additions and 6 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 @@ -3,30 +3,35 @@ import os import subprocess # > python subprocessdemote.py # > sudo python subprocessdemote.py def check_username(): """Check who is running this script""" print os.getuid() print os.getgid() def check_id(): """Run the command 'id' in a subprocess, return the result""" cmd = ['id'] return subprocess.check_output(cmd) def check_id_as_user(): """Run the command 'id' in a subprocess as user 1000, return the result""" cmd = ['id'] return subprocess.check_output(cmd, preexec_fn=demote(1000, 1000)) def demote(user_uid, user_gid): """Pass the function 'set_ids' to preexec_fn, rather than just calling setuid and setgid. This will change the ids for that subprocess only""" def set_ids(): os.setgid(user_gid) @@ -41,4 +46,4 @@ def set_ids(): print check_id() print check_id_as_user() print check_id() -
sweenzor revised this gist
Jan 26, 2012 . 1 changed file with 9 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,33 +1,38 @@ #!/usr/bin/env python import os import subprocess def check_username(): """Check who is running this script""" print os.getuid() print os.getgid() def check_id(): """Run the command 'id' in a subprocess, return the result""" cmd = ['id'] return subprocess.check_output(cmd) def check_id_as_user(): """Run the command 'id' in a subprocess as user 1000, return the result""" cmd = ['id'] return subprocess.check_output(cmd, preexec_fn=demote(1000, 1000)) def demote(user_uid, user_gid): """Pass the function 'set_ids' to preexec_fn, rather than just calling setuid and setgid. This will change the ids for that subprocess only""" def set_ids(): os.setgid(user_gid) os.setuid(user_uid) return set_ids if __name__=='__main__': @@ -36,4 +41,4 @@ def result(): print check_id() print check_id_as_user() print check_id() -
sweenzor created this gist
Jan 26, 2012 .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,39 @@ #!/usr/bin/env python import os, pwd import subprocess def check_username(): print os.getuid() print os.getgid() def check_id(): cmd = ['id'] return subprocess.check_output(cmd) def check_id_as_user(): cmd = ['id'] return subprocess.check_output(cmd, preexec_fn=demote(1000, 1000)) def demote(user_uid, user_gid): def result(): os.setgid(user_gid) os.setuid(user_uid) return result if __name__=='__main__': check_username() print check_id() print check_id_as_user() print check_id()