Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sqlmapproject
GitHub Repository: sqlmapproject/sqlmap
Path: blob/master/lib/core/wordlist.py
2989 views
1
#!/usr/bin/env python
2
3
"""
4
Copyright (c) 2006-2025 sqlmap developers (https://sqlmap.org)
5
See the file 'LICENSE' for copying permission
6
"""
7
8
import zipfile
9
10
from lib.core.common import getSafeExString
11
from lib.core.common import isZipFile
12
from lib.core.exception import SqlmapDataException
13
from lib.core.exception import SqlmapInstallationException
14
from thirdparty import six
15
16
class Wordlist(six.Iterator):
17
"""
18
Iterator for looping over a large dictionaries
19
20
>>> from lib.core.option import paths
21
>>> isinstance(next(Wordlist(paths.SMALL_DICT)), six.binary_type)
22
True
23
>>> isinstance(next(Wordlist(paths.WORDLIST)), six.binary_type)
24
True
25
"""
26
27
def __init__(self, filenames, proc_id=None, proc_count=None, custom=None):
28
self.filenames = [filenames] if isinstance(filenames, six.string_types) else filenames
29
self.fp = None
30
self.index = 0
31
self.counter = -1
32
self.current = None
33
self.iter = None
34
self.custom = custom or []
35
self.proc_id = proc_id
36
self.proc_count = proc_count
37
self.adjust()
38
39
def __iter__(self):
40
return self
41
42
def adjust(self):
43
self.closeFP()
44
if self.index > len(self.filenames):
45
return # Note: https://stackoverflow.com/a/30217723 (PEP 479)
46
elif self.index == len(self.filenames):
47
self.iter = iter(self.custom)
48
else:
49
self.current = self.filenames[self.index]
50
if isZipFile(self.current):
51
try:
52
_ = zipfile.ZipFile(self.current, 'r')
53
except zipfile.error as ex:
54
errMsg = "something appears to be wrong with "
55
errMsg += "the file '%s' ('%s'). Please make " % (self.current, getSafeExString(ex))
56
errMsg += "sure that you haven't made any changes to it"
57
raise SqlmapInstallationException(errMsg)
58
if len(_.namelist()) == 0:
59
errMsg = "no file(s) inside '%s'" % self.current
60
raise SqlmapDataException(errMsg)
61
self.fp = _.open(_.namelist()[0])
62
else:
63
self.fp = open(self.current, "rb")
64
self.iter = iter(self.fp)
65
66
self.index += 1
67
68
def closeFP(self):
69
if self.fp:
70
self.fp.close()
71
self.fp = None
72
73
def __next__(self):
74
retVal = None
75
while True:
76
self.counter += 1
77
try:
78
retVal = next(self.iter).rstrip()
79
except zipfile.error as ex:
80
errMsg = "something appears to be wrong with "
81
errMsg += "the file '%s' ('%s'). Please make " % (self.current, getSafeExString(ex))
82
errMsg += "sure that you haven't made any changes to it"
83
raise SqlmapInstallationException(errMsg)
84
except StopIteration:
85
self.adjust()
86
retVal = next(self.iter).rstrip()
87
if not self.proc_count or self.counter % self.proc_count == self.proc_id:
88
break
89
return retVal
90
91
def rewind(self):
92
self.index = 0
93
self.adjust()
94
95