Skip to content

Word Cloud

These are helpers related to BBOT's Word Cloud, a mechanism for storing target-specific keywords that are useful for custom wordlists, etc.

Note that these helpers can be invoked directly from self.helpers, e.g.:

self.helpers.word_cloud

DNSMutator

Bases: Mutator

DNS-specific mutator used by the massdns module to generate target-specific subdomain mutations.

This class extends the Mutator base class to add DNS-specific logic for generating subdomain mutations based on input words. It utilizes custom word extraction patterns and a wordninja model trained on DNS-specific data.

Examples:

>>> s = Scanner("www1.evilcorp.com", "www-test.evilcorp.com")
>>> s.start_without_generator()
>>> s.helpers.word_cloud.dns_mutator.mutations("word")
[
    "word",
    "word-test",
    "word1",
    "wordtest",
    "www-word",
    "wwwword"
]
Source code in bbot/core/helpers/wordcloud.py
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
class DNSMutator(Mutator):
    """
    DNS-specific mutator used by the `massdns` module to generate target-specific subdomain mutations.

    This class extends the Mutator base class to add DNS-specific logic for generating
    subdomain mutations based on input words. It utilizes custom word extraction patterns
    and a wordninja model trained on DNS-specific data.

    Examples:
        >>> s = Scanner("www1.evilcorp.com", "www-test.evilcorp.com")
        >>> s.start_without_generator()
        >>> s.helpers.word_cloud.dns_mutator.mutations("word")
        [
            "word",
            "word-test",
            "word1",
            "wordtest",
            "www-word",
            "wwwword"
        ]
    """

    extract_word_regexes = [
        re.compile(r, re.I)
        for r in [
            r"[a-z]+",
            r"[a-z_-]+",
            r"[a-z0-9]+",
            r"[a-z0-9_-]+",
        ]
    ]

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        wordlist_dir = Path(__file__).parent.parent.parent / "wordlists"
        wordninja_dns_wordlist = wordlist_dir / "wordninja_dns.txt.gz"
        self.model = wordninja.LanguageModel(wordninja_dns_wordlist)

    def mutations(self, words, max_mutations=None):
        if isinstance(words, str):
            words = [words]
        new_words = set()
        for word in words:
            for e in extract_words(word, acronyms=False, model=self.model, word_regexes=self.extract_word_regexes):
                new_words.add(e)
        return super().mutations(new_words, max_mutations=max_mutations)

    def add_word(self, word):
        spans = set()
        mutations = set()
        for r in self.extract_word_regexes:
            for match in r.finditer(word):
                span = match.span()
                if span not in spans:
                    spans.add(span)
        for start, end in spans:
            match_str = word[start:end]
            # skip digits
            if match_str.isdigit():
                continue
            before = word[:start]
            after = word[end:]
            basic_mutation = (before, None, after)
            mutations.add(basic_mutation)
            match_str_split = self.model.split(match_str)
            if len(match_str_split) > 1:
                for i, s in enumerate(match_str_split):
                    if s.isdigit():
                        continue
                    split_before = "".join(match_str_split[:i])
                    split_after = "".join(match_str_split[i + 1 :])
                    wordninja_mutation = (before + split_before, None, split_after + after)
                    mutations.add(wordninja_mutation)
        for m in mutations:
            self._add_mutation(m)

Mutator

Bases: dict

Base class for generating mutations from a list of words. It accumulates words and produces mutations from them.

Source code in bbot/core/helpers/wordcloud.py
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
class Mutator(dict):
    """
    Base class for generating mutations from a list of words.
    It accumulates words and produces mutations from them.
    """

    def mutations(self, words, max_mutations=None):
        mutations = self.top_mutations(max_mutations)
        ret = set()
        if isinstance(words, str):
            words = [words]
        for word in words:
            for m in self.mutate(word, mutations=mutations):
                ret.add("".join(m))
        return ret

    def mutate(self, word, max_mutations=None, mutations=None):
        if mutations is None:
            mutations = self.top_mutations(max_mutations)
        for mutation, count in mutations.items():
            ret = []
            for s in mutation:
                if s is not None:
                    ret.append(s)
                else:
                    ret.append(word)
            yield ret

    def top_mutations(self, n=None):
        if n is not None:
            return dict(sorted(self.items(), key=lambda x: x[-1], reverse=True)[:n])
        else:
            return dict(self)

    def _add_mutation(self, mutation):
        if None not in mutation:
            return
        mutation = tuple([m for m in mutation if m != ""])
        try:
            self[mutation] += 1
        except KeyError:
            self[mutation] = 1

    def add_word(self, word):
        pass

