|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +$Id$ |
| 5 | +
|
| 6 | +Copyright (c) 2006-2012 sqlmap developers (http://www.sqlmap.org/) |
| 7 | +See the file 'doc/COPYING' for copying permission |
| 8 | +""" |
| 9 | + |
| 10 | +import os |
| 11 | +import random |
| 12 | +import shutil |
| 13 | +import stat |
| 14 | +import string |
| 15 | + |
| 16 | +from lib.core.data import logger |
| 17 | + |
| 18 | +def purge(directory): |
| 19 | + """ |
| 20 | + Safely removes content from a given directory |
| 21 | + """ |
| 22 | + |
| 23 | + filepaths = [] |
| 24 | + dirpaths = [] |
| 25 | + |
| 26 | + for rootpath, directories, filenames in os.walk(directory): |
| 27 | + dirpaths.extend([os.path.abspath(os.path.join(rootpath, _)) for _ in directories]) |
| 28 | + filepaths.extend([os.path.abspath(os.path.join(rootpath, _)) for _ in filenames]) |
| 29 | + |
| 30 | + logger.info("changing file attributes...") |
| 31 | + for filepath in filepaths: |
| 32 | + try: |
| 33 | + os.chmod(filepath, stat.S_IREAD | stat.S_IWRITE) |
| 34 | + except: |
| 35 | + pass |
| 36 | + |
| 37 | + logger.info("writing random data to files...") |
| 38 | + for filepath in filepaths: |
| 39 | + try: |
| 40 | + filesize = os.path.getsize(filepath) |
| 41 | + with open(filepath, 'w+b') as f: |
| 42 | + f.write("".join(chr(random.randint(0, 255)) for _ in xrange(filesize))) |
| 43 | + except: |
| 44 | + pass |
| 45 | + |
| 46 | + logger.info("truncating files...") |
| 47 | + for filepath in filepaths: |
| 48 | + try: |
| 49 | + with open(filepath, 'w') as f: |
| 50 | + pass |
| 51 | + except: |
| 52 | + pass |
| 53 | + |
| 54 | + logger.info("renaming filenames to random values...") |
| 55 | + for filepath in filepaths: |
| 56 | + try: |
| 57 | + os.rename(filepath, os.path.join(os.path.dirname(filepath), "".join(random.sample(string.letters, random.randint(4, 8))))) |
| 58 | + except: |
| 59 | + pass |
| 60 | + |
| 61 | + dirpaths.sort(cmp = lambda x, y: y.count(os.path.sep) - x.count(os.path.sep)) |
| 62 | + |
| 63 | + logger.info("renaming directory names to random values...") |
| 64 | + for dirpath in dirpaths: |
| 65 | + try: |
| 66 | + os.rename(dirpath, os.path.join(os.path.dirname(dirpath), "".join(random.sample(string.letters, random.randint(4, 8))))) |
| 67 | + except: |
| 68 | + pass |
| 69 | + |
| 70 | + logger.info("deleting the whole directory tree...") |
| 71 | + os.chdir(os.path.join(directory, "..")) |
| 72 | + shutil.rmtree(directory) |
| 73 | + |
| 74 | + logger.info("purging done") |
0 commit comments