From 65cee8871ea95fcee4535bc1c339606abd8f98a3 Mon Sep 17 00:00:00 2001 From: lanxu Date: Sun, 19 Mar 2017 19:56:44 +0200 Subject: [PATCH] superior extract.py created --- extract | 16 -------- extract.py | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++ sandbox | 14 +++++++ 3 files changed, 124 insertions(+), 16 deletions(-) delete mode 100755 extract create mode 100755 extract.py create mode 100755 sandbox diff --git a/extract b/extract deleted file mode 100755 index 3cba8bc..0000000 --- a/extract +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/sh -for archive in "$@"; do - # count files in root dir - rootfiles=$(7z l -slt "${archive}" | grep -c 'Path = [^/]*$') - - # add -o switch, if more than one file in root - # checks for >2, because path of the zip file itself is listed too - if [ $rootfiles -gt 2 ]; then - filename=${archive##*/} - rootname=${filename%.*} - opt="-o${rootname}" - fi - - #unpack - 7z x "${opt}" "${archive}" -done diff --git a/extract.py b/extract.py new file mode 100755 index 0000000..f730690 --- /dev/null +++ b/extract.py @@ -0,0 +1,110 @@ +#!/bin/python +import os +import re +import sys +import getopt +import subprocess +import re +import logging +from subprocess import check_output +from subprocess import call + +def is_available(command): + status, result = subprocess.getstatusoutput(command) + if status < 127: + return True + return False + +def is_exe(fpath): + return os.path.isfile(fpath) and os.access(fpath, os.X_OK) + +def run_command(command): + try: + return_value = call(command) + except (RuntimeError, TypeError, NameError, OSError) as error: + print(error) + raise + return return_value + +def has_multiple_files(file): + file = re.escape(file) + check_7z_command = "7z l -slt " + file + " | grep -c 'Path = [^/]*$'" + check_tar_command = "tar --exclude=\"*/*\" -tf " + file + " | wc -l" # returns top level list of files + + check_command = None + + m = re.search("\.tar\.", file) + if m != None: + check_command = check_tar_command + else: + check_command = check_7z_command + + return_command = check_output(check_command, shell=True) + if return_command is None: + print('Something went horribly wrong') + sys.exit(2) + + number_of_files = int(return_command.decode("utf-8")) + + if number_of_files > 1: + if check_command is check_7z_command and number_of_files > 2: + return True + else: + return False + return True + return False + +def main(argv): + extractTools = [ + { "extension": "\.tar\.gz$", "command": "tar", "params": "-xf", "oparams": "-C" }, + { "extension": "\.tar\.xz$", "command": "tar", "params": "-xf", "oparams": "-C" }, + { "extension": "\.zip$", "command": "unzip", "params": "", "oparams": "-d" } + ] + + if os.path.isfile(argv[0]) == False: + print("No such file") + sys.exit(2) + + print("Processing file " + argv[0]) + + for tool in extractTools: + m = re.search(tool["extension"], argv[0]) + if m != None: + if is_available(tool["command"]): + command = [tool["command"]] + + if tool["params"] is not "": + command.append(tool["params"]) + + command.append(argv[0]) + + output_path = "." + + if has_multiple_files(argv[0]): + base = os.path.basename(argv[0]) + basefile = os.path.splitext(base)[0] + + output_path = os.path.dirname(os.path.abspath(argv[0])) + output_path = os.path.join(output_path, basefile) + print("Extracting to " + output_path) + + if not os.path.exists(output_path): + os.makedirs(output_path) + + command.append(tool['oparams']) + command.append(output_path) + + try: + + val = run_command(command) + if val > 0: + sys.exit(2) + except: + print('Something went horribly wrong') + sys.exit(2) + else: + print("Command " + tool["command"] + " not found") + sys.exit(2) + +if __name__ == "__main__": + main(sys.argv[1:]) diff --git a/sandbox b/sandbox new file mode 100755 index 0000000..8abff82 --- /dev/null +++ b/sandbox @@ -0,0 +1,14 @@ +#!/bin/sh + +# Run without any access +#% sudo groupadd sandbox +#% useradd -g sandbox sandbox +# +# Disable network +# iptables -A OUTPUT -m owner --gid-owner sandbox -j DROP +# Or +# iptables -A OUTPUT -m owner --gid-owner sandbox -d 192.168.1.0/24 -j ACCEPT +# iptables -A OUTPUT -m owner --gid-owner sandbox -d 127.0.0.0/8 -j ACCEPT +# iptables -A OUTPUT -m owner --gid-owner sandbox -j DROP + +sg sandbox "$@"