avatarStackZero

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3134

Abstract

js-string">"FILE"</span> ) parser.add_option( <span class="hljs-string">"-w"</span>, <span class="hljs-string">"--wordlist"</span>, <span class="hljs-attribute">dest</span>=<span class="hljs-string">"wordlist"</span>, <span class="hljs-attribute">help</span>=<span class="hljs-string">"Select the wordlist"</span>, <span class="hljs-attribute">metavar</span>=<span class="hljs-string">"WORDLIST"</span>, )</pre></div><div id="78f7"><pre> (<span class="hljs-keyword">options</span>, <span class="hljs-keyword">args</span>) = parser.parse_args() <span class="hljs-keyword">print</span>(<span class="hljs-keyword">options</span>.wordlist) <span class="hljs-keyword">for</span> <span class="hljs-keyword">p</span> in Bar(<span class="hljs-string">"Processing"</span>).iter(get_wordlist(<span class="hljs-keyword">options</span>.wordlist)): <span class="hljs-keyword">try</span>: extract(<span class="hljs-keyword">options</span>.filename) <span class="hljs-keyword">print</span>(<span class="hljs-keyword">f</span><span class="hljs-string">"\n[+] Password found: {p}"</span>) <span class="hljs-keyword">break</span> except RuntimeError <span class="hljs-keyword">as</span> <span class="hljs-keyword">e</span>: pass</pre></div><p id="14db">The first part is just an argument’s parsing, easy to understand. The extract generates an Exception in case of failure, so into the for loop we try the extract until it doesn’t throw the RuntimeException.</p><h1 id="c44b">How to launch zip password cracker</h1><p id="7c00">Let see the complete code:</p><div id="8d69"><pre><span class="hljs-title">from</span> optparse <span class="hljs-keyword">import</span> OptionParser <span class="hljs-keyword">import</span> pyzipper <span class="hljs-title">from</span> progress.bar <span class="hljs-keyword">import</span> Bar</pre></div><div id="662c"><pre>def get_wordlist(wordlist_file): <span class="hljs-keyword">with</span> <span class="hljs-built_in">open</span>(wordlist_file, <span class="hljs-string">"r"</span>) <span class="hljs-keyword">as</span> f: <span class="hljs-literal">return</span> f.<span class="hljs-built_in">read</span>().<span class="hljs-built_in">split</span>(<span class="hljs-string">"\n"</span>)</pre></div><div id="b59b"><pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">extract</span>(<span class="hljs-params">file_name</span>)<span class="hljs-symbol">:</span></pre></div><div id="97e3"><pre>with pyzipper<span class="hljs-selector-class">.AESZipFile</span>(file_name, <span class="hljs-string">"r"</span>) as f: f<span class="hljs-selector-class">.extractall</span>(pwd=<span class="hljs-built_in">bytes</span>(<span class="hljs-selector-tag">p</span>, <span class="hljs-string">"utf-8"</span>))</pre></div><div id="319c"><pre>if <span class="hljs-emphasis">name</span> == "<span class="hljs-emphasis">main</span>":</pre></div><div id="5f95"><pre> parser = OptionParser() parser.add_option( <span class="hljs-string">"-f"

Options

</span>, <span class="hljs-string">"--file"</span>, <span class="hljs-attribute">dest</span>=<span class="hljs-string">"filename"</span>, <span class="hljs-attribute">help</span>=<span class="hljs-string">"compressed file"</span>, <span class="hljs-attribute">metavar</span>=<span class="hljs-string">"FILE"</span> ) parser.add_option( <span class="hljs-string">"-w"</span>, <span class="hljs-string">"--wordlist"</span>, <span class="hljs-attribute">dest</span>=<span class="hljs-string">"wordlist"</span>, <span class="hljs-attribute">help</span>=<span class="hljs-string">"Select the wordlist"</span>, <span class="hljs-attribute">metavar</span>=<span class="hljs-string">"WORDLIST"</span>, )</pre></div><div id="9b71"><pre> (<span class="hljs-keyword">options</span>, <span class="hljs-keyword">args</span>) = parser.parse_args() <span class="hljs-keyword">print</span>(<span class="hljs-keyword">options</span>.wordlist) <span class="hljs-keyword">for</span> <span class="hljs-keyword">p</span> in Bar(<span class="hljs-string">"Processing"</span>).iter(get_wordlist(<span class="hljs-keyword">options</span>.wordlist)): <span class="hljs-keyword">try</span>: extract(<span class="hljs-keyword">options</span>.filename) <span class="hljs-keyword">print</span>(<span class="hljs-keyword">f</span><span class="hljs-string">"\n[+] Password found: {p}"</span>) <span class="hljs-keyword">break</span> except RuntimeError <span class="hljs-keyword">as</span> <span class="hljs-keyword">e</span>: pass</pre></div><p id="4fdf">After saving our script in a file called <i>main.py</i> we can launch it with the following command:</p><div id="26e1"><pre>python3 <span class="hljs-selector-tag">main</span><span class="hljs-selector-class">.py</span> -f test<span class="hljs-selector-class">.zip</span> -w wordlist.dic</pre></div><p id="c9ec">The output will be something similar to this:</p><div id="6a6e"><pre>Processing |######################## | <span class="hljs-number">21168</span>/<span class="hljs-number">27608</span> [+] Password found: roland</pre></div><h1 id="64c3">Conclusion and improvements</h1><p id="f700">With this project, you can see how easy is an attack on a zip password-protected file, so try to use most secure ones for your important files.</p><p id="98a4">You can also try to make some improvements, you could add numbers to the ends, or maybe you can merge words from two files.</p><h1 id="bc8b">Further readings</h1><p id="8b93">If you liked to write the zip password cracker in python, you may want to read those articles:</p><ul><li><a href="https://www.stackzero.net/subdomain-scanner-in-python/">Write subdomain scanner in python</a></li><li><a href="https://www.stackzero.net/write-a-network-scanner-tool/">Create a network scanner in python</a></li></ul><p id="5c67"><i>Originally published at <a href="https://www.stackzero.net/how-to-easily-create-a-zip-password-cracker/">https://www.stackzero.net</a> on May 19, 2022.</i></p></article></body>