WordCloud

Bases: dict

WordCloud is a specialized dictionary-like class for storing and aggregating words extracted from various data sources such as DNS names and URLs. The class is intended to facilitate the generation of target-specific wordlists and mutations.

The WordCloud class can be accessed and manipulated like a standard Python dictionary. It also offers additional methods for generating mutations based on the words it contains.

Attributes:

  • parent_helper

    The parent helper object that provides necessary utilities.

  • devops_mutations

    A set containing common devops-related mutations, loaded from a file.

  • dns_mutator

    An instance of the DNSMutator class for generating DNS-based mutations.

Examples:

>>> s = Scanner("www1.evilcorp.com", "www-test.evilcorp.com")
>>> s.start_without_generator()
>>> print(s.helpers.word_cloud)
{
    "evilcorp": 2,
    "ec": 2,
    "www1": 1,
    "evil": 2,
    "www": 2,
    "w1": 1,
    "corp": 2,
    "1": 1,
    "wt": 1,
    "test": 1,
    "www-test": 1
}
>>> s.helpers.word_cloud.mutations(["word"], cloud=True, numbers=0, devops=False, letters=False)
[
    [
        "1",
        "word"
    ],
    [
        "corp",
        "word"
    ],
    [
        "ec",
        "word"
    ],
    [
        "evil",
        "word"
    ],
    ...
]
>>> s.helpers.word_cloud.dns_mutator.mutations("word")
[
    "word",
    "word-test",
    "word1",
    "wordtest",
    "www-word",
    "wwwword"
]
Source code in bbot/core/helpers/wordcloud.py
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
class WordCloud(dict):
    """
    WordCloud is a specialized dictionary-like class for storing and aggregating
    words extracted from various data sources such as DNS names and URLs. The class
    is intended to facilitate the generation of target-specific wordlists and mutations.

    The WordCloud class can be accessed and manipulated like a standard Python dictionary.
    It also offers additional methods for generating mutations based on the words it contains.

    Attributes:
        parent_helper: The parent helper object that provides necessary utilities.
        devops_mutations: A set containing common devops-related mutations, loaded from a file.
        dns_mutator: An instance of the DNSMutator class for generating DNS-based mutations.

    Examples:
        >>> s = Scanner("www1.evilcorp.com", "www-test.evilcorp.com")
        >>> s.start_without_generator()
        >>> print(s.helpers.word_cloud)
        {
            "evilcorp": 2,
            "ec": 2,
            "www1": 1,
            "evil": 2,
            "www": 2,
            "w1": 1,
            "corp": 2,
            "1": 1,
            "wt": 1,
            "test": 1,
            "www-test": 1
        }

        >>> s.helpers.word_cloud.mutations(["word"], cloud=True, numbers=0, devops=False, letters=False)
        [
            [
                "1",
                "word"
            ],
            [
                "corp",
                "word"
            ],
            [
                "ec",
                "word"
            ],
            [
                "evil",
                "word"
            ],
            ...
        ]

        >>> s.helpers.word_cloud.dns_mutator.mutations("word")
        [
            "word",
            "word-test",
            "word1",
            "wordtest",
            "www-word",
            "wwwword"
        ]
    """

    def __init__(self, parent_helper, *args, **kwargs):
        self.parent_helper = parent_helper

        devops_filename = self.parent_helper.wordlist_dir / "devops_mutations.txt"
        self.devops_mutations = set(self.parent_helper.read_file(devops_filename))

        self.dns_mutator = DNSMutator()

        super().__init__(*args, **kwargs)

    def mutations(
        self, words, devops=True, cloud=True, letters=True, numbers=5, number_padding=2, substitute_numbers=True
    ):
        """
        Generate various mutations for the given list of words based on different criteria.

        Yields tuples of strings which can be joined on the desired delimiter, e.g. "-" or "_".

        Args:
            words (Union[str, Iterable[str]]): A single word or list of words to mutate.
            devops (bool): Whether to include devops-related mutations.
            cloud (bool): Whether to include mutations from the word cloud.
            letters (bool): Whether to include letter-based mutations.
            numbers (int): The maximum numeric mutations to include.
            number_padding (int): Padding for numeric mutations.
            substitute_numbers (bool): Whether to substitute numbers in mutations.

        Yields:
            tuple: A tuple containing each of the mutation segments.
        """
        if isinstance(words, str):
            words = (words,)
        results = set()
        for word in words:
            h = hash(word)
            if not h in results:
                results.add(h)
                yield (word,)
        if numbers > 0:
            if substitute_numbers:
                for word in words:
                    for number_mutation in self.get_number_mutations(word, n=numbers, padding=number_padding):
                        h = hash(number_mutation)
                        if not h in results:
                            results.add(h)
                            yield (number_mutation,)
        for word in words:
            for modifier in self.modifiers(
                devops=devops, cloud=cloud, letters=letters, numbers=numbers, number_padding=number_padding
            ):
                a = (word, modifier)
                b = (modifier, word)
                for _ in (a, b):
                    h = hash(_)
                    if h not in results:
                        results.add(h)
                        yield _

    def modifiers(self, devops=True, cloud=True, letters=True, numbers=5, number_padding=2):
        modifiers = set()
        if devops:
            modifiers.update(self.devops_mutations)
        if cloud:
            modifiers.update(set(self))
        if letters:
            modifiers.update(set(string.ascii_lowercase))
        if numbers > 0:
            modifiers.update(self.parent_helper.gen_numbers(numbers, number_padding))
        return modifiers

    def absorb_event(self, event):
        """
        Absorbs an event from a BBOT scan into the word cloud.

        This method updates the word cloud by extracting words from the given event. It aims to avoid including PTR
        (Pointer) records, as they tend to produce unhelpful mutations in the word cloud.

        Args:
            event (Event): The event object containing the words to be absorbed into the word cloud.
        """
        for word in event.words:
            self.add_word(word)
        if event.scope_distance == 0 and event.type.startswith("DNS_NAME"):
            subdomain = tldextract(event.data).subdomain
            if subdomain and not self.parent_helper.is_ptr(subdomain):
                for s in subdomain.split("."):
                    self.dns_mutator.add_word(s)

    def absorb_word(self, word, ninja=True):
        """
        Absorbs a word into the word cloud after splitting it using a word extraction algorithm.

        This method splits the input word into smaller meaningful words using word extraction, and then adds each
        of them to the word cloud. The splitting is done using a predefined algorithm in the parent helper.

        Args:
            word (str): The word to be split and absorbed into the word cloud.
            ninja (bool, optional): If True, word extraction is enabled. Defaults to True.

        Examples:
            >>> self.helpers.word_cloud.absorb_word("blacklantern")
            >>> print(self.helpers.word_cloud)
            {
                "blacklantern": 1,
                "black": 1,
                "bl": 1,
                "lantern": 1
            }
        """
        for w in self.parent_helper.extract_words(word):
            self.add_word(w)

    def add_word(self, word, lowercase=True):
        """
        Adds a word to the word cloud.

        This method updates the word cloud by adding a given word. If the word already exists in the cloud,
        its frequency count is incremented by 1. Optionally, the word can be converted to lowercase before adding.

        Args:
            word (str): The word to be added to the word cloud.
            lowercase (bool, optional): If True, the word will be converted to lowercase before adding. Defaults to True.

        Examples:
            >>> self.helpers.word_cloud.add_word("Example")
            >>> self.helpers.word_cloud.add_word("example")
            >>> print(self.helpers.word_cloud)
            {'example': 2}
        """
        if lowercase:
            word = word.lower()
        try:
            self[word] += 1
        except KeyError:
            self[word] = 1

    def get_number_mutations(self, base, n=5, padding=2):
        """
        Generates mutations of a base string by modifying the numerical parts or appending numbers.

        This method detects existing numbers in the base string and tries incrementing and decrementing them within a
        specified range. It also appends numbers at the end or after each word to generate more mutations.

        Args:
            base (str): The base string to generate mutations from.
            n (int, optional): The range of numbers to use for incrementing/decrementing. Defaults to 5.
            padding (int, optional): Zero-pad numbers up to this length. Defaults to 2.

        Returns:
            set: A set of mutated strings based on the base input.

        Examples:
            >>> self.helpers.word_cloud.get_number_mutations("www2-test", n=2)
            {
                "www0-test",
                "www1-test",
                "www2-test",
                "www2-test0",
                "www2-test00",
                "www2-test01",
                "www2-test1",
                "www3-test",
                "www4-test"
            }
        """
        results = set()

        # detects numbers and increments/decrements them
        # e.g. for "base2_p013", we would try:
        # - "base0_p013" through "base12_p013"
        # - "base2_p003" through "base2_p023"
        # limited to three iterations for sanity's sake
        for match in list(self.parent_helper.regexes.num_regex.finditer(base))[-3:]:
            span = match.span()
            before = base[: span[0]]
            after = base[span[-1] :]
            number = base[span[0] : span[-1]]
            numlen = len(number)
            maxnum = min(int("9" * numlen), int(number) + n)
            minnum = max(0, int(number) - n)
            for i in range(minnum, maxnum + 1):
                filled_num = str(i).zfill(numlen)
                results.add(f"{before}{filled_num}{after}")
                if not number.startswith("0"):
                    results.add(f"{before}{i}{after}")

        # appends numbers after each word
        # e.g., for "base_www", we would try:
        # - "base1_www", "base2_www", etc.
        # - "base_www1", "base_www2", etc.
        # limited to three iterations for sanity's sake
        number_suffixes = self.parent_helper.gen_numbers(n, padding)
        for match in list(self.parent_helper.regexes.word_regex.finditer(base))[-3:]:
            span = match.span()
            for suffix in number_suffixes:
                before = base[: span[-1]]
                after = base[span[-1] :]
                # skip if there's already a number
                if len(after) > 1 and not after[0].isdigit():
                    results.add(f"{before}{suffix}{after}")
        # basic cases so we don't miss anything
        for s in number_suffixes:
            results.add(f"{base}{s}")
            results.add(base)

        return results

    def truncate(self, limit):
        """
        Truncates the word cloud dictionary to retain only the top `limit` entries based on their occurrence frequencies.

        Args:
            limit (int): The maximum number of entries to retain in the word cloud.

        Examples:
            >>> self.helpers.word_cloud.update({"apple": 5, "banana": 2, "cherry": 8})
            >>> self.helpers.word_cloud.truncate(2)
            >>> self.helpers.word_cloud
            {'cherry': 8, 'apple': 5}
        """
        new_self = dict(self.json(limit=limit))
        self.clear()
        self.update(new_self)

    def json(self, limit=None):
        """
        Returns the word cloud as a sorted OrderedDict, optionally truncated to the top `limit` entries.

        Args:
            limit (int, optional): The maximum number of entries to include in the returned OrderedDict. If None, all entries are included.

        Returns:
            OrderedDict: A dictionary sorted by word frequencies, potentially truncated to the top `limit` entries.

        Examples:
            >>> self.helpers.word_cloud.update({"apple": 5, "banana": 2, "cherry": 8})
            >>> self.helpers.word_cloud.json(limit=2)
            OrderedDict([('cherry', 8), ('apple', 5)])
        """
        cloud_sorted = sorted(self.items(), key=lambda x: x[-1], reverse=True)
        if limit is not None:
            cloud_sorted = cloud_sorted[:limit]
        return OrderedDict(cloud_sorted)

    @property
    def default_filename(self):
        return self.parent_helper.scan.home / f"wordcloud.tsv"

    def save(self, filename=None, limit=None):
        """
        Saves the word cloud to a file. The cloud can optionally be truncated to the top `limit` entries.

        Args:
            filename (str, optional): The path to the file where the word cloud will be saved. If None, uses a default filename.
            limit (int, optional): The maximum number of entries to save to the file. If None, all entries are saved.

        Returns:
            tuple: A tuple containing a boolean indicating success or failure, and the resolved filename.

        Examples:
            >>> self.helpers.word_cloud.update({"apple": 5, "banana": 2, "cherry": 8})
            >>> self.helpers.word_cloud.save(filename="word_cloud.txt", limit=2)
            (True, Path('word_cloud.txt'))
        """
        if filename is None:
            filename = self.default_filename
        else:
            filename = Path(filename).resolve()
        try:
            if not self.parent_helper.mkdir(filename.parent):
                log.error(f"Failure creating or error writing to {filename.parent} when saving word cloud")
                return
            if len(self) > 0:
                log.debug(f"Saving word cloud to {filename}")
                with open(str(filename), mode="w", newline="") as f:
                    c = csv.writer(f, delimiter="\t")
                    for word, count in self.json(limit).items():
                        c.writerow([count, word])
                log.debug(f"Saved word cloud ({len(self):,} words) to {filename}")
                return True, filename
            else:
                log.debug(f"No words to save")
        except Exception as e:
            import traceback

            log.warning(f"Failed to save word cloud to {filename}: {e}")
            log.trace(traceback.format_exc())
        return False, filename

    def load(self, filename=None):
        """
        Loads a word cloud from a file. The file can be either a standard wordlist with one entry per line
        or a .tsv (tab-separated) file where the first row is the count and the second row is the associated entry.

        Args:
            filename (str, optional): The path to the file from which to load the word cloud. If None, uses a default filename.
        """
        if filename is None:
            wordcloud_path = self.default_filename
        else:
            wordcloud_path = Path(filename).resolve()
        log.verbose(f"Loading word cloud from {wordcloud_path}")
        try:
            with open(str(wordcloud_path), newline="") as f:
                c = csv.reader(f, delimiter="\t")
                for row in c:
                    if len(row) == 1:
                        self.add_word(row[0])
                    elif len(row) == 2:
                        with suppress(Exception):
                            count, word = row
                            count = int(count)
                            self[word] = count
            if len(self) > 0:
                log.success(f"Loaded word cloud ({len(self):,} words) from {wordcloud_path}")
        except Exception as e:
            import traceback

            log_fn = log.debug
            if filename is not None:
                log_fn = log.warning
            log_fn(f"Failed to load word cloud from {wordcloud_path}: {e}")
            if filename is not None:
                log.trace(traceback.format_exc())

