-
-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from gigkokman/wordlist_helper
Wordlist helper
- Loading branch information
Showing
4 changed files
with
99 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import sys | ||
import os | ||
from lib.helpers.file_helper import get_combined_word_lists | ||
|
||
DEFAULT_WORDLIST_FILE = os.path.join( | ||
os.path.dirname(os.path.abspath(__file__)), | ||
'../..', | ||
'wordlists', | ||
'virtual-host-scanning.txt' | ||
) | ||
|
||
|
||
class WordList: | ||
def __init__(self): | ||
self.wordlist = [] | ||
self.wordlist_types = [] | ||
|
||
def get_stdin_wordlist(self): | ||
return list(line for line in sys.stdin.read().splitlines()) \ | ||
if not sys.stdin.isatty() else [] | ||
|
||
def get_wordlist(self, wordlist_files=None): | ||
default_wordlist_file = DEFAULT_WORDLIST_FILE | ||
|
||
stdin_words = self.get_stdin_wordlist() | ||
if stdin_words: | ||
self.set_words(words_type='stdin', words=stdin_words) | ||
default_wordlist_file = None | ||
|
||
combined_files = wordlist_files or default_wordlist_file | ||
combined = get_combined_word_lists(combined_files) | ||
if combined: | ||
words_type = 'wordlists: {}'.format( | ||
', '.join(combined['file_paths'])) | ||
self.set_words(words_type=words_type, words=combined['words']) | ||
|
||
return self.wordlist, self.wordlist_types | ||
|
||
def set_words(self, words_type, words): | ||
self.wordlist_types.append(words_type) | ||
self.wordlist.extend(words) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import unittest | ||
import pytest | ||
from mock import patch | ||
|
||
from lib.helpers.wordlist_helper import WordList | ||
from lib.helpers.wordlist_helper import DEFAULT_WORDLIST_FILE | ||
|
||
|
||
@pytest.fixture(scope='class') | ||
def user_wordlist(request, tmpdir_factory): | ||
user_wordlist = ['user-word1', 'user-word2'] | ||
tmpdir = tmpdir_factory.mktemp('user_wordlist') | ||
user_wordlist_file = tmpdir.join('user-wordlist.txt') | ||
user_wordlist_file.write('\n'.join(user_wordlist)) | ||
request.cls.user_wordlist_file = str(user_wordlist_file) | ||
request.cls.user_wordlist = user_wordlist | ||
|
||
|
||
@pytest.mark.usefixtures('user_wordlist') | ||
class TestWordList(unittest.TestCase): | ||
def setUp(self): | ||
self.wordlist = WordList() | ||
with open(DEFAULT_WORDLIST_FILE, 'r') as word_file: | ||
self.default_wordlist = list(word_file.read().splitlines()) | ||
|
||
def test_get_wordlist_from_stdin(self): | ||
stdin_wordlist = ['keyword1', 'keyword1'] | ||
expected_wordlist = [] | ||
expected_wordlist.extend(stdin_wordlist) | ||
with patch('lib.helpers.wordlist_helper.WordList.get_stdin_wordlist', return_value=stdin_wordlist): | ||
wordlist, wordlist_types = self.wordlist.get_wordlist() | ||
self.assertEqual(wordlist, expected_wordlist) | ||
|
||
def test_get_wordlist_from_stdin_and_wordlist(self): | ||
stdin_wordlist = ['keyword1', 'keyword1'] | ||
expected_wordlist = [] | ||
expected_wordlist.extend(stdin_wordlist) | ||
expected_wordlist.extend(self.user_wordlist) | ||
with patch('lib.helpers.wordlist_helper.WordList.get_stdin_wordlist', return_value=stdin_wordlist): | ||
wordlist, wordlist_types = self.wordlist.get_wordlist(self.user_wordlist_file) | ||
self.assertEqual(wordlist, expected_wordlist) | ||
|
||
def test_using_default_wordlist(self): | ||
stdin_wordlist = [] | ||
with patch('lib.helpers.wordlist_helper.WordList.get_stdin_wordlist', return_value=stdin_wordlist): | ||
wordlist, wordlist_types = self.wordlist.get_wordlist() | ||
self.assertEqual(wordlist, self.default_wordlist) |