我的代码不会中断:
(我删去了一些化妆品用的部分)
lchars = ['', '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', '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', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '!', '@', '\\', '#', '$', '%', '^', '&', '*', '(', ')', '-', '=', '_', '+', '`', '~', '[', ']', '{', '}', '|', ';', ':', "'", ',', '.', '/', '<', '>', '?', '`', '¡', '™', '£', '¢', '∞', '§', '¶', '•', 'ª', 'º', '–', '≠', 'œ', '∑','´', '®', '†', '¥', '¨', 'ˆ', 'ø', 'π', '“', '‘', '«', 'å', 'ß', '∆', '˚', '¬', '…', '˜', 'µ', '≤', '≥', '÷', 'æ', 'Ω', '≈', 'ç', '√', '"', ' ']
guessThisPass = str(input("Enter the password you want the python-based brute force hacker to guess (Character limit is 4): "))
if len(guessThisPass) > 4:
print('I SAID CHARACTER LIMIT IS 4!! I AM TRUNCATING YOUR PASSWORD NOW! ?')
guessThisPass[0:4]
time.sleep(5)
print("Starting...")
time.sleep(2)
start = time.time()
for d in lchars:
for c in lchars:
for b in lchars:
for a in lchars:
tryPass = str(str(a) + str(b) + str(c) + str(d)). # I know the strs are probably unnessecary.
print(tryPass). # Outputing the attempted password
if tryPass == guessThisPass:
break # This never happens
print("Whoo!")
print("That took ", time.time()-start, "seconds.")
使用此代码,假设我的密码是“ a”(IRL,它是不是)。然后,逻辑上,它应该几乎可以立即突破,对吧?除了不是;它只会继续前进,不会中断,甚至达到组合'^ Bc'或更高的水平。为什么不破裂?我是否需要在每个循环中的每个循环中添加if语句?
此外,这是我用来测试可能组合的代码:
(也被剪掉多余的化妆品)
# All possible combinations
lchars = ['', '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', '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', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '!', '@', '\\', '#', '$', '%', '^', '&', '*', '(', ')', '-', '=', '_', '+', '`', '~', '[', ']', '{', '}', '|', ';', ':', "'", ',', '.', '/', '<', '>', '?', '`', '¡', '™', '£', '¢', '∞', '§', '¶', '•', 'ª', 'º', '–', '≠', 'œ', '∑','´', '®', '†', '¥', '¨', 'ˆ', 'ø', 'π', '“', '‘', '«', 'å', 'ß', '∆', '˚', '¬', '…', '˜', 'µ', '≤', '≥', '÷', 'æ', 'Ω', '≈', 'ç', '√', '"', ' ']
# print("Also, there are ", len(lchars), "characters in our character database\n\n")
from time import time
start = time()
for d in lchars:
for c in lchars:
for b in lchars:
for a in lchars:
print(a+b+c+d)
print("Whoo!")
print("That took ", time()-start, "seconds.")
我检查了上面代码的输出。字母“ a”出现在输出中。它应该工作。为什么它不起作用?
答案 0 :(得分:3)
它是 break
。问题是,break
仅从内部循环中中断。并非每个包含它的循环。最内层的循环会中断,然后围绕它的循环会从中断的地方开始。
无需过多更改代码,最简单的解决方案是将代码包装在一个函数中,并从中return
:
def try_all(lchars):
for d in lchars:
for c in lchars:
for b in lchars:
for a in lchars:
tryPass = a + b + c + d
print(tryPass)
if tryPass == guessThisPass:
return tryPass
cracked_password = try_all(lchars)
还有许多其他需要改进的地方,但这在这里不合适。代码正常工作并完成后,您可以将其发布在Code Review上以获取一般建议。
答案 1 :(得分:2)
实际上它会中断,但由于您有4个嵌套循环,因此它只会中断最内层的循环。您可以做一些事情来解决这个问题:
itertools.product
创建您的试用字符串。这样,您只需要一个循环,因此 break 的创建将很简单。答案 2 :(得分:1)
是的,break
只会使您跳出最内层的循环!我会将代码移到一个函数中,找到合适的函数就可以return tryPass
。
快乐编码!