Skip to content

Commit 00d3b10

Browse files
authored
Merge pull request #79 from gigkokman/wordlist_helper
Wordlist helper
2 parents 1dfa952 + 0847bcf commit 00d3b10

File tree

4 files changed

+99
-21
lines changed

4 files changed

+99
-21
lines changed

VHostScan.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
from socket import gethostbyaddr
88
from lib.core.virtual_host_scanner import *
99
from lib.helpers.output_helper import *
10-
from lib.helpers.file_helper import get_combined_word_lists
1110
from lib.helpers.file_helper import load_random_user_agents
11+
from lib.helpers.wordlist_helper import WordList
1212
from lib.core.__version__ import __version__
1313
from lib.input import cli_argument_parser
1414

@@ -31,21 +31,9 @@ def main():
3131
parser = cli_argument_parser()
3232
arguments = parser.parse(sys.argv[1:])
3333

34-
wordlist = []
35-
word_list_types = []
36-
37-
default_wordlist = DEFAULT_WORDLIST_FILE \
38-
if sys.stdin.isatty() else None
39-
40-
if not sys.stdin.isatty():
41-
word_list_types.append('stdin')
42-
wordlist.extend(list(line for line in sys.stdin.read().splitlines()))
43-
44-
combined = get_combined_word_lists(arguments.wordlists or default_wordlist)
45-
word_list_types.append('wordlists: {}'.format(
46-
', '.join(combined['file_paths']),
47-
))
48-
wordlist.extend(combined['words'])
34+
wordlist_helper = WordList()
35+
wordlist, wordlist_types = wordlist_helper.get_wordlist(
36+
arguments.wordlists)
4937

5038
if len(wordlist) == 0:
5139
print("[!] No words found in provided wordlists, unable to scan.")
@@ -56,7 +44,7 @@ def main():
5644
"port {port} and {inputs}".format(
5745
host=arguments.target_hosts,
5846
port=arguments.port,
59-
inputs=', '.join(word_list_types),
47+
inputs=', '.join(wordlist_types),
6048
)
6149
)
6250

lib/helpers/output_helper.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ def write_grepable(self, filename):
3535
def output_normal_likely(self):
3636
uniques = False
3737
depth = str(self.scanner.unique_depth)
38-
output = "\n[+] Most likely matches with a unique count "\
39-
"of {} or less:".format(depth)
38+
output = (
39+
"\n[+] Most likely matches with a unique count "
40+
"of {} or less:").format(depth)
4041

4142
for p in self.scanner.likely_matches():
4243
output += "\n\t[>] {}".format(p)
@@ -45,8 +46,9 @@ def output_normal_likely(self):
4546
if(uniques):
4647
return output
4748
else:
48-
return "\n[!] No matches with an" \
49-
" unique count of {} or less.".format(depth)
49+
return (
50+
"\n[!] No matches with an"
51+
" unique count of {} or less.").format(depth)
5052

5153
def output_json(self, filename):
5254
file = file_helper(filename)

lib/helpers/wordlist_helper.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import sys
2+
import os
3+
from lib.helpers.file_helper import get_combined_word_lists
4+
5+
DEFAULT_WORDLIST_FILE = os.path.join(
6+
os.path.dirname(os.path.abspath(__file__)),
7+
'../..',
8+
'wordlists',
9+
'virtual-host-scanning.txt'
10+
)
11+
12+
13+
class WordList:
14+
def __init__(self):
15+
self.wordlist = []
16+
self.wordlist_types = []
17+
18+
def get_stdin_wordlist(self):
19+
return list(line for line in sys.stdin.read().splitlines()) \
20+
if not sys.stdin.isatty() else []
21+
22+
def get_wordlist(self, wordlist_files=None):
23+
default_wordlist_file = DEFAULT_WORDLIST_FILE
24+
25+
stdin_words = self.get_stdin_wordlist()
26+
if stdin_words:
27+
self.set_words(words_type='stdin', words=stdin_words)
28+
default_wordlist_file = None
29+
30+
combined_files = wordlist_files or default_wordlist_file
31+
combined = get_combined_word_lists(combined_files)
32+
if combined:
33+
words_type = 'wordlists: {}'.format(
34+
', '.join(combined['file_paths']))
35+
self.set_words(words_type=words_type, words=combined['words'])
36+
37+
return self.wordlist, self.wordlist_types
38+
39+
def set_words(self, words_type, words):
40+
self.wordlist_types.append(words_type)
41+
self.wordlist.extend(words)

tests/helpers/test_wordlist_helper.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import unittest
2+
import pytest
3+
from mock import patch
4+
5+
from lib.helpers.wordlist_helper import WordList
6+
from lib.helpers.wordlist_helper import DEFAULT_WORDLIST_FILE
7+
8+
9+
@pytest.fixture(scope='class')
10+
def user_wordlist(request, tmpdir_factory):
11+
user_wordlist = ['user-word1', 'user-word2']
12+
tmpdir = tmpdir_factory.mktemp('user_wordlist')
13+
user_wordlist_file = tmpdir.join('user-wordlist.txt')
14+
user_wordlist_file.write('\n'.join(user_wordlist))
15+
request.cls.user_wordlist_file = str(user_wordlist_file)
16+
request.cls.user_wordlist = user_wordlist
17+
18+
19+
@pytest.mark.usefixtures('user_wordlist')
20+
class TestWordList(unittest.TestCase):
21+
def setUp(self):
22+
self.wordlist = WordList()
23+
with open(DEFAULT_WORDLIST_FILE, 'r') as word_file:
24+
self.default_wordlist = list(word_file.read().splitlines())
25+
26+
def test_get_wordlist_from_stdin(self):
27+
stdin_wordlist = ['keyword1', 'keyword1']
28+
expected_wordlist = []
29+
expected_wordlist.extend(stdin_wordlist)
30+
with patch('lib.helpers.wordlist_helper.WordList.get_stdin_wordlist', return_value=stdin_wordlist):
31+
wordlist, wordlist_types = self.wordlist.get_wordlist()
32+
self.assertEqual(wordlist, expected_wordlist)
33+
34+
def test_get_wordlist_from_stdin_and_wordlist(self):
35+
stdin_wordlist = ['keyword1', 'keyword1']
36+
expected_wordlist = []
37+
expected_wordlist.extend(stdin_wordlist)
38+
expected_wordlist.extend(self.user_wordlist)
39+
with patch('lib.helpers.wordlist_helper.WordList.get_stdin_wordlist', return_value=stdin_wordlist):
40+
wordlist, wordlist_types = self.wordlist.get_wordlist(self.user_wordlist_file)
41+
self.assertEqual(wordlist, expected_wordlist)
42+
43+
def test_using_default_wordlist(self):
44+
stdin_wordlist = []
45+
with patch('lib.helpers.wordlist_helper.WordList.get_stdin_wordlist', return_value=stdin_wordlist):
46+
wordlist, wordlist_types = self.wordlist.get_wordlist()
47+
self.assertEqual(wordlist, self.default_wordlist)

0 commit comments

Comments
 (0)