2013-02-14 15:32:17 +04:00
|
|
|
#!/usr/bin/env python
|
2012-02-16 13:46:41 +04:00
|
|
|
|
|
|
|
"""
|
2015-01-06 17:02:16 +03:00
|
|
|
Copyright (c) 2006-2015 sqlmap developers (http://sqlmap.org/)
|
2012-02-16 13:46:41 +04:00
|
|
|
See the file 'doc/COPYING' for copying permission
|
|
|
|
"""
|
|
|
|
|
2012-07-18 16:24:10 +04:00
|
|
|
import os
|
|
|
|
import zipfile
|
|
|
|
|
2012-12-06 17:14:19 +04:00
|
|
|
from lib.core.exception import SqlmapDataException
|
2014-12-19 11:37:06 +03:00
|
|
|
from lib.core.exception import SqlmapInstallationException
|
2012-10-25 02:03:00 +04:00
|
|
|
from lib.core.settings import UNICODE_ENCODING
|
2012-07-18 16:24:10 +04:00
|
|
|
|
2012-12-06 13:42:53 +04:00
|
|
|
class Wordlist(object):
|
2012-02-16 13:46:41 +04:00
|
|
|
"""
|
|
|
|
Iterator for looping over a large dictionaries
|
|
|
|
"""
|
|
|
|
|
2012-07-18 15:32:34 +04:00
|
|
|
def __init__(self, filenames, proc_id=None, proc_count=None, custom=None):
|
2012-02-16 13:46:41 +04:00
|
|
|
self.filenames = filenames
|
|
|
|
self.fp = None
|
|
|
|
self.index = 0
|
2012-07-18 15:32:34 +04:00
|
|
|
self.counter = -1
|
2014-12-19 11:37:06 +03:00
|
|
|
self.current = None
|
2012-02-16 13:46:41 +04:00
|
|
|
self.iter = None
|
2012-07-18 15:32:34 +04:00
|
|
|
self.custom = custom or []
|
|
|
|
self.proc_id = proc_id
|
|
|
|
self.proc_count = proc_count
|
2012-02-16 13:46:41 +04:00
|
|
|
self.adjust()
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def adjust(self):
|
|
|
|
self.closeFP()
|
|
|
|
if self.index > len(self.filenames):
|
|
|
|
raise StopIteration
|
|
|
|
elif self.index == len(self.filenames):
|
2012-07-18 16:09:04 +04:00
|
|
|
self.iter = iter(self.custom)
|
2012-02-16 13:46:41 +04:00
|
|
|
else:
|
2014-12-19 11:37:06 +03:00
|
|
|
self.current = self.filenames[self.index]
|
|
|
|
if os.path.splitext(self.current)[1].lower() == ".zip":
|
2015-08-30 23:52:24 +03:00
|
|
|
try:
|
|
|
|
_ = zipfile.ZipFile(self.current, 'r')
|
|
|
|
except zipfile.error, ex:
|
|
|
|
errMsg = "something seems to be wrong with "
|
|
|
|
errMsg += "the file '%s' ('%s'). Please make " % (self.current, ex)
|
|
|
|
errMsg += "sure that you haven't made any changes to it"
|
|
|
|
raise SqlmapInstallationException, errMsg
|
2012-07-18 16:24:10 +04:00
|
|
|
if len(_.namelist()) == 0:
|
2014-12-19 11:37:06 +03:00
|
|
|
errMsg = "no file(s) inside '%s'" % self.current
|
2013-01-04 02:20:55 +04:00
|
|
|
raise SqlmapDataException(errMsg)
|
2012-07-18 16:24:10 +04:00
|
|
|
self.fp = _.open(_.namelist()[0])
|
|
|
|
else:
|
2014-12-19 11:37:06 +03:00
|
|
|
self.fp = open(self.current, 'r')
|
2012-02-16 13:46:41 +04:00
|
|
|
self.iter = iter(self.fp)
|
|
|
|
|
|
|
|
self.index += 1
|
|
|
|
|
|
|
|
def closeFP(self):
|
|
|
|
if self.fp:
|
|
|
|
self.fp.close()
|
|
|
|
self.fp = None
|
|
|
|
|
|
|
|
def next(self):
|
|
|
|
retVal = None
|
2012-07-18 15:32:34 +04:00
|
|
|
while True:
|
2012-07-18 16:09:04 +04:00
|
|
|
self.counter += 1
|
2012-07-18 15:32:34 +04:00
|
|
|
try:
|
|
|
|
retVal = self.iter.next().rstrip()
|
2014-12-19 11:37:06 +03:00
|
|
|
except zipfile.error, ex:
|
|
|
|
errMsg = "something seems to be wrong with "
|
|
|
|
errMsg += "the file '%s' ('%s'). Please make " % (self.current, ex)
|
|
|
|
errMsg += "sure that you haven't made any changes to it"
|
|
|
|
raise SqlmapInstallationException, errMsg
|
2012-07-18 15:32:34 +04:00
|
|
|
except StopIteration:
|
|
|
|
self.adjust()
|
|
|
|
retVal = self.iter.next().rstrip()
|
2012-07-18 16:09:04 +04:00
|
|
|
if not self.proc_count or self.counter % self.proc_count == self.proc_id:
|
2012-07-18 15:32:34 +04:00
|
|
|
break
|
2012-02-16 13:46:41 +04:00
|
|
|
return retVal
|
|
|
|
|
|
|
|
def rewind(self):
|
|
|
|
self.index = 0
|
|
|
|
self.adjust()
|