Skip to content

Commit

Permalink
Adding permutator feature for usernames (#1575)
Browse files Browse the repository at this point in the history
* Adding permutator feature for usernames
("", "_", "-", ".") when id_type == username

File : maigret/permutator.py

Arg : --permute

For now, only permute from 2 elements and doesn't return single elements (element1, _element1, element1_,  element2, _element2, ...). 12 permuts for 2 elements.

To return single elements as well, Permute(usernames).gather(method="all"), but not implemented in maigrat.py. 18 permuts for 2 elements. Should we ? With another argument ?

* Update test_cli.py

permute arg added
  • Loading branch information
balestek authored Jul 23, 2024
1 parent e02a557 commit 6fb0dc1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
16 changes: 16 additions & 0 deletions maigret/maigret.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from .types import QueryResultWrapper
from .utils import get_dict_ascii_tree
from .settings import Settings
from .permutator import Permute


def notify_about_errors(search_results: QueryResultWrapper, query_notify):
Expand Down Expand Up @@ -205,6 +206,12 @@ def setup_arguments_parser(settings: Settings):
choices=SUPPORTED_IDS,
help="Specify identifier(s) type (default: username).",
)
parser.add_argument(
"--permute",
action="store_true",
default=False,
help="Permute at least 2 usernames to generate more possible usernames.",
)
parser.add_argument(
"--db",
metavar="DB_FILE",
Expand Down Expand Up @@ -502,6 +509,10 @@ async def main():
for u in args.username
if u and u not in ['-'] and u not in args.ignore_ids_list
}
original_usernames = ""
if args.permute and len(usernames) > 1 and args.id_type == 'username':
original_usernames = " ".join(usernames.keys())
usernames = Permute(usernames).gather(method='strict')

parsing_enabled = not args.disable_extracting
recursive_search_enabled = not args.disable_recursive_search
Expand Down Expand Up @@ -591,6 +602,11 @@ async def main():
query_notify.warning('No usernames to check, exiting.')
sys.exit(0)

if len(usernames) > 1 and args.permute and args.id_type == 'username':
query_notify.warning(
f"{len(usernames)} permutations from {original_usernames} to check..."
)

if not site_data:
query_notify.warning('No sites to check, exiting!')
sys.exit(2)
Expand Down
26 changes: 26 additions & 0 deletions maigret/permutator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# License MIT. by balestek https://github.com/balestek
from itertools import permutations


class Permute:
def __init__(self, elements: dict):
self.separators = ["", "_", "-", "."]
self.elements = elements

def gather(self, method: str = "strict" or "all") -> dict:
permutations_dict = {}
for i in range(1, len(self.elements) + 1):
for subset in permutations(self.elements, i):
if i == 1:
if method == "all":
permutations_dict[subset[0]] = self.elements[subset[0]]
permutations_dict["_" + subset[0]] = self.elements[subset[0]]
permutations_dict[subset[0] + "_"] = self.elements[subset[0]]
else:
for separator in self.separators:
perm = separator.join(subset)
permutations_dict[perm] = self.elements[subset[0]]
if separator == "":
permutations_dict["_" + perm] = self.elements[subset[0]]
permutations_dict[perm + "_"] = self.elements[subset[0]]
return permutations_dict
1 change: 1 addition & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
'no_progressbar': False,
'parse_url': '',
'pdf': False,
'permute': False,
'print_check_errors': False,
'print_not_found': False,
'proxy': None,
Expand Down

0 comments on commit 6fb0dc1

Please sign in to comment.