From d6ce7974f6bc934adcc5e2eee4cc4a1a4e7f74ba Mon Sep 17 00:00:00 2001 From: linted Date: Sat, 29 Sep 2018 00:31:20 -0400 Subject: [PATCH 1/3] added better version checking --- linuxprivchecker.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/linuxprivchecker.py b/linuxprivchecker.py index b3ea060..d0c62c5 100755 --- a/linuxprivchecker.py +++ b/linuxprivchecker.py @@ -19,14 +19,17 @@ ############################################################################################################### # conditional import for older versions of python not compatible with subprocess -try: +from sys import version_info +if version_info >= (3,5): #import subprocess as sub from subprocess import run, PIPE compatmode = 0 # newer version of python, no need for compatibility mode -except ImportError: +elif version_info >= (3,): #import os # older version of python, need to use ### instead from subprocess import check_output, PIPE compatmode = 1 +else: + print("Error: please run in python3 only.") # title / formatting bigline = "=" * 80 From 3adb891588489b3d1ccb98c84f475c67cb43b3ba Mon Sep 17 00:00:00 2001 From: linted Date: Sat, 29 Sep 2018 00:36:51 -0400 Subject: [PATCH 2/3] made two different version of execCMD --- linuxprivchecker.py | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/linuxprivchecker.py b/linuxprivchecker.py index d0c62c5..e8de2e9 100755 --- a/linuxprivchecker.py +++ b/linuxprivchecker.py @@ -23,13 +23,37 @@ if version_info >= (3,5): #import subprocess as sub from subprocess import run, PIPE - compatmode = 0 # newer version of python, no need for compatibility mode + # loop through dictionary, execute the commands, store the results, return updated dict + def execCmd(cmdDict): + ''' for versions >= 3.5 ''' + for item in cmdDict: + cmd = cmdDict[item]["cmd"] + try: + process = run(cmd, stdout=PIPE, stderr=PIPE, shell=True) + results = process.stdout.decode().split('\n') + except Exception as e: + results = ['[-] failed: {}'.format(e)] + cmdDict[item]["results"]=results + return cmdDict + elif version_info >= (3,): #import os # older version of python, need to use ### instead from subprocess import check_output, PIPE - compatmode = 1 + # loop through dictionary, execute the commands, store the results, return updated dict + def execCmd(cmdDict): + ''' for version < 3.5 ''' + for item in cmdDict: + cmd = cmdDict[item]["cmd"] + try: + echo_stdout = check_output(cmd, shell=True) + results = echo_stdout.decode().split('\n') + except Exception as e: + results = ['[-] failed: {}'.format(e)] + cmdDict[item]["results"]=results + return cmdDict else: print("Error: please run in python3 only.") + exit(1) # title / formatting bigline = "=" * 80 @@ -43,22 +67,7 @@ def header(message): print(bigline) print("") -# loop through dictionary, execute the commands, store the results, return updated dict -def execCmd(cmdDict): - for item in cmdDict: - cmd = cmdDict[item]["cmd"] - try: - if compatmode == 0: # newer version of python, use preferred subprocess - process = run(cmd, stdout=PIPE, stderr=PIPE, shell=True) - results = process.stdout.decode().split('\n') - else: # older version of python, use os.popen - echo_stdout = check_output(cmd, shell=True) - results = stdout.decode().split('\n') - except Exception as e: - results = ['[-] failed: {}'.format(e)] - cmdDict[item]["results"]=results - - return cmdDict + # print results for each previously executed command, no return value def printResults(cmdDict): From 02cd1af1167fec3c8bbe5a759f8f63daf327e2b8 Mon Sep 17 00:00:00 2001 From: linted Date: Sat, 29 Sep 2018 00:51:51 -0400 Subject: [PATCH 3/3] changed to a slightly easier to maintain versioning of the two. now it's only a single line difference --- linuxprivchecker.py | 44 +++++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/linuxprivchecker.py b/linuxprivchecker.py index e8de2e9..6b90208 100755 --- a/linuxprivchecker.py +++ b/linuxprivchecker.py @@ -23,34 +23,13 @@ if version_info >= (3,5): #import subprocess as sub from subprocess import run, PIPE - # loop through dictionary, execute the commands, store the results, return updated dict - def execCmd(cmdDict): - ''' for versions >= 3.5 ''' - for item in cmdDict: - cmd = cmdDict[item]["cmd"] - try: - process = run(cmd, stdout=PIPE, stderr=PIPE, shell=True) - results = process.stdout.decode().split('\n') - except Exception as e: - results = ['[-] failed: {}'.format(e)] - cmdDict[item]["results"]=results - return cmdDict - + def do_cmd(cmd): + return run(cmd, stdout=PIPE, stderr=PIPE, shell=True).stdout elif version_info >= (3,): #import os # older version of python, need to use ### instead - from subprocess import check_output, PIPE - # loop through dictionary, execute the commands, store the results, return updated dict - def execCmd(cmdDict): - ''' for version < 3.5 ''' - for item in cmdDict: - cmd = cmdDict[item]["cmd"] - try: - echo_stdout = check_output(cmd, shell=True) - results = echo_stdout.decode().split('\n') - except Exception as e: - results = ['[-] failed: {}'.format(e)] - cmdDict[item]["results"]=results - return cmdDict + from subprocess import check_output, STDOUT + def do_cmd(cmd): + return check_output(cmd, shell=True, stderr=STDOUT) else: print("Error: please run in python3 only.") exit(1) @@ -67,7 +46,18 @@ def header(message): print(bigline) print("") - +# loop through dictionary, execute the commands, store the results, return updated dict +def execCmd(cmdDict): + for item in cmdDict: + cmd = cmdDict[item]["cmd"] + try: + stdout = do_cmd(cmd) + results = stdout.decode().split('\n') + except Exception as e: + results = ['[-] failed: {}'.format(e)] + cmdDict[item]["results"]=results + + return cmdDict # print results for each previously executed command, no return value def printResults(cmdDict):