SIGN IN SIGN UP
sqlmapproject / sqlmap UNCLAIMED

Automatic SQL injection and database takeover tool

36950 0 0 Python
2019-05-08 12:47:52 +02:00
#!/usr/bin/env python
2012-02-16 09:46:41 +00:00
"""
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
2012-02-16 09:46:41 +00:00
"""
2012-07-18 14:24:10 +02:00
import zipfile
2016-01-12 10:27:04 +01:00
from lib.core.common import getSafeExString
2019-05-24 12:01:39 +02:00
from lib.core.common import isZipFile
from lib.core.exception import SqlmapDataException
2014-12-19 09:37:06 +01:00
from lib.core.exception import SqlmapInstallationException
2019-05-03 00:13:05 +02:00
from thirdparty import six
2012-07-18 14:24:10 +02:00
2019-05-03 00:13:05 +02:00
class Wordlist(six.Iterator):
2012-02-16 09:46:41 +00:00
"""
Iterator for looping over a large dictionaries
2019-05-06 14:41:35 +02:00
>>> from lib.core.option import paths
2019-07-03 10:56:05 +02:00
>>> isinstance(next(Wordlist(paths.SMALL_DICT)), six.binary_type)
True
>>> isinstance(next(Wordlist(paths.WORDLIST)), six.binary_type)
2019-05-06 14:41:35 +02:00
True
2012-02-16 09:46:41 +00:00
"""
2012-07-18 13:32:34 +02:00
def __init__(self, filenames, proc_id=None, proc_count=None, custom=None):
2019-05-06 14:41:35 +02:00
self.filenames = [filenames] if isinstance(filenames, six.string_types) else filenames
2012-02-16 09:46:41 +00:00
self.fp = None
2026-01-17 23:03:53 +01:00
self.zip_file = None
2012-02-16 09:46:41 +00:00
self.index = 0
2012-07-18 13:32:34 +02:00
self.counter = -1
2014-12-19 09:37:06 +01:00
self.current = None
2012-02-16 09:46:41 +00:00
self.iter = None
2012-07-18 13:32:34 +02:00
self.custom = custom or []
self.proc_id = proc_id
self.proc_count = proc_count
2012-02-16 09:46:41 +00:00
self.adjust()
def __iter__(self):
return self
def adjust(self):
self.closeFP()
if self.index > len(self.filenames):
2019-09-13 11:38:26 +02:00
return # Note: https://stackoverflow.com/a/30217723 (PEP 479)
2012-02-16 09:46:41 +00:00
elif self.index == len(self.filenames):
2012-07-18 14:09:04 +02:00
self.iter = iter(self.custom)
2012-02-16 09:46:41 +00:00
else:
2014-12-19 09:37:06 +01:00
self.current = self.filenames[self.index]
2019-05-24 12:01:39 +02:00
if isZipFile(self.current):
2015-08-30 22:52:24 +02:00
try:
2026-01-17 23:03:53 +01:00
self.zip_file = zipfile.ZipFile(self.current, 'r')
2019-01-22 00:40:48 +01:00
except zipfile.error as ex:
2016-05-22 21:44:17 +02:00
errMsg = "something appears to be wrong with "
2016-01-12 10:27:04 +01:00
errMsg += "the file '%s' ('%s'). Please make " % (self.current, getSafeExString(ex))
2015-08-30 22:52:24 +02:00
errMsg += "sure that you haven't made any changes to it"
2018-03-13 11:13:38 +01:00
raise SqlmapInstallationException(errMsg)
2026-01-17 23:03:53 +01:00
if len(self.zip_file.namelist()) == 0:
2014-12-19 09:37:06 +01:00
errMsg = "no file(s) inside '%s'" % self.current
raise SqlmapDataException(errMsg)
2026-01-17 23:03:53 +01:00
self.fp = self.zip_file.open(self.zip_file.namelist()[0])
2012-07-18 14:24:10 +02:00
else:
2019-07-03 10:56:05 +02:00
self.fp = open(self.current, "rb")
2012-02-16 09:46:41 +00:00
self.iter = iter(self.fp)
self.index += 1
def closeFP(self):
if self.fp:
self.fp.close()
self.fp = None
2026-01-17 23:03:53 +01:00
if self.zip_file:
self.zip_file.close()
self.zip_file = None
2019-05-03 00:13:05 +02:00
def __next__(self):
2012-02-16 09:46:41 +00:00
retVal = None
2012-07-18 13:32:34 +02:00
while True:
2012-07-18 14:09:04 +02:00
self.counter += 1
2012-07-18 13:32:34 +02:00
try:
2019-01-22 02:47:06 +01:00
retVal = next(self.iter).rstrip()
2019-01-22 00:40:48 +01:00
except zipfile.error as ex:
2016-05-22 21:44:17 +02:00
errMsg = "something appears to be wrong with "
2016-01-12 10:27:04 +01:00
errMsg += "the file '%s' ('%s'). Please make " % (self.current, getSafeExString(ex))
2014-12-19 09:37:06 +01:00
errMsg += "sure that you haven't made any changes to it"
2018-03-13 11:13:38 +01:00
raise SqlmapInstallationException(errMsg)
2012-07-18 13:32:34 +02:00
except StopIteration:
self.adjust()
2019-01-22 02:47:06 +01:00
retVal = next(self.iter).rstrip()
2012-07-18 14:09:04 +02:00
if not self.proc_count or self.counter % self.proc_count == self.proc_id:
2012-07-18 13:32:34 +02:00
break
2012-02-16 09:46:41 +00:00
return retVal
def rewind(self):
self.index = 0
self.adjust()