absorb_event

absorb_event(event)

Absorbs an event from a BBOT scan into the word cloud.

This method updates the word cloud by extracting words from the given event. It aims to avoid including PTR (Pointer) records, as they tend to produce unhelpful mutations in the word cloud.

Parameters:

  • event (Event) –

    The event object containing the words to be absorbed into the word cloud.

Source code in bbot/core/helpers/wordcloud.py
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
def absorb_event(self, event):
    """
    Absorbs an event from a BBOT scan into the word cloud.

    This method updates the word cloud by extracting words from the given event. It aims to avoid including PTR
    (Pointer) records, as they tend to produce unhelpful mutations in the word cloud.

    Args:
        event (Event): The event object containing the words to be absorbed into the word cloud.
    """
    for word in event.words:
        self.add_word(word)
    if event.scope_distance == 0 and event.type.startswith("DNS_NAME"):
        subdomain = tldextract(event.data).subdomain
        if subdomain and not self.parent_helper.is_ptr(subdomain):
            for s in subdomain.split("."):
                self.dns_mutator.add_word(s)

absorb_word

absorb_word(word, ninja=True)

Absorbs a word into the word cloud after splitting it using a word extraction algorithm.

This method splits the input word into smaller meaningful words using word extraction, and then adds each of them to the word cloud. The splitting is done using a predefined algorithm in the parent helper.

