sqlmap/lib/utils/purge.py

87 lines
2.6 KiB
Python
Raw Permalink Normal View History

2019-05-08 13:47:52 +03:00
#!/usr/bin/env python
"""
2022-01-03 13:30:34 +03:00
Copyright (c) 2006-2022 sqlmap developers (https://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-05-15 11:57:22 +03:00
from lib.core.common import openFile
2019-03-28 18:04:38 +03:00
from lib.core.compat import xrange
2019-09-27 22:03:21 +03:00
from lib.core.convert import getUnicode
from lib.core.data import logger
2019-05-15 11:57:22 +03:00
from thirdparty.six import unichr as _unichr
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)
2019-05-15 11:57:22 +03:00
with openFile(filepath, "w+b") as f:
f.write("".join(_unichr(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-09 15:10:18 +03:00
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")
2013-11-24 18:01:26 +04:00
try:
shutil.rmtree(directory)
2019-01-22 02:40:48 +03:00
except OSError as ex:
2019-09-27 22:03:21 +03:00
logger.error("problem occurred while removing directory '%s' ('%s')" % (getUnicode(directory), getSafeExString(ex)))