From 1eecfb3dce2508b04278ad3a21e235e3ce256c44 Mon Sep 17 00:00:00 2001 From: Miroslav Stampar Date: Mon, 23 Apr 2012 14:25:16 +0000 Subject: [PATCH] adding new file related to the last commit --- lib/core/purge.py | 74 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 lib/core/purge.py diff --git a/lib/core/purge.py b/lib/core/purge.py new file mode 100644 index 000000000..1ec9b7e84 --- /dev/null +++ b/lib/core/purge.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python + +""" +$Id$ + +Copyright (c) 2006-2012 sqlmap developers (http://www.sqlmap.org/) +See the file 'doc/COPYING' for copying permission +""" + +import os +import random +import shutil +import stat +import string + +from lib.core.data import logger + +def purge(directory): + """ + Safely removes content from a given directory + """ + + filepaths = [] + dirpaths = [] + + for rootpath, directories, filenames in os.walk(directory): + dirpaths.extend([os.path.abspath(os.path.join(rootpath, _)) for _ in directories]) + filepaths.extend([os.path.abspath(os.path.join(rootpath, _)) for _ in filenames]) + + logger.info("changing file attributes...") + for filepath in filepaths: + try: + os.chmod(filepath, stat.S_IREAD | stat.S_IWRITE) + except: + pass + + logger.info("writing random data to files...") + for filepath in filepaths: + try: + filesize = os.path.getsize(filepath) + with open(filepath, 'w+b') as f: + f.write("".join(chr(random.randint(0, 255)) for _ in xrange(filesize))) + except: + pass + + logger.info("truncating files...") + for filepath in filepaths: + try: + with open(filepath, 'w') as f: + pass + except: + pass + + logger.info("renaming filenames to random values...") + for filepath in filepaths: + try: + os.rename(filepath, os.path.join(os.path.dirname(filepath), "".join(random.sample(string.letters, random.randint(4, 8))))) + except: + pass + + dirpaths.sort(cmp = lambda x, y: y.count(os.path.sep) - x.count(os.path.sep)) + + logger.info("renaming directory names to random values...") + for dirpath in dirpaths: + try: + os.rename(dirpath, os.path.join(os.path.dirname(dirpath), "".join(random.sample(string.letters, random.randint(4, 8))))) + except: + pass + + logger.info("deleting the whole directory tree...") + os.chdir(os.path.join(directory, "..")) + shutil.rmtree(directory) + + logger.info("purging done")