Parameters:

  • word (str) –

    The word to be split and absorbed into the word cloud.

  • ninja (bool, default: True ) –

    If True, word extraction is enabled. Defaults to True.

Examples:

>>> self.helpers.word_cloud.absorb_word("blacklantern")
>>> print(self.helpers.word_cloud)
{
    "blacklantern": 1,
    "black": 1,
    "bl": 1,
    "lantern": 1
}
Source code in bbot/core/helpers/wordcloud.py
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
def absorb_word(self, word, ninja=True):
    """
    Absorbs a word into the word cloud after splitting it using a word extraction algorithm.

    This method splits the input word into smaller meaningful words using word extraction, and then adds each
    of them to the word cloud. The splitting is done using a predefined algorithm in the parent helper.

    Args:
        word (str): The word to be split and absorbed into the word cloud.
        ninja (bool, optional): If True, word extraction is enabled. Defaults to True.

    Examples:
        >>> self.helpers.word_cloud.absorb_word("blacklantern")
        >>> print(self.helpers.word_cloud)
        {
            "blacklantern": 1,
            "black": 1,
            "bl": 1,
            "lantern": 1
        }
    """
    for w in self.parent_helper.extract_words(word):
        self.add_word(w)

add_word

add_word(word, lowercase=True)

Adds a word to the word cloud.

