Python 暴力通配符

Python Brute wildcard

您好,我正在尝试为渗透测试制作一个简单的字母暴力破解:

    astric = ['a', 'b', 'c', 'd', 'e', 'f', 'g','h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    usern = input("username you want to check with '*' for unknown chars: ")
    paramsGet = {"Name": usern}

所以输入会询问什么用户名,我会输入 meep**,然后它会将 astric 列表随机字母或数字分配给列表,这样它将是一个完整的请求。我该怎么做? for _ in list: 我猜是这样的

你可以尝试用递归的方法来解决这个问题。

def getWords(words):
    matched_words = []
    break_flag = True
    for word in words:
        if '*' not in word:
            matched_words.append(word)
        else:
            break_flag = False
            for char in astric:
                matched_words.append(word.replace('*', char, 1))
    if break_flag:
        return matched_words
    else:
        return getWords(matched_words)
print(getWords(["meep*"]))

实际上,它接受单词列表并检查 * 并将其替换为字符列表 astric 中的每个字符并递归进一步 *s

这应该有效:

astric = ['a', 'b', 'c', 'd', 'e', 'f', 'g','h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
usern = input("username you want to check with '*' for unknown chars: ")
paramsGet = {"Name": usern}

def get(usern):
    if len(usern) == 0:
        yield ''
        return
    head, tail = usern[0], usern[1:]
    for each in get(tail):
        if head == '*':
            for c in astric:
                yield c + each
        else:
            yield head + each

for each in get(usern):
    print(each)

我认为这不是最好的方法,也不是最快的

您可以记下每个星号的索引。

asterisk_location = [i for i, x in enumerate(source) if x == "*"]

您可以利用 itertools.product 根据出现的星号数量生成所有可能的重复对。

product(astric, repeat=len(asterisk_location))

然后您可以将已知星号索引处的每个值替换为 itertools 生成的值之一。

source[index] = value

总而言之,你最终会得到类似这样的结果:

from itertools import product
from typing import Generator

astric = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
          'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
          'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']


def generate_possible_options(source: str) -> Generator[str, None, None]:
    source = list(source)
    asterisk_location = [i for i, x in enumerate(source) if x == "*"]
    for option in product(astric, repeat=len(asterisk_location)):
        for index, value in zip(asterisk_location, option):
            source[index] = value
        yield ''.join(source)


if __name__ == '__main__':
    for possibility in generate_possible_options("m*ep*"):
        print(possibility)

当然,您可以添加用户输入并将结果传递给 generate_possible_options 函数,但我将其保留为静态值,以便可以轻松复制和 运行 而不会挂起供用户输入。

这生成了 1296 个可能的选项。

缩略输出:

maepa
maepb
maepc
maepd
maepe
maepf
maepg
maeph
maepi
maepj
maepk
maepl
...
m9ep2
m9ep3
m9ep4
m9ep5
m9ep6
m9ep7
m9ep8
m9ep9

您还可以通过以下方式获取列表对象:

possibilities = list(generate_possible_options("m*ep*"))