SIGN IN SIGN UP
sqlmapproject / sqlmap UNCLAIMED

Automatic SQL injection and database takeover tool

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