Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sqlmapproject
GitHub Repository: sqlmapproject/sqlmap
Path: blob/master/lib/core/wordlist.py
3556 views
1
#!/usr/bin/env python
2
3
"""
4
Copyright (c) 2006-2026 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.zip_file = None
31
self.index = 0
32
self.counter = -1
33
self.current = None
34
self.iter = None
35
self.custom = custom or []
36
self.proc_id = proc_id
37
self.proc_count = proc_count
38
self.adjust()
39
40
def __iter__(self):
41
return self
42
43
def adjust(self):
44
self.closeFP()
45
if self.index > len(self.filenames):
46
return # Note: https://stackoverflow.com/a/30217723 (PEP 479)
47
elif self.index == len(self.filenames):
48
self.iter = iter(self.custom)
49
else:
50
self.current = self.filenames[self.index]
51
if isZipFile(self.current):
52
try:
53
self.zip_file = zipfile.ZipFile(self.current, 'r')
54
except zipfile.error as ex:
55
errMsg = "something appears to be wrong with "
56
errMsg += "the file '%s' ('%s'). Please make " % (self.current, getSafeExString(ex))
57
errMsg += "sure that you haven't made any changes to it"
58
raise SqlmapInstallationException(errMsg)
59
if len(self.zip_file.namelist()) == 0:
60
errMsg = "no file(s) inside '%s'" % self.current
61
raise SqlmapDataException(errMsg)
62
self.fp = self.zip_file.open(self.zip_file.namelist()[0])
63
else:
64
self.fp = open(self.current, "rb")
65
self.iter = iter(self.fp)
66
67
self.index += 1
68
69
def closeFP(self):
70
if self.fp:
71
self.fp.close()
72
self.fp = None
73
74
if self.zip_file:
75
self.zip_file.close()
76
self.zip_file = None
77
78
def __next__(self):
79
retVal = None
80
while True:
81
self.counter += 1
82
try:
83
retVal = next(self.iter).rstrip()
84
except zipfile.error as ex:
85
errMsg = "something appears to be wrong with "
86
errMsg += "the file '%s' ('%s'). Please make " % (self.current, getSafeExString(ex))
87
errMsg += "sure that you haven't made any changes to it"
88
raise SqlmapInstallationException(errMsg)
89
except StopIteration:
90
self.adjust()
91
retVal = next(self.iter).rstrip()
92
if not self.proc_count or self.counter % self.proc_count == self.proc_id:
93
break
94
return retVal
95
96
def rewind(self):
97
self.index = 0
98
self.adjust()
99
100