How to Easily Create a zip password cracker in Just Seconds! — StackZero

Introduction to zip password cracker

If you’ve ever forgotten a password for a zip file, you know how frustrating it can be. There’s no need to worry anymore, because in this article we’ll show you how to crack a zip password using a dictionary attack. Usually zip files are protected with very simple passwords. With a dictionary attack, you can use a list of common passwords to try and guess the password for a zip file. This is a very effective method, and it’s easy to do. So let’s get started to code our zip password cracker!

Prerequisites

In order to complete the project you just need:

You don’t need nothing more, so we can start with the code.

The code

Imports:

from optparse import OptionParser 
import pyzipper 
from progress.bar import Bar

Auxiliary methods

def get_wordlist(wordlist_file): 
    with open(wordlist_file, 'r') as f: 
        return f.read().split('\n')

The first method gets the wordlist from a file and saves it into a file (if the list is too big, think to use a generator).

def extract(file_name): 
    with pyzipper.AESZipFile(file_name, 'r') as f: 
        f.extractall(pwd = bytes(p, 'utf-8'))

This is the method that extracts the file.

The main method

if __name__ == "__main__":
    parser = OptionParser()
    parser.add_option(
        "-f", "--file", dest="filename",   
        help="compressedfile",
        metavar="FILE"
    )
    parser.add_option(
        "-w",
        "--wordlist",
        dest="wordlist",
        help="Select the wordlist",
        metavar="WORDLIST",
    )
    (options, args) = parser.parse_args()
    print(options.wordlist)
    for p in Bar("Processing").iter(get_wordlist(options.wordlist)):
        try:
            extract(options.filename)
            print(f"\n[+] Password found: {p}")
            break
        except RuntimeError as e:
            pass

The first part is just an argument’s parsing, easy to understand. The extract generates an Exception in case of failure, so into the for loop we try the extract until it doesn’t throw the RuntimeException.

How to launch zip password cracker

Let see the complete code:

from optparse import OptionParser
import pyzipper
from progress.bar import Bar
def get_wordlist(wordlist_file):
    with open(wordlist_file, "r") as f:
        return f.read().split("\n")
def extract(file_name):
with pyzipper.AESZipFile(file_name, "r") as f:
        f.extractall(pwd=bytes(p, "utf-8"))
if __name__ == "__main__":
    parser = OptionParser()
    parser.add_option(
        "-f", "--file", 
        dest="filename", 
        help="compressed file", 
        metavar="FILE"
    )
    parser.add_option(
        "-w",
        "--wordlist",
        dest="wordlist",
        help="Select the wordlist",
        metavar="WORDLIST",
    )
    (options, args) = parser.parse_args()
    print(options.wordlist)
    for p in Bar("Processing").iter(get_wordlist(options.wordlist)):
        try:
            extract(options.filename)
            print(f"\n[+] Password found: {p}")
            break
        except RuntimeError as e:
            pass

After saving our script in a file called main.py we can launch it with the following command:

python3 main.py -f test.zip -w wordlist.dic

The output will be something similar to this:

Processing |######################## | 21168/27608 
[+] Password found: roland

Conclusion and improvements

With this project, you can see how easy is an attack on a zip password-protected file, so try to use most secure ones for your important files.

You can also try to make some improvements, you could add numbers to the ends, or maybe you can merge words from two files.

Further readings

If you liked to write the zip password cracker in python, you may want to read those articles:

Originally published at https://www.stackzero.net on May 19, 2022.

Dictionary Attack
Ethical Hacking
Python
Hacking
Passwords
Recommended from ReadMedium