This method updates the word cloud by adding a given word. If the word already exists in the cloud, its frequency count is incremented by 1. Optionally, the word can be converted to lowercase before adding.

Parameters:

  • word (str) –

    The word to be added to the word cloud.

  • lowercase (bool, default: True ) –

    If True, the word will be converted to lowercase before adding. Defaults to True.

Examples:

>>> self.helpers.word_cloud.add_word("Example")
>>> self.helpers.word_cloud.add_word("example")
>>> print(self.helpers.word_cloud)
{'example': 2}
Source code in bbot/core/helpers/wordcloud.py
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
def add_word(self, word, lowercase=True):
    """
    Adds a word to the word cloud.

    This method updates the word cloud by adding a given word. If the word already exists in the cloud,
    its frequency count is incremented by 1. Optionally, the word can be converted to lowercase before adding.

    Args:
        word (str): The word to be added to the word cloud.
        lowercase (bool, optional): If True, the word will be converted to lowercase before adding. Defaults to True.

    Examples:
        >>> self.helpers.word_cloud.add_word("Example")
        >>> self.helpers.word_cloud.add_word("example")
        >>> print(self.helpers.word_cloud)
        {'example': 2}
    """
    if lowercase:
        word = word.lower()
    try:
        self[word] += 1
    except KeyError:
        self[word] = 1

get_number_mutations

get_number_mutations(base, n=5, padding=2)

Generates mutations of a base string by modifying the numerical parts or appending numbers.

This method detects existing numbers in the base string and tries incrementing and decrementing them within a specified range. It also appends numbers at the end or after each word to generate more mutations.

Parameters:

  • base (str) –

    The base string to generate mutations from.

  • n (int, default: 5 ) –

    The range of numbers to use for incrementing/decrementing. Defaults to 5.

  • padding (int, default: 2 ) –

    Zero-pad numbers up to this length. Defaults to 2.

Returns:

  • set

    A set of mutated strings based on the base input.

Examples:

>>> self.helpers.word_cloud.get_number_mutations("www2-test", n=2)
{
    "www0-test",
    "www1-test",
    "www2-test",
    "www2-test0",
    "www2-test00",
    "www2-test01",
    "www2-test1",
    "www3-test",
    "www4-test"
}
Source code in bbot/core/helpers/wordcloud.py
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
def get_number_mutations(self, base, n=5, padding=2):
    """
    Generates mutations of a base string by modifying the numerical parts or appending numbers.

    This method detects existing numbers in the base string and tries incrementing and decrementing them within a
    specified range. It also appends numbers at the end or after each word to generate more mutations.

    Args:
        base (str): The base string to generate mutations from.
        n (int, optional): The range of numbers to use for incrementing/decrementing. Defaults to 5.
        padding (int, optional): Zero-pad numbers up to this length. Defaults to 2.

    Returns:
        set: A set of mutated strings based on the base input.

    Examples:
        >>> self.helpers.word_cloud.get_number_mutations("www2-test", n=2)
        {
            "www0-test",
            "www1-test",
            "www2-test",
            "www2-test0",
            "www2-test00",
            "www2-test01",
            "www2-test1",
            "www3-test",
            "www4-test"
        }
    """
    results = set()

    # detects numbers and increments/decrements them
    # e.g. for "base2_p013", we would try:
    # - "base0_p013" through "base12_p013"
    # - "base2_p003" through "base2_p023"
    # limited to three iterations for sanity's sake
    for match in list(self.parent_helper.regexes.num_regex.finditer(base))[-3:]:
        span = match.span()
        before = base[: span[0]]
        after = base[span[-1] :]
        number = base[span[0] : span[-1]]
        numlen = len(number)
        maxnum = min(int("9" * numlen), int(number) + n)
        minnum = max(0, int(number) - n)
        for i in range(minnum, maxnum + 1):
            filled_num = str(i).zfill(numlen)
            results.add(f"{before}{filled_num}{after}")
            if not number.startswith("0"):
                results.add(f"{before}{i}{after}")

    # appends numbers after each word
    # e.g., for "base_www", we would try:
    # - "base1_www", "base2_www", etc.
    # - "base_www1", "base_www2", etc.
    # limited to three iterations for sanity's sake
    number_suffixes = self.parent_helper.gen_numbers(n, padding)
    for match in list(self.parent_helper.regexes.word_regex.finditer(base))[-3:]:
        span = match.span()
        for suffix in number_suffixes:
            before = base[: span[-1]]
            after = base[span[-1] :]
            # skip if there's already a number
            if len(after) > 1 and not after[0].isdigit():
                results.add(f"{before}{suffix}{after}")
    # basic cases so we don't miss anything
    for s in number_suffixes:
        results.add(f"{base}{s}")
        results.add(base)

    return results

json

json(limit=None)

Returns the word cloud as a sorted OrderedDict, optionally truncated to the top limit entries.

Parameters:

  • limit (int, default: None ) –

    The maximum number of entries to include in the returned OrderedDict. If None, all entries are included.

Returns:

  • OrderedDict

    A dictionary sorted by word frequencies, potentially truncated to the top limit entries.

Examples:

>>> self.helpers.word_cloud.update({"apple": 5, "banana": 2, "cherry": 8})
>>> self.helpers.word_cloud.json(limit=2)
OrderedDict([('cherry', 8), ('apple', 5)])
Source code in bbot/core/helpers/wordcloud.py
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
def json(self, limit=None):
    """
    Returns the word cloud as a sorted OrderedDict, optionally truncated to the top `limit` entries.

    Args:
        limit (int, optional): The maximum number of entries to include in the returned OrderedDict. If None, all entries are included.

    Returns:
        OrderedDict: A dictionary sorted by word frequencies, potentially truncated to the top `limit` entries.

    Examples:
        >>> self.helpers.word_cloud.update({"apple": 5, "banana": 2, "cherry": 8})
        >>> self.helpers.word_cloud.json(limit=2)
        OrderedDict([('cherry', 8), ('apple', 5)])
    """
    cloud_sorted = sorted(self.items(), key=lambda x: x[-1], reverse=True)
    if limit is not None:
        cloud_sorted = cloud_sorted[:limit]
    return OrderedDict(cloud_sorted)

load

load(filename=None)

Loads a word cloud from a file. The file can be either a standard wordlist with one entry per line or a .tsv (tab-separated) file where the first row is the count and the second row is the associated entry.

Parameters:

  • filename (str, default: None ) –

    The path to the file from which to load the word cloud. If None, uses a default filename.

Source code in bbot/core/helpers/wordcloud.py
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
def load(self, filename=None):
    """
    Loads a word cloud from a file. The file can be either a standard wordlist with one entry per line
    or a .tsv (tab-separated) file where the first row is the count and the second row is the associated entry.

    Args:
        filename (str, optional): The path to the file from which to load the word cloud. If None, uses a default filename.
    """
    if filename is None:
        wordcloud_path = self.default_filename
    else:
        wordcloud_path = Path(filename).resolve()
    log.verbose(f"Loading word cloud from {wordcloud_path}")
    try:
        with open(str(wordcloud_path), newline="") as f:
            c = csv.reader(f, delimiter="\t")
            for row in c:
                if len(row) == 1:
                    self.add_word(row[0])
                elif len(row) == 2:
                    with suppress(Exception):
                        count, word = row
                        count = int(count)
                        self[word] = count
        if len(self) > 0:
            log.success(f"Loaded word cloud ({len(self):,} words) from {wordcloud_path}")
    except Exception as e:
        import traceback

        log_fn = log.debug
        if filename is not None:
            log_fn = log.warning
        log_fn(f"Failed to load word cloud from {wordcloud_path}: {e}")
        if filename is not None:
            log.trace(traceback.format_exc())

mutations

mutations(words, devops=True, cloud=True, letters=True, numbers=5, number_padding=2, substitute_numbers=True)

Generate various mutations for the given list of words based on different criteria.

Yields tuples of strings which can be joined on the desired delimiter, e.g. "-" or "_".

Parameters:

  • words (Union[str, Iterable[str]]) –

    A single word or list of words to mutate.

  • devops (bool, default: True ) –

    Whether to include devops-related mutations.

  • cloud (bool, default: True ) –

    Whether to include mutations from the word cloud.

  • letters (bool, default: True ) –

    Whether to include letter-based mutations.

  • numbers (int, default: 5 ) –

    The maximum numeric mutations to include.

  • number_padding (int, default: 2 ) –

    Padding for numeric mutations.

  • substitute_numbers (bool, default: True ) –

    Whether to substitute numbers in mutations.

Yields:

  • tuple

    A tuple containing each of the mutation segments.

Source code in bbot/core/helpers/wordcloud.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
def mutations(
    self, words, devops=True, cloud=True, letters=True, numbers=5, number_padding=2, substitute_numbers=True
):
    """
    Generate various mutations for the given list of words based on different criteria.

    Yields tuples of strings which can be joined on the desired delimiter, e.g. "-" or "_".

    Args:
        words (Union[str, Iterable[str]]): A single word or list of words to mutate.
        devops (bool): Whether to include devops-related mutations.
        cloud (bool): Whether to include mutations from the word cloud.
        letters (bool): Whether to include letter-based mutations.
        numbers (int): The maximum numeric mutations to include.
        number_padding (int): Padding for numeric mutations.
        substitute_numbers (bool): Whether to substitute numbers in mutations.

    Yields:
        tuple: A tuple containing each of the mutation segments.
    """
    if isinstance(words, str):
        words = (words,)
    results = set()
    for word in words:
        h = hash(word)
        if not h in results:
            results.add(h)
            yield (word,)
    if numbers > 0:
        if substitute_numbers:
            for word in words:
                for number_mutation in self.get_number_mutations(word, n=numbers, padding=number_padding):
                    h = hash(number_mutation)
                    if not h in results:
                        results.add(h)
                        yield (number_mutation,)
    for word in words:
        for modifier in self.modifiers(
            devops=devops, cloud=cloud, letters=letters, numbers=numbers, number_padding=number_padding
        ):
            a = (word, modifier)
            b = (modifier, word)
            for _ in (a, b):
                h = hash(_)
                if h not in results:
                    results.add(h)
                    yield _

save

save(filename=None, limit=None)

Saves the word cloud to a file. The cloud can optionally be truncated to the top limit entries.

Parameters:

  • filename (str, default: None ) –

    The path to the file where the word cloud will be saved. If None, uses a default filename.

  • limit (int, default: None ) –

    The maximum number of entries to save to the file. If None, all entries are saved.

Returns:

  • tuple

    A tuple containing a boolean indicating success or failure, and the resolved filename.

Examples:

>>> self.helpers.word_cloud.update({"apple": 5, "banana": 2, "cherry": 8})
>>> self.helpers.word_cloud.save(filename="word_cloud.txt", limit=2)
(True, Path('word_cloud.txt'))
Source code in bbot/core/helpers/wordcloud.py
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
def save(self, filename=None, limit=None):
    """
    Saves the word cloud to a file. The cloud can optionally be truncated to the top `limit` entries.

    Args:
        filename (str, optional): The path to the file where the word cloud will be saved. If None, uses a default filename.
        limit (int, optional): The maximum number of entries to save to the file. If None, all entries are saved.

    Returns:
        tuple: A tuple containing a boolean indicating success or failure, and the resolved filename.

    Examples:
        >>> self.helpers.word_cloud.update({"apple": 5, "banana": 2, "cherry": 8})
        >>> self.helpers.word_cloud.save(filename="word_cloud.txt", limit=2)
        (True, Path('word_cloud.txt'))
    """
    if filename is None:
        filename = self.default_filename
    else:
        filename = Path(filename).resolve()
    try:
        if not self.parent_helper.mkdir(filename.parent):
            log.error(f"Failure creating or error writing to {filename.parent} when saving word cloud")
            return
        if len(self) > 0:
            log.debug(f"Saving word cloud to {filename}")
            with open(str(filename), mode="w", newline="") as f:
                c = csv.writer(f, delimiter="\t")
                for word, count in self.json(limit).items():
                    c.writerow([count, word])
            log.debug(f"Saved word cloud ({len(self):,} words) to {filename}")
            return True, filename
        else:
            log.debug(f"No words to save")
    except Exception as e:
        import traceback

        log.warning(f"Failed to save word cloud to {filename}: {e}")
        log.trace(traceback.format_exc())
    return False, filename

truncate

truncate(limit)

Truncates the word cloud dictionary to retain only the top limit entries based on their occurrence frequencies.

Parameters:

  • limit (int) –

    The maximum number of entries to retain in the word cloud.

Examples:

>>> self.helpers.word_cloud.update({"apple": 5, "banana": 2, "cherry": 8})
>>> self.helpers.word_cloud.truncate(2)
>>> self.helpers.word_cloud
{'cherry': 8, 'apple': 5}
Source code in bbot/core/helpers/wordcloud.py
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
def truncate(self, limit):
    """
    Truncates the word cloud dictionary to retain only the top `limit` entries based on their occurrence frequencies.

    Args:
        limit (int): The maximum number of entries to retain in the word cloud.

    Examples:
        >>> self.helpers.word_cloud.update({"apple": 5, "banana": 2, "cherry": 8})
        >>> self.helpers.word_cloud.truncate(2)
        >>> self.helpers.word_cloud
        {'cherry': 8, 'apple': 5}
    """
    new_self = dict(self.json(limit=limit))
    self.clear()
    self.update(new_self)