2019-05-08 13:47:52 +03:00
|
|
|
#!/usr/bin/env python
|
2012-04-23 18:25:16 +04:00
|
|
|
|
|
|
|
"""
|
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
|
2012-04-23 18:25:16 +04:00
|
|
|
"""
|
|
|
|
|
2019-05-08 00:00:15 +03:00
|
|
|
import functools
|
2012-04-23 18:25:16 +04:00
|
|
|
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
|
2012-04-23 18:25:16 +04:00
|
|
|
from lib.core.data import logger
|
2019-05-08 00:00:15 +03:00
|
|
|
from thirdparty import six
|
2012-04-23 18:25:16 +04:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2012-04-23 18:25:16 +04:00
|
|
|
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)
|
2012-04-23 18:25:16 +04:00
|
|
|
|
2013-04-22 13:38:47 +04:00
|
|
|
logger.debug("changing file attributes")
|
2012-04-23 18:25:16 +04:00
|
|
|
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")
|
2012-04-23 18:25:16 +04:00
|
|
|
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:
|
2012-04-23 18:25:16 +04:00
|
|
|
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")
|
2012-04-23 18:25:16 +04:00
|
|
|
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")
|
2012-04-23 18:25:16 +04:00
|
|
|
for filepath in filepaths:
|
|
|
|
try:
|
2013-06-01 16:06:58 +04:00
|
|
|
os.rename(filepath, os.path.join(os.path.dirname(filepath), "".join(random.sample(string.ascii_letters, random.randint(4, 8)))))
|
2012-04-23 18:25:16 +04:00
|
|
|
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)))
|
2012-04-23 18:25:16 +04:00
|
|
|
|
2013-04-22 13:38:47 +04:00
|
|
|
logger.debug("renaming directory names to random values")
|
2012-04-23 18:25:16 +04:00
|
|
|
for dirpath in dirpaths:
|
|
|
|
try:
|
2013-06-01 16:06:58 +04:00
|
|
|
os.rename(dirpath, os.path.join(os.path.dirname(dirpath), "".join(random.sample(string.ascii_letters, random.randint(4, 8)))))
|
2012-04-23 18:25:16 +04:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2013-04-22 13:38:47 +04:00
|
|
|
logger.debug("deleting the whole directory tree")
|
2012-04-23 18:25:16 +04:00
|
|
|
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)))
|