sqlmap/lib/utils/purge.py

90 lines
2.7 KiB
Python
Raw Normal View History

2019-03-21 16:00:09 +03:00
#!/usr/bin/env python2
"""
2019-01-05 23:38:52 +03:00
Copyright (c) 2006-2019 sqlmap developers (http://sqlmap.org/)
2017-10-11 15:50:46 +03:00
See the file 'LICENSE' for copying permission
"""
2019-05-08 00:00:15 +03:00
import functools
import os
import random
import shutil
import stat
import string
2015-11-23 11:24:30 +03:00
from lib.core.common import getSafeExString
2019-03-28 18:04:38 +03:00
from lib.core.compat import xrange
from lib.core.data import logger
2019-05-08 00:00:15 +03:00
from thirdparty import six
def purge(directory):
"""
Safely removes content from a given directory
"""
2012-04-23 18:33:36 +04:00
if not os.path.isdir(directory):
2012-04-23 18:43:59 +04:00
warnMsg = "skipping purging of directory '%s' as it does not exist" % directory
logger.warn(warnMsg)
2012-04-23 18:33:36 +04:00
return
2013-04-22 13:38:47 +04:00
infoMsg = "purging content of directory '%s'..." % directory
2012-04-23 18:33:36 +04:00
logger.info(infoMsg)
filepaths = []
dirpaths = []
for rootpath, directories, filenames in os.walk(directory):
2017-12-04 15:24:51 +03:00
dirpaths.extend(os.path.abspath(os.path.join(rootpath, _)) for _ in directories)
filepaths.extend(os.path.abspath(os.path.join(rootpath, _)) for _ in filenames)
2013-04-22 13:38:47 +04:00
logger.debug("changing file attributes")
for filepath in filepaths:
try:
os.chmod(filepath, stat.S_IREAD | stat.S_IWRITE)
except:
pass
2013-04-22 13:38:47 +04:00
logger.debug("writing random data to files")
for filepath in filepaths:
try:
filesize = os.path.getsize(filepath)
2014-04-07 22:06:03 +04:00
with open(filepath, "w+b") as f:
f.write("".join(chr(random.randint(0, 255)) for _ in xrange(filesize)))
except:
pass
2013-04-22 13:38:47 +04:00
logger.debug("truncating files")
for filepath in filepaths:
try:
with open(filepath, 'w') as f:
pass
except:
pass
2013-04-22 13:38:47 +04:00
logger.debug("renaming filenames to random values")
for filepath in filepaths:
try:
os.rename(filepath, os.path.join(os.path.dirname(filepath), "".join(random.sample(string.ascii_letters, random.randint(4, 8)))))
except:
pass
2019-05-08 00:00:15 +03:00
if six.PY2:
dirpaths.sort(cmp=lambda x, y: y.count(os.path.sep) - x.count(os.path.sep))
else:
dirpaths.sort(key=functools.cmp_to_key(lambda x, y: y.count(os.path.sep) - x.count(os.path.sep)))
2013-04-22 13:38:47 +04:00
logger.debug("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.ascii_letters, random.randint(4, 8)))))
except:
pass
2013-04-22 13:38:47 +04:00
logger.debug("deleting the whole directory tree")
os.chdir(os.path.join(directory, ".."))
2013-11-24 18:01:26 +04:00
try:
shutil.rmtree(directory)
2019-01-22 02:40:48 +03:00
except OSError as ex:
2015-11-23 11:24:30 +03:00
logger.error("problem occurred while removing directory '%s' ('%s')" % (directory, getSafeExString(